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

Thu, 24 May 2018 17:55:52 +0800

author
aoqi
date
Thu, 24 May 2018 17:55:52 +0800
changeset 1435
a90b319bae7a
parent 1386
65d3b0e44551
parent 637
9c07ef4934dd
permissions
-rw-r--r--

Merge

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

mercurial