1 19 20 package org.netbeans.updater; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.transform.OutputKeys ; 28 import javax.xml.transform.Result ; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.TransformerFactoryConfigurationError ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.stream.StreamResult ; 35 import org.w3c.dom.DOMException ; 36 import org.w3c.dom.DOMImplementation ; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.DocumentType ; 39 import org.xml.sax.EntityResolver ; 40 import org.xml.sax.ErrorHandler ; 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.SAXException ; 43 44 49 public final class XMLUtil extends Object { 50 51 public static Document parse ( 52 InputSource input, 53 boolean validate, 54 boolean namespaceAware, 55 ErrorHandler errorHandler, 56 EntityResolver entityResolver 57 ) throws IOException , SAXException { 58 59 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 60 factory.setValidating(validate); 61 factory.setNamespaceAware(namespaceAware); 62 DocumentBuilder builder = null; 63 try { 64 builder = factory.newDocumentBuilder(); 65 } catch (ParserConfigurationException ex) { 66 throw new SAXException (ex); 67 } 68 69 if (errorHandler != null) { 70 builder.setErrorHandler(errorHandler); 71 } 72 73 if (entityResolver != null) { 74 builder.setEntityResolver(entityResolver); 75 } 76 77 assert input != null : "InputSource cannot be null"; 78 79 return builder.parse(input); 80 } 81 82 public static Document createDocument(String rootQName) throws DOMException { 83 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 84 try { 85 return factory.newDocumentBuilder ().getDOMImplementation ().createDocument (null, rootQName, null); 86 } catch (ParserConfigurationException ex) { 87 throw (DOMException )new DOMException (DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); } 89 } 90 91 public static void write(Document doc, OutputStream out) throws IOException { 92 try { 96 Transformer t = TransformerFactory.newInstance().newTransformer(); 97 DocumentType dt = doc.getDoctype(); 98 if (dt != null) { 99 String pub = dt.getPublicId(); 100 if (pub != null) { 101 t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub); 102 } 103 t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId()); 104 } 105 t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); Source source = new DOMSource (doc); 109 Result result = new StreamResult (out); 110 t.transform(source, result); 111 } catch (Exception e) { 112 throw (IOException )new IOException (e.toString()).initCause(e); 113 } catch (TransformerFactoryConfigurationError e) { 114 throw (IOException )new IOException (e.toString()).initCause(e); 115 } 116 } 117 118 121 public static EntityResolver createAUResolver() { 122 return new EntityResolver () { 123 public InputSource resolveEntity(String publicID, String systemID) throws IOException , SAXException { 124 if ("-//NetBeans//DTD Autoupdate Catalog 1.0//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-catalog-1_0.dtd").toString()); 126 } else if ("-//NetBeans//DTD Autoupdate Module Info 1.0//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-info-1_0.dtd").toString()); 128 } else if ("-//NetBeans//DTD Autoupdate Catalog 2.0//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-catalog-2_0.dtd").toString()); 130 } else if ("-//NetBeans//DTD Autoupdate Module Info 2.0//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-info-2_0.dtd").toString()); 132 } else if ("-//NetBeans//DTD Autoupdate Catalog 2.2//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-catalog-2_2.dtd").toString()); 134 } else if ("-//NetBeans//DTD Autoupdate Module Info 2.2//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-info-2_2.dtd").toString()); 136 } else if ("-//NetBeans//DTD Autoupdate Catalog 2.3//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-catalog-2_3.dtd").toString()); 138 } else if ("-//NetBeans//DTD Autoupdate Module Info 2.3//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-info-2_3.dtd").toString()); 140 } else if ("-//NetBeans//DTD Autoupdate Catalog 2.4//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-catalog-2_4.dtd").toString()); 142 } else if ("-//NetBeans//DTD Autoupdate Module Info 2.4//EN".equals(publicID)) { return new InputSource (XMLUtil.class.getResource("resources/autoupdate-info-2_4.dtd").toString()); 144 } else { 145 return null; 146 } 147 } 148 }; 149 } 150 151 152 } 153 | Popular Tags |