KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > updater > XMLUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.updater;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.transform.OutputKeys JavaDoc;
28 import javax.xml.transform.Result JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
35 import org.w3c.dom.DOMException JavaDoc;
36 import org.w3c.dom.DOMImplementation JavaDoc;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.DocumentType JavaDoc;
39 import org.xml.sax.EntityResolver JavaDoc;
40 import org.xml.sax.ErrorHandler JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  * Utility class collecting library methods related to XML processing.
46  * Stolen from nbbuild/antsrc and openide/.../xml.
47  * @author Petr Kuzel, Jesse Glick
48  */

49 public final class XMLUtil extends Object JavaDoc {
50
51     public static Document JavaDoc parse (
52             InputSource JavaDoc input,
53             boolean validate,
54             boolean namespaceAware,
55             ErrorHandler JavaDoc errorHandler,
56             EntityResolver JavaDoc entityResolver
57         ) throws IOException JavaDoc, SAXException JavaDoc {
58
59         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
60         factory.setValidating(validate);
61         factory.setNamespaceAware(namespaceAware);
62         DocumentBuilder JavaDoc builder = null;
63         try {
64              builder = factory.newDocumentBuilder();
65         } catch (ParserConfigurationException JavaDoc ex) {
66             throw new SAXException JavaDoc(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 JavaDoc createDocument(String JavaDoc rootQName) throws DOMException JavaDoc {
83         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
84         try {
85             return factory.newDocumentBuilder ().getDOMImplementation ().createDocument (null, rootQName, null);
86         } catch (ParserConfigurationException JavaDoc ex) {
87             throw (DOMException JavaDoc)new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); // NOI18N
88
}
89     }
90     
91     public static void write(Document JavaDoc doc, OutputStream JavaDoc out) throws IOException JavaDoc {
92         // XXX note that this may fail to write out namespaces correctly if the document
93
// is created with namespaces and no explicit prefixes; however no code in
94
// this package is likely to be doing so
95
try {
96             Transformer JavaDoc t = TransformerFactory.newInstance().newTransformer();
97             DocumentType JavaDoc dt = doc.getDoctype();
98             if (dt != null) {
99                 String JavaDoc 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"); // NOI18N
106
t.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
107
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
108
Source JavaDoc source = new DOMSource JavaDoc(doc);
109             Result JavaDoc result = new StreamResult JavaDoc(out);
110             t.transform(source, result);
111         } catch (Exception JavaDoc e) {
112             throw (IOException JavaDoc)new IOException JavaDoc(e.toString()).initCause(e);
113         } catch (TransformerFactoryConfigurationError JavaDoc e) {
114             throw (IOException JavaDoc)new IOException JavaDoc(e.toString()).initCause(e);
115         }
116     }
117
118     /** Entity resolver that knows about AU DTDs, so no network is needed.
119      * @author Jesse Glick
120      */

121     public static EntityResolver JavaDoc createAUResolver() {
122         return new EntityResolver JavaDoc() {
123             public InputSource JavaDoc resolveEntity(String JavaDoc publicID, String JavaDoc systemID) throws IOException JavaDoc, SAXException JavaDoc {
124                 if ("-//NetBeans//DTD Autoupdate Catalog 1.0//EN".equals(publicID)) { // NOI18N
125
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-catalog-1_0.dtd").toString());
126                 } else if ("-//NetBeans//DTD Autoupdate Module Info 1.0//EN".equals(publicID)) { // NOI18N
127
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-info-1_0.dtd").toString());
128                 } else if ("-//NetBeans//DTD Autoupdate Catalog 2.0//EN".equals(publicID)) { // NOI18N
129
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-catalog-2_0.dtd").toString());
130                 } else if ("-//NetBeans//DTD Autoupdate Module Info 2.0//EN".equals(publicID)) { // NOI18N
131
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-info-2_0.dtd").toString());
132                 } else if ("-//NetBeans//DTD Autoupdate Catalog 2.2//EN".equals(publicID)) { // NOI18N
133
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-catalog-2_2.dtd").toString());
134                 } else if ("-//NetBeans//DTD Autoupdate Module Info 2.2//EN".equals(publicID)) { // NOI18N
135
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-info-2_2.dtd").toString());
136                 } else if ("-//NetBeans//DTD Autoupdate Catalog 2.3//EN".equals(publicID)) { // NOI18N
137
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-catalog-2_3.dtd").toString());
138                 } else if ("-//NetBeans//DTD Autoupdate Module Info 2.3//EN".equals(publicID)) { // NOI18N
139
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-info-2_3.dtd").toString());
140                 } else if ("-//NetBeans//DTD Autoupdate Catalog 2.4//EN".equals(publicID)) { // NOI18N
141
return new InputSource JavaDoc(XMLUtil.class.getResource("resources/autoupdate-catalog-2_4.dtd").toString());
142                 } else if ("-//NetBeans//DTD Autoupdate Module Info 2.4//EN".equals(publicID)) { // NOI18N
143
return new InputSource JavaDoc(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