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