~ubuntu-branches/ubuntu/vivid/jaxb/vivid

« back to all changes in this revision

Viewing changes to runtime/src/com/sun/xml/bind/unmarshaller/DOMScanner.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2014-09-01 14:26:44 UTC
  • Revision ID: package-import@ubuntu.com-20140901142644-nox3y6t2unq2wxjf
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 
5
 *
 
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.
 
14
 *
 
15
 * When distributing the software, include this License Header Notice in each
 
16
 * file and include the License file at packager/legal/LICENSE.txt.
 
17
 *
 
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.
 
22
 *
 
23
 * Modifications:
 
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]"
 
27
 *
 
28
 * Contributor(s):
 
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
 
38
 * holder.
 
39
 */
 
40
 
 
41
package com.sun.xml.bind.unmarshaller;
 
42
 
 
43
import java.util.Enumeration;
 
44
 
 
45
import javax.xml.bind.ValidationEventLocator;
 
46
import javax.xml.bind.helpers.AbstractUnmarshallerImpl;
 
47
import javax.xml.bind.helpers.ValidationEventLocatorImpl;
 
48
 
 
49
import com.sun.xml.bind.v2.runtime.unmarshaller.LocatorEx;
 
50
 
 
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;
 
63
 
 
64
/**
 
65
 * Visits a W3C DOM tree and generates SAX2 events from it.
 
66
 * 
 
67
 * <p>
 
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
 
70
 * classes like this.
 
71
 *
 
72
 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
 
73
 * @since JAXB1.0
 
74
 */
 
75
public class DOMScanner implements LocatorEx,InfosetScanner/*<Node> --- but can't do this to protect 1.0 clients, or can I? */
 
76
{
 
77
    
 
78
    /** reference to the current node being scanned - used for determining
 
79
     *  location info for validation events */
 
80
    private Node currentNode = null;
 
81
    
 
82
    /** To save memory, only one instance of AttributesImpl will be used. */
 
83
    private final AttributesImpl atts = new AttributesImpl();
 
84
    
 
85
    /** This handler will receive SAX2 events. */
 
86
    private ContentHandler receiver=null;
 
87
 
 
88
    private Locator locator=this;
 
89
 
 
90
    public DOMScanner() {
 
91
    }
 
92
    
 
93
 
 
94
    /**
 
95
     * Configures the locator object that the SAX {@link ContentHandler} will see.
 
96
     */
 
97
    public void setLocator( Locator loc ) {
 
98
        this.locator = loc;
 
99
    }
 
100
 
 
101
    public void scan(Object node) throws SAXException {
 
102
        if( node instanceof Document ) {
 
103
            scan( (Document)node );
 
104
        } else {
 
105
            scan( (Element)node );
 
106
        }
 
107
    }
 
108
    
 
109
    public void scan( Document doc ) throws SAXException {
 
110
        scan( doc.getDocumentElement() );
 
111
    }
 
112
    
 
113
    public void scan( Element e) throws SAXException {
 
114
        setCurrentLocation( e );
 
115
 
 
116
        receiver.setDocumentLocator(locator);
 
117
        receiver.startDocument();
 
118
 
 
119
        NamespaceSupport nss = new NamespaceSupport();
 
120
        buildNamespaceSupport( nss, e.getParentNode() );
 
121
        
 
122
        for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
 
123
            String prefix = (String)en.nextElement();
 
124
            receiver.startPrefixMapping( prefix, nss.getURI(prefix) );
 
125
        }
 
126
        
 
127
        visit(e);
 
128
        
 
129
        for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) {
 
130
            String prefix = (String)en.nextElement();
 
131
            receiver.endPrefixMapping( prefix );
 
132
        }
 
133
        
 
134
        
 
135
        setCurrentLocation( e );
 
136
        receiver.endDocument();
 
137
    }
 
138
        
 
139
    /**
 
140
     * Parses a subtree starting from the element e and
 
141
     * reports SAX2 events to the specified handler.
 
142
     * 
 
143
     * @deprecated in JAXB 2.0
 
144
     *      Use {@link #scan(Element)}
 
145
     */
 
146
    public void parse( Element e, ContentHandler handler ) throws SAXException {
 
147
        // it might be better to set receiver at the constructor.
 
148
        receiver = handler;
 
149
        
 
150
        setCurrentLocation( e );
 
151
        receiver.startDocument();
 
152
        
 
153
        receiver.setDocumentLocator(locator);
 
154
        visit(e);
 
155
        
 
156
        setCurrentLocation( e );
 
157
        receiver.endDocument();
 
158
    }
 
159
    
 
160
    /**
 
161
     * Similar to the parse method but it visits the ancestor nodes
 
162
     * and properly emulate the all in-scope namespace declarations.
 
163
     * 
 
164
     * @deprecated in JAXB 2.0
 
165
     *      Use {@link #scan(Element)}
 
166
     */
 
167
    public void parseWithContext( Element e, ContentHandler handler ) throws SAXException {
 
168
        setContentHandler(handler);
 
169
        scan(e);
 
170
    }
 
171
    
 
172
    /**
 
173
     * Recursively visit ancestors and build up {@link NamespaceSupport} oject.
 
174
     */
 
175
    private void buildNamespaceSupport(NamespaceSupport nss, Node node) {
 
176
        if(node==null || node.getNodeType()!=Node.ELEMENT_NODE)
 
177
            return;
 
178
            
 
179
        buildNamespaceSupport( nss, node.getParentNode() );
 
180
        
 
181
        nss.pushContext();
 
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() );
 
187
                continue;
 
188
            }
 
189
            if( "xmlns".equals(a.getName()) ) {
 
190
                nss.declarePrefix( "", a.getValue() );
 
191
                continue;
 
192
            }
 
193
        }
 
194
    }
 
195
 
 
196
    /**
 
197
     * Visits an element and its subtree.
 
198
     */
 
199
    public void visit( Element e ) throws SAXException {
 
200
        setCurrentLocation( e );
 
201
        final NamedNodeMap attributes = e.getAttributes();
 
202
        
 
203
        atts.clear();
 
204
        int len = attributes==null ? 0: attributes.getLength();
 
205
        
 
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() );
 
213
                } else {
 
214
                    String localName = a.getLocalName();
 
215
                    if(localName==null) {
 
216
                        // DOM built without namespace support has this problem
 
217
                        localName = name.substring(6);
 
218
                    }
 
219
                    receiver.startPrefixMapping( localName, a.getValue() );
 
220
                }
 
221
                continue;
 
222
            }
 
223
            
 
224
            String uri = a.getNamespaceURI();
 
225
            if(uri==null)   uri="";
 
226
            
 
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
 
231
            atts.addAttribute(
 
232
                uri,
 
233
                local,
 
234
                a.getName(),
 
235
                "CDATA",
 
236
                a.getValue());
 
237
        }
 
238
        
 
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 );
 
245
        
 
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));
 
251
        
 
252
        
 
253
        
 
254
        setCurrentLocation( e );
 
255
        receiver.endElement( uri, local, qname );
 
256
        
 
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")) {
 
262
                if(name.length()==5)
 
263
                    receiver.endPrefixMapping("");
 
264
                else
 
265
                    receiver.endPrefixMapping(a.getLocalName());
 
266
            }
 
267
        }
 
268
    }
 
269
    
 
270
    private void visit( Node n ) throws SAXException {
 
271
        setCurrentLocation( n );
 
272
        
 
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:
 
276
        case Node.TEXT_NODE:
 
277
            String value = n.getNodeValue();
 
278
            receiver.characters( value.toCharArray(), 0, value.length() );
 
279
            break;
 
280
        case Node.ELEMENT_NODE:
 
281
            visit( (Element)n );
 
282
            break;
 
283
        case Node.ENTITY_REFERENCE_NODE:
 
284
            receiver.skippedEntity(n.getNodeName());
 
285
            break;
 
286
        case Node.PROCESSING_INSTRUCTION_NODE:
 
287
            ProcessingInstruction pi = (ProcessingInstruction)n;
 
288
            receiver.processingInstruction(pi.getTarget(),pi.getData());
 
289
            break;
 
290
        }
 
291
    }
 
292
    
 
293
    private void setCurrentLocation( Node currNode ) {
 
294
        currentNode = currNode;
 
295
    }
 
296
    
 
297
    /**
 
298
     * The same as {@link #getCurrentElement()} but
 
299
     * better typed.
 
300
     */
 
301
    public Node getCurrentLocation() {
 
302
        return currentNode;
 
303
    }
 
304
 
 
305
    public Object getCurrentElement() {
 
306
        return currentNode;
 
307
    }
 
308
 
 
309
    public LocatorEx getLocator() {
 
310
        return this;
 
311
    }
 
312
 
 
313
    public void setContentHandler(ContentHandler handler) {
 
314
        this.receiver = handler;
 
315
    }
 
316
 
 
317
    public ContentHandler getContentHandler() {
 
318
        return this.receiver;
 
319
    }
 
320
 
 
321
 
 
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; }
 
327
 
 
328
    public ValidationEventLocator getLocation() {
 
329
        return new ValidationEventLocatorImpl(getCurrentLocation());
 
330
    }
 
331
}