1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/marshaller/SAX2DOMEx.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,234 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.marshaller; 1.30 + 1.31 +import java.util.Stack; 1.32 + 1.33 +import javax.xml.parsers.DocumentBuilderFactory; 1.34 +import javax.xml.parsers.ParserConfigurationException; 1.35 + 1.36 +import com.sun.xml.internal.bind.util.Which; 1.37 +import com.sun.istack.internal.FinalArrayList; 1.38 + 1.39 +import com.sun.xml.internal.bind.v2.util.XmlFactory; 1.40 +import org.w3c.dom.Document; 1.41 +import org.w3c.dom.Element; 1.42 +import org.w3c.dom.Node; 1.43 +import org.w3c.dom.Text; 1.44 +import org.xml.sax.Attributes; 1.45 +import org.xml.sax.ContentHandler; 1.46 +import org.xml.sax.Locator; 1.47 + 1.48 +/** 1.49 + * Builds a DOM tree from SAX2 events. 1.50 + * 1.51 + * @author Vivek Pandey 1.52 + * @since 1.0 1.53 + */ 1.54 +public class SAX2DOMEx implements ContentHandler { 1.55 + 1.56 + private Node node = null; 1.57 + private boolean isConsolidate; 1.58 + protected final Stack<Node> nodeStack = new Stack<Node>(); 1.59 + private final FinalArrayList<String> unprocessedNamespaces = new FinalArrayList<String>(); 1.60 + /** 1.61 + * Document object that owns the specified node. 1.62 + */ 1.63 + protected final Document document; 1.64 + 1.65 + /** 1.66 + * @param node 1.67 + * Nodes will be created and added under this object. 1.68 + */ 1.69 + public SAX2DOMEx(Node node) { 1.70 + this(node, false); 1.71 + } 1.72 + 1.73 + /** 1.74 + * @param node 1.75 + * Nodes will be created and added under this object. 1.76 + */ 1.77 + public SAX2DOMEx(Node node, boolean isConsolidate) { 1.78 + this.node = node; 1.79 + this.isConsolidate = isConsolidate; 1.80 + nodeStack.push(this.node); 1.81 + 1.82 + if (node instanceof Document) { 1.83 + this.document = (Document) node; 1.84 + } else { 1.85 + this.document = node.getOwnerDocument(); 1.86 + } 1.87 + } 1.88 + 1.89 + /** 1.90 + * Creates a fresh empty DOM document and adds nodes under this document. 1.91 + */ 1.92 + public SAX2DOMEx(DocumentBuilderFactory f) throws ParserConfigurationException { 1.93 + f.setValidating(false); 1.94 + document = f.newDocumentBuilder().newDocument(); 1.95 + node = document; 1.96 + nodeStack.push(document); 1.97 + } 1.98 + 1.99 + /** 1.100 + * Creates a fresh empty DOM document and adds nodes under this document. 1.101 + * @deprecated 1.102 + */ 1.103 + public SAX2DOMEx() throws ParserConfigurationException { 1.104 + DocumentBuilderFactory factory = XmlFactory.createDocumentBuilderFactory(false); 1.105 + factory.setValidating(false); 1.106 + 1.107 + document = factory.newDocumentBuilder().newDocument(); 1.108 + node = document; 1.109 + nodeStack.push(document); 1.110 + } 1.111 + 1.112 + public final Element getCurrentElement() { 1.113 + return (Element) nodeStack.peek(); 1.114 + } 1.115 + 1.116 + public Node getDOM() { 1.117 + return node; 1.118 + } 1.119 + 1.120 + public void startDocument() { 1.121 + } 1.122 + 1.123 + public void endDocument() { 1.124 + } 1.125 + 1.126 + protected void namespace(Element element, String prefix, String uri) { 1.127 + String qname; 1.128 + if ("".equals(prefix) || prefix == null) { 1.129 + qname = "xmlns"; 1.130 + } else { 1.131 + qname = "xmlns:" + prefix; 1.132 + } 1.133 + 1.134 + // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0) 1.135 + // have a problem of re-setting the same namespace attribute twice. 1.136 + // work around this bug removing it first. 1.137 + if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) { 1.138 + // further workaround for an old Crimson bug where the removeAttribtueNS 1.139 + // method throws NPE when the element doesn't have any attribute. 1.140 + // to be on the safe side, check the existence of attributes before 1.141 + // attempting to remove it. 1.142 + // for details about this bug, see org.apache.crimson.tree.ElementNode2 1.143 + // line 540 or the following message: 1.144 + // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767 1.145 + element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname); 1.146 + } 1.147 + // workaround until here 1.148 + 1.149 + element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri); 1.150 + } 1.151 + 1.152 + public void startElement(String namespace, String localName, String qName, Attributes attrs) { 1.153 + Node parent = nodeStack.peek(); 1.154 + 1.155 + // some broken DOM implementation (we confirmed it with SAXON) 1.156 + // return null from this method. 1.157 + Element element = document.createElementNS(namespace, qName); 1.158 + 1.159 + if (element == null) { 1.160 + // if so, report an user-friendly error message, 1.161 + // rather than dying mysteriously with NPE. 1.162 + throw new AssertionError( 1.163 + Messages.format(Messages.DOM_IMPL_DOESNT_SUPPORT_CREATELEMENTNS, 1.164 + document.getClass().getName(), 1.165 + Which.which(document.getClass()))); 1.166 + } 1.167 + 1.168 + // process namespace bindings 1.169 + for (int i = 0; i < unprocessedNamespaces.size(); i += 2) { 1.170 + String prefix = unprocessedNamespaces.get(i); 1.171 + String uri = unprocessedNamespaces.get(i + 1); 1.172 + 1.173 + namespace(element, prefix, uri); 1.174 + } 1.175 + unprocessedNamespaces.clear(); 1.176 + 1.177 + 1.178 + if (attrs != null) { 1.179 + int length = attrs.getLength(); 1.180 + for (int i = 0; i < length; i++) { 1.181 + String namespaceuri = attrs.getURI(i); 1.182 + String value = attrs.getValue(i); 1.183 + String qname = attrs.getQName(i); 1.184 + element.setAttributeNS(namespaceuri, qname, value); 1.185 + } 1.186 + } 1.187 + // append this new node onto current stack node 1.188 + parent.appendChild(element); 1.189 + // push this node onto stack 1.190 + nodeStack.push(element); 1.191 + } 1.192 + 1.193 + public void endElement(String namespace, String localName, String qName) { 1.194 + nodeStack.pop(); 1.195 + } 1.196 + 1.197 + public void characters(char[] ch, int start, int length) { 1.198 + characters(new String(ch, start, length)); 1.199 + } 1.200 + 1.201 + protected Text characters(String s) { 1.202 + Node parent = nodeStack.peek(); 1.203 + Node lastChild = parent.getLastChild(); 1.204 + Text text; 1.205 + if (isConsolidate && lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) { 1.206 + text = (Text) lastChild; 1.207 + text.appendData(s); 1.208 + } else { 1.209 + text = document.createTextNode(s); 1.210 + parent.appendChild(text); 1.211 + } 1.212 + return text; 1.213 + } 1.214 + 1.215 + public void ignorableWhitespace(char[] ch, int start, int length) { 1.216 + } 1.217 + 1.218 + public void processingInstruction(String target, String data) throws org.xml.sax.SAXException { 1.219 + Node parent = nodeStack.peek(); 1.220 + Node n = document.createProcessingInstruction(target, data); 1.221 + parent.appendChild(n); 1.222 + } 1.223 + 1.224 + public void setDocumentLocator(Locator locator) { 1.225 + } 1.226 + 1.227 + public void skippedEntity(String name) { 1.228 + } 1.229 + 1.230 + public void startPrefixMapping(String prefix, String uri) { 1.231 + unprocessedNamespaces.add(prefix); 1.232 + unprocessedNamespaces.add(uri); 1.233 + } 1.234 + 1.235 + public void endPrefixMapping(String prefix) { 1.236 + } 1.237 +}