1 17 18 package org.apache.jmeter.util; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 27 import org.apache.jorphan.logging.LoggingManager; 28 import org.apache.log.Logger; 29 import org.w3c.dom.Document ; 30 import org.w3c.tidy.Tidy; 31 import org.xml.sax.ErrorHandler ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.SAXParseException ; 34 35 42 public class XPathUtil { 43 transient private static Logger log = LoggingManager.getLoggerForClass(); 44 private XPathUtil() {super();} 45 private static DocumentBuilderFactory documentBuilderFactory; 46 47 48 52 private static synchronized DocumentBuilderFactory 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 documentBuilderFactory = DocumentBuilderFactory.newInstance(); 62 documentBuilderFactory.setValidating( validate ); 63 documentBuilderFactory.setNamespaceAware( namespace ); 64 documentBuilderFactory.setIgnoringElementContentWhitespace(whitespace); 65 } 66 return XPathUtil.documentBuilderFactory; 67 } 68 69 78 public static DocumentBuilder makeDocumentBuilder( 79 boolean validate, boolean whitespace, boolean namespace) 80 throws ParserConfigurationException , SAXException { 81 DocumentBuilder builder = 82 makeDocumentBuilderFactory(validate, whitespace, namespace).newDocumentBuilder(); 83 builder.setErrorHandler(new MyErrorHandler(validate, false)); 84 return builder; 85 } 86 87 100 public static Document makeDocument( 101 InputStream stream, 102 boolean validate, 103 boolean whitespace, 104 boolean namespace, 105 boolean tolerant) 106 throws ParserConfigurationException , SAXException , IOException { 107 Document doc; 108 if (tolerant) { 109 doc=tidyDoc(stream); 110 } else { 112 doc=makeDocumentBuilder( validate, whitespace, namespace).parse(stream); 113 } 114 return doc; 115 } 116 117 125 private static Document tidyDoc(InputStream stream){ 126 Document doc=null; 127 doc = makeTidyParser().parseDOM(stream,null); 128 doc.normalize(); 129 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); return tidy; 147 } 148 149 155 static class MyErrorHandler implements ErrorHandler { 156 private final boolean val,tol; 157 private final String 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 ex) throws SAXException { 166 log.info("Type="+type+" "+ex); 167 if (val && !tol) throw new SAXException (ex); 168 } 169 170 public void error(SAXParseException ex) throws SAXException { 171 log.warn("Type="+type+" "+ex); 172 if (val && !tol) throw new SAXException (ex); 173 } 174 175 public void fatalError(SAXParseException ex) throws SAXException { 176 log.error("Type="+type+" "+ex); 177 if (val && !tol) throw new SAXException (ex); 178 } 179 } 180 } | Popular Tags |