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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,228 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, 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 +/* 1.30 + * SAX2DOMEx.java 1.31 + * 1.32 + * Created on February 22, 2002, 1:55 PM 1.33 + */ 1.34 +package com.sun.xml.internal.bind.marshaller; 1.35 + 1.36 +import java.util.Stack; 1.37 + 1.38 +import javax.xml.parsers.DocumentBuilderFactory; 1.39 +import javax.xml.parsers.ParserConfigurationException; 1.40 + 1.41 +import com.sun.xml.internal.bind.util.Which; 1.42 +import com.sun.istack.internal.FinalArrayList; 1.43 + 1.44 +import org.w3c.dom.Document; 1.45 +import org.w3c.dom.Element; 1.46 +import org.w3c.dom.Node; 1.47 +import org.w3c.dom.Text; 1.48 +import org.xml.sax.Attributes; 1.49 +import org.xml.sax.ContentHandler; 1.50 +import org.xml.sax.Locator; 1.51 + 1.52 +/** 1.53 + * Builds a DOM tree from SAX2 events. 1.54 + * 1.55 + * @author Vivek Pandey 1.56 + * @since 1.0 1.57 + */ 1.58 +public class SAX2DOMEx implements ContentHandler { 1.59 + 1.60 + private Node node = null; 1.61 + private boolean isConsolidate; 1.62 + protected final Stack<Node> nodeStack = new Stack<Node>(); 1.63 + private final FinalArrayList<String> unprocessedNamespaces = new FinalArrayList<String>(); 1.64 + /** 1.65 + * Document object that owns the specified node. 1.66 + */ 1.67 + protected final Document document; 1.68 + 1.69 + /** 1.70 + * @param node 1.71 + * Nodes will be created and added under this object. 1.72 + */ 1.73 + public SAX2DOMEx(Node node) { 1.74 + this(node, false); 1.75 + } 1.76 + 1.77 + /** 1.78 + * @param node 1.79 + * Nodes will be created and added under this object. 1.80 + */ 1.81 + public SAX2DOMEx(Node node, boolean isConsolidate) { 1.82 + this.node = node; 1.83 + this.isConsolidate = isConsolidate; 1.84 + nodeStack.push(this.node); 1.85 + 1.86 + if (node instanceof Document) { 1.87 + this.document = (Document) node; 1.88 + } else { 1.89 + this.document = node.getOwnerDocument(); 1.90 + } 1.91 + } 1.92 + 1.93 + /** 1.94 + * Creates a fresh empty DOM document and adds nodes under this document. 1.95 + */ 1.96 + public SAX2DOMEx() throws ParserConfigurationException { 1.97 + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 1.98 + factory.setNamespaceAware(true); 1.99 + factory.setValidating(false); 1.100 + 1.101 + document = factory.newDocumentBuilder().newDocument(); 1.102 + node = document; 1.103 + nodeStack.push(document); 1.104 + } 1.105 + 1.106 + public final Element getCurrentElement() { 1.107 + return (Element) nodeStack.peek(); 1.108 + } 1.109 + 1.110 + public Node getDOM() { 1.111 + return node; 1.112 + } 1.113 + 1.114 + public void startDocument() { 1.115 + } 1.116 + 1.117 + public void endDocument() { 1.118 + } 1.119 + 1.120 + protected void namespace(Element element, String prefix, String uri) { 1.121 + String qname; 1.122 + if ("".equals(prefix) || prefix == null) { 1.123 + qname = "xmlns"; 1.124 + } else { 1.125 + qname = "xmlns:" + prefix; 1.126 + } 1.127 + 1.128 + // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0) 1.129 + // have a problem of re-setting the same namespace attribute twice. 1.130 + // work around this bug removing it first. 1.131 + if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) { 1.132 + // further workaround for an old Crimson bug where the removeAttribtueNS 1.133 + // method throws NPE when the element doesn't have any attribute. 1.134 + // to be on the safe side, check the existence of attributes before 1.135 + // attempting to remove it. 1.136 + // for details about this bug, see org.apache.crimson.tree.ElementNode2 1.137 + // line 540 or the following message: 1.138 + // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767 1.139 + element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname); 1.140 + } 1.141 + // workaround until here 1.142 + 1.143 + element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri); 1.144 + } 1.145 + 1.146 + public void startElement(String namespace, String localName, String qName, Attributes attrs) { 1.147 + Node parent = nodeStack.peek(); 1.148 + 1.149 + // some broken DOM implementatino (we confirmed it with SAXON) 1.150 + // return null from this method. 1.151 + Element element = document.createElementNS(namespace, qName); 1.152 + 1.153 + if (element == null) { 1.154 + // if so, report an user-friendly error message, 1.155 + // rather than dying mysteriously with NPE. 1.156 + throw new AssertionError( 1.157 + Messages.format(Messages.DOM_IMPL_DOESNT_SUPPORT_CREATELEMENTNS, 1.158 + document.getClass().getName(), 1.159 + Which.which(document.getClass()))); 1.160 + } 1.161 + 1.162 + // process namespace bindings 1.163 + for (int i = 0; i < unprocessedNamespaces.size(); i += 2) { 1.164 + String prefix = unprocessedNamespaces.get(i + 0); 1.165 + String uri = unprocessedNamespaces.get(i + 1); 1.166 + 1.167 + namespace(element, prefix, uri); 1.168 + } 1.169 + unprocessedNamespaces.clear(); 1.170 + 1.171 + 1.172 + if (attrs != null) { 1.173 + int length = attrs.getLength(); 1.174 + for (int i = 0; i < length; i++) { 1.175 + String namespaceuri = attrs.getURI(i); 1.176 + String value = attrs.getValue(i); 1.177 + String qname = attrs.getQName(i); 1.178 + element.setAttributeNS(namespaceuri, qname, value); 1.179 + } 1.180 + } 1.181 + // append this new node onto current stack node 1.182 + parent.appendChild(element); 1.183 + // push this node onto stack 1.184 + nodeStack.push(element); 1.185 + } 1.186 + 1.187 + public void endElement(String namespace, String localName, String qName) { 1.188 + nodeStack.pop(); 1.189 + } 1.190 + 1.191 + public void characters(char[] ch, int start, int length) { 1.192 + characters(new String(ch, start, length)); 1.193 + } 1.194 + 1.195 + protected Text characters(String s) { 1.196 + Node parent = nodeStack.peek(); 1.197 + Node lastChild = parent.getLastChild(); 1.198 + Text text; 1.199 + if (isConsolidate && lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) { 1.200 + text = (Text) lastChild; 1.201 + text.appendData(s); 1.202 + } else { 1.203 + text = document.createTextNode(s); 1.204 + parent.appendChild(text); 1.205 + } 1.206 + return text; 1.207 + } 1.208 + 1.209 + public void ignorableWhitespace(char[] ch, int start, int length) { 1.210 + } 1.211 + 1.212 + public void processingInstruction(String target, String data) throws org.xml.sax.SAXException { 1.213 + Node parent = nodeStack.peek(); 1.214 + Node n = document.createProcessingInstruction(target, data); 1.215 + parent.appendChild(n); 1.216 + } 1.217 + 1.218 + public void setDocumentLocator(Locator locator) { 1.219 + } 1.220 + 1.221 + public void skippedEntity(String name) { 1.222 + } 1.223 + 1.224 + public void startPrefixMapping(String prefix, String uri) { 1.225 + unprocessedNamespaces.add(prefix); 1.226 + unprocessedNamespaces.add(uri); 1.227 + } 1.228 + 1.229 + public void endPrefixMapping(String prefix) { 1.230 + } 1.231 +}