1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.StringReader ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import javax.xml.transform.OutputKeys ; 31 import javax.xml.transform.Result ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.Transformer ; 34 import javax.xml.transform.TransformerFactory ; 35 import javax.xml.transform.TransformerFactoryConfigurationError ; 36 import javax.xml.transform.dom.DOMSource ; 37 import javax.xml.transform.stream.StreamResult ; 38 import javax.xml.transform.stream.StreamSource ; 39 import org.w3c.dom.DOMException ; 40 import org.w3c.dom.DOMImplementation ; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.DocumentType ; 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.w3c.dom.Text ; 47 import org.xml.sax.EntityResolver ; 48 import org.xml.sax.ErrorHandler ; 49 import org.xml.sax.InputSource ; 50 import org.xml.sax.SAXException ; 51 52 56 final class XMLUtil extends Object { 57 58 @SuppressWarnings ("unchecked") 59 private static final ThreadLocal <DocumentBuilder >[] builderTL = new ThreadLocal [4]; 60 static { 61 for (int i = 0; i < 4; i++) { 62 builderTL[i] = new ThreadLocal <DocumentBuilder >(); 63 } 64 } 65 public static Document parse ( 66 InputSource input, 67 boolean validate, 68 boolean namespaceAware, 69 ErrorHandler errorHandler, 70 EntityResolver entityResolver 71 ) throws IOException , SAXException { 72 73 int index = (validate ? 0 : 1) + (namespaceAware ? 0 : 2); 74 DocumentBuilder builder = builderTL[index].get(); 75 if (builder == null) { 76 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 77 factory.setValidating(validate); 78 factory.setNamespaceAware(namespaceAware); 79 80 try { 81 builder = factory.newDocumentBuilder(); 82 } catch (ParserConfigurationException ex) { 83 throw new SAXException (ex); 84 } 85 builderTL[index].set(builder); 86 } 87 88 if (errorHandler != null) { 89 builder.setErrorHandler(errorHandler); 90 } 91 92 if (entityResolver != null) { 93 builder.setEntityResolver(entityResolver); 94 } 95 96 return builder.parse(input); 97 } 98 99 public static Document createDocument(String rootQName) throws DOMException { 100 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 101 try { 102 return factory.newDocumentBuilder().getDOMImplementation().createDocument(null, rootQName, null); 103 } catch (ParserConfigurationException ex) { 104 throw (DOMException )new DOMException (DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); } 106 } 107 108 private static DOMImplementation getDOMImplementation() throws DOMException { 110 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 111 112 try { 113 return factory.newDocumentBuilder().getDOMImplementation(); 114 } catch (ParserConfigurationException ex) { 115 throw (DOMException )new DOMException (DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); } 117 } 118 119 private static final String IDENTITY_XSLT_WITH_INDENT = 121 "<xsl:stylesheet version='1.0' " + "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " + "xmlns:xalan='http://xml.apache.org/xslt' " + "exclude-result-prefixes='xalan'>" + "<xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>" + "<xsl:template match='@*|node()'>" + "<xsl:copy>" + "<xsl:apply-templates select='@*|node()'/>" + "</xsl:copy>" + "</xsl:template>" + "</xsl:stylesheet>"; 133 public static void write(Document doc, OutputStream out) throws IOException { 134 try { 138 Transformer t = TransformerFactory.newInstance().newTransformer( 139 new StreamSource (new StringReader (IDENTITY_XSLT_WITH_INDENT))); 140 DocumentType dt = doc.getDoctype(); 141 if (dt != null) { 142 String pub = dt.getPublicId(); 143 if (pub != null) { 144 t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub); 145 } 146 t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId()); 147 } 148 t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); Source source = new DOMSource (doc); 150 Result result = new StreamResult (out); 151 t.transform(source, result); 152 } catch (Exception e) { 153 throw (IOException )new IOException (e.toString()).initCause(e); 154 } catch (TransformerFactoryConfigurationError e) { 155 throw (IOException )new IOException (e.toString()).initCause(e); 156 } 157 } 158 159 public static void write(Element el, OutputStream out) throws IOException { 160 try { 161 Transformer t = TransformerFactory.newInstance().newTransformer( 162 new StreamSource (new StringReader (IDENTITY_XSLT_WITH_INDENT))); 163 t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 165 Source source = new DOMSource (el); 166 Result result = new StreamResult (out); 167 t.transform(source, result); 168 } catch (Exception e) { 169 throw (IOException ) new IOException (e.toString()).initCause(e); 170 } catch (TransformerFactoryConfigurationError e) { 171 throw (IOException ) new IOException (e.toString()).initCause(e); 172 } 173 } 174 175 185 public static Element findElement(Element parent, String name, String namespace) { 186 Element result = null; 187 NodeList l = parent.getChildNodes(); 188 for (int i = 0; i < l.getLength(); i++) { 189 if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { 190 Element el = (Element )l.item(i); 191 if ((namespace == null && name.equals(el.getTagName())) || 192 (namespace != null && name.equals(el.getLocalName()) && 193 namespace.equals(el.getNamespaceURI()))) { 194 if (result == null) { 195 result = el; 196 } else { 197 return null; 198 } 199 } 200 } 201 } 202 return result; 203 } 204 205 211 static String findText(Element parent) { 212 NodeList l = parent.getChildNodes(); 213 for (int i = 0; i < l.getLength(); i++) { 214 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 215 Text text = (Text )l.item(i); 216 return text.getNodeValue(); 217 } 218 } 219 return null; 220 } 221 222 232 static List <Element > findSubElements(Element parent) throws IllegalArgumentException { 233 NodeList l = parent.getChildNodes(); 234 List <Element > elements = new ArrayList <Element >(l.getLength()); 235 for (int i = 0; i < l.getLength(); i++) { 236 Node n = l.item(i); 237 if (n.getNodeType() == Node.ELEMENT_NODE) { 238 elements.add((Element )n); 239 } else if (n.getNodeType() == Node.TEXT_NODE) { 240 String text = ((Text )n).getNodeValue(); 241 if (text.trim().length() > 0) { 242 throw new IllegalArgumentException ("non-ws text encountered in " + parent + ": " + text); } 244 } else if (n.getNodeType() == Node.COMMENT_NODE) { 245 } else { 247 throw new IllegalArgumentException ("unexpected non-element child of " + parent + ": " + n); } 249 } 250 return elements; 251 } 252 253 } 254 | Popular Tags |