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

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
child 1386
65d3b0e44551
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

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

mercurial