001 //
002 // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.1-05/30/2003 05:06 AM(java_re)-fcs
003 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
004 // Any modifications to this file will be lost upon recompilation of the source schema.
005 // Generated on: 2004.10.11 at 12:13:34 EDT
006 //
007
008 /*
009 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
010 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
011 */
012
013 /*
014 * @(#)$Id: UnmarshallerImpl.java,v 1.7 2003/05/19 17:56:48 kk122374 Exp $
015 */
016 package astronomy.data.spectra.impl.runtime;
017
018 import java.io.IOException;
019
020 import javax.xml.bind.DatatypeConverter;
021 import javax.xml.bind.JAXBException;
022 import javax.xml.bind.UnmarshallerHandler;
023 import javax.xml.bind.helpers.AbstractUnmarshallerImpl;
024
025 import org.w3c.dom.Document;
026 import org.w3c.dom.Element;
027 import org.w3c.dom.Node;
028 import org.xml.sax.InputSource;
029 import org.xml.sax.SAXException;
030 import org.xml.sax.XMLReader;
031 import org.xml.sax.helpers.DefaultHandler;
032
033 import com.sun.xml.bind.DatatypeConverterImpl;
034 import com.sun.xml.bind.unmarshaller.DOMScanner;
035 import com.sun.xml.bind.validator.DOMLocator;
036 import com.sun.xml.bind.validator.Locator;
037 import com.sun.xml.bind.validator.SAXLocator;
038
039 /**
040 * Default Unmarshall implementation.
041 *
042 * <p>
043 * This class can be extended by the generated code to provide
044 * type-safe unmarshall methods.
045 *
046 * @author
047 * <a href="mailto:kohsuke.kawaguchi@sun.com>Kohsuke KAWAGUCHI</a>
048 */
049 public class UnmarshallerImpl extends AbstractUnmarshallerImpl
050 {
051 /** parent JAXBContext object that created this unmarshaller */
052 private DefaultJAXBContextImpl context = null;
053
054 private final GrammarInfo grammarInfo;
055
056 public UnmarshallerImpl( DefaultJAXBContextImpl context, GrammarInfo gi ) {
057
058 this.context = context;
059 this.grammarInfo = gi;
060
061 // initialize datatype converter with ours
062 DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
063 }
064
065 public void setValidating(boolean validating) throws JAXBException {
066 super.setValidating(validating);
067 if(validating==true)
068 // make sure that we can actually load the grammar.
069 // this could be a lengthy operation if your schema is big.
070 context.getGrammar();
071 }
072
073 public UnmarshallerHandler getUnmarshallerHandler() {
074 // TODO: use only one instance.
075
076 // we don't know the Locator to be used,
077 // but SAXLocator would always be a good default,
078 // as the source of SAX2 events can always set org.xml.sax.Locator.
079 return createUnmarshallerHandler(new SAXLocator());
080 }
081
082
083
084 /**
085 * Creates and configures a new unmarshalling pipe line.
086 * Depending on the setting, we put a validator as a filter.
087 *
088 * @return
089 * A component that implements both UnmarshallerHandler
090 * and ValidationEventHandler. All the parsing errors
091 * should be reported to this error handler for the unmarshalling
092 * process to work correctly.
093 *
094 * @param locator
095 * The object that is responsible to obtain the source
096 * location information for {@link ValidationEvent}s.
097 */
098 public SAXUnmarshallerHandler createUnmarshallerHandler( Locator locator ) {
099
100 SAXUnmarshallerHandler unmarshaller =
101 new SAXUnmarshallerHandlerImpl( this, grammarInfo );
102
103 try {
104
105 // use the simple check to determine if validation is on
106 if( isValidating() ) {
107 // if the validation is turned on, insert another
108 // component into the event pipe line.
109 unmarshaller = ValidatingUnmarshaller.create(
110 context.getGrammar(), unmarshaller, locator );
111 }
112 } catch( JAXBException e ) {
113 // impossible since we've already made sure that a grammar is accessible.
114 e.printStackTrace();
115 }
116
117 return unmarshaller;
118 }
119
120
121 protected Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException {
122
123 SAXLocator locator = new SAXLocator();
124 SAXUnmarshallerHandler handler = createUnmarshallerHandler(locator);
125
126 reader.setContentHandler(handler);
127 // saxErrorHandler will be set by the createUnmarshallerHandler method.
128 // configure XMLReader so that the error will be sent to it.
129 // This is essential for the UnmarshallerHandler to be able to abort
130 // unmarshalling when an error is found.
131 //
132 // Note that when this XMLReader is provided by the client code,
133 // it might be already configured to call a client error handler.
134 // This will clobber such handler, if any.
135 //
136 // Ryan noted that we might want to report errors to such a client
137 // error handler as well.
138 reader.setErrorHandler(
139 new ErrorHandlerAdaptor(handler,locator));
140
141 try {
142 reader.parse(source);
143 } catch( IOException e ) {
144 throw new JAXBException(e);
145 } catch( SAXException e ) {
146 throw createUnmarshalException(e);
147 }
148
149 Object result = handler.getResult();
150
151 // avoid keeping unnecessary references too long to let the GC
152 // reclaim more memory.
153 // setting null upsets some parsers, so use a dummy instance instead.
154 reader.setContentHandler(dummyHandler);
155 reader.setErrorHandler(dummyHandler);
156
157 return result;
158 }
159
160 public final Object unmarshal( Node node ) throws JAXBException {
161 try {
162 DOMScanner scanner = new DOMScanner();
163 UnmarshallerHandler handler = createUnmarshallerHandler(new DOMLocator(scanner));
164
165 if(node instanceof Element)
166 scanner.parse((Element)node,handler);
167 else
168 if(node instanceof Document)
169 scanner.parse(((Document)node).getDocumentElement(),handler);
170 else
171 // no other type of input is supported
172 throw new IllegalArgumentException();
173
174 return handler.getResult();
175 } catch( SAXException e ) {
176 throw createUnmarshalException(e);
177 }
178 }
179
180
181 private static void _assert( boolean b, String msg ) {
182 if( !b ) {
183 throw new InternalError( msg );
184 }
185 }
186
187 private static final DefaultHandler dummyHandler = new DefaultHandler();
188 }