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: }
|