2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common Development
8
* and Distribution License("CDDL") (collectively, the "License"). You
9
* may not use this file except in compliance with the License. You can
10
* obtain a copy of the License at
11
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12
* or packager/legal/LICENSE.txt. See the License for the specific
13
* language governing permissions and limitations under the License.
15
* When distributing the software, include this License Header Notice in each
16
* file and include the License file at packager/legal/LICENSE.txt.
18
* GPL Classpath Exception:
19
* Oracle designates this particular file as subject to the "Classpath"
20
* exception as provided by Oracle in the GPL Version 2 section of the License
21
* file that accompanied this code.
24
* If applicable, add the following below the License Header, with the fields
25
* enclosed by brackets [] replaced by your own identifying information:
26
* "Portions Copyright [year] [name of copyright owner]"
29
* If you wish your version of this file to be governed by only the CDDL or
30
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31
* elects to include this software in this distribution under the [CDDL or GPL
32
* Version 2] license." If you don't indicate a single choice of license, a
33
* recipient has the option to distribute your version of this file under
34
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35
* its licensees as provided above. However, if you add GPL Version 2 code
36
* and therefore, elected the GPL Version 2 license, then the option applies
37
* only if the new code is made subject to such option by the copyright
41
package com.sun.xml.bind.unmarshaller;
43
import java.util.Enumeration;
45
import javax.xml.bind.ValidationEventLocator;
46
import javax.xml.bind.helpers.AbstractUnmarshallerImpl;
47
import javax.xml.bind.helpers.ValidationEventLocatorImpl;
49
import com.sun.xml.bind.v2.runtime.unmarshaller.LocatorEx;
51
import org.w3c.dom.Attr;
52
import org.w3c.dom.Document;
53
import org.w3c.dom.Element;
54
import org.w3c.dom.NamedNodeMap;
55
import org.w3c.dom.Node;
56
import org.w3c.dom.NodeList;
57
import org.w3c.dom.ProcessingInstruction;
58
import org.xml.sax.ContentHandler;
59
import org.xml.sax.Locator;
60
import org.xml.sax.SAXException;
61
import org.xml.sax.helpers.AttributesImpl;
62
import org.xml.sax.helpers.NamespaceSupport;
65
* Visits a W3C DOM tree and generates SAX2 events from it.
68
* This class is just intended to be used by {@link AbstractUnmarshallerImpl}.
69
* The javax.xml.bind.helpers package is generally a wrong place to put
72
* @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
75
public class DOMScanner implements LocatorEx,InfosetScanner/*<Node> --- but can't do this to protect 1.0 clients, or can I? */
78
/** reference to the current node being scanned - used for determining
79
* location info for validation events */
80
private Node currentNode = null;
82
/** To save memory, only one instance of AttributesImpl will be used. */
83
private final AttributesImpl atts = new AttributesImpl();
85
/** This handler will receive SAX2 events. */
86
private ContentHandler receiver=null;
88
private Locator locator=this;
95
* Configures the locator object that the SAX {@link ContentHandler} will see.
97
public void setLocator( Locator loc ) {
101
public void scan(Object node) throws SAXException {
102
if( node instanceof Document ) {
103
scan( (Document)node );
105
scan( (Element)node );
109
public void scan( Document doc ) throws SAXException {
110
scan( doc.getDocumentElement() );
113
public void scan( Element e) throws SAXException {
114
setCurrentLocation( e );
116
receiver.setDocumentLocator(locator);
117
receiver.startDocument();
119
NamespaceSupport nss = new NamespaceSupport();
120
buildNamespaceSupport( nss, e.getParentNode() );
122
for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
123
String prefix = (String)en.nextElement();
124
receiver.startPrefixMapping( prefix, nss.getURI(prefix) );
129
for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
130
String prefix = (String)en.nextElement();
131
receiver.endPrefixMapping( prefix );
135
setCurrentLocation( e );
136
receiver.endDocument();
140
* Parses a subtree starting from the element e and
141
* reports SAX2 events to the specified handler.
143
* @deprecated in JAXB 2.0
144
* Use {@link #scan(Element)}
146
public void parse( Element e, ContentHandler handler ) throws SAXException {
147
// it might be better to set receiver at the constructor.
150
setCurrentLocation( e );
151
receiver.startDocument();
153
receiver.setDocumentLocator(locator);
156
setCurrentLocation( e );
157
receiver.endDocument();
161
* Similar to the parse method but it visits the ancestor nodes
162
* and properly emulate the all in-scope namespace declarations.
164
* @deprecated in JAXB 2.0
165
* Use {@link #scan(Element)}
167
public void parseWithContext( Element e, ContentHandler handler ) throws SAXException {
168
setContentHandler(handler);
173
* Recursively visit ancestors and build up {@link NamespaceSupport} oject.
175
private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
176
if(node==null || node.getNodeType()!=Node.ELEMENT_NODE)
179
buildNamespaceSupport( nss, node.getParentNode() );
182
NamedNodeMap atts = node.getAttributes();
183
for( int i=0; i<atts.getLength(); i++ ) {
184
Attr a = (Attr)atts.item(i);
185
if( "xmlns".equals(a.getPrefix()) ) {
186
nss.declarePrefix( a.getLocalName(), a.getValue() );
189
if( "xmlns".equals(a.getName()) ) {
190
nss.declarePrefix( "", a.getValue() );
197
* Visits an element and its subtree.
199
public void visit( Element e ) throws SAXException {
200
setCurrentLocation( e );
201
final NamedNodeMap attributes = e.getAttributes();
204
int len = attributes==null ? 0: attributes.getLength();
206
for( int i=len-1; i>=0; i-- ) {
207
Attr a = (Attr)attributes.item(i);
208
String name = a.getName();
209
// start namespace binding
210
if(name.startsWith("xmlns")) {
211
if(name.length()==5) {
212
receiver.startPrefixMapping( "", a.getValue() );
214
String localName = a.getLocalName();
215
if(localName==null) {
216
// DOM built without namespace support has this problem
217
localName = name.substring(6);
219
receiver.startPrefixMapping( localName, a.getValue() );
224
String uri = a.getNamespaceURI();
225
if(uri==null) uri="";
227
String local = a.getLocalName();
228
if(local==null) local = a.getName();
229
// add other attributes to the attribute list
230
// that we will pass to the ContentHandler
239
String uri = e.getNamespaceURI();
240
if(uri==null) uri="";
241
String local = e.getLocalName();
242
String qname = e.getTagName();
243
if(local==null) local = qname;
244
receiver.startElement( uri, local, qname, atts );
246
// visit its children
247
NodeList children = e.getChildNodes();
248
int clen = children.getLength();
249
for( int i=0; i<clen; i++ )
250
visit(children.item(i));
254
setCurrentLocation( e );
255
receiver.endElement( uri, local, qname );
257
// call the endPrefixMapping method
258
for( int i=len-1; i>=0; i-- ) {
259
Attr a = (Attr)attributes.item(i);
260
String name = a.getName();
261
if(name.startsWith("xmlns")) {
263
receiver.endPrefixMapping("");
265
receiver.endPrefixMapping(a.getLocalName());
270
private void visit( Node n ) throws SAXException {
271
setCurrentLocation( n );
273
// if a case statement gets too big, it should be made into a separate method.
274
switch(n.getNodeType()) {
275
case Node.CDATA_SECTION_NODE:
277
String value = n.getNodeValue();
278
receiver.characters( value.toCharArray(), 0, value.length() );
280
case Node.ELEMENT_NODE:
283
case Node.ENTITY_REFERENCE_NODE:
284
receiver.skippedEntity(n.getNodeName());
286
case Node.PROCESSING_INSTRUCTION_NODE:
287
ProcessingInstruction pi = (ProcessingInstruction)n;
288
receiver.processingInstruction(pi.getTarget(),pi.getData());
293
private void setCurrentLocation( Node currNode ) {
294
currentNode = currNode;
298
* The same as {@link #getCurrentElement()} but
301
public Node getCurrentLocation() {
305
public Object getCurrentElement() {
309
public LocatorEx getLocator() {
313
public void setContentHandler(ContentHandler handler) {
314
this.receiver = handler;
317
public ContentHandler getContentHandler() {
318
return this.receiver;
322
// LocatorEx implementation
323
public String getPublicId() { return null; }
324
public String getSystemId() { return null; }
325
public int getLineNumber() { return -1; }
326
public int getColumnNumber() { return -1; }
328
public ValidationEventLocator getLocation() {
329
return new ValidationEventLocatorImpl(getCurrentLocation());