comparison WebServiceToolWorkflow/lib/SAWADLParser/src/edu/uga/cs/lsdis/meteors/wadls/util/xml/DOM2Writer.java @ 0:d5cd409b8a18 default tip

Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author ganjoo
date Tue, 07 Jun 2011 18:00:50 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d5cd409b8a18
1 /*
2 * (c) Copyright IBM Corp 2001, 2005
3 */
4
5 package edu.uga.cs.lsdis.meteors.wadls.util.xml;
6
7 import java.io.*;
8 import java.util.*;
9 import org.w3c.dom.*;
10
11 import edu.uga.cs.lsdis.meteors.wadls.*;
12 import edu.uga.cs.lsdis.meteors.wadls.util.*;
13
14 /**
15 * This class is a utility to serialize a DOM node as XML. This class
16 * uses the <code>DOM Level 2</code> APIs.
17 * The main difference between this class and DOMWriter is that this class
18 * generates and prints out namespace declarations.
19 *
20 * @author Matthew J. Duftler (duftler@us.ibm.com)
21 * @author Joseph Kesselman
22 */
23 public class DOM2Writer
24 {
25 /**
26 * The namespaceURI represented by the prefix <code>xmlns</code>.
27 */
28 private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";
29
30 /**
31 * The namespaceURI represented by the prefix <code>xml</code>.
32 */
33 private static String NS_URI_XML = "http://www.w3.org/XML/1998/namespace";
34
35 private static Map xmlEncodingMap = new HashMap();
36
37 static
38 {
39 xmlEncodingMap.put(null, Constants.XML_DECL_DEFAULT);
40 xmlEncodingMap.put(System.getProperty("file.encoding"),
41 Constants.XML_DECL_DEFAULT);
42 xmlEncodingMap.put("UTF8", "UTF-8");
43 xmlEncodingMap.put("UTF-16", "UTF-16");
44 xmlEncodingMap.put("UnicodeBig", "UTF-16");
45 xmlEncodingMap.put("UnicodeLittle", "UTF-16");
46 xmlEncodingMap.put("ASCII", "US-ASCII");
47 xmlEncodingMap.put("ISO8859_1", "ISO-8859-1");
48 xmlEncodingMap.put("ISO8859_2", "ISO-8859-2");
49 xmlEncodingMap.put("ISO8859_3", "ISO-8859-3");
50 xmlEncodingMap.put("ISO8859_4", "ISO-8859-4");
51 xmlEncodingMap.put("ISO8859_5", "ISO-8859-5");
52 xmlEncodingMap.put("ISO8859_6", "ISO-8859-6");
53 xmlEncodingMap.put("ISO8859_7", "ISO-8859-7");
54 xmlEncodingMap.put("ISO8859_8", "ISO-8859-8");
55 xmlEncodingMap.put("ISO8859_9", "ISO-8859-9");
56 xmlEncodingMap.put("ISO8859_13", "ISO-8859-13");
57 xmlEncodingMap.put("ISO8859_15_FDIS", "ISO-8859-15");
58 xmlEncodingMap.put("GBK", "GBK");
59 xmlEncodingMap.put("Big5", "Big5");
60 }
61
62 /**
63 * Return a string containing this node serialized as XML.
64 */
65 public static String nodeToString(Node node)
66 {
67 StringWriter sw = new StringWriter();
68
69 serializeAsXML(node, sw);
70
71 return sw.toString();
72 }
73
74
75 /**
76 * Print an XML declaration before serializing the element.
77 */
78 public static void serializeElementAsDocument(Element el, Writer writer)
79 {
80 PrintWriter pw = new PrintWriter(writer);
81 String javaEncoding = (writer instanceof OutputStreamWriter)
82 ? ((OutputStreamWriter) writer).getEncoding()
83 : null;
84
85 String xmlEncoding = java2XMLEncoding(javaEncoding);
86
87 if (xmlEncoding != null)
88 {
89 pw.println(Constants.XML_DECL_START +
90 xmlEncoding +
91 Constants.XML_DECL_END);
92 }
93 else
94 {
95 pw.println("<?xml version=\"1.0\"?>");
96 }
97
98 serializeAsXML(el, writer);
99 }
100
101 /**
102 * Serialize this node into the writer as XML.
103 */
104 public static void serializeAsXML(Node node, Writer writer)
105 {
106 ObjectRegistry namespaceStack = new ObjectRegistry();
107
108 namespaceStack.register("xml", NS_URI_XML);
109
110 PrintWriter pw = new PrintWriter(writer);
111 String javaEncoding = (writer instanceof OutputStreamWriter)
112 ? ((OutputStreamWriter) writer).getEncoding()
113 : null;
114
115 print(node, namespaceStack, pw, java2XMLEncoding(javaEncoding));
116 }
117
118 private static void print(Node node, ObjectRegistry namespaceStack,
119 PrintWriter out, String xmlEncoding)
120 {
121 if (node == null)
122 {
123 return;
124 }
125
126 boolean hasChildren = false;
127 int type = node.getNodeType();
128
129 switch (type)
130 {
131 case Node.DOCUMENT_NODE :
132 {
133 if (xmlEncoding != null)
134 {
135 out.println(Constants.XML_DECL_START +
136 xmlEncoding +
137 Constants.XML_DECL_END);
138 }
139 else
140 {
141 out.println("<?xml version=\"1.0\"?>");
142 }
143
144 NodeList children = node.getChildNodes();
145
146 if (children != null)
147 {
148 int numChildren = children.getLength();
149
150 for (int i = 0; i < numChildren; i++)
151 {
152 print(children.item(i), namespaceStack, out, xmlEncoding);
153 }
154 }
155 break;
156 }
157
158 case Node.ELEMENT_NODE :
159 {
160 namespaceStack = new ObjectRegistry(namespaceStack);
161
162 out.print('<' + node.getNodeName());
163
164 String elPrefix = node.getPrefix();
165 String elNamespaceURI = node.getNamespaceURI();
166
167 if (elPrefix != null && elNamespaceURI != null)
168 {
169 boolean prefixIsDeclared = false;
170
171 try
172 {
173 String namespaceURI = (String)namespaceStack.lookup(elPrefix);
174
175 if (elNamespaceURI.equals(namespaceURI))
176 {
177 prefixIsDeclared = true;
178 }
179 }
180 catch (IllegalArgumentException e)
181 {
182 // ignore this and carry on
183 }
184
185 if (!prefixIsDeclared)
186 {
187 printNamespaceDecl(node, namespaceStack, out);
188 }
189 }
190
191 NamedNodeMap attrs = node.getAttributes();
192 int len = (attrs != null) ? attrs.getLength() : 0;
193
194 for (int i = 0; i < len; i++)
195 {
196 Attr attr = (Attr)attrs.item(i);
197
198 out.print(' ' + attr.getNodeName() +"=\"" +
199 normalize(attr.getValue()) + '\"');
200
201 String attrPrefix = attr.getPrefix();
202 String attrNamespaceURI = attr.getNamespaceURI();
203
204 if (attrPrefix != null && attrNamespaceURI != null)
205 {
206 boolean prefixIsDeclared = false;
207
208 try
209 {
210 String namespaceURI = (String)namespaceStack.lookup(attrPrefix);
211
212 if (attrNamespaceURI.equals(namespaceURI))
213 {
214 prefixIsDeclared = true;
215 }
216 }
217 catch (IllegalArgumentException e)
218 {
219 // ignore this and carry on
220 }
221
222 if (!prefixIsDeclared)
223 {
224 printNamespaceDecl(attr, namespaceStack, out);
225 }
226 }
227 }
228
229 NodeList children = node.getChildNodes();
230
231 if (children != null)
232 {
233 int numChildren = children.getLength();
234
235 hasChildren = (numChildren > 0);
236
237 if (hasChildren)
238 {
239 out.print('>');
240 }
241
242 for (int i = 0; i < numChildren; i++)
243 {
244 print(children.item(i), namespaceStack, out, xmlEncoding);
245 }
246 }
247 else
248 {
249 hasChildren = false;
250 }
251
252 if (!hasChildren)
253 {
254 out.print("/>");
255 }
256 break;
257 }
258
259 case Node.ENTITY_REFERENCE_NODE :
260 {
261 out.print('&');
262 out.print(node.getNodeName());
263 out.print(';');
264 break;
265 }
266
267 case Node.CDATA_SECTION_NODE :
268 {
269 out.print("<![CDATA[");
270 out.print(node.getNodeValue());
271 out.print("]]>");
272 break;
273 }
274
275 case Node.TEXT_NODE :
276 {
277 out.print(normalize(node.getNodeValue()));
278 break;
279 }
280
281 case Node.COMMENT_NODE :
282 {
283 out.print("<!--");
284 out.print(node.getNodeValue());
285 out.print("-->");
286 break;
287 }
288
289 case Node.PROCESSING_INSTRUCTION_NODE :
290 {
291 out.print("<?");
292 out.print(node.getNodeName());
293
294 String data = node.getNodeValue();
295
296 if (data != null && data.length() > 0)
297 {
298 out.print(' ');
299 out.print(data);
300 }
301
302 out.println("?>");
303 break;
304 }
305 }
306
307 if (type == Node.ELEMENT_NODE && hasChildren == true)
308 {
309 out.print("</");
310 out.print(node.getNodeName());
311 out.print('>');
312 hasChildren = false;
313 }
314 }
315
316 public static String java2XMLEncoding(String javaEnc)
317 {
318 return (String)xmlEncodingMap.get(javaEnc);
319 }
320
321
322 private static void printNamespaceDecl(Node node,
323 ObjectRegistry namespaceStack,
324 PrintWriter out)
325 {
326 switch (node.getNodeType())
327 {
328 case Node.ATTRIBUTE_NODE :
329 {
330 printNamespaceDecl(((Attr)node).getOwnerElement(), node,
331 namespaceStack, out);
332 break;
333 }
334
335 case Node.ELEMENT_NODE :
336 {
337 printNamespaceDecl((Element)node, node, namespaceStack, out);
338 break;
339 }
340 }
341 }
342
343 private static void printNamespaceDecl(Element owner, Node node,
344 ObjectRegistry namespaceStack,
345 PrintWriter out)
346 {
347 String namespaceURI = node.getNamespaceURI();
348 String prefix = node.getPrefix();
349
350 if (!(namespaceURI.equals(NS_URI_XMLNS) && prefix.equals("xmlns")))
351 {
352 if (DOMUtils.getAttributeNS(owner, NS_URI_XMLNS, prefix) == null)
353 {
354 out.print(" xmlns:" + prefix + "=\"" + namespaceURI + '\"');
355 }
356 }
357 else
358 {
359 prefix = node.getLocalName();
360 namespaceURI = node.getNodeValue();
361 }
362
363 namespaceStack.register(prefix, namespaceURI);
364 }
365
366 private static String normalize(String s)
367 {
368 StringBuffer str = new StringBuffer();
369 int len = (s != null) ? s.length() : 0;
370
371 for (int i = 0; i < len; i++)
372 {
373 char ch = s.charAt(i);
374
375 switch (ch)
376 {
377 case '<' :
378 {
379 str.append("&lt;");
380 break;
381 }
382 case '>' :
383 {
384 str.append("&gt;");
385 break;
386 }
387 case '&' :
388 {
389 str.append("&amp;");
390 break;
391 }
392 case '"' :
393 {
394 str.append("&quot;");
395 break;
396 }
397 case '\n' :
398 {
399 if (i > 0)
400 {
401 char lastChar = str.charAt(str.length() - 1);
402
403 if (lastChar != '\r')
404 {
405 str.append(StringUtils.lineSeparator);
406 }
407 else
408 {
409 str.append('\n');
410 }
411 }
412 else
413 {
414 str.append(StringUtils.lineSeparator);
415 }
416 break;
417 }
418 default :
419 {
420 str.append(ch);
421 }
422 }
423 }
424
425 return (str.toString());
426 }
427 }