View Javadoc

1   //
2   // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v@@BUILD_VERSION@@ 
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.30 um 05:21:08 CEST 
6   //
7   
8   package de.nierbeck.timeTrack.model.impl.runtime;
9   
10  import java.io.IOException;
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;
18  import org.w3c.dom.Element;
19  import org.w3c.dom.Node;
20  import org.xml.sax.InputSource;
21  import org.xml.sax.SAXException;
22  import org.xml.sax.XMLReader;
23  import org.xml.sax.helpers.DefaultHandler;
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 type-safe
37   * unmarshall methods.
38   * 
39   * @author <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
40   */
41  public class UnmarshallerImpl extends AbstractUnmarshallerImpl {
42  	/*** parent JAXBContext object that created this unmarshaller */
43  	private DefaultJAXBContextImpl context = null;
44  
45  	private final GrammarInfo grammarInfo;
46  
47  	public UnmarshallerImpl(DefaultJAXBContextImpl context, GrammarInfo gi) {
48  
49  		this.context = context;
50  		this.grammarInfo = gi;
51  
52  		// initialize datatype converter with ours
53  		DatatypeConverter
54  				.setDatatypeConverter(DatatypeConverterImpl.theInstance);
55  	}
56  
57  	public void setValidating(boolean validating) throws JAXBException {
58  		super.setValidating(validating);
59  		if (validating == true)
60  			// make sure that we can actually load the grammar.
61  			// this could be a lengthy operation if your schema is big.
62  			context.getGrammar();
63  	}
64  
65  	public UnmarshallerHandler getUnmarshallerHandler() {
66  
67  		// use InterningUnmarshallerHandler since we don't know
68  		// if the caller will intern Strings before firing SAX events.
69  
70  		// we don't know the Locator to be used,
71  		// but SAXLocator would always be a good default,
72  		// as the source of SAX2 events can always set org.xml.sax.Locator.
73  		return new InterningUnmarshallerHandler(
74  				createUnmarshallerHandler(new SAXLocator()));
75  	}
76  
77  	/***
78  	 * Creates and configures a new unmarshalling pipe line. Depending on the
79  	 * setting, we put a validator as a filter.
80  	 * 
81  	 * @return A component that implements both UnmarshallerHandler and
82  	 *         ValidationEventHandler. All the parsing errors should be reported
83  	 *         to this error handler for the unmarshalling process to work
84  	 *         correctly.
85  	 * 
86  	 * @param locator
87  	 *            The object that is responsible to obtain the source location
88  	 *            information for {@link ValidationEvent}s.
89  	 */
90  	private SAXUnmarshallerHandler createUnmarshallerHandler(Locator locator) {
91  
92  		SAXUnmarshallerHandler unmarshaller = new SAXUnmarshallerHandlerImpl(
93  				this, grammarInfo);
94  
95  		try {
96  
97  			// use the simple check to determine if validation is on
98  			if (isValidating()) {
99  				// if the validation is turned on, insert another
100 				// component into the event pipe line.
101 				unmarshaller = ValidatingUnmarshaller.create(context
102 						.getGrammar(), unmarshaller, locator);
103 			}
104 		} catch (JAXBException e) {
105 			// impossible since we've already made sure that a grammar is
106 			// accessible.
107 			e.printStackTrace();
108 		}
109 
110 		return unmarshaller;
111 	}
112 
113 	protected Object unmarshal(XMLReader reader, InputSource source)
114 			throws JAXBException {
115 
116 		SAXLocator locator = new SAXLocator();
117 		SAXUnmarshallerHandler handler = createUnmarshallerHandler(locator);
118 
119 		reader = InterningXMLReader.adapt(reader);
120 
121 		reader.setContentHandler(handler);
122 		// saxErrorHandler will be set by the createUnmarshallerHandler method.
123 		// configure XMLReader so that the error will be sent to it.
124 		// This is essential for the UnmarshallerHandler to be able to abort
125 		// unmarshalling when an error is found.
126 		//
127 		// Note that when this XMLReader is provided by the client code,
128 		// it might be already configured to call a client error handler.
129 		// This will clobber such handler, if any.
130 		//
131 		// Ryan noted that we might want to report errors to such a client
132 		// error handler as well.
133 		reader.setErrorHandler(new ErrorHandlerAdaptor(handler, locator));
134 
135 		try {
136 			reader.parse(source);
137 		} catch (IOException e) {
138 			throw new JAXBException(e);
139 		} catch (SAXException e) {
140 			throw createUnmarshalException(e);
141 		}
142 
143 		Object result = handler.getResult();
144 
145 		// avoid keeping unnecessary references too long to let the GC
146 		// reclaim more memory.
147 		// setting null upsets some parsers, so use a dummy instance instead.
148 		reader.setContentHandler(dummyHandler);
149 		reader.setErrorHandler(dummyHandler);
150 
151 		return result;
152 	}
153 
154 	public final Object unmarshal(Node node) throws JAXBException {
155 		try {
156 			DOMScanner scanner = new DOMScanner();
157 			UnmarshallerHandler handler = new InterningUnmarshallerHandler(
158 					createUnmarshallerHandler(new DOMLocator(scanner)));
159 
160 			if (node instanceof Element)
161 				scanner.parse((Element) node, handler);
162 			else if (node instanceof Document)
163 				scanner.parse(((Document) node).getDocumentElement(), handler);
164 			else
165 				// no other type of input is supported
166 				throw new IllegalArgumentException();
167 
168 			return handler.getResult();
169 		} catch (SAXException e) {
170 			throw createUnmarshalException(e);
171 		}
172 	}
173 
174 	private static final DefaultHandler dummyHandler = new DefaultHandler();
175 }