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