001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.updater;
043:
044: import java.io.IOException;
045: import java.io.OutputStream;
046: import javax.xml.parsers.DocumentBuilder;
047: import javax.xml.parsers.DocumentBuilderFactory;
048: import javax.xml.parsers.ParserConfigurationException;
049: import javax.xml.transform.OutputKeys;
050: import javax.xml.transform.Result;
051: import javax.xml.transform.Source;
052: import javax.xml.transform.Transformer;
053: import javax.xml.transform.TransformerFactory;
054: import javax.xml.transform.TransformerFactoryConfigurationError;
055: import javax.xml.transform.dom.DOMSource;
056: import javax.xml.transform.stream.StreamResult;
057: import org.w3c.dom.DOMException;
058: import org.w3c.dom.DOMImplementation;
059: import org.w3c.dom.Document;
060: import org.w3c.dom.DocumentType;
061: import org.xml.sax.EntityResolver;
062: import org.xml.sax.ErrorHandler;
063: import org.xml.sax.InputSource;
064: import org.xml.sax.SAXException;
065:
066: /**
067: * Utility class collecting library methods related to XML processing.
068: * Stolen from nbbuild/antsrc and openide/.../xml.
069: * @author Petr Kuzel, Jesse Glick
070: */
071: public final class XMLUtil extends Object {
072:
073: public static Document parse(InputSource input, boolean validate,
074: boolean namespaceAware, ErrorHandler errorHandler,
075: EntityResolver entityResolver) throws IOException,
076: SAXException {
077:
078: DocumentBuilderFactory factory = DocumentBuilderFactory
079: .newInstance();
080: factory.setValidating(validate);
081: factory.setNamespaceAware(namespaceAware);
082: DocumentBuilder builder = null;
083: try {
084: builder = factory.newDocumentBuilder();
085: } catch (ParserConfigurationException ex) {
086: throw new SAXException(ex);
087: }
088:
089: if (errorHandler != null) {
090: builder.setErrorHandler(errorHandler);
091: }
092:
093: if (entityResolver != null) {
094: builder.setEntityResolver(entityResolver);
095: }
096:
097: assert input != null : "InputSource cannot be null";
098:
099: return builder.parse(input);
100: }
101:
102: public static Document createDocument(String rootQName)
103: throws DOMException {
104: DocumentBuilderFactory factory = DocumentBuilderFactory
105: .newInstance();
106: try {
107: return factory.newDocumentBuilder().getDOMImplementation()
108: .createDocument(null, rootQName, null);
109: } catch (ParserConfigurationException ex) {
110: throw (DOMException) new DOMException(
111: DOMException.NOT_SUPPORTED_ERR,
112: "Cannot create parser").initCause(ex); // NOI18N
113: }
114: }
115:
116: public static void write(Document doc, OutputStream out)
117: throws IOException {
118: // XXX note that this may fail to write out namespaces correctly if the document
119: // is created with namespaces and no explicit prefixes; however no code in
120: // this package is likely to be doing so
121: try {
122: Transformer t = TransformerFactory.newInstance()
123: .newTransformer();
124: DocumentType dt = doc.getDoctype();
125: if (dt != null) {
126: String pub = dt.getPublicId();
127: if (pub != null) {
128: t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
129: }
130: t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt
131: .getSystemId());
132: }
133: t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
134: t.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
135: t.setOutputProperty(
136: "{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
137: Source source = new DOMSource(doc);
138: Result result = new StreamResult(out);
139: t.transform(source, result);
140: } catch (Exception e) {
141: throw (IOException) new IOException(e.toString())
142: .initCause(e);
143: } catch (TransformerFactoryConfigurationError e) {
144: throw (IOException) new IOException(e.toString())
145: .initCause(e);
146: }
147: }
148:
149: /** Entity resolver that knows about AU DTDs, so no network is needed.
150: * @author Jesse Glick
151: */
152: public static EntityResolver createAUResolver() {
153: return new EntityResolver() {
154: public InputSource resolveEntity(String publicID,
155: String systemID) throws IOException, SAXException {
156: if ("-//NetBeans//DTD Autoupdate Catalog 1.0//EN"
157: .equals(publicID)) { // NOI18N
158: return new InputSource(XMLUtil.class.getResource(
159: "resources/autoupdate-catalog-1_0.dtd")
160: .toString());
161: } else if ("-//NetBeans//DTD Autoupdate Module Info 1.0//EN"
162: .equals(publicID)) { // NOI18N
163: return new InputSource(XMLUtil.class.getResource(
164: "resources/autoupdate-info-1_0.dtd")
165: .toString());
166: } else if ("-//NetBeans//DTD Autoupdate Catalog 2.0//EN"
167: .equals(publicID)) { // NOI18N
168: return new InputSource(XMLUtil.class.getResource(
169: "resources/autoupdate-catalog-2_0.dtd")
170: .toString());
171: } else if ("-//NetBeans//DTD Autoupdate Module Info 2.0//EN"
172: .equals(publicID)) { // NOI18N
173: return new InputSource(XMLUtil.class.getResource(
174: "resources/autoupdate-info-2_0.dtd")
175: .toString());
176: } else if ("-//NetBeans//DTD Autoupdate Catalog 2.2//EN"
177: .equals(publicID)) { // NOI18N
178: return new InputSource(XMLUtil.class.getResource(
179: "resources/autoupdate-catalog-2_2.dtd")
180: .toString());
181: } else if ("-//NetBeans//DTD Autoupdate Module Info 2.2//EN"
182: .equals(publicID)) { // NOI18N
183: return new InputSource(XMLUtil.class.getResource(
184: "resources/autoupdate-info-2_2.dtd")
185: .toString());
186: } else if ("-//NetBeans//DTD Autoupdate Catalog 2.3//EN"
187: .equals(publicID)) { // NOI18N
188: return new InputSource(XMLUtil.class.getResource(
189: "resources/autoupdate-catalog-2_3.dtd")
190: .toString());
191: } else if ("-//NetBeans//DTD Autoupdate Module Info 2.3//EN"
192: .equals(publicID)) { // NOI18N
193: return new InputSource(XMLUtil.class.getResource(
194: "resources/autoupdate-info-2_3.dtd")
195: .toString());
196: } else if ("-//NetBeans//DTD Autoupdate Catalog 2.4//EN"
197: .equals(publicID)) { // NOI18N
198: return new InputSource(XMLUtil.class.getResource(
199: "resources/autoupdate-catalog-2_4.dtd")
200: .toString());
201: } else if ("-//NetBeans//DTD Autoupdate Module Info 2.4//EN"
202: .equals(publicID)) { // NOI18N
203: return new InputSource(XMLUtil.class.getResource(
204: "resources/autoupdate-info-2_4.dtd")
205: .toString());
206: } else if ("-//NetBeans//DTD Autoupdate Catalog 2.5//EN"
207: .equals(publicID)) { // NOI18N
208: return new InputSource(XMLUtil.class.getResource(
209: "resources/autoupdate-catalog-2_5.dtd")
210: .toString());
211: } else if ("-//NetBeans//DTD Autoupdate Module Info 2.5//EN"
212: .equals(publicID)) { // NOI18N
213: return new InputSource(XMLUtil.class.getResource(
214: "resources/autoupdate-info-2_5.dtd")
215: .toString());
216: } else {
217: return null;
218: }
219: }
220: };
221: }
222:
223: }
|