Lines 7-30
Link Here
|
7 |
* http://www.sun.com/ |
7 |
* http://www.sun.com/ |
8 |
* |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
12 |
*/ |
13 |
|
13 |
|
14 |
package org.netbeans.updater; |
14 |
package org.netbeans.updater; |
15 |
|
15 |
|
16 |
import java.io.*; |
16 |
import java.io.IOException; |
17 |
import java.lang.reflect.*; |
17 |
import java.io.OutputStream; |
18 |
|
18 |
import javax.xml.parsers.DocumentBuilder; |
19 |
import javax.xml.parsers.*; |
19 |
import javax.xml.parsers.DocumentBuilderFactory; |
20 |
|
20 |
import javax.xml.parsers.ParserConfigurationException; |
21 |
import org.w3c.dom.*; |
21 |
import javax.xml.transform.OutputKeys; |
22 |
import org.xml.sax.*; |
22 |
import javax.xml.transform.Result; |
|
|
23 |
import javax.xml.transform.Source; |
24 |
import javax.xml.transform.Transformer; |
25 |
import javax.xml.transform.TransformerFactory; |
26 |
import javax.xml.transform.TransformerFactoryConfigurationError; |
27 |
import javax.xml.transform.dom.DOMSource; |
28 |
import javax.xml.transform.stream.StreamResult; |
29 |
import org.w3c.dom.DOMException; |
30 |
import org.w3c.dom.DOMImplementation; |
31 |
import org.w3c.dom.Document; |
32 |
import org.w3c.dom.DocumentType; |
33 |
import org.xml.sax.EntityResolver; |
34 |
import org.xml.sax.ErrorHandler; |
35 |
import org.xml.sax.InputSource; |
36 |
import org.xml.sax.SAXException; |
23 |
|
37 |
|
24 |
/** |
38 |
/** |
25 |
* Utility class collecting library methods related to XML processing. |
39 |
* Utility class collecting library methods related to XML processing. |
26 |
* |
40 |
* Stolen from nbbuild/antsrc and openide/.../xml. |
27 |
* @author pkuzel |
41 |
* @author Petr Kuzel, Jesse Glick |
28 |
*/ |
42 |
*/ |
29 |
public final class XMLUtil extends Object { |
43 |
public final class XMLUtil extends Object { |
30 |
|
44 |
|
Lines 35-50
Link Here
|
35 |
ErrorHandler errorHandler, |
49 |
ErrorHandler errorHandler, |
36 |
EntityResolver entityResolver |
50 |
EntityResolver entityResolver |
37 |
) throws IOException, SAXException { |
51 |
) throws IOException, SAXException { |
38 |
|
52 |
|
39 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
53 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
40 |
factory.setValidating(validate); |
54 |
factory.setValidating(validate); |
41 |
factory.setNamespaceAware(namespaceAware); |
55 |
factory.setNamespaceAware(namespaceAware); |
42 |
|
|
|
43 |
DocumentBuilder builder = null; |
56 |
DocumentBuilder builder = null; |
44 |
try { |
57 |
try { |
45 |
builder = factory.newDocumentBuilder(); |
58 |
builder = factory.newDocumentBuilder(); |
46 |
} catch (ParserConfigurationException ex) { |
59 |
} catch (ParserConfigurationException ex) { |
47 |
throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N |
60 |
throw new SAXException(ex); |
48 |
} |
61 |
} |
49 |
|
62 |
|
50 |
if (errorHandler != null) { |
63 |
if (errorHandler != null) { |
Lines 59-68
Link Here
|
59 |
} |
72 |
} |
60 |
|
73 |
|
61 |
public static Document createDocument(String rootQName) throws DOMException { |
74 |
public static Document createDocument(String rootQName) throws DOMException { |
62 |
|
75 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
63 |
DOMImplementation impl = getDOMImplementation(); |
76 |
try { |
64 |
|
77 |
return factory.newDocumentBuilder().newDocument(); |
65 |
return impl.createDocument(null, rootQName, null); |
78 |
} catch (ParserConfigurationException ex) { |
|
|
79 |
throw (DOMException)new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); // NOI18N |
80 |
} |
66 |
} |
81 |
} |
67 |
|
82 |
|
68 |
private static DOMImplementation getDOMImplementation() throws DOMException { //can be made public |
83 |
private static DOMImplementation getDOMImplementation() throws DOMException { //can be made public |
Lines 72-169
Link Here
|
72 |
try { |
87 |
try { |
73 |
return factory.newDocumentBuilder().getDOMImplementation(); |
88 |
return factory.newDocumentBuilder().getDOMImplementation(); |
74 |
} catch (ParserConfigurationException ex) { |
89 |
} catch (ParserConfigurationException ex) { |
75 |
throw new DOMException(DOMException.NOT_SUPPORTED_ERR , "Cannot create parser satisfying configuration parameters"); //NOI18N |
90 |
throw (DOMException)new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); // NOI18N |
76 |
} |
91 |
} |
77 |
} |
92 |
} |
78 |
|
93 |
|
79 |
static void write(Document doc, OutputStream out) throws IOException { |
94 |
public static void write(Document doc, OutputStream out) throws IOException { |
80 |
Class dock = doc.getClass(); |
95 |
// XXX note that this may fail to write out namespaces correctly if the document |
81 |
|
96 |
// is created with namespaces and no explicit prefixes; however no code in |
82 |
// no implementation neutral write exist |
97 |
// this package is likely to be doing so |
83 |
try { |
98 |
try { |
84 |
if ("com.sun.xml.tree.XmlDocument".equals(dock.getName()) //NOI18N |
99 |
Transformer t = TransformerFactory.newInstance().newTransformer(); |
85 |
|| "org.apache.crimson.tree.XmlDocument".equals(dock.getName())) { //NOI18N |
100 |
DocumentType dt = doc.getDoctype(); |
86 |
// these DOM implementations are self writing |
101 |
if (dt != null) { |
87 |
Method write = dock.getDeclaredMethod("write", new Class[] {OutputStream.class});//NOI18N |
102 |
String pub = dt.getPublicId(); |
88 |
write.invoke(doc,new Object[] {out}); |
103 |
if (pub != null) { |
89 |
|
104 |
t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub); |
90 |
} else { |
105 |
} |
91 |
|
106 |
t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId()); |
92 |
Class serka = |
|
|
93 |
Class.forName("org.apache.xml.serialize.XMLSerializer"); //NOI18N |
94 |
|
95 |
Class forka = |
96 |
Class.forName("org.apache.xml.serialize.OutputFormat"); //NOI18N |
97 |
|
98 |
Object serin = serka.newInstance(); |
99 |
Object forin = forka.newInstance(); |
100 |
|
101 |
// hopefully it could improve output readability |
102 |
|
103 |
Method setmet = null; |
104 |
|
105 |
setmet = forka.getMethod("setMethod", new Class[] {String.class}); //NOI18N |
106 |
setmet.invoke(forin, new Object[] {"xml"}); //NOI18N |
107 |
|
108 |
setmet = forka.getMethod("setIndenting", new Class[] {Boolean.TYPE}); //NOI18N |
109 |
setmet.invoke(forin, new Object[] {Boolean.TRUE}); //NOI18N |
110 |
|
111 |
setmet = forka.getMethod("setLineWidth", new Class[] {Integer.TYPE}); //NOI18N |
112 |
setmet.invoke(forin, new Object[] {new Integer(0)}); //NOI18N |
113 |
|
114 |
String detectedEncoding = null; |
115 |
Method init; |
116 |
|
117 |
init = serka.getMethod("setOutputByteStream", new Class[] {OutputStream.class}); //NOI18N |
118 |
init.invoke(serin, new Object[] {out}); |
119 |
|
120 |
Method setenc = forka.getMethod("setEncoding", new Class[] {String.class}); //NOI18N |
121 |
setenc.invoke(forin, new Object[] {"UTF-8"} ); // NOI18N |
122 |
|
123 |
Method setout = serka.getMethod("setOutputFormat", new Class[] {forka}); //NOI18N |
124 |
setout.invoke(serin, new Object[] {forin}); |
125 |
|
126 |
Method asDOM = serka.getMethod("asDOMSerializer", new Class[0]);//NOI18N |
127 |
Object impl = asDOM.invoke(serin, new Object[0]); |
128 |
|
129 |
Method serialize = impl.getClass().getMethod("serialize", new Class[] {Document.class}); //NOI18N |
130 |
serialize.invoke(impl, new Object[] {doc}); |
131 |
|
132 |
} |
107 |
} |
133 |
|
108 |
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N |
134 |
} catch (IllegalAccessException ex) { |
109 |
t.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N |
135 |
handleImplementationException(ex); |
110 |
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N |
136 |
} catch (InstantiationException ex) { |
111 |
Source source = new DOMSource(doc); |
137 |
handleImplementationException(ex); |
112 |
Result result = new StreamResult(out); |
138 |
} catch (IllegalArgumentException ex) { |
113 |
t.transform(source, result); |
139 |
handleImplementationException(ex); |
114 |
} catch (Exception e) { |
140 |
} catch (NoSuchMethodException ex) { |
115 |
throw (IOException)new IOException(e.toString()).initCause(e); |
141 |
handleImplementationException(ex); |
116 |
} catch (TransformerFactoryConfigurationError e) { |
142 |
} catch (ClassNotFoundException ex) { |
117 |
throw (IOException)new IOException(e.toString()).initCause(e); |
143 |
handleImplementationException(ex); |
|
|
144 |
} catch (InvocationTargetException ex) { |
145 |
handleTargetException(ex); |
146 |
} |
118 |
} |
147 |
|
|
|
148 |
} |
149 |
|
150 |
|
151 |
/** TargetException handler */ |
152 |
private static void handleTargetException(InvocationTargetException ex) throws IOException { |
153 |
Throwable t = ex.getTargetException(); |
154 |
if (t instanceof IOException) { |
155 |
throw (IOException) t; |
156 |
} else if (t instanceof RuntimeException) { |
157 |
throw (RuntimeException) t; |
158 |
} else if (t instanceof Error) { |
159 |
throw (Error) t; |
160 |
} |
161 |
} |
162 |
|
163 |
private static void handleImplementationException(Exception ex) throws IOException { |
164 |
StringWriter wr = new StringWriter(); |
165 |
ex.printStackTrace(new PrintWriter(wr)); //jessie could you provide the [catch] code |
166 |
throw new IOException("Unsupported DOM Document implementation!\n" + wr.toString() ); // NOI18N |
167 |
} |
119 |
} |
168 |
|
120 |
|
169 |
/** Entity resolver that knows about AU DTDs, so no network is needed. |
121 |
/** Entity resolver that knows about AU DTDs, so no network is needed. |