Index: autoupdate/libsrc/org/netbeans/updater/XMLUtil.java =================================================================== RCS file: /cvs/autoupdate/libsrc/org/netbeans/updater/XMLUtil.java,v retrieving revision 1.6 diff -u -r1.6 XMLUtil.java --- autoupdate/libsrc/org/netbeans/updater/XMLUtil.java 24 Apr 2003 18:44:56 -0000 1.6 +++ autoupdate/libsrc/org/netbeans/updater/XMLUtil.java 21 Jan 2004 15:48:34 -0000 @@ -7,24 +7,38 @@ * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original - * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.updater; -import java.io.*; -import java.lang.reflect.*; - -import javax.xml.parsers.*; - -import org.w3c.dom.*; -import org.xml.sax.*; +import java.io.IOException; +import java.io.OutputStream; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; /** * Utility class collecting library methods related to XML processing. - * - * @author pkuzel + * Stolen from nbbuild/antsrc and openide/.../xml. + * @author Petr Kuzel, Jesse Glick */ public final class XMLUtil extends Object { @@ -35,16 +49,15 @@ ErrorHandler errorHandler, EntityResolver entityResolver ) throws IOException, SAXException { - + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); - DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { - throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N + throw new SAXException(ex); } if (errorHandler != null) { @@ -59,10 +72,12 @@ } public static Document createDocument(String rootQName) throws DOMException { - - DOMImplementation impl = getDOMImplementation(); - - return impl.createDocument(null, rootQName, null); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + return factory.newDocumentBuilder().newDocument(); + } catch (ParserConfigurationException ex) { + throw (DOMException)new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); // NOI18N + } } private static DOMImplementation getDOMImplementation() throws DOMException { //can be made public @@ -72,98 +87,35 @@ try { return factory.newDocumentBuilder().getDOMImplementation(); } catch (ParserConfigurationException ex) { - throw new DOMException(DOMException.NOT_SUPPORTED_ERR , "Cannot create parser satisfying configuration parameters"); //NOI18N + throw (DOMException)new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); // NOI18N } } - static void write(Document doc, OutputStream out) throws IOException { - Class dock = doc.getClass(); - - // no implementation neutral write exist + public static void write(Document doc, OutputStream out) throws IOException { + // XXX note that this may fail to write out namespaces correctly if the document + // is created with namespaces and no explicit prefixes; however no code in + // this package is likely to be doing so try { - if ("com.sun.xml.tree.XmlDocument".equals(dock.getName()) //NOI18N - || "org.apache.crimson.tree.XmlDocument".equals(dock.getName())) { //NOI18N - // these DOM implementations are self writing - Method write = dock.getDeclaredMethod("write", new Class[] {OutputStream.class});//NOI18N - write.invoke(doc,new Object[] {out}); - - } else { - - Class serka = - Class.forName("org.apache.xml.serialize.XMLSerializer"); //NOI18N - - Class forka = - Class.forName("org.apache.xml.serialize.OutputFormat"); //NOI18N - - Object serin = serka.newInstance(); - Object forin = forka.newInstance(); - - // hopefully it could improve output readability - - Method setmet = null; - - setmet = forka.getMethod("setMethod", new Class[] {String.class}); //NOI18N - setmet.invoke(forin, new Object[] {"xml"}); //NOI18N - - setmet = forka.getMethod("setIndenting", new Class[] {Boolean.TYPE}); //NOI18N - setmet.invoke(forin, new Object[] {Boolean.TRUE}); //NOI18N - - setmet = forka.getMethod("setLineWidth", new Class[] {Integer.TYPE}); //NOI18N - setmet.invoke(forin, new Object[] {new Integer(0)}); //NOI18N - - String detectedEncoding = null; - Method init; - - init = serka.getMethod("setOutputByteStream", new Class[] {OutputStream.class}); //NOI18N - init.invoke(serin, new Object[] {out}); - - Method setenc = forka.getMethod("setEncoding", new Class[] {String.class}); //NOI18N - setenc.invoke(forin, new Object[] {"UTF-8"} ); // NOI18N - - Method setout = serka.getMethod("setOutputFormat", new Class[] {forka}); //NOI18N - setout.invoke(serin, new Object[] {forin}); - - Method asDOM = serka.getMethod("asDOMSerializer", new Class[0]);//NOI18N - Object impl = asDOM.invoke(serin, new Object[0]); - - Method serialize = impl.getClass().getMethod("serialize", new Class[] {Document.class}); //NOI18N - serialize.invoke(impl, new Object[] {doc}); - + Transformer t = TransformerFactory.newInstance().newTransformer(); + DocumentType dt = doc.getDoctype(); + if (dt != null) { + String pub = dt.getPublicId(); + if (pub != null) { + t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub); + } + t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId()); } - - } catch (IllegalAccessException ex) { - handleImplementationException(ex); - } catch (InstantiationException ex) { - handleImplementationException(ex); - } catch (IllegalArgumentException ex) { - handleImplementationException(ex); - } catch (NoSuchMethodException ex) { - handleImplementationException(ex); - } catch (ClassNotFoundException ex) { - handleImplementationException(ex); - } catch (InvocationTargetException ex) { - handleTargetException(ex); + t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N + t.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N + t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N + Source source = new DOMSource(doc); + Result result = new StreamResult(out); + t.transform(source, result); + } catch (Exception e) { + throw (IOException)new IOException(e.toString()).initCause(e); + } catch (TransformerFactoryConfigurationError e) { + throw (IOException)new IOException(e.toString()).initCause(e); } - - } - - - /** TargetException handler */ - private static void handleTargetException(InvocationTargetException ex) throws IOException { - Throwable t = ex.getTargetException(); - if (t instanceof IOException) { - throw (IOException) t; - } else if (t instanceof RuntimeException) { - throw (RuntimeException) t; - } else if (t instanceof Error) { - throw (Error) t; - } - } - - private static void handleImplementationException(Exception ex) throws IOException { - StringWriter wr = new StringWriter(); - ex.printStackTrace(new PrintWriter(wr)); //jessie could you provide the [catch] code - throw new IOException("Unsupported DOM Document implementation!\n" + wr.toString() ); // NOI18N } /** Entity resolver that knows about AU DTDs, so no network is needed.