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: package com.sun.xml.bind.unmarshaller;
037:
038: import java.util.Enumeration;
039:
040: import javax.xml.bind.ValidationEventLocator;
041: import javax.xml.bind.helpers.AbstractUnmarshallerImpl;
042: import javax.xml.bind.helpers.ValidationEventLocatorImpl;
043:
044: import com.sun.xml.bind.v2.runtime.unmarshaller.LocatorEx;
045:
046: import org.w3c.dom.Attr;
047: import org.w3c.dom.Document;
048: import org.w3c.dom.Element;
049: import org.w3c.dom.NamedNodeMap;
050: import org.w3c.dom.Node;
051: import org.w3c.dom.NodeList;
052: import org.w3c.dom.ProcessingInstruction;
053: import org.xml.sax.ContentHandler;
054: import org.xml.sax.Locator;
055: import org.xml.sax.SAXException;
056: import org.xml.sax.helpers.AttributesImpl;
057: import org.xml.sax.helpers.NamespaceSupport;
058:
059: /**
060: * Visits a W3C DOM tree and generates SAX2 events from it.
061: *
062: * <p>
063: * This class is just intended to be used by {@link AbstractUnmarshallerImpl}.
064: * The javax.xml.bind.helpers package is generally a wrong place to put
065: * classes like this.
066: *
067: * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
068: * @version $Revision: 1.6.4.2 $ $Date: 2007/05/31 21:58:56 $
069: * @since JAXB1.0
070: */
071: public class DOMScanner implements LocatorEx, InfosetScanner/*<Node> --- but can't do this to protect 1.0 clients, or can I? */
072: {
073:
074: /** reference to the current node being scanned - used for determining
075: * location info for validation events */
076: private Node currentNode = null;
077:
078: /** To save memory, only one instance of AttributesImpl will be used. */
079: private final AttributesImpl atts = new AttributesImpl();
080:
081: /** This handler will receive SAX2 events. */
082: private ContentHandler receiver = null;
083:
084: private Locator locator = this ;
085:
086: public DOMScanner() {
087: }
088:
089: /**
090: * Configures the locator object that the SAX {@link ContentHandler} will see.
091: */
092: public void setLocator(Locator loc) {
093: this .locator = loc;
094: }
095:
096: public void scan(Object node) throws SAXException {
097: if (node instanceof Document) {
098: scan((Document) node);
099: } else {
100: scan((Element) node);
101: }
102: }
103:
104: public void scan(Document doc) throws SAXException {
105: scan(doc.getDocumentElement());
106: }
107:
108: public void scan(Element e) throws SAXException {
109: setCurrentLocation(e);
110:
111: receiver.setDocumentLocator(locator);
112: receiver.startDocument();
113:
114: NamespaceSupport nss = new NamespaceSupport();
115: buildNamespaceSupport(nss, e.getParentNode());
116:
117: for (Enumeration en = nss.getPrefixes(); en.hasMoreElements();) {
118: String prefix = (String) en.nextElement();
119: receiver.startPrefixMapping(prefix, nss.getURI(prefix));
120: }
121:
122: visit(e);
123:
124: for (Enumeration en = nss.getPrefixes(); en.hasMoreElements();) {
125: String prefix = (String) en.nextElement();
126: receiver.endPrefixMapping(prefix);
127: }
128:
129: setCurrentLocation(e);
130: receiver.endDocument();
131: }
132:
133: /**
134: * Parses a subtree starting from the element e and
135: * reports SAX2 events to the specified handler.
136: *
137: * @deprecated in JAXB 2.0
138: * Use {@link #scan(Element)}
139: */
140: public void parse(Element e, ContentHandler handler)
141: throws SAXException {
142: // it might be better to set receiver at the constructor.
143: receiver = handler;
144:
145: setCurrentLocation(e);
146: receiver.startDocument();
147:
148: receiver.setDocumentLocator(locator);
149: visit(e);
150:
151: setCurrentLocation(e);
152: receiver.endDocument();
153: }
154:
155: /**
156: * Similar to the parse method but it visits the ancestor nodes
157: * and properly emulate the all in-scope namespace declarations.
158: *
159: * @deprecated in JAXB 2.0
160: * Use {@link #scan(Element)}
161: */
162: public void parseWithContext(Element e, ContentHandler handler)
163: throws SAXException {
164: setContentHandler(handler);
165: scan(e);
166: }
167:
168: /**
169: * Recursively visit ancestors and build up {@link NamespaceSupport} oject.
170: */
171: private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
172: if (node == null || node.getNodeType() != Node.ELEMENT_NODE)
173: return;
174:
175: buildNamespaceSupport(nss, node.getParentNode());
176:
177: nss.pushContext();
178: NamedNodeMap atts = node.getAttributes();
179: for (int i = 0; i < atts.getLength(); i++) {
180: Attr a = (Attr) atts.item(i);
181: if ("xmlns".equals(a.getPrefix())) {
182: nss.declarePrefix(a.getLocalName(), a.getValue());
183: continue;
184: }
185: if ("xmlns".equals(a.getName())) {
186: nss.declarePrefix("", a.getValue());
187: continue;
188: }
189: }
190: }
191:
192: /**
193: * Visits an element and its subtree.
194: */
195: public void visit(Element e) throws SAXException {
196: setCurrentLocation(e);
197: final NamedNodeMap attributes = e.getAttributes();
198:
199: atts.clear();
200: int len = attributes == null ? 0 : attributes.getLength();
201:
202: for (int i = len - 1; i >= 0; i--) {
203: Attr a = (Attr) attributes.item(i);
204: String name = a.getName();
205: // start namespace binding
206: if (name.startsWith("xmlns")) {
207: if (name.length() == 5) {
208: receiver.startPrefixMapping("", a.getValue());
209: } else {
210: String localName = a.getLocalName();
211: if (localName == null) {
212: // DOM built without namespace support has this problem
213: localName = name.substring(6);
214: }
215: receiver
216: .startPrefixMapping(localName, a.getValue());
217: }
218: continue;
219: }
220:
221: String uri = a.getNamespaceURI();
222: if (uri == null)
223: uri = "";
224:
225: String local = a.getLocalName();
226: if (local == null)
227: local = a.getName();
228: // add other attributes to the attribute list
229: // that we will pass to the ContentHandler
230: atts.addAttribute(uri, local, a.getName(), "CDATA", a
231: .getValue());
232: }
233:
234: String uri = e.getNamespaceURI();
235: if (uri == null)
236: uri = "";
237: String local = e.getLocalName();
238: String qname = e.getTagName();
239: if (local == null)
240: local = qname;
241: receiver.startElement(uri, local, qname, atts);
242:
243: // visit its children
244: NodeList children = e.getChildNodes();
245: int clen = children.getLength();
246: for (int i = 0; i < clen; i++)
247: visit(children.item(i));
248:
249: setCurrentLocation(e);
250: receiver.endElement(uri, local, qname);
251:
252: // call the endPrefixMapping method
253: for (int i = len - 1; i >= 0; i--) {
254: Attr a = (Attr) attributes.item(i);
255: String name = a.getName();
256: if (name.startsWith("xmlns")) {
257: if (name.length() == 5)
258: receiver.endPrefixMapping("");
259: else
260: receiver.endPrefixMapping(a.getLocalName());
261: }
262: }
263: }
264:
265: private void visit(Node n) throws SAXException {
266: setCurrentLocation(n);
267:
268: // if a case statement gets too big, it should be made into a separate method.
269: switch (n.getNodeType()) {
270: case Node.CDATA_SECTION_NODE:
271: case Node.TEXT_NODE:
272: String value = n.getNodeValue();
273: receiver.characters(value.toCharArray(), 0, value.length());
274: break;
275: case Node.ELEMENT_NODE:
276: visit((Element) n);
277: break;
278: case Node.ENTITY_REFERENCE_NODE:
279: receiver.skippedEntity(n.getNodeName());
280: break;
281: case Node.PROCESSING_INSTRUCTION_NODE:
282: ProcessingInstruction pi = (ProcessingInstruction) n;
283: receiver
284: .processingInstruction(pi.getTarget(), pi.getData());
285: break;
286: }
287: }
288:
289: private void setCurrentLocation(Node currNode) {
290: currentNode = currNode;
291: }
292:
293: /**
294: * The same as {@link #getCurrentElement()} but
295: * better typed.
296: */
297: public Node getCurrentLocation() {
298: return currentNode;
299: }
300:
301: public Object getCurrentElement() {
302: return currentNode;
303: }
304:
305: public LocatorEx getLocator() {
306: return this ;
307: }
308:
309: public void setContentHandler(ContentHandler handler) {
310: this .receiver = handler;
311: }
312:
313: public ContentHandler getContentHandler() {
314: return this .receiver;
315: }
316:
317: // LocatorEx implementation
318: public String getPublicId() {
319: return null;
320: }
321:
322: public String getSystemId() {
323: return null;
324: }
325:
326: public int getLineNumber() {
327: return -1;
328: }
329:
330: public int getColumnNumber() {
331: return -1;
332: }
333:
334: public ValidationEventLocator getLocation() {
335: return new ValidationEventLocatorImpl(getCurrentLocation());
336: }
337: }
|