001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * SAX2DOMEx.java
039: *
040: * Created on February 22, 2002, 1:55 PM
041: */
042:
043: package com.sun.xml.bind.marshaller;
044:
045: import java.util.Stack;
046:
047: import javax.xml.parsers.DocumentBuilderFactory;
048: import javax.xml.parsers.ParserConfigurationException;
049:
050: import com.sun.xml.bind.util.Which;
051: import com.sun.istack.FinalArrayList;
052:
053: import org.w3c.dom.Document;
054: import org.w3c.dom.Element;
055: import org.w3c.dom.Node;
056: import org.w3c.dom.Text;
057: import org.xml.sax.Attributes;
058: import org.xml.sax.ContentHandler;
059: import org.xml.sax.Locator;
060:
061: /**
062: * Builds a DOM tree from SAX2 events.
063: *
064: * @author Vivek Pandey
065: * @since 1.0
066: */
067: public class SAX2DOMEx implements ContentHandler {
068:
069: private Node node = null;
070: private final Stack<Node> nodeStack = new Stack<Node>();
071: private final FinalArrayList<String> unprocessedNamespaces = new FinalArrayList<String>();
072:
073: /**
074: * Document object that owns the specified node.
075: */
076: private final Document document;
077:
078: /**
079: * @param node
080: * Nodes will be created and added under this object.
081: */
082: public SAX2DOMEx(Node node) {
083: this .node = node;
084: nodeStack.push(this .node);
085:
086: if (node instanceof Document)
087: this .document = (Document) node;
088: else
089: this .document = node.getOwnerDocument();
090: }
091:
092: /**
093: * Creates a fresh empty DOM document and adds nodes under this document.
094: */
095: public SAX2DOMEx() throws ParserConfigurationException {
096: DocumentBuilderFactory factory = DocumentBuilderFactory
097: .newInstance();
098: factory.setNamespaceAware(true);
099: factory.setValidating(false);
100:
101: document = factory.newDocumentBuilder().newDocument();
102: node = document;
103: nodeStack.push(document);
104: }
105:
106: public final Element getCurrentElement() {
107: return (Element) nodeStack.peek();
108: }
109:
110: public Node getDOM() {
111: return node;
112: }
113:
114: public void startDocument() {
115: }
116:
117: public void endDocument() {
118: }
119:
120: public void startElement(String namespace, String localName,
121: String qName, Attributes attrs) {
122: Node parent = nodeStack.peek();
123:
124: // some broken DOM implementatino (we confirmed it with SAXON)
125: // return null from this method.
126: Element element = document.createElementNS(namespace, qName);
127:
128: if (element == null) {
129: // if so, report an user-friendly error message,
130: // rather than dying mysteriously with NPE.
131: throw new AssertionError(Messages.format(
132: Messages.DOM_IMPL_DOESNT_SUPPORT_CREATELEMENTNS,
133: document.getClass().getName(), Which.which(document
134: .getClass())));
135: }
136:
137: // process namespace bindings
138: for (int i = 0; i < unprocessedNamespaces.size(); i += 2) {
139: String prefix = unprocessedNamespaces.get(i + 0);
140: String uri = unprocessedNamespaces.get(i + 1);
141:
142: String qname;
143: if ("".equals(prefix) || prefix == null)
144: qname = "xmlns";
145: else
146: qname = "xmlns:" + prefix;
147:
148: // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
149: // have a problem of re-setting the same namespace attribute twice.
150: // work around this bug removing it first.
151: if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/",
152: qname)) {
153: // further workaround for an old Crimson bug where the removeAttribtueNS
154: // method throws NPE when the element doesn't have any attribute.
155: // to be on the safe side, check the existence of attributes before
156: // attempting to remove it.
157: // for details about this bug, see org.apache.crimson.tree.ElementNode2
158: // line 540 or the following message:
159: // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
160: element.removeAttributeNS(
161: "http://www.w3.org/2000/xmlns/", qname);
162: }
163: // workaround until here
164:
165: element.setAttributeNS("http://www.w3.org/2000/xmlns/",
166: qname, uri);
167: }
168: unprocessedNamespaces.clear();
169:
170: int length = attrs.getLength();
171: for (int i = 0; i < length; i++) {
172: String namespaceuri = attrs.getURI(i);
173: String value = attrs.getValue(i);
174: String qname = attrs.getQName(i);
175: element.setAttributeNS(namespaceuri, qname, value);
176: }
177: // append this new node onto current stack node
178: parent.appendChild(element);
179: // push this node onto stack
180: nodeStack.push(element);
181: }
182:
183: public void endElement(String namespace, String localName,
184: String qName) {
185: nodeStack.pop();
186: }
187:
188: public void characters(char[] ch, int start, int length) {
189: Node parent = nodeStack.peek();
190: Text text = document.createTextNode(new String(ch, start,
191: length));
192: parent.appendChild(text);
193: }
194:
195: public void ignorableWhitespace(char[] ch, int start, int length) {
196: }
197:
198: public void processingInstruction(String target, String data)
199: throws org.xml.sax.SAXException {
200: Node parent = nodeStack.peek();
201: Node node = document.createProcessingInstruction(target, data);
202: parent.appendChild(node);
203: }
204:
205: public void setDocumentLocator(Locator locator) {
206: }
207:
208: public void skippedEntity(String name) {
209: }
210:
211: public void startPrefixMapping(String prefix, String uri) {
212: unprocessedNamespaces.add(prefix);
213: unprocessedNamespaces.add(uri);
214: }
215:
216: public void endPrefixMapping(String prefix) {
217: }
218: }
|