KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > util > XPathUtil


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.jmeter.util;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26
27 import org.apache.jorphan.logging.LoggingManager;
28 import org.apache.log.Logger;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.tidy.Tidy;
31 import org.xml.sax.ErrorHandler JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.SAXParseException JavaDoc;
34
35 /**
36  * author Justin Spears jspears@astrology.com
37  *
38  * This class provides a few utility methods for dealing with XML/XPath. Might
39  * think about creating an interface for the setup, but, works fine now...
40  *
41  */

42 public class XPathUtil {
43     transient private static Logger log = LoggingManager.getLoggerForClass();
44     private XPathUtil() {super();}
45     private static DocumentBuilderFactory JavaDoc documentBuilderFactory;
46     
47
48       /**
49        * Might
50        * @return javax.xml.parsers.DocumentBuilderFactory
51        */

52       private static synchronized DocumentBuilderFactory JavaDoc makeDocumentBuilderFactory(
53               boolean validate, boolean whitespace, boolean namespace) {
54         if ( XPathUtil.documentBuilderFactory == null
55             || documentBuilderFactory.isValidating() != validate
56             || documentBuilderFactory.isNamespaceAware() != namespace
57             || documentBuilderFactory.isIgnoringElementContentWhitespace() != whitespace
58             )
59         {
60           // configure the document builder factory
61
documentBuilderFactory = DocumentBuilderFactory.newInstance();
62           documentBuilderFactory.setValidating( validate );
63           documentBuilderFactory.setNamespaceAware( namespace );
64           documentBuilderFactory.setIgnoringElementContentWhitespace(whitespace);
65         }
66         return XPathUtil.documentBuilderFactory;
67       }
68
69       /**
70       * Create a DocumentBuilder using the makeDocumentFactory func.
71       * @param validate
72       * @param whitespace
73       * @param namespace
74       * @return document builder
75       * @throws ParserConfigurationException
76       * @throws SAXException
77       */

78      public static DocumentBuilder JavaDoc makeDocumentBuilder(
79              boolean validate, boolean whitespace, boolean namespace)
80      throws ParserConfigurationException JavaDoc, SAXException JavaDoc{
81          DocumentBuilder JavaDoc builder =
82             makeDocumentBuilderFactory(validate, whitespace, namespace).newDocumentBuilder();
83         builder.setErrorHandler(new MyErrorHandler(validate, false));
84         return builder;
85      }
86
87     /**
88      * Utility function to get new Document
89      *
90      * @param stream Document Input stream
91      * @param validate Validate Document
92      * @param whitespace Element Whitespace
93      * @param namespace Is Namespace aware.
94      * @param tolerant Is tolerant
95      * @return document
96      * @throws ParserConfigurationException
97      * @throws IOException
98      * @throws SAXException
99      */

100     public static Document JavaDoc makeDocument(
101             InputStream JavaDoc stream,
102             boolean validate,
103             boolean whitespace,
104             boolean namespace,
105             boolean tolerant)
106     throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
107         Document JavaDoc doc;
108         if (tolerant) {
109             doc=tidyDoc(stream);
110             //doc=makeTolerantDocumentBuilder().parse(new InputStreamReader(stream));
111
} else {
112         doc=makeDocumentBuilder( validate, whitespace, namespace).parse(stream);
113         }
114         return doc;
115     }
116
117 // private static HTMLDocumentBuilder makeTolerantDocumentBuilder()
118
// throws ParserConfigurationException, SAXException, IOException {
119
// HTMLDocumentBuilder builder = new HTMLDocumentBuilder(
120
// new TolerantSaxDocumentBuilder(makeDocumentBuilder(false,false,false)
121
// ));
122
// return builder;
123
// }
124

125     private static Document JavaDoc tidyDoc(InputStream JavaDoc stream){
126         Document JavaDoc doc=null;
127         doc = makeTidyParser().parseDOM(stream,null);
128         doc.normalize();
129         // remove the document declaration cause I think it causes
130
// issues this is only needed for JDOM, since I am not
131
// using it... But in case we change.
132
// Node name = doc.getDoctype();
133
// doc.removeChild(name);
134

135         return doc;
136     }
137     
138     private static Tidy makeTidyParser() {
139         Tidy tidy = new Tidy();
140         tidy.setCharEncoding(org.w3c.tidy.Configuration.UTF8);
141         tidy.setQuiet(true);
142         tidy.setShowWarnings(false);
143         tidy.setMakeClean(true);
144         tidy.setXmlTags(false); // Input is not valid XML
145
//tidy.setShowErrors(1);
146
return tidy;
147     }
148
149     // Not used
150
// public static Document makeDocument(InputStream stream)
151
// throws ParserConfigurationException, SAXException, IOException {
152
// return makeDocumentBuilder( false, false, false).parse(stream);
153
// }
154

155     static class MyErrorHandler implements ErrorHandler JavaDoc{
156           private final boolean val,tol;
157           private final String JavaDoc type;
158
159           MyErrorHandler(boolean validate, boolean tolerate){
160               val=validate;
161               tol=tolerate;
162               type="Val="+val+" Tol="+tol;
163           }
164
165           public void warning(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
166             log.info("Type="+type+" "+ex);
167              if (val && !tol) throw new SAXException JavaDoc(ex);
168           }
169
170           public void error(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
171             log.warn("Type="+type+" "+ex);
172              if (val && !tol) throw new SAXException JavaDoc(ex);
173           }
174
175           public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
176             log.error("Type="+type+" "+ex);
177              if (val && !tol) throw new SAXException JavaDoc(ex);
178           }
179       }
180 }
Popular Tags