src/share/jaxws_classes/com/sun/xml/internal/bind/marshaller/SAX2DOMEx.java

Sun, 18 Jun 2017 23:18:45 +0100

author
aefimov
date
Sun, 18 Jun 2017 23:18:45 +0100
changeset 1443
dffc222439a1
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea

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

mercurial