src/share/jaxws_classes/com/sun/xml/internal/bind/marshaller/SAX2DOMEx.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
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, 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.bind.marshaller;
aoqi@0 27
aoqi@0 28 import java.util.Stack;
aoqi@0 29
aoqi@0 30 import javax.xml.parsers.DocumentBuilderFactory;
aoqi@0 31 import javax.xml.parsers.ParserConfigurationException;
aoqi@0 32
aoqi@0 33 import com.sun.xml.internal.bind.util.Which;
aoqi@0 34 import com.sun.istack.internal.FinalArrayList;
aoqi@0 35
aoqi@0 36 import com.sun.xml.internal.bind.v2.util.XmlFactory;
aoqi@0 37 import org.w3c.dom.Document;
aoqi@0 38 import org.w3c.dom.Element;
aoqi@0 39 import org.w3c.dom.Node;
aoqi@0 40 import org.w3c.dom.Text;
aoqi@0 41 import org.xml.sax.Attributes;
aoqi@0 42 import org.xml.sax.ContentHandler;
aoqi@0 43 import org.xml.sax.Locator;
aoqi@0 44
aoqi@0 45 /**
aoqi@0 46 * Builds a DOM tree from SAX2 events.
aoqi@0 47 *
aoqi@0 48 * @author Vivek Pandey
aoqi@0 49 * @since 1.0
aoqi@0 50 */
aoqi@0 51 public class SAX2DOMEx implements ContentHandler {
aoqi@0 52
aoqi@0 53 private Node node = null;
aoqi@0 54 private boolean isConsolidate;
aoqi@0 55 protected final Stack<Node> nodeStack = new Stack<Node>();
aoqi@0 56 private final FinalArrayList<String> unprocessedNamespaces = new FinalArrayList<String>();
aoqi@0 57 /**
aoqi@0 58 * Document object that owns the specified node.
aoqi@0 59 */
aoqi@0 60 protected final Document document;
aoqi@0 61
aoqi@0 62 /**
aoqi@0 63 * @param node
aoqi@0 64 * Nodes will be created and added under this object.
aoqi@0 65 */
aoqi@0 66 public SAX2DOMEx(Node node) {
aoqi@0 67 this(node, false);
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 /**
aoqi@0 71 * @param node
aoqi@0 72 * Nodes will be created and added under this object.
aoqi@0 73 */
aoqi@0 74 public SAX2DOMEx(Node node, boolean isConsolidate) {
aoqi@0 75 this.node = node;
aoqi@0 76 this.isConsolidate = isConsolidate;
aoqi@0 77 nodeStack.push(this.node);
aoqi@0 78
aoqi@0 79 if (node instanceof Document) {
aoqi@0 80 this.document = (Document) node;
aoqi@0 81 } else {
aoqi@0 82 this.document = node.getOwnerDocument();
aoqi@0 83 }
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 /**
aoqi@0 87 * Creates a fresh empty DOM document and adds nodes under this document.
aoqi@0 88 */
aoqi@0 89 public SAX2DOMEx(DocumentBuilderFactory f) throws ParserConfigurationException {
aoqi@0 90 f.setValidating(false);
aoqi@0 91 document = f.newDocumentBuilder().newDocument();
aoqi@0 92 node = document;
aoqi@0 93 nodeStack.push(document);
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 /**
aoqi@0 97 * Creates a fresh empty DOM document and adds nodes under this document.
aoqi@0 98 * @deprecated
aoqi@0 99 */
aoqi@0 100 public SAX2DOMEx() throws ParserConfigurationException {
aoqi@0 101 DocumentBuilderFactory factory = XmlFactory.createDocumentBuilderFactory(false);
aoqi@0 102 factory.setValidating(false);
aoqi@0 103
aoqi@0 104 document = factory.newDocumentBuilder().newDocument();
aoqi@0 105 node = document;
aoqi@0 106 nodeStack.push(document);
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 public final Element getCurrentElement() {
aoqi@0 110 return (Element) nodeStack.peek();
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 public Node getDOM() {
aoqi@0 114 return node;
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 public void startDocument() {
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 public void endDocument() {
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 protected void namespace(Element element, String prefix, String uri) {
aoqi@0 124 String qname;
aoqi@0 125 if ("".equals(prefix) || prefix == null) {
aoqi@0 126 qname = "xmlns";
aoqi@0 127 } else {
aoqi@0 128 qname = "xmlns:" + prefix;
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
aoqi@0 132 // have a problem of re-setting the same namespace attribute twice.
aoqi@0 133 // work around this bug removing it first.
aoqi@0 134 if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) {
aoqi@0 135 // further workaround for an old Crimson bug where the removeAttribtueNS
aoqi@0 136 // method throws NPE when the element doesn't have any attribute.
aoqi@0 137 // to be on the safe side, check the existence of attributes before
aoqi@0 138 // attempting to remove it.
aoqi@0 139 // for details about this bug, see org.apache.crimson.tree.ElementNode2
aoqi@0 140 // line 540 or the following message:
aoqi@0 141 // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
aoqi@0 142 element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname);
aoqi@0 143 }
aoqi@0 144 // workaround until here
aoqi@0 145
aoqi@0 146 element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri);
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 public void startElement(String namespace, String localName, String qName, Attributes attrs) {
aoqi@0 150 Node parent = nodeStack.peek();
aoqi@0 151
aoqi@0 152 // some broken DOM implementation (we confirmed it with SAXON)
aoqi@0 153 // return null from this method.
aoqi@0 154 Element element = document.createElementNS(namespace, qName);
aoqi@0 155
aoqi@0 156 if (element == null) {
aoqi@0 157 // if so, report an user-friendly error message,
aoqi@0 158 // rather than dying mysteriously with NPE.
aoqi@0 159 throw new AssertionError(
aoqi@0 160 Messages.format(Messages.DOM_IMPL_DOESNT_SUPPORT_CREATELEMENTNS,
aoqi@0 161 document.getClass().getName(),
aoqi@0 162 Which.which(document.getClass())));
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 // process namespace bindings
aoqi@0 166 for (int i = 0; i < unprocessedNamespaces.size(); i += 2) {
aoqi@0 167 String prefix = unprocessedNamespaces.get(i);
aoqi@0 168 String uri = unprocessedNamespaces.get(i + 1);
aoqi@0 169
aoqi@0 170 namespace(element, prefix, uri);
aoqi@0 171 }
aoqi@0 172 unprocessedNamespaces.clear();
aoqi@0 173
aoqi@0 174
aoqi@0 175 if (attrs != null) {
aoqi@0 176 int length = attrs.getLength();
aoqi@0 177 for (int i = 0; i < length; i++) {
aoqi@0 178 String namespaceuri = attrs.getURI(i);
aoqi@0 179 String value = attrs.getValue(i);
aoqi@0 180 String qname = attrs.getQName(i);
aoqi@0 181 element.setAttributeNS(namespaceuri, qname, value);
aoqi@0 182 }
aoqi@0 183 }
aoqi@0 184 // append this new node onto current stack node
aoqi@0 185 parent.appendChild(element);
aoqi@0 186 // push this node onto stack
aoqi@0 187 nodeStack.push(element);
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 public void endElement(String namespace, String localName, String qName) {
aoqi@0 191 nodeStack.pop();
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 public void characters(char[] ch, int start, int length) {
aoqi@0 195 characters(new String(ch, start, length));
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 protected Text characters(String s) {
aoqi@0 199 Node parent = nodeStack.peek();
aoqi@0 200 Node lastChild = parent.getLastChild();
aoqi@0 201 Text text;
aoqi@0 202 if (isConsolidate && lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) {
aoqi@0 203 text = (Text) lastChild;
aoqi@0 204 text.appendData(s);
aoqi@0 205 } else {
aoqi@0 206 text = document.createTextNode(s);
aoqi@0 207 parent.appendChild(text);
aoqi@0 208 }
aoqi@0 209 return text;
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 public void ignorableWhitespace(char[] ch, int start, int length) {
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 public void processingInstruction(String target, String data) throws org.xml.sax.SAXException {
aoqi@0 216 Node parent = nodeStack.peek();
aoqi@0 217 Node n = document.createProcessingInstruction(target, data);
aoqi@0 218 parent.appendChild(n);
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 public void setDocumentLocator(Locator locator) {
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 public void skippedEntity(String name) {
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 public void startPrefixMapping(String prefix, String uri) {
aoqi@0 228 unprocessedNamespaces.add(prefix);
aoqi@0 229 unprocessedNamespaces.add(uri);
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 public void endPrefixMapping(String prefix) {
aoqi@0 233 }
aoqi@0 234 }

mercurial