KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > testsuite > impl > runtime > UnmarshallerImpl


1 //
2
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.3-b18-fcs
3
// See <a HREF="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
4
// Any modifications to this file will be lost upon recompilation of the source schema.
5
// Generated on: 2005.06.14 at 12:02:17 PDT
6
//
7

8 package com.sun.japex.testsuite.impl.runtime;
9
10 import java.io.IOException JavaDoc;
11
12 import javax.xml.bind.DatatypeConverter;
13 import javax.xml.bind.JAXBException;
14 import javax.xml.bind.UnmarshallerHandler;
15 import javax.xml.bind.helpers.AbstractUnmarshallerImpl;
16
17 import org.w3c.dom.Document JavaDoc;
18 import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20 import org.xml.sax.InputSource JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.XMLReader JavaDoc;
23 import org.xml.sax.helpers.DefaultHandler JavaDoc;
24
25 import com.sun.xml.bind.DatatypeConverterImpl;
26 import com.sun.xml.bind.unmarshaller.DOMScanner;
27 import com.sun.xml.bind.unmarshaller.InterningXMLReader;
28 import com.sun.xml.bind.validator.DOMLocator;
29 import com.sun.xml.bind.validator.Locator;
30 import com.sun.xml.bind.validator.SAXLocator;
31
32 /**
33  * Default Unmarshall implementation.
34  *
35  * <p>
36  * This class can be extended by the generated code to provide
37  * type-safe unmarshall methods.
38  *
39  * @author
40  * <a HREF="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
41  */

42 public class UnmarshallerImpl extends AbstractUnmarshallerImpl
43 {
44     /** parent JAXBContext object that created this unmarshaller */
45     private DefaultJAXBContextImpl context = null;
46     
47     private final GrammarInfo grammarInfo;
48     
49     public UnmarshallerImpl( DefaultJAXBContextImpl context, GrammarInfo gi ) {
50         
51         this.context = context;
52         this.grammarInfo = gi;
53
54         // initialize datatype converter with ours
55
DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
56     }
57     
58     public void setValidating(boolean validating) throws JAXBException {
59             super.setValidating(validating);
60             if(validating==true)
61                 // make sure that we can actually load the grammar.
62
// this could be a lengthy operation if your schema is big.
63
context.getGrammar();
64     }
65     
66     public UnmarshallerHandler getUnmarshallerHandler() {
67         
68         // use InterningUnmarshallerHandler since we don't know
69
// if the caller will intern Strings before firing SAX events.
70

71         // we don't know the Locator to be used,
72
// but SAXLocator would always be a good default,
73
// as the source of SAX2 events can always set org.xml.sax.Locator.
74
return new InterningUnmarshallerHandler(
75                 createUnmarshallerHandler(new SAXLocator()));
76     }
77     
78     
79     
80     /**
81      * Creates and configures a new unmarshalling pipe line.
82      * Depending on the setting, we put a validator as a filter.
83      *
84      * @return
85      * A component that implements both UnmarshallerHandler
86      * and ValidationEventHandler. All the parsing errors
87      * should be reported to this error handler for the unmarshalling
88      * process to work correctly.
89      *
90      * @param locator
91      * The object that is responsible to obtain the source
92      * location information for {@link ValidationEvent}s.
93      */

94     private SAXUnmarshallerHandler createUnmarshallerHandler( Locator locator ) {
95
96         SAXUnmarshallerHandler unmarshaller =
97             new SAXUnmarshallerHandlerImpl( this, grammarInfo );
98
99         try {
100             
101             // use the simple check to determine if validation is on
102
if( isValidating() ) {
103                 // if the validation is turned on, insert another
104
// component into the event pipe line.
105
unmarshaller = ValidatingUnmarshaller.create(
106                     context.getGrammar(), unmarshaller, locator );
107             }
108         } catch( JAXBException e ) {
109             // impossible since we've already made sure that a grammar is accessible.
110
e.printStackTrace();
111         }
112         
113         return unmarshaller;
114     }
115
116
117     protected Object JavaDoc unmarshal( XMLReader JavaDoc reader, InputSource JavaDoc source ) throws JAXBException {
118         
119         SAXLocator locator = new SAXLocator();
120         SAXUnmarshallerHandler handler = createUnmarshallerHandler(locator);
121         
122         reader = InterningXMLReader.adapt(reader);
123         
124         reader.setContentHandler(handler);
125         // saxErrorHandler will be set by the createUnmarshallerHandler method.
126
// configure XMLReader so that the error will be sent to it.
127
// This is essential for the UnmarshallerHandler to be able to abort
128
// unmarshalling when an error is found.
129
//
130
// Note that when this XMLReader is provided by the client code,
131
// it might be already configured to call a client error handler.
132
// This will clobber such handler, if any.
133
//
134
// Ryan noted that we might want to report errors to such a client
135
// error handler as well.
136
reader.setErrorHandler(
137             new ErrorHandlerAdaptor(handler,locator));
138         
139         try {
140             reader.parse(source);
141         } catch( IOException JavaDoc e ) {
142             throw new JAXBException(e);
143         } catch( SAXException JavaDoc e ) {
144             throw createUnmarshalException(e);
145         }
146         
147         Object JavaDoc result = handler.getResult();
148         
149         // avoid keeping unnecessary references too long to let the GC
150
// reclaim more memory.
151
// setting null upsets some parsers, so use a dummy instance instead.
152
reader.setContentHandler(dummyHandler);
153         reader.setErrorHandler(dummyHandler);
154         
155         return result;
156     }
157     
158     public final Object JavaDoc unmarshal( Node JavaDoc node ) throws JAXBException {
159         try {
160             DOMScanner scanner = new DOMScanner();
161             UnmarshallerHandler handler = new InterningUnmarshallerHandler(
162                 createUnmarshallerHandler(new DOMLocator(scanner)));
163             
164             if(node instanceof Element JavaDoc)
165                 scanner.parse((Element JavaDoc)node,handler);
166             else
167             if(node instanceof Document JavaDoc)
168                 scanner.parse(((Document JavaDoc)node).getDocumentElement(),handler);
169             else
170                 // no other type of input is supported
171
throw new IllegalArgumentException JavaDoc();
172             
173             return handler.getResult();
174         } catch( SAXException JavaDoc e ) {
175             throw createUnmarshalException(e);
176         }
177     }
178     
179     private static final DefaultHandler JavaDoc dummyHandler = new DefaultHandler JavaDoc();
180 }
181
Popular Tags