Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
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.
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 */
26 package com.sun.xml.internal.ws.util;
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.*;
33 import javax.xml.XMLConstants;
34 import javax.xml.namespace.NamespaceContext;
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;
41 import java.util.ArrayList;
42 import java.util.Iterator;
43 import java.util.List;
45 /**
46 * @author JAXWS Development Team
47 */
48 public class DOMUtil {
50 private static DocumentBuilder db;
52 /**
53 * Creates a new DOM document.
54 */
55 public static Document createDom() {
56 synchronized (DOMUtil.class) {
57 if (db == null) {
58 try {
59 DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory();
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 }
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);
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());
86 break;
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;
101 default: break;
102 }
103 }
104 }
105 writer.writeEndElement();
106 }
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();
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);
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 }
149 // Write all other attributes which are not namespace decl.
150 if (node.hasAttributes()) {
151 NamedNodeMap attrs = node.getAttributes();
152 int numOfAttributes = attrs.getLength();
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 }
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 }
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;
198 if (c.getLocalName().equals(local) && c.getNamespaceURI().equals(nsUri)) {
199 return c;
200 }
201 }
202 }
203 return null;
204 }
206 private static
207 @NotNull
208 String fixNull(@Nullable String s) {
209 if (s == null) {
210 return "";
211 } else {
212 return s;
213 }
214 }
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 }
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 }