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