KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > UnmarshallerImpl


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

35 public class UnmarshallerImpl extends AbstractUnmarshallerImpl
36 {
37     /** parent JAXBContext object that created this unmarshaller */
38     private DefaultJAXBContextImpl context = null;
39     
40     private final GrammarInfo grammarInfo;
41     
42     public UnmarshallerImpl( DefaultJAXBContextImpl context, GrammarInfo gi ) {
43         
44         this.context = context;
45         this.grammarInfo = gi;
46
47         // initialize datatype converter with ours
48
DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
49     }
50     
51     public void setValidating(boolean validating) throws JAXBException {
52         if( MetaVariable.W ) {// META-IF(W)
53
super.setValidating(validating);
54             if(validating==true)
55                 // make sure that we can actually load the grammar.
56
// this could be a lengthy operation if your schema is big.
57
context.getGrammar();
58         } else {// META-ELSE
59
throw new UnsupportedOperationException JavaDoc(
60                 "When generating this code, the compiler option was specified not to generate code for the unmarshal-time validation");
61         }// META-ENDIF
62
}
63     
64     public UnmarshallerHandler getUnmarshallerHandler() {
65         
66         // use InterningUnmarshallerHandler since we don't know
67
// if the caller will intern Strings before firing SAX events.
68

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

92     private SAXUnmarshallerHandler createUnmarshallerHandler( Locator locator ) {
93
94         SAXUnmarshallerHandler unmarshaller =
95             new SAXUnmarshallerHandlerImpl( this, grammarInfo );
96
97 // META-IF(W)
98
try {
99             
100             // use the simple check to determine if validation is on
101
if( isValidating() ) {
102                 // if the validation is turned on, insert another
103
// component into the event pipe line.
104
unmarshaller = ValidatingUnmarshaller.create(
105                     context.getGrammar(), unmarshaller, locator );
106             }
107         } catch( JAXBException e ) {
108             // impossible since we've already made sure that a grammar is accessible.
109
e.printStackTrace();
110         }
111 // META-ENDIF
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