author | aefimov |
Fri, 10 Apr 2015 14:54:20 +0300 | |
changeset 29839 | 6d5d546e953b |
parent 25871 | b80b84e87032 |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.ws.util; |
|
27 |
||
16791 | 28 |
import com.sun.istack.internal.NotNull; |
29 |
import com.sun.istack.internal.Nullable; |
|
30 |
import com.sun.xml.internal.ws.util.xml.XmlUtil; |
|
31 |
import org.w3c.dom.*; |
|
12009 | 32 |
|
16791 | 33 |
import javax.xml.XMLConstants; |
34 |
import javax.xml.namespace.NamespaceContext; |
|
12009 | 35 |
import javax.xml.parsers.DocumentBuilder; |
36 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
37 |
import javax.xml.parsers.FactoryConfigurationError; |
|
38 |
import javax.xml.parsers.ParserConfigurationException; |
|
39 |
import javax.xml.stream.XMLStreamException; |
|
40 |
import javax.xml.stream.XMLStreamWriter; |
|
16791 | 41 |
import java.util.ArrayList; |
12009 | 42 |
import java.util.Iterator; |
43 |
import java.util.List; |
|
44 |
||
45 |
/** |
|
16791 | 46 |
* @author JAXWS Development Team |
12009 | 47 |
*/ |
48 |
public class DOMUtil { |
|
49 |
||
50 |
private static DocumentBuilder db; |
|
51 |
||
52 |
/** |
|
53 |
* Creates a new DOM document. |
|
54 |
*/ |
|
55 |
public static Document createDom() { |
|
56 |
synchronized (DOMUtil.class) { |
|
57 |
if (db == null) { |
|
58 |
try { |
|
29839
6d5d546e953b
8076549: Update JAX-WS RI integration to latest version (2.2.11-b150402.1412)
aefimov
parents:
25871
diff
changeset
|
59 |
DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory(true); |
12009 | 60 |
dbf.setNamespaceAware(true); |
61 |
db = dbf.newDocumentBuilder(); |
|
62 |
} catch (ParserConfigurationException e) { |
|
63 |
throw new FactoryConfigurationError(e); |
|
64 |
} |
|
65 |
} |
|
66 |
return db.newDocument(); |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Traverses a DOM node and writes out on a streaming writer. |
|
72 |
* |
|
73 |
* @param node |
|
74 |
* @param writer |
|
75 |
*/ |
|
76 |
public static void serializeNode(Element node, XMLStreamWriter writer) throws XMLStreamException { |
|
77 |
writeTagWithAttributes(node, writer); |
|
78 |
||
79 |
if (node.hasChildNodes()) { |
|
80 |
NodeList children = node.getChildNodes(); |
|
81 |
for (int i = 0; i < children.getLength(); i++) { |
|
82 |
Node child = children.item(i); |
|
83 |
switch (child.getNodeType()) { |
|
84 |
case Node.PROCESSING_INSTRUCTION_NODE: |
|
85 |
writer.writeProcessingInstruction(child.getNodeValue()); |
|
16791 | 86 |
break; |
12009 | 87 |
case Node.DOCUMENT_TYPE_NODE: |
88 |
break; |
|
89 |
case Node.CDATA_SECTION_NODE: |
|
90 |
writer.writeCData(child.getNodeValue()); |
|
91 |
break; |
|
92 |
case Node.COMMENT_NODE: |
|
93 |
writer.writeComment(child.getNodeValue()); |
|
94 |
break; |
|
95 |
case Node.TEXT_NODE: |
|
96 |
writer.writeCharacters(child.getNodeValue()); |
|
97 |
break; |
|
98 |
case Node.ELEMENT_NODE: |
|
99 |
serializeNode((Element) child, writer); |
|
100 |
break; |
|
16791 | 101 |
default: break; |
12009 | 102 |
} |
103 |
} |
|
104 |
} |
|
105 |
writer.writeEndElement(); |
|
106 |
} |
|
107 |
||
108 |
public static void writeTagWithAttributes(Element node, XMLStreamWriter writer) throws XMLStreamException { |
|
109 |
String nodePrefix = fixNull(node.getPrefix()); |
|
110 |
String nodeNS = fixNull(node.getNamespaceURI()); |
|
111 |
//fix to work with DOM level 1 nodes. |
|
112 |
String nodeLocalName = node.getLocalName()== null?node.getNodeName():node.getLocalName(); |
|
113 |
||
114 |
// See if nodePrefix:nodeNS is declared in writer's NamespaceContext before writing start element |
|
115 |
// Writing start element puts nodeNS in NamespaceContext even though namespace declaration not written |
|
116 |
boolean prefixDecl = isPrefixDeclared(writer, nodeNS, nodePrefix); |
|
117 |
writer.writeStartElement(nodePrefix, nodeLocalName, nodeNS); |
|
118 |
||
119 |
if (node.hasAttributes()) { |
|
120 |
NamedNodeMap attrs = node.getAttributes(); |
|
121 |
int numOfAttributes = attrs.getLength(); |
|
122 |
// write namespace declarations first. |
|
123 |
// if we interleave this with attribue writing, |
|
124 |
// Zephyr will try to fix it and we end up getting inconsistent namespace bindings. |
|
125 |
for (int i = 0; i < numOfAttributes; i++) { |
|
126 |
Node attr = attrs.item(i); |
|
127 |
String nsUri = fixNull(attr.getNamespaceURI()); |
|
128 |
if (nsUri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) { |
|
129 |
// handle default ns declarations |
|
130 |
String local = attr.getLocalName().equals(XMLConstants.XMLNS_ATTRIBUTE) ? "" : attr.getLocalName(); |
|
131 |
if (local.equals(nodePrefix) && attr.getNodeValue().equals(nodeNS)) { |
|
132 |
prefixDecl = true; |
|
133 |
} |
|
134 |
if (local.equals("")) { |
|
135 |
writer.writeDefaultNamespace(attr.getNodeValue()); |
|
136 |
} else { |
|
137 |
// this is a namespace declaration, not an attribute |
|
138 |
writer.setPrefix(attr.getLocalName(), attr.getNodeValue()); |
|
139 |
writer.writeNamespace(attr.getLocalName(), attr.getNodeValue()); |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
// node's namespace is not declared as attribute, but declared on ancestor |
|
145 |
if (!prefixDecl) { |
|
146 |
writer.writeNamespace(nodePrefix, nodeNS); |
|
147 |
} |
|
148 |
||
149 |
// Write all other attributes which are not namespace decl. |
|
150 |
if (node.hasAttributes()) { |
|
151 |
NamedNodeMap attrs = node.getAttributes(); |
|
152 |
int numOfAttributes = attrs.getLength(); |
|
153 |
||
154 |
for (int i = 0; i < numOfAttributes; i++) { |
|
155 |
Node attr = attrs.item(i); |
|
156 |
String attrPrefix = fixNull(attr.getPrefix()); |
|
157 |
String attrNS = fixNull(attr.getNamespaceURI()); |
|
158 |
if (!attrNS.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) { |
|
159 |
String localName = attr.getLocalName(); |
|
160 |
if (localName == null) { |
|
161 |
// TODO: this is really a bug in the caller for not creating proper DOM tree. |
|
162 |
// will remove this workaround after plugfest |
|
163 |
localName = attr.getNodeName(); |
|
164 |
} |
|
165 |
boolean attrPrefixDecl = isPrefixDeclared(writer, attrNS, attrPrefix); |
|
166 |
if (!attrPrefix.equals("") && !attrPrefixDecl) { |
|
167 |
// attr has namespace but namespace decl is there in ancestor node |
|
168 |
// So write the namespace decl before writing the attr |
|
169 |
writer.setPrefix(attr.getLocalName(), attr.getNodeValue()); |
|
170 |
writer.writeNamespace(attrPrefix, attrNS); |
|
171 |
} |
|
172 |
writer.writeAttribute(attrPrefix, attrNS, localName, attr.getNodeValue()); |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
private static boolean isPrefixDeclared(XMLStreamWriter writer, String nsUri, String prefix) { |
|
179 |
boolean prefixDecl = false; |
|
180 |
NamespaceContext nscontext = writer.getNamespaceContext(); |
|
181 |
Iterator prefixItr = nscontext.getPrefixes(nsUri); |
|
182 |
while (prefixItr.hasNext()) { |
|
183 |
if (prefix.equals(prefixItr.next())) { |
|
184 |
prefixDecl = true; |
|
185 |
break; |
|
186 |
} |
|
187 |
} |
|
188 |
return prefixDecl; |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Gets the first child of the given name, or null. |
|
193 |
*/ |
|
194 |
public static Element getFirstChild(Element e, String nsUri, String local) { |
|
195 |
for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) { |
|
196 |
if (n.getNodeType() == Node.ELEMENT_NODE) { |
|
197 |
Element c = (Element) n; |
|
16791 | 198 |
if (c.getLocalName().equals(local) && c.getNamespaceURI().equals(nsUri)) { |
12009 | 199 |
return c; |
16791 | 200 |
} |
12009 | 201 |
} |
202 |
} |
|
203 |
return null; |
|
204 |
} |
|
205 |
||
206 |
private static |
|
207 |
@NotNull |
|
208 |
String fixNull(@Nullable String s) { |
|
16791 | 209 |
if (s == null) { |
210 |
return ""; |
|
211 |
} else { |
|
212 |
return s; |
|
213 |
} |
|
12009 | 214 |
} |
215 |
||
216 |
/** |
|
217 |
* Gets the first element child. |
|
218 |
*/ |
|
219 |
public static |
|
220 |
@Nullable |
|
221 |
Element getFirstElementChild(Node parent) { |
|
222 |
for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) { |
|
223 |
if (n.getNodeType() == Node.ELEMENT_NODE) { |
|
224 |
return (Element) n; |
|
225 |
} |
|
226 |
} |
|
227 |
return null; |
|
228 |
} |
|
229 |
||
230 |
public static @NotNull |
|
231 |
List<Element> getChildElements(Node parent){ |
|
232 |
List<Element> elements = new ArrayList<Element>(); |
|
233 |
for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) { |
|
234 |
if (n.getNodeType() == Node.ELEMENT_NODE) { |
|
235 |
elements.add((Element)n); |
|
236 |
} |
|
237 |
} |
|
238 |
return elements; |
|
239 |
} |
|
240 |
} |