1 /* |
1 /* |
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.xml.internal.ws.util; |
26 package com.sun.xml.internal.ws.util; |
27 |
27 |
28 import org.w3c.dom.Document; |
28 import com.sun.istack.internal.NotNull; |
29 import org.w3c.dom.NamedNodeMap; |
29 import com.sun.istack.internal.Nullable; |
30 import org.w3c.dom.Node; |
30 import com.sun.xml.internal.ws.util.xml.XmlUtil; |
31 import org.w3c.dom.NodeList; |
31 import org.w3c.dom.*; |
32 import org.w3c.dom.Element; |
32 |
33 import org.xml.sax.SAXException; |
33 import javax.xml.XMLConstants; |
34 |
34 import javax.xml.namespace.NamespaceContext; |
35 import javax.xml.parsers.DocumentBuilder; |
35 import javax.xml.parsers.DocumentBuilder; |
36 import javax.xml.parsers.DocumentBuilderFactory; |
36 import javax.xml.parsers.DocumentBuilderFactory; |
37 import javax.xml.parsers.FactoryConfigurationError; |
37 import javax.xml.parsers.FactoryConfigurationError; |
38 import javax.xml.parsers.ParserConfigurationException; |
38 import javax.xml.parsers.ParserConfigurationException; |
39 import javax.xml.stream.XMLStreamException; |
39 import javax.xml.stream.XMLStreamException; |
40 import javax.xml.stream.XMLStreamWriter; |
40 import javax.xml.stream.XMLStreamWriter; |
41 import javax.xml.XMLConstants; |
41 import java.util.ArrayList; |
42 import javax.xml.namespace.NamespaceContext; |
|
43 import java.io.IOException; |
|
44 import java.io.InputStream; |
|
45 import java.util.Iterator; |
42 import java.util.Iterator; |
46 import java.util.List; |
43 import java.util.List; |
47 import java.util.ArrayList; |
|
48 |
|
49 import com.sun.istack.internal.NotNull; |
|
50 import com.sun.istack.internal.Nullable; |
|
51 |
44 |
52 /** |
45 /** |
53 * @author: JAXWS Development Team |
46 * @author JAXWS Development Team |
54 */ |
47 */ |
55 public class DOMUtil { |
48 public class DOMUtil { |
56 |
49 |
57 private static DocumentBuilder db; |
50 private static DocumentBuilder db; |
58 |
51 |
61 */ |
54 */ |
62 public static Document createDom() { |
55 public static Document createDom() { |
63 synchronized (DOMUtil.class) { |
56 synchronized (DOMUtil.class) { |
64 if (db == null) { |
57 if (db == null) { |
65 try { |
58 try { |
66 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
59 DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory(); |
67 dbf.setNamespaceAware(true); |
60 dbf.setNamespaceAware(true); |
68 db = dbf.newDocumentBuilder(); |
61 db = dbf.newDocumentBuilder(); |
69 } catch (ParserConfigurationException e) { |
62 } catch (ParserConfigurationException e) { |
70 throw new FactoryConfigurationError(e); |
63 throw new FactoryConfigurationError(e); |
71 } |
64 } |
72 } |
65 } |
73 return db.newDocument(); |
66 return db.newDocument(); |
74 } |
67 } |
75 } |
|
76 |
|
77 public static Node createDOMNode(InputStream inputStream) { |
|
78 |
|
79 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|
80 dbf.setNamespaceAware(true); |
|
81 dbf.setValidating(false); |
|
82 try { |
|
83 DocumentBuilder builder = dbf.newDocumentBuilder(); |
|
84 try { |
|
85 return builder.parse(inputStream); |
|
86 } catch (SAXException e) { |
|
87 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
|
88 } catch (IOException e) { |
|
89 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
|
90 } |
|
91 } catch (ParserConfigurationException pce) { |
|
92 IllegalArgumentException iae = new IllegalArgumentException(pce.getMessage()); |
|
93 iae.initCause(pce); |
|
94 throw iae; |
|
95 } |
|
96 return null; |
|
97 } |
68 } |
98 |
69 |
99 /** |
70 /** |
100 * Traverses a DOM node and writes out on a streaming writer. |
71 * Traverses a DOM node and writes out on a streaming writer. |
101 * |
72 * |
110 for (int i = 0; i < children.getLength(); i++) { |
81 for (int i = 0; i < children.getLength(); i++) { |
111 Node child = children.item(i); |
82 Node child = children.item(i); |
112 switch (child.getNodeType()) { |
83 switch (child.getNodeType()) { |
113 case Node.PROCESSING_INSTRUCTION_NODE: |
84 case Node.PROCESSING_INSTRUCTION_NODE: |
114 writer.writeProcessingInstruction(child.getNodeValue()); |
85 writer.writeProcessingInstruction(child.getNodeValue()); |
|
86 break; |
115 case Node.DOCUMENT_TYPE_NODE: |
87 case Node.DOCUMENT_TYPE_NODE: |
116 break; |
88 break; |
117 case Node.CDATA_SECTION_NODE: |
89 case Node.CDATA_SECTION_NODE: |
118 writer.writeCData(child.getNodeValue()); |
90 writer.writeCData(child.getNodeValue()); |
119 break; |
91 break; |
220 */ |
193 */ |
221 public static Element getFirstChild(Element e, String nsUri, String local) { |
194 public static Element getFirstChild(Element e, String nsUri, String local) { |
222 for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) { |
195 for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) { |
223 if (n.getNodeType() == Node.ELEMENT_NODE) { |
196 if (n.getNodeType() == Node.ELEMENT_NODE) { |
224 Element c = (Element) n; |
197 Element c = (Element) n; |
225 if (c.getLocalName().equals(local) && c.getNamespaceURI().equals(nsUri)) |
198 if (c.getLocalName().equals(local) && c.getNamespaceURI().equals(nsUri)) { |
226 return c; |
199 return c; |
|
200 } |
227 } |
201 } |
228 } |
202 } |
229 return null; |
203 return null; |
230 } |
204 } |
231 |
205 |
232 private static |
206 private static |
233 @NotNull |
207 @NotNull |
234 String fixNull(@Nullable String s) { |
208 String fixNull(@Nullable String s) { |
235 if (s == null) return ""; |
209 if (s == null) { |
236 else return s; |
210 return ""; |
|
211 } else { |
|
212 return s; |
|
213 } |
237 } |
214 } |
238 |
215 |
239 /** |
216 /** |
240 * Gets the first element child. |
217 * Gets the first element child. |
241 */ |
218 */ |