src/share/jaxws_classes/com/sun/xml/internal/ws/util/DOMUtil.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
child 1435
a90b319bae7a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, 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 dbf.setNamespaceAware(true);
aoqi@0 61 db = dbf.newDocumentBuilder();
aoqi@0 62 } catch (ParserConfigurationException e) {
aoqi@0 63 throw new FactoryConfigurationError(e);
aoqi@0 64 }
aoqi@0 65 }
aoqi@0 66 return db.newDocument();
aoqi@0 67 }
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 /**
aoqi@0 71 * Traverses a DOM node and writes out on a streaming writer.
aoqi@0 72 *
aoqi@0 73 * @param node
aoqi@0 74 * @param writer
aoqi@0 75 */
aoqi@0 76 public static void serializeNode(Element node, XMLStreamWriter writer) throws XMLStreamException {
aoqi@0 77 writeTagWithAttributes(node, writer);
aoqi@0 78
aoqi@0 79 if (node.hasChildNodes()) {
aoqi@0 80 NodeList children = node.getChildNodes();
aoqi@0 81 for (int i = 0; i < children.getLength(); i++) {
aoqi@0 82 Node child = children.item(i);
aoqi@0 83 switch (child.getNodeType()) {
aoqi@0 84 case Node.PROCESSING_INSTRUCTION_NODE:
aoqi@0 85 writer.writeProcessingInstruction(child.getNodeValue());
aoqi@0 86 break;
aoqi@0 87 case Node.DOCUMENT_TYPE_NODE:
aoqi@0 88 break;
aoqi@0 89 case Node.CDATA_SECTION_NODE:
aoqi@0 90 writer.writeCData(child.getNodeValue());
aoqi@0 91 break;
aoqi@0 92 case Node.COMMENT_NODE:
aoqi@0 93 writer.writeComment(child.getNodeValue());
aoqi@0 94 break;
aoqi@0 95 case Node.TEXT_NODE:
aoqi@0 96 writer.writeCharacters(child.getNodeValue());
aoqi@0 97 break;
aoqi@0 98 case Node.ELEMENT_NODE:
aoqi@0 99 serializeNode((Element) child, writer);
aoqi@0 100 break;
aoqi@0 101 default: break;
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104 }
aoqi@0 105 writer.writeEndElement();
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 public static void writeTagWithAttributes(Element node, XMLStreamWriter writer) throws XMLStreamException {
aoqi@0 109 String nodePrefix = fixNull(node.getPrefix());
aoqi@0 110 String nodeNS = fixNull(node.getNamespaceURI());
aoqi@0 111 //fix to work with DOM level 1 nodes.
aoqi@0 112 String nodeLocalName = node.getLocalName()== null?node.getNodeName():node.getLocalName();
aoqi@0 113
aoqi@0 114 // See if nodePrefix:nodeNS is declared in writer's NamespaceContext before writing start element
aoqi@0 115 // Writing start element puts nodeNS in NamespaceContext even though namespace declaration not written
aoqi@0 116 boolean prefixDecl = isPrefixDeclared(writer, nodeNS, nodePrefix);
aoqi@0 117 writer.writeStartElement(nodePrefix, nodeLocalName, nodeNS);
aoqi@0 118
aoqi@0 119 if (node.hasAttributes()) {
aoqi@0 120 NamedNodeMap attrs = node.getAttributes();
aoqi@0 121 int numOfAttributes = attrs.getLength();
aoqi@0 122 // write namespace declarations first.
aoqi@0 123 // if we interleave this with attribue writing,
aoqi@0 124 // Zephyr will try to fix it and we end up getting inconsistent namespace bindings.
aoqi@0 125 for (int i = 0; i < numOfAttributes; i++) {
aoqi@0 126 Node attr = attrs.item(i);
aoqi@0 127 String nsUri = fixNull(attr.getNamespaceURI());
aoqi@0 128 if (nsUri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
aoqi@0 129 // handle default ns declarations
aoqi@0 130 String local = attr.getLocalName().equals(XMLConstants.XMLNS_ATTRIBUTE) ? "" : attr.getLocalName();
aoqi@0 131 if (local.equals(nodePrefix) && attr.getNodeValue().equals(nodeNS)) {
aoqi@0 132 prefixDecl = true;
aoqi@0 133 }
aoqi@0 134 if (local.equals("")) {
aoqi@0 135 writer.writeDefaultNamespace(attr.getNodeValue());
aoqi@0 136 } else {
aoqi@0 137 // this is a namespace declaration, not an attribute
aoqi@0 138 writer.setPrefix(attr.getLocalName(), attr.getNodeValue());
aoqi@0 139 writer.writeNamespace(attr.getLocalName(), attr.getNodeValue());
aoqi@0 140 }
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144 // node's namespace is not declared as attribute, but declared on ancestor
aoqi@0 145 if (!prefixDecl) {
aoqi@0 146 writer.writeNamespace(nodePrefix, nodeNS);
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 // Write all other attributes which are not namespace decl.
aoqi@0 150 if (node.hasAttributes()) {
aoqi@0 151 NamedNodeMap attrs = node.getAttributes();
aoqi@0 152 int numOfAttributes = attrs.getLength();
aoqi@0 153
aoqi@0 154 for (int i = 0; i < numOfAttributes; i++) {
aoqi@0 155 Node attr = attrs.item(i);
aoqi@0 156 String attrPrefix = fixNull(attr.getPrefix());
aoqi@0 157 String attrNS = fixNull(attr.getNamespaceURI());
aoqi@0 158 if (!attrNS.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
aoqi@0 159 String localName = attr.getLocalName();
aoqi@0 160 if (localName == null) {
aoqi@0 161 // TODO: this is really a bug in the caller for not creating proper DOM tree.
aoqi@0 162 // will remove this workaround after plugfest
aoqi@0 163 localName = attr.getNodeName();
aoqi@0 164 }
aoqi@0 165 boolean attrPrefixDecl = isPrefixDeclared(writer, attrNS, attrPrefix);
aoqi@0 166 if (!attrPrefix.equals("") && !attrPrefixDecl) {
aoqi@0 167 // attr has namespace but namespace decl is there in ancestor node
aoqi@0 168 // So write the namespace decl before writing the attr
aoqi@0 169 writer.setPrefix(attr.getLocalName(), attr.getNodeValue());
aoqi@0 170 writer.writeNamespace(attrPrefix, attrNS);
aoqi@0 171 }
aoqi@0 172 writer.writeAttribute(attrPrefix, attrNS, localName, attr.getNodeValue());
aoqi@0 173 }
aoqi@0 174 }
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 private static boolean isPrefixDeclared(XMLStreamWriter writer, String nsUri, String prefix) {
aoqi@0 179 boolean prefixDecl = false;
aoqi@0 180 NamespaceContext nscontext = writer.getNamespaceContext();
aoqi@0 181 Iterator prefixItr = nscontext.getPrefixes(nsUri);
aoqi@0 182 while (prefixItr.hasNext()) {
aoqi@0 183 if (prefix.equals(prefixItr.next())) {
aoqi@0 184 prefixDecl = true;
aoqi@0 185 break;
aoqi@0 186 }
aoqi@0 187 }
aoqi@0 188 return prefixDecl;
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 /**
aoqi@0 192 * Gets the first child of the given name, or null.
aoqi@0 193 */
aoqi@0 194 public static Element getFirstChild(Element e, String nsUri, String local) {
aoqi@0 195 for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
aoqi@0 196 if (n.getNodeType() == Node.ELEMENT_NODE) {
aoqi@0 197 Element c = (Element) n;
aoqi@0 198 if (c.getLocalName().equals(local) && c.getNamespaceURI().equals(nsUri)) {
aoqi@0 199 return c;
aoqi@0 200 }
aoqi@0 201 }
aoqi@0 202 }
aoqi@0 203 return null;
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 private static
aoqi@0 207 @NotNull
aoqi@0 208 String fixNull(@Nullable String s) {
aoqi@0 209 if (s == null) {
aoqi@0 210 return "";
aoqi@0 211 } else {
aoqi@0 212 return s;
aoqi@0 213 }
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 /**
aoqi@0 217 * Gets the first element child.
aoqi@0 218 */
aoqi@0 219 public static
aoqi@0 220 @Nullable
aoqi@0 221 Element getFirstElementChild(Node parent) {
aoqi@0 222 for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
aoqi@0 223 if (n.getNodeType() == Node.ELEMENT_NODE) {
aoqi@0 224 return (Element) n;
aoqi@0 225 }
aoqi@0 226 }
aoqi@0 227 return null;
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 public static @NotNull
aoqi@0 231 List<Element> getChildElements(Node parent){
aoqi@0 232 List<Element> elements = new ArrayList<Element>();
aoqi@0 233 for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
aoqi@0 234 if (n.getNodeType() == Node.ELEMENT_NODE) {
aoqi@0 235 elements.add((Element)n);
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238 return elements;
aoqi@0 239 }
aoqi@0 240 }

mercurial