Source Code Cross Referenced for XPathUtil.java in  » Testing » jakarta-jmeter » org » apache » jmeter » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta jmeter » org.apache.jmeter.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *   http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         * 
017:         */
018:
019:        package org.apache.jmeter.util;
020:
021:        import java.io.IOException;
022:        import java.io.InputStream;
023:
024:        import javax.xml.parsers.DocumentBuilder;
025:        import javax.xml.parsers.DocumentBuilderFactory;
026:        import javax.xml.parsers.ParserConfigurationException;
027:
028:        import org.apache.jorphan.logging.LoggingManager;
029:        import org.apache.log.Logger;
030:        import org.w3c.dom.Document;
031:        import org.w3c.tidy.Tidy;
032:        import org.xml.sax.ErrorHandler;
033:        import org.xml.sax.SAXException;
034:        import org.xml.sax.SAXParseException;
035:
036:        /**
037:         * author Justin Spears jspears@astrology.com
038:         * 
039:         * This class provides a few utility methods for dealing with XML/XPath. Might
040:         * think about creating an interface for the setup, but, works fine now...
041:         * 
042:         */
043:        public class XPathUtil {
044:            private static final Logger log = LoggingManager
045:                    .getLoggerForClass();
046:
047:            private XPathUtil() {
048:                super ();
049:            }
050:
051:            private static DocumentBuilderFactory documentBuilderFactory;
052:
053:            /**
054:             * Returns a suitable document builder factory.
055:             * Caches the factory in case the next caller wants the same options.
056:             * 
057:             * @param validate should the parser validate documents?
058:             * @param whitespace should the parser eliminate whitespace in element content?
059:             * @param namespace should the parser be namespace aware?
060:             * 
061:             * @return javax.xml.parsers.DocumentBuilderFactory
062:             */
063:            private static synchronized DocumentBuilderFactory makeDocumentBuilderFactory(
064:                    boolean validate, boolean whitespace, boolean namespace) {
065:                if (XPathUtil.documentBuilderFactory == null
066:                        || documentBuilderFactory.isValidating() != validate
067:                        || documentBuilderFactory.isNamespaceAware() != namespace
068:                        || documentBuilderFactory
069:                                .isIgnoringElementContentWhitespace() != whitespace) {
070:                    // configure the document builder factory
071:                    documentBuilderFactory = DocumentBuilderFactory
072:                            .newInstance();
073:                    documentBuilderFactory.setValidating(validate);
074:                    documentBuilderFactory.setNamespaceAware(namespace);
075:                    documentBuilderFactory
076:                            .setIgnoringElementContentWhitespace(whitespace);
077:                }
078:                return XPathUtil.documentBuilderFactory;
079:            }
080:
081:            /**
082:             * Create a DocumentBuilder using the makeDocumentFactory func.
083:             * 
084:             * @param validate should the parser validate documents?
085:             * @param whitespace should the parser eliminate whitespace in element content?
086:             * @param namespace should the parser be namespace aware?
087:             * @return document builder
088:             * @throws ParserConfigurationException
089:             */
090:            public static DocumentBuilder makeDocumentBuilder(boolean validate,
091:                    boolean whitespace, boolean namespace)
092:                    throws ParserConfigurationException {
093:                DocumentBuilder builder = makeDocumentBuilderFactory(validate,
094:                        whitespace, namespace).newDocumentBuilder();
095:                builder.setErrorHandler(new MyErrorHandler(validate, false));
096:                return builder;
097:            }
098:
099:            /**
100:             * Utility function to get new Document
101:             * 
102:             * @param stream
103:             *            Document Input stream
104:             * @param validate
105:             *            Validate Document
106:             * @param whitespace
107:             *            Element Whitespace
108:             * @param namespace
109:             *            Is Namespace aware.
110:             * @param tolerant
111:             *            Is tolerant - i.e. use the Tidy parser
112:             *
113:             * @return document
114:             * @throws ParserConfigurationException
115:             * @throws IOException
116:             * @throws SAXException
117:             */
118:            public static Document makeDocument(InputStream stream,
119:                    boolean validate, boolean whitespace, boolean namespace,
120:                    boolean tolerant) throws ParserConfigurationException,
121:                    SAXException, IOException {
122:                Document doc;
123:                if (tolerant) {
124:                    doc = tidyDoc(stream);
125:                    // doc=makeTolerantDocumentBuilder().parse(new
126:                    // InputStreamReader(stream));
127:                } else {
128:                    doc = makeDocumentBuilder(validate, whitespace, namespace)
129:                            .parse(stream);
130:                }
131:                return doc;
132:            }
133:
134:            // private static HTMLDocumentBuilder makeTolerantDocumentBuilder()
135:            // throws ParserConfigurationException, SAXException, IOException {
136:            // HTMLDocumentBuilder builder = new HTMLDocumentBuilder(
137:            // new TolerantSaxDocumentBuilder(makeDocumentBuilder(false,false,false)
138:            // ));
139:            // return builder;
140:            // }
141:
142:            private static Document tidyDoc(InputStream stream) {
143:                Document doc = null;
144:                doc = makeTidyParser().parseDOM(stream, null);
145:                doc.normalize();
146:                // remove the document declaration cause I think it causes
147:                // issues this is only needed for JDOM, since I am not
148:                // using it... But in case we change.
149:                // Node name = doc.getDoctype();
150:                // doc.removeChild(name);
151:
152:                return doc;
153:            }
154:
155:            private static Tidy makeTidyParser() {
156:                Tidy tidy = new Tidy();
157:                tidy.setCharEncoding(org.w3c.tidy.Configuration.UTF8);
158:                tidy.setQuiet(true);
159:                tidy.setShowWarnings(false);
160:                tidy.setMakeClean(true);
161:                tidy.setXmlTags(false); // Input is not valid XML
162:                // tidy.setShowErrors(1);
163:                return tidy;
164:            }
165:
166:            // Not used
167:            // public static Document makeDocument(InputStream stream)
168:            // throws ParserConfigurationException, SAXException, IOException {
169:            // return makeDocumentBuilder( false, false, false).parse(stream);
170:            // }
171:
172:            static class MyErrorHandler implements  ErrorHandler {
173:                private final boolean val, tol;
174:
175:                private final String type;
176:
177:                MyErrorHandler(boolean validate, boolean tolerate) {
178:                    val = validate;
179:                    tol = tolerate;
180:                    type = "Val=" + val + " Tol=" + tol;
181:                }
182:
183:                public void warning(SAXParseException ex) throws SAXException {
184:                    log.info("Type=" + type + " " + ex);
185:                    if (val && !tol)
186:                        throw new SAXException(ex);
187:                }
188:
189:                public void error(SAXParseException ex) throws SAXException {
190:                    log.warn("Type=" + type + " " + ex);
191:                    if (val && !tol)
192:                        throw new SAXException(ex);
193:                }
194:
195:                public void fatalError(SAXParseException ex)
196:                        throws SAXException {
197:                    log.error("Type=" + type + " " + ex);
198:                    if (val && !tol)
199:                        throw new SAXException(ex);
200:                }
201:            }
202:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.