1 /*
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.xml.bind;
27
28 import javax.xml.bind.annotation.adapters.XmlAdapter;
29 import javax.xml.bind.attachment.AttachmentUnmarshaller;
30 import javax.xml.validation.Schema;
31 import java.io.Reader;
32
33 /**
34 * The <tt>Unmarshaller</tt> class governs the process of deserializing XML
35 * data into newly created Java content trees, optionally validating the XML
36 * data as it is unmarshalled. It provides an overloading of unmarshal methods
37 * for many different input kinds.
38 *
39 * <p>
40 * Unmarshalling from a File:
41 * <blockquote>
42 * <pre>
43 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
44 * Unmarshaller u = jc.createUnmarshaller();
45 * Object o = u.unmarshal( new File( "nosferatu.xml" ) );
46 * </pre>
47 * </blockquote>
48 *
49 *
50 * <p>
51 * Unmarshalling from an InputStream:
52 * <blockquote>
53 * <pre>
54 * InputStream is = new FileInputStream( "nosferatu.xml" );
55 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
56 * Unmarshaller u = jc.createUnmarshaller();
57 * Object o = u.unmarshal( is );
58 * </pre>
59 * </blockquote>
60 *
61 * <p>
62 * Unmarshalling from a URL:
63 * <blockquote>
64 * <pre>
65 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
66 * Unmarshaller u = jc.createUnmarshaller();
67 * URL url = new URL( "http://beaker.east/nosferatu.xml" );
68 * Object o = u.unmarshal( url );
69 * </pre>
70 * </blockquote>
71 *
72 * <p>
73 * Unmarshalling from a StringBuffer using a
74 * <tt>javax.xml.transform.stream.StreamSource</tt>:
75 * <blockquote>
76 * <pre>
77 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
78 * Unmarshaller u = jc.createUnmarshaller();
79 * StringBuffer xmlStr = new StringBuffer( "<?xml version="1.0"?>..." );
80 * Object o = u.unmarshal( new StreamSource( new StringReader( xmlStr.toString() ) ) );
81 * </pre>
82 * </blockquote>
83 *
84 * <p>
85 * Unmarshalling from a <tt>org.w3c.dom.Node</tt>:
86 * <blockquote>
87 * <pre>
88 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
89 * Unmarshaller u = jc.createUnmarshaller();
90 *
91 * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
92 * dbf.setNamespaceAware(true);
93 * DocumentBuilder db = dbf.newDocumentBuilder();
94 * Document doc = db.parse(new File( "nosferatu.xml"));
95
96 * Object o = u.unmarshal( doc );
97 * </pre>
98 * </blockquote>
99 *
100 * <p>
101 * Unmarshalling from a <tt>javax.xml.transform.sax.SAXSource</tt> using a
102 * client specified validating SAX2.0 parser:
103 * <blockquote>
104 * <pre>
105 * // configure a validating SAX2.0 parser (Xerces2)
106 * static final String JAXP_SCHEMA_LANGUAGE =
107 * "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
108 * static final String JAXP_SCHEMA_LOCATION =
109 * "http://java.sun.com/xml/jaxp/properties/schemaSource";
110 * static final String W3C_XML_SCHEMA =
111 * "http://www.w3.org/2001/XMLSchema";
112 *
113 * System.setProperty( "javax.xml.parsers.SAXParserFactory",
114 * "org.apache.xerces.jaxp.SAXParserFactoryImpl" );
115 *
116 * SAXParserFactory spf = SAXParserFactory.newInstance();
117 * spf.setNamespaceAware(true);
118 * spf.setValidating(true);
119 * SAXParser saxParser = spf.newSAXParser();
120 *
121 * try {
122 * saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
123 * saxParser.setProperty(JAXP_SCHEMA_LOCATION, "http://....");
124 * } catch (SAXNotRecognizedException x) {
125 * // exception handling omitted
126 * }
127 *
128 * XMLReader xmlReader = saxParser.getXMLReader();
129 * SAXSource source =
130 * new SAXSource( xmlReader, new InputSource( "http://..." ) );
131 *
132 * // Setup JAXB to unmarshal
133 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
134 * Unmarshaller u = jc.createUnmarshaller();
135 * ValidationEventCollector vec = new ValidationEventCollector();
136 * u.setEventHandler( vec );
137 *
138 * // turn off the JAXB provider's default validation mechanism to
139 * // avoid duplicate validation
140 * u.setValidating( false )
141 *
142 * // unmarshal
143 * Object o = u.unmarshal( source );
144 *
145 * // check for events
146 * if( vec.hasEvents() ) {
147 * // iterate over events
148 * }
149 * </pre>
150 * </blockquote>
151 *
152 * <p>
153 * Unmarshalling from a StAX XMLStreamReader:
154 * <blockquote>
155 * <pre>
156 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
157 * Unmarshaller u = jc.createUnmarshaller();
158 *
159 * javax.xml.stream.XMLStreamReader xmlStreamReader =
160 * javax.xml.stream.XMLInputFactory().newInstance().createXMLStreamReader( ... );
161 *
162 * Object o = u.unmarshal( xmlStreamReader );
163 * </pre>
164 * </blockquote>
165 *
166 * <p>
167 * Unmarshalling from a StAX XMLEventReader:
168 * <blockquote>
169 * <pre>
170 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
171 * Unmarshaller u = jc.createUnmarshaller();
172 *
173 * javax.xml.stream.XMLEventReader xmlEventReader =
174 * javax.xml.stream.XMLInputFactory().newInstance().createXMLEventReader( ... );
175 *
176 * Object o = u.unmarshal( xmlEventReader );
177 * </pre>
178 * </blockquote>
179 *
180 * <p>
181 * <a name="unmarshalEx"></a>
182 * <b>Unmarshalling XML Data</b><br>
183 * <blockquote>
184 * Unmarshalling can deserialize XML data that represents either an entire XML document
185 * or a subtree of an XML document. Typically, it is sufficient to use the
186 * unmarshalling methods described by
187 * <a href="#unmarshalGlobal">Unmarshal root element that is declared globally</a>.
188 * These unmarshal methods utilize {@link JAXBContext}'s mapping of global XML element
189 * declarations and type definitions to JAXB mapped classes to initiate the
190 * unmarshalling of the root element of XML data. When the {@link JAXBContext}'s
191 * mappings are not sufficient to unmarshal the root element of XML data,
192 * the application can assist the unmarshalling process by using the
193 * <a href="#unmarshalByDeclaredType">unmarshal by declaredType methods</a>.
194 * These methods are useful for unmarshalling XML data where
195 * the root element corresponds to a local element declaration in the schema.
196 * </blockquote>
197 *
198 * <blockquote>
199 * An unmarshal method never returns null. If the unmarshal process is unable to unmarshal
200 * the root of XML content to a JAXB mapped object, a fatal error is reported that
201 * terminates processing by throwing JAXBException.
202 * </blockquote>
203 *
204 * <p>
205 * <a name="unmarshalGlobal"></a>
206 * <b>Unmarshal a root element that is globally declared</b><br>
207 * <blockquote>
208 * The unmarshal methods that do not have an <tt>declaredType</tt> parameter use
209 * {@link JAXBContext} to unmarshal the root element of an XML data. The {@link JAXBContext}
210 * instance is the one that was used to create this <tt>Unmarshaller</tt>. The {@link JAXBContext}
211 * instance maintains a mapping of globally declared XML element and type definition names to
212 * JAXB mapped classes. The unmarshal method checks if {@link JAXBContext} has a mapping
213 * from the root element's XML name and/or <tt>@xsi:type</tt> to a JAXB mapped class. If it does, it umarshalls the
214 * XML data using the appropriate JAXB mapped class. Note that when the root element name is unknown and the root
215 * element has an <tt>@xsi:type</tt>, the XML data is unmarshalled
216 * using that JAXB mapped class as the value of a {@link JAXBElement}.
217 * When the {@link JAXBContext} object does not have a mapping for the root element's name
218 * nor its <tt>@xsi:type</tt>, if it exists,
219 * then the unmarshal operation will abort immediately by throwing a {@link UnmarshalException
220 * UnmarshalException}. This exception scenario can be worked around by using the unmarshal by
221 * declaredType methods described in the next subsection.
222 * </blockquote>
223 *
224 * <p>
225 * <a name="unmarshalByDeclaredType"></a>
226 * <b>Unmarshal by Declared Type</b><br>
227 * <blockquote>
228 * The unmarshal methods with a <code>declaredType</code> parameter enable an
229 * application to deserialize a root element of XML data, even when
230 * there is no mapping in {@link JAXBContext} of the root element's XML name.
231 * The unmarshaller unmarshals the root element using the application provided
232 * mapping specified as the <tt>declaredType</tt> parameter.
233 * Note that even when the root element's element name is mapped by {@link JAXBContext},
234 * the <code>declaredType</code> parameter overrides that mapping for
235 * deserializing the root element when using these unmarshal methods.
236 * Additionally, when the root element of XML data has an <tt>xsi:type</tt> attribute and
237 * that attribute's value references a type definition that is mapped
238 * to a JAXB mapped class by {@link JAXBContext}, that the root
239 * element's <tt>xsi:type</tt> attribute takes
240 * precedence over the unmarshal methods <tt>declaredType</tt> parameter.
241 * These methods always return a <tt>JAXBElement<declaredType></tt>
242 * instance. The table below shows how the properties of the returned JAXBElement
243 * instance are set.
244 *
245 * <a name="unmarshalDeclaredTypeReturn"></a>
246 * <p>
247 * <table border="2" rules="all" cellpadding="4">
248 * <thead>
249 * <tr>
250 * <th align="center" colspan="2">
251 * Unmarshal By Declared Type returned JAXBElement
252 * </tr>
253 * <tr>
254 * <th>JAXBElement Property</th>
255 * <th>Value</th>
256 * </tr>
257 * </tr>
258 * <tr>
259 * <td>name</td>
260 * <td><code>xml element name</code></td>
261 * </tr>
262 * </thead>
263 * <tbody>
264 * <tr>
265 * <td>value</td>
266 * <td><code>instanceof declaredType</code></td>
267 * </tr>
268 * <tr>
269 * <td>declaredType</td>
270 * <td>unmarshal method <code>declaredType</code> parameter</td>
271 * </tr>
272 * <tr>
273 * <td>scope</td>
274 * <td><code>null</code> <i>(actual scope is unknown)</i></td>
275 * </tr>
276 * </tbody>
277 * </table>
278 * </blockquote>
279 *
280 * <p>
281 * The following is an example of
282 * <a href="#unmarshalByDeclaredType">unmarshal by declaredType method</a>.
283 * <p>
284 * Unmarshal by declaredType from a <tt>org.w3c.dom.Node</tt>:
285 * <blockquote>
286 * <pre>
287 * Schema fragment for example
288 * <xs:schema>
289 * <xs:complexType name="FooType">...<\xs:complexType>
290 * <!-- global element declaration "PurchaseOrder" -->
291 * <xs:element name="PurchaseOrder">
292 * <xs:complexType>
293 * <xs:sequence>
294 * <!-- local element declaration "foo" -->
295 * <xs:element name="foo" type="FooType"/>
296 * ...
297 * </xs:sequence>
298 * </xs:complexType>
299 * </xs:element>
300 * </xs:schema>
301 *
302 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
303 * Unmarshaller u = jc.createUnmarshaller();
304 *
305 * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
306 * dbf.setNamespaceAware(true);
307 * DocumentBuilder db = dbf.newDocumentBuilder();
308 * Document doc = db.parse(new File( "nosferatu.xml"));
309 * Element fooSubtree = ...; // traverse DOM till reach xml element foo, constrained by a
310 * // local element declaration in schema.
311 *
312 * // FooType is the JAXB mapping of the type of local element declaration foo.
313 * JAXBElement<FooType> foo = u.unmarshal( fooSubtree, FooType.class);
314 * </pre>
315 * </blockquote>
316 *
317 * <p>
318 * <b>Support for SAX2.0 Compliant Parsers</b><br>
319 * <blockquote>
320 * A client application has the ability to select the SAX2.0 compliant parser
321 * of their choice. If a SAX parser is not selected, then the JAXB Provider's
322 * default parser will be used. Even though the JAXB Provider's default parser
323 * is not required to be SAX2.0 compliant, all providers are required to allow
324 * a client application to specify their own SAX2.0 parser. Some providers may
325 * require the client application to specify the SAX2.0 parser at schema compile
326 * time. See {@link #unmarshal(javax.xml.transform.Source) unmarshal(Source)}
327 * for more detail.
328 * </blockquote>
329 *
330 * <p>
331 * <b>Validation and Well-Formedness</b><br>
332 * <blockquote>
333 * <p>
334 * A client application can enable or disable JAXP 1.3 validation
335 * mechanism via the <tt>setSchema(javax.xml.validation.Schema)</tt> API.
336 * Sophisticated clients can specify their own validating SAX 2.0 compliant
337 * parser and bypass the JAXP 1.3 validation mechanism using the
338 * {@link #unmarshal(javax.xml.transform.Source) unmarshal(Source)} API.
339 *
340 * <p>
341 * Since unmarshalling invalid XML content is defined in JAXB 2.0,
342 * the Unmarshaller default validation event handler was made more lenient
343 * than in JAXB 1.0. When schema-derived code generated
344 * by JAXB 1.0 binding compiler is registered with {@link JAXBContext},
345 * the default unmarshal validation handler is
346 * {@link javax.xml.bind.helpers.DefaultValidationEventHandler} and it
347 * terminates the marshal operation after encountering either a fatal error or an error.
348 * For a JAXB 2.0 client application, there is no explicitly defined default
349 * validation handler and the default event handling only
350 * terminates the marshal operation after encountering a fatal error.
351 *
352 * </blockquote>
353 *
354 * <p>
355 * <a name="supportedProps"></a>
356 * <b>Supported Properties</b><br>
357 * <blockquote>
358 * <p>
359 * There currently are not any properties required to be supported by all
360 * JAXB Providers on Unmarshaller. However, some providers may support
361 * their own set of provider specific properties.
362 * </blockquote>
363 *
364 * <p>
365 * <a name="unmarshalEventCallback"></a>
366 * <b>Unmarshal Event Callbacks</b><br>
367 * <blockquote>
368 * The {@link Unmarshaller} provides two styles of callback mechanisms
369 * that allow application specific processing during key points in the
370 * unmarshalling process. In 'class defined' event callbacks, application
371 * specific code placed in JAXB mapped classes is triggered during
372 * unmarshalling. 'External listeners' allow for centralized processing
373 * of unmarshal events in one callback method rather than by type event callbacks.
374 * <p>
375 * 'Class defined' event callback methods allow any JAXB mapped class to specify
376 * its own specific callback methods by defining methods with the following method signature:
377 * <blockquote>
378 * <pre>
379 * // This method is called immediately after the object is created and before the unmarshalling of this
380 * // object begins. The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.
381 * void beforeUnmarshal(Unmarshaller, Object parent);
382 *
383 * //This method is called after all the properties (except IDREF) are unmarshalled for this object,
384 * //but before this object is set to the parent object.
385 * void afterUnmarshal(Unmarshaller, Object parent);
386 * </pre>
387 * </blockquote>
388 * The class defined callback methods should be used when the callback method requires
389 * access to non-public methods and/or fields of the class.
390 * <p>
391 * The external listener callback mechanism enables the registration of a {@link Listener}
392 * instance with an {@link Unmarshaller#setListener(Listener)}. The external listener receives all callback events,
393 * allowing for more centralized processing than per class defined callback methods. The external listener
394 * receives events when unmarshalling proces is marshalling to a JAXB element or to JAXB mapped class.
395 * <p>
396 * The 'class defined' and external listener event callback methods are independent of each other,
397 * both can be called for one event. The invocation ordering when both listener callback methods exist is
398 * defined in {@link Listener#beforeUnmarshal(Object, Object)} and {@link Listener#afterUnmarshal(Object, Object)}.
399 * <p>
400 * An event callback method throwing an exception terminates the current unmarshal process.
401 *
402 * </blockquote>
403 *
404 * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
405 * @see JAXBContext
406 * @see Marshaller
407 * @see Validator
408 * @since JAXB1.0
409 */
410 public interface Unmarshaller {
411
412 /**
413 * Unmarshal XML data from the specified file and return the resulting
414 * content tree.
415 *
416 * <p>
417 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
418 *
419 * @param f the file to unmarshal XML data from
420 * @return the newly created root object of the java content tree
421 *
422 * @throws JAXBException
423 * If any unexpected errors occur while unmarshalling
424 * @throws UnmarshalException
425 * If the {@link ValidationEventHandler ValidationEventHandler}
426 * returns false from its <tt>handleEvent</tt> method or the
427 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
428 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
429 * @throws IllegalArgumentException
430 * If the file parameter is null
431 */
432 public Object unmarshal( java.io.File f ) throws JAXBException;
433
434 /**
435 * Unmarshal XML data from the specified InputStream and return the
436 * resulting content tree. Validation event location information may
437 * be incomplete when using this form of the unmarshal API.
438 *
439 * <p>
440 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
441 *
442 * @param is the InputStream to unmarshal XML data from
443 * @return the newly created root object of the java content tree
444 *
445 * @throws JAXBException
446 * If any unexpected errors occur while unmarshalling
447 * @throws UnmarshalException
448 * If the {@link ValidationEventHandler ValidationEventHandler}
449 * returns false from its <tt>handleEvent</tt> method or the
450 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
451 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
452 * @throws IllegalArgumentException
453 * If the InputStream parameter is null
454 */
455 public Object unmarshal( java.io.InputStream is ) throws JAXBException;
456
457 /**
458 * Unmarshal XML data from the specified Reader and return the
459 * resulting content tree. Validation event location information may
460 * be incomplete when using this form of the unmarshal API,
461 * because a Reader does not provide the system ID.
462 *
463 * <p>
464 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
465 *
466 * @param reader the Reader to unmarshal XML data from
467 * @return the newly created root object of the java content tree
468 *
469 * @throws JAXBException
470 * If any unexpected errors occur while unmarshalling
471 * @throws UnmarshalException
472 * If the {@link ValidationEventHandler ValidationEventHandler}
473 * returns false from its <tt>handleEvent</tt> method or the
474 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
475 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
476 * @throws IllegalArgumentException
477 * If the InputStream parameter is null
478 * @since JAXB2.0
479 */
480 public Object unmarshal( Reader reader ) throws JAXBException;
481
482 /**
483 * Unmarshal XML data from the specified URL and return the resulting
484 * content tree.
485 *
486 * <p>
487 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
488 *
489 * @param url the url to unmarshal XML data from
490 * @return the newly created root object of the java content tree
491 *
492 * @throws JAXBException
493 * If any unexpected errors occur while unmarshalling
494 * @throws UnmarshalException
495 * If the {@link ValidationEventHandler ValidationEventHandler}
496 * returns false from its <tt>handleEvent</tt> method or the
497 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
498 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
499 * @throws IllegalArgumentException
500 * If the URL parameter is null
501 */
502 public Object unmarshal( java.net.URL url ) throws JAXBException;
503
504 /**
505 * Unmarshal XML data from the specified SAX InputSource and return the
506 * resulting content tree.
507 *
508 * <p>
509 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
510 *
511 * @param source the input source to unmarshal XML data from
512 * @return the newly created root object of the java content tree
513 *
514 * @throws JAXBException
515 * If any unexpected errors occur while unmarshalling
516 * @throws UnmarshalException
517 * If the {@link ValidationEventHandler ValidationEventHandler}
518 * returns false from its <tt>handleEvent</tt> method or the
519 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
520 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
521 * @throws IllegalArgumentException
522 * If the InputSource parameter is null
523 */
524 public Object unmarshal( org.xml.sax.InputSource source ) throws JAXBException;
525
526 /**
527 * Unmarshal global XML data from the specified DOM tree and return the resulting
528 * content tree.
529 *
530 * <p>
531 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
532 *
533 * @param node
534 * the document/element to unmarshal XML data from.
535 * The caller must support at least Document and Element.
536 * @return the newly created root object of the java content tree
537 *
538 * @throws JAXBException
539 * If any unexpected errors occur while unmarshalling
540 * @throws UnmarshalException
541 * If the {@link ValidationEventHandler ValidationEventHandler}
542 * returns false from its <tt>handleEvent</tt> method or the
543 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
544 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
545 * @throws IllegalArgumentException
546 * If the Node parameter is null
547 * @see #unmarshal(org.w3c.dom.Node, Class)
548 */
549 public Object unmarshal( org.w3c.dom.Node node ) throws JAXBException;
550
551 /**
552 * Unmarshal XML data by JAXB mapped <tt>declaredType</tt>
553 * and return the resulting content tree.
554 *
555 * <p>
556 * Implements <a href="#unmarshalByDeclaredType">Unmarshal by Declared Type</a>
557 *
558 * @param node
559 * the document/element to unmarshal XML data from.
560 * The caller must support at least Document and Element.
561 * @param declaredType
562 * appropriate JAXB mapped class to hold <tt>node</tt>'s XML data.
563 *
564 * @return <a href="#unmarshalDeclaredTypeReturn">JAXB Element</a> representation of <tt>node</tt>
565 *
566 * @throws JAXBException
567 * If any unexpected errors occur while unmarshalling
568 * @throws UnmarshalException
569 * If the {@link ValidationEventHandler ValidationEventHandler}
570 * returns false from its <tt>handleEvent</tt> method or the
571 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
572 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
573 * @throws IllegalArgumentException
574 * If any parameter is null
575 * @since JAXB2.0
576 */
577 public <T> JAXBElement<T> unmarshal( org.w3c.dom.Node node, Class<T> declaredType ) throws JAXBException;
578
579 /**
580 * Unmarshal XML data from the specified XML Source and return the
581 * resulting content tree.
582 *
583 * <p>
584 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
585 *
586 * <p>
587 * <a name="saxParserPlugable"></a>
588 * <b>SAX 2.0 Parser Pluggability</b>
589 * <p>
590 * A client application can choose not to use the default parser mechanism
591 * supplied with their JAXB provider. Any SAX 2.0 compliant parser can be
592 * substituted for the JAXB provider's default mechanism. To do so, the
593 * client application must properly configure a <tt>SAXSource</tt> containing
594 * an <tt>XMLReader</tt> implemented by the SAX 2.0 parser provider. If the
595 * <tt>XMLReader</tt> has an <tt>org.xml.sax.ErrorHandler</tt> registered
596 * on it, it will be replaced by the JAXB Provider so that validation errors
597 * can be reported via the <tt>ValidationEventHandler</tt> mechanism of
598 * JAXB. If the <tt>SAXSource</tt> does not contain an <tt>XMLReader</tt>,
599 * then the JAXB provider's default parser mechanism will be used.
600 * <p>
601 * This parser replacement mechanism can also be used to replace the JAXB
602 * provider's unmarshal-time validation engine. The client application
603 * must properly configure their SAX 2.0 compliant parser to perform
604 * validation (as shown in the example above). Any <tt>SAXParserExceptions
605 * </tt> encountered by the parser during the unmarshal operation will be
606 * processed by the JAXB provider and converted into JAXB
607 * <tt>ValidationEvent</tt> objects which will be reported back to the
608 * client via the <tt>ValidationEventHandler</tt> registered with the
609 * <tt>Unmarshaller</tt>. <i>Note:</i> specifying a substitute validating
610 * SAX 2.0 parser for unmarshalling does not necessarily replace the
611 * validation engine used by the JAXB provider for performing on-demand
612 * validation.
613 * <p>
614 * The only way for a client application to specify an alternate parser
615 * mechanism to be used during unmarshal is via the
616 * <tt>unmarshal(SAXSource)</tt> API. All other forms of the unmarshal
617 * method (File, URL, Node, etc) will use the JAXB provider's default
618 * parser and validator mechanisms.
619 *
620 * @param source the XML Source to unmarshal XML data from (providers are
621 * only required to support SAXSource, DOMSource, and StreamSource)
622 * @return the newly created root object of the java content tree
623 *
624 * @throws JAXBException
625 * If any unexpected errors occur while unmarshalling
626 * @throws UnmarshalException
627 * If the {@link ValidationEventHandler ValidationEventHandler}
628 * returns false from its <tt>handleEvent</tt> method or the
629 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
630 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
631 * @throws IllegalArgumentException
632 * If the Source parameter is null
633 * @see #unmarshal(javax.xml.transform.Source, Class)
634 */
635 public Object unmarshal( javax.xml.transform.Source source )
636 throws JAXBException;
637
638
639 /**
640 * Unmarshal XML data from the specified XML Source by <tt>declaredType</tt> and return the
641 * resulting content tree.
642 *
643 * <p>
644 * Implements <a href="#unmarshalByDeclaredType">Unmarshal by Declared Type</a>
645 *
646 * <p>
647 * See <a href="#saxParserPlugable">SAX 2.0 Parser Pluggability</a>
648 *
649 * @param source the XML Source to unmarshal XML data from (providers are
650 * only required to support SAXSource, DOMSource, and StreamSource)
651 * @param declaredType
652 * appropriate JAXB mapped class to hold <tt>source</tt>'s xml root element
653 * @return Java content rooted by <a href="#unmarshalDeclaredTypeReturn">JAXB Element</a>
654 *
655 * @throws JAXBException
656 * If any unexpected errors occur while unmarshalling
657 * @throws UnmarshalException
658 * If the {@link ValidationEventHandler ValidationEventHandler}
659 * returns false from its <tt>handleEvent</tt> method or the
660 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
661 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
662 * @throws IllegalArgumentException
663 * If any parameter is null
664 * @since JAXB2.0
665 */
666 public <T> JAXBElement<T> unmarshal( javax.xml.transform.Source source, Class<T> declaredType )
667 throws JAXBException;
668
669 /**
670 * Unmarshal XML data from the specified pull parser and return the
671 * resulting content tree.
672 *
673 * <p>
674 * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
675 *
676 * <p>
677 * This method assumes that the parser is on a START_DOCUMENT or
678 * START_ELEMENT event. Unmarshalling will be done from this
679 * start event to the corresponding end event. If this method
680 * returns successfully, the <tt>reader</tt> will be pointing at
681 * the token right after the end event.
682 *
683 * @param reader
684 * The parser to be read.
685 * @return
686 * the newly created root object of the java content tree.
687 *
688 * @throws JAXBException
689 * If any unexpected errors occur while unmarshalling
690 * @throws UnmarshalException
691 * If the {@link ValidationEventHandler ValidationEventHandler}
692 * returns false from its <tt>handleEvent</tt> method or the
693 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
694 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
695 * @throws IllegalArgumentException
696 * If the <tt>reader</tt> parameter is null
697 * @throws IllegalStateException
698 * If <tt>reader</tt> is not pointing to a START_DOCUMENT or
699 * START_ELEMENT event.
700 * @since JAXB2.0
701 * @see #unmarshal(javax.xml.stream.XMLStreamReader, Class)
702 */
703 public Object unmarshal( javax.xml.stream.XMLStreamReader reader )
704 throws JAXBException;
705
706 /**
707 * Unmarshal root element to JAXB mapped <tt>declaredType</tt>
708 * and return the resulting content tree.
709 *
710 * <p>
711 * This method implements <a href="#unmarshalByDeclaredType">unmarshal by declaredType</a>.
712 * <p>
713 * This method assumes that the parser is on a START_DOCUMENT or
714 * START_ELEMENT event. Unmarshalling will be done from this
715 * start event to the corresponding end event. If this method
716 * returns successfully, the <tt>reader</tt> will be pointing at
717 * the token right after the end event.
718 *
719 * @param reader
720 * The parser to be read.
721 * @param declaredType
722 * appropriate JAXB mapped class to hold <tt>reader</tt>'s START_ELEMENT XML data.
723 *
724 * @return content tree rooted by <a href="#unmarshalDeclaredTypeReturn">JAXB Element representation</a>
725 *
726 * @throws JAXBException
727 * If any unexpected errors occur while unmarshalling
728 * @throws UnmarshalException
729 * If the {@link ValidationEventHandler ValidationEventHandler}
730 * returns false from its <tt>handleEvent</tt> method or the
731 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
732 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
733 * @throws IllegalArgumentException
734 * If any parameter is null
735 * @since JAXB2.0
736 */
737 public <T> JAXBElement<T> unmarshal( javax.xml.stream.XMLStreamReader reader, Class<T> declaredType ) throws JAXBException;
738
739 /**
740 * Unmarshal XML data from the specified pull parser and return the
741 * resulting content tree.
742 *
743 * <p>
744 * This method is an <a href="#unmarshalGlobal">Unmarshal Global Root method</a>.
745 *
746 * <p>
747 * This method assumes that the parser is on a START_DOCUMENT or
748 * START_ELEMENT event. Unmarshalling will be done from this
749 * start event to the corresponding end event. If this method
750 * returns successfully, the <tt>reader</tt> will be pointing at
751 * the token right after the end event.
752 *
753 * @param reader
754 * The parser to be read.
755 * @return
756 * the newly created root object of the java content tree.
757 *
758 * @throws JAXBException
759 * If any unexpected errors occur while unmarshalling
760 * @throws UnmarshalException
761 * If the {@link ValidationEventHandler ValidationEventHandler}
762 * returns false from its <tt>handleEvent</tt> method or the
763 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
764 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
765 * @throws IllegalArgumentException
766 * If the <tt>reader</tt> parameter is null
767 * @throws IllegalStateException
768 * If <tt>reader</tt> is not pointing to a START_DOCUMENT or
769 * START_ELEMENT event.
770 * @since JAXB2.0
771 * @see #unmarshal(javax.xml.stream.XMLEventReader, Class)
772 */
773 public Object unmarshal( javax.xml.stream.XMLEventReader reader )
774 throws JAXBException;
775
776 /**
777 * Unmarshal root element to JAXB mapped <tt>declaredType</tt>
778 * and return the resulting content tree.
779 *
780 * <p>
781 * This method implements <a href="#unmarshalByDeclaredType">unmarshal by declaredType</a>.
782 *
783 * <p>
784 * This method assumes that the parser is on a START_DOCUMENT or
785 * START_ELEMENT event. Unmarshalling will be done from this
786 * start event to the corresponding end event. If this method
787 * returns successfully, the <tt>reader</tt> will be pointing at
788 * the token right after the end event.
789 *
790 * @param reader
791 * The parser to be read.
792 * @param declaredType
793 * appropriate JAXB mapped class to hold <tt>reader</tt>'s START_ELEMENT XML data.
794 *
795 * @return content tree rooted by <a href="#unmarshalDeclaredTypeReturn">JAXB Element representation</a>
796 *
797 * @throws JAXBException
798 * If any unexpected errors occur while unmarshalling
799 * @throws UnmarshalException
800 * If the {@link ValidationEventHandler ValidationEventHandler}
801 * returns false from its <tt>handleEvent</tt> method or the
802 * <tt>Unmarshaller</tt> is unable to perform the XML to Java
803 * binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>
804 * @throws IllegalArgumentException
805 * If any parameter is null
806 * @since JAXB2.0
807 */
808 public <T> JAXBElement<T> unmarshal( javax.xml.stream.XMLEventReader reader, Class<T> declaredType ) throws JAXBException;
809
810 /**
811 * Get an unmarshaller handler object that can be used as a component in
812 * an XML pipeline.
813 *
814 * <p>
815 * The JAXB Provider can return the same handler object for multiple
816 * invocations of this method. In other words, this method does not
817 * necessarily create a new instance of <tt>UnmarshallerHandler</tt>. If the
818 * application needs to use more than one <tt>UnmarshallerHandler</tt>, it
819 * should create more than one <tt>Unmarshaller</tt>.
820 *
821 * @return the unmarshaller handler object
822 * @see UnmarshallerHandler
823 */
824 public UnmarshallerHandler getUnmarshallerHandler();
825
826 /**
827 * Specifies whether or not the default validation mechanism of the
828 * <tt>Unmarshaller</tt> should validate during unmarshal operations.
829 * By default, the <tt>Unmarshaller</tt> does not validate.
830 * <p>
831 * This method may only be invoked before or after calling one of the
832 * unmarshal methods.
833 * <p>
834 * This method only controls the JAXB Provider's default unmarshal-time
835 * validation mechanism - it has no impact on clients that specify their
836 * own validating SAX 2.0 compliant parser. Clients that specify their
837 * own unmarshal-time validation mechanism may wish to turn off the JAXB
838 * Provider's default validation mechanism via this API to avoid "double
839 * validation".
840 * <p>
841 * This method is deprecated as of JAXB 2.0 - please use the new
842 * {@link #setSchema(javax.xml.validation.Schema)} API.
843 *
844 * @param validating true if the Unmarshaller should validate during
845 * unmarshal, false otherwise
846 * @throws JAXBException if an error occurred while enabling or disabling
847 * validation at unmarshal time
848 * @throws UnsupportedOperationException could be thrown if this method is
849 * invoked on an Unmarshaller created from a JAXBContext referencing
850 * JAXB 2.0 mapped classes
851 * @deprecated since JAXB2.0, please see {@link #setSchema(javax.xml.validation.Schema)}
852 */
853 public void setValidating( boolean validating )
854 throws JAXBException;
855
856 /**
857 * Indicates whether or not the <tt>Unmarshaller</tt> is configured to
858 * validate during unmarshal operations.
859 * <p>
860 * This API returns the state of the JAXB Provider's default unmarshal-time
861 * validation mechanism.
862 * <p>
863 * This method is deprecated as of JAXB 2.0 - please use the new
864 * {@link #getSchema()} API.
865 *
866 * @return true if the Unmarshaller is configured to validate during
867 * unmarshal operations, false otherwise
868 * @throws JAXBException if an error occurs while retrieving the validating
869 * flag
870 * @throws UnsupportedOperationException could be thrown if this method is
871 * invoked on an Unmarshaller created from a JAXBContext referencing
872 * JAXB 2.0 mapped classes
873 * @deprecated since JAXB2.0, please see {@link #getSchema()}
874 */
875 public boolean isValidating()
876 throws JAXBException;
877
878 /**
879 * Allow an application to register a <tt>ValidationEventHandler</tt>.
880 * <p>
881 * The <tt>ValidationEventHandler</tt> will be called by the JAXB Provider
882 * if any validation errors are encountered during calls to any of the
883 * unmarshal methods. If the client application does not register a
884 * <tt>ValidationEventHandler</tt> before invoking the unmarshal methods,
885 * then <tt>ValidationEvents</tt> will be handled by the default event
886 * handler which will terminate the unmarshal operation after the first
887 * error or fatal error is encountered.
888 * <p>
889 * Calling this method with a null parameter will cause the Unmarshaller
890 * to revert back to the default event handler.
891 *
892 * @param handler the validation event handler
893 * @throws JAXBException if an error was encountered while setting the
894 * event handler
895 */
896 public void setEventHandler( ValidationEventHandler handler )
897 throws JAXBException;
898
899 /**
900 * Return the current event handler or the default event handler if one
901 * hasn't been set.
902 *
903 * @return the current ValidationEventHandler or the default event handler
904 * if it hasn't been set
905 * @throws JAXBException if an error was encountered while getting the
906 * current event handler
907 */
908 public ValidationEventHandler getEventHandler()
909 throws JAXBException;
910
911 /**
912 * Set the particular property in the underlying implementation of
913 * <tt>Unmarshaller</tt>. This method can only be used to set one of
914 * the standard JAXB defined properties above or a provider specific
915 * property. Attempting to set an undefined property will result in
916 * a PropertyException being thrown. See <a href="#supportedProps">
917 * Supported Properties</a>.
918 *
919 * @param name the name of the property to be set. This value can either
920 * be specified using one of the constant fields or a user
921 * supplied string.
922 * @param value the value of the property to be set
923 *
924 * @throws PropertyException when there is an error processing the given
925 * property or value
926 * @throws IllegalArgumentException
927 * If the name parameter is null
928 */
929 public void setProperty( String name, Object value )
930 throws PropertyException;
931
932 /**
933 * Get the particular property in the underlying implementation of
934 * <tt>Unmarshaller</tt>. This method can only be used to get one of
935 * the standard JAXB defined properties above or a provider specific
936 * property. Attempting to get an undefined property will result in
937 * a PropertyException being thrown. See <a href="#supportedProps">
938 * Supported Properties</a>.
939 *
940 * @param name the name of the property to retrieve
941 * @return the value of the requested property
942 *
943 * @throws PropertyException
944 * when there is an error retrieving the given property or value
945 * property name
946 * @throws IllegalArgumentException
947 * If the name parameter is null
948 */
949 public Object getProperty( String name ) throws PropertyException;
950
951 /**
952 * Specify the JAXP 1.3 {@link javax.xml.validation.Schema Schema}
953 * object that should be used to validate subsequent unmarshal operations
954 * against. Passing null into this method will disable validation.
955 * <p>
956 * This method replaces the deprecated {@link #setValidating(boolean) setValidating(boolean)}
957 * API.
958 *
959 * <p>
960 * Initially this property is set to <tt>null</tt>.
961 *
962 * @param schema Schema object to validate unmarshal operations against or null to disable validation
963 * @throws UnsupportedOperationException could be thrown if this method is
964 * invoked on an Unmarshaller created from a JAXBContext referencing
965 * JAXB 1.0 mapped classes
966 * @since JAXB2.0
967 */
968 public void setSchema( javax.xml.validation.Schema schema );
969
970 /**
971 * Get the JAXP 1.3 {@link javax.xml.validation.Schema Schema} object
972 * being used to perform unmarshal-time validation. If there is no
973 * Schema set on the unmarshaller, then this method will return null
974 * indicating that unmarshal-time validation will not be performed.
975 * <p>
976 * This method provides replacement functionality for the deprecated
977 * {@link #isValidating()} API as well as access to the Schema object.
978 * To determine if the Unmarshaller has validation enabled, simply
979 * test the return type for null:
980 * <p>
981 * <code>
982 * boolean isValidating = u.getSchema()!=null;
983 * </code>
984 *
985 * @return the Schema object being used to perform unmarshal-time
986 * validation or null if not present
987 * @throws UnsupportedOperationException could be thrown if this method is
988 * invoked on an Unmarshaller created from a JAXBContext referencing
989 * JAXB 1.0 mapped classes
990 * @since JAXB2.0
991 */
992 public javax.xml.validation.Schema getSchema();
993
994 /**
995 * Associates a configured instance of {@link XmlAdapter} with this unmarshaller.
996 *
997 * <p>
998 * This is a convenience method that invokes <code>setAdapter(adapter.getClass(),adapter);</code>.
999 *
1000 * @see #setAdapter(Class,XmlAdapter)
1001 * @throws IllegalArgumentException
1002 * if the adapter parameter is null.
1003 * @throws UnsupportedOperationException
1004 * if invoked agains a JAXB 1.0 implementation.
1005 * @since JAXB2.0
1006 */
1007 public void setAdapter( XmlAdapter adapter );
1008
1009 /**
1010 * Associates a configured instance of {@link XmlAdapter} with this unmarshaller.
1011 *
1012 * <p>
1013 * Every unmarshaller internally maintains a
1014 * {@link java.util.Map}<{@link Class},{@link XmlAdapter}>,
1015 * which it uses for unmarshalling classes whose fields/methods are annotated
1016 * with {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}.
1017 *
1018 * <p>
1019 * This method allows applications to use a configured instance of {@link XmlAdapter}.
1020 * When an instance of an adapter is not given, an unmarshaller will create
1021 * one by invoking its default constructor.
1022 *
1023 * @param type
1024 * The type of the adapter. The specified instance will be used when
1025 * {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter#value()}
1026 * refers to this type.
1027 * @param adapter
1028 * The instance of the adapter to be used. If null, it will un-register
1029 * the current adapter set for this type.
1030 * @throws IllegalArgumentException
1031 * if the type parameter is null.
1032 * @throws UnsupportedOperationException
1033 * if invoked agains a JAXB 1.0 implementation.
1034 * @since JAXB2.0
1035 */
1036 public <A extends XmlAdapter> void setAdapter( Class<A> type, A adapter );
1037
1038 /**
1039 * Gets the adapter associated with the specified type.
1040 *
1041 * This is the reverse operation of the {@link #setAdapter} method.
1042 *
1043 * @throws IllegalArgumentException
1044 * if the type parameter is null.
1045 * @throws UnsupportedOperationException
1046 * if invoked agains a JAXB 1.0 implementation.
1047 * @since JAXB2.0
1048 */
1049 public <A extends XmlAdapter> A getAdapter( Class<A> type );
1050
1051 /**
1052 * <p>Associate a context that resolves cid's, content-id URIs, to
1053 * binary data passed as attachments.</p>
1054 * <p/>
1055 * <p>Unmarshal time validation, enabled via {@link #setSchema(Schema)},
1056 * must be supported even when unmarshaller is performing XOP processing.
1057 * </p>
1058 *
1059 * @throws IllegalStateException if attempt to concurrently call this
1060 * method during a unmarshal operation.
1061 */
1062 void setAttachmentUnmarshaller(AttachmentUnmarshaller au);
1063
1064 AttachmentUnmarshaller getAttachmentUnmarshaller();
1065
1066 /**
1067 * <p/>
1068 * Register an instance of an implementation of this class with {@link Unmarshaller} to externally listen
1069 * for unmarshal events.
1070 * <p/>
1071 * <p/>
1072 * This class enables pre and post processing of an instance of a JAXB mapped class
1073 * as XML data is unmarshalled into it. The event callbacks are called when unmarshalling
1074 * XML content into a JAXBElement instance or a JAXB mapped class that represents a complex type definition.
1075 * The event callbacks are not called when unmarshalling to an instance of a
1076 * Java datatype that represents a simple type definition.
1077 * <p/>
1078 * <p/>
1079 * External listener is one of two different mechanisms for defining unmarshal event callbacks.
1080 * See <a href="Unmarshaller.html#unmarshalEventCallback">Unmarshal Event Callbacks</a> for an overview.
1081 * <p/>
1082 * (@link #setListener(Listener)}
1083 * (@link #getListener()}
1084 *
1085 * @since JAXB2.0
1086 */
1087 public static abstract class Listener {
1088 /**
1089 * <p/>
1090 * Callback method invoked before unmarshalling into <tt>target</tt>.
1091 * <p/>
1092 * <p/>
1093 * This method is invoked immediately after <tt>target</tt> was created and
1094 * before the unmarshalling of this object begins. Note that
1095 * if the class of <tt>target</tt> defines its own <tt>beforeUnmarshal</tt> method,
1096 * the class specific callback method is invoked before this method is invoked.
1097 *
1098 * @param target non-null instance of JAXB mapped class prior to unmarshalling into it.
1099 * @param parent instance of JAXB mapped class that will eventually reference <tt>target</tt>.
1100 * <tt>null</tt> when <tt>target</tt> is root element.
1101 */
1102 public void beforeUnmarshal(Object target, Object parent) {
1103 }
1104
1105 /**
1106 * <p/>
1107 * Callback method invoked after unmarshalling XML data into <tt>target</tt>.
1108 * <p/>
1109 * <p/>
1110 * This method is invoked after all the properties (except IDREF) are unmarshalled into <tt>target</tt>,
1111 * but before <tt>target</tt> is set into its <tt>parent</tt> object.
1112 * Note that if the class of <tt>target</tt> defines its own <tt>afterUnmarshal</tt> method,
1113 * the class specific callback method is invoked before this method is invoked.
1114 *
1115 * @param target non-null instance of JAXB mapped class prior to unmarshalling into it.
1116 * @param parent instance of JAXB mapped class that will reference <tt>target</tt>.
1117 * <tt>null</tt> when <tt>target</tt> is root element.
1118 */
1119 public void afterUnmarshal(Object target, Object parent) {
1120 }
1121 }
1122
1123 /**
1124 * <p>
1125 * Register unmarshal event callback {@link Listener} with this {@link Unmarshaller}.
1126 *
1127 * <p>
1128 * There is only one Listener per Unmarshaller. Setting a Listener replaces the previous set Listener.
1129 * One can unregister current Listener by setting listener to <tt>null</tt>.
1130 *
1131 * @param listener provides unmarshal event callbacks for this {@link Unmarshaller}
1132 * @since JAXB2.0
1133 */
1134 public void setListener(Listener listener);
1135
1136 /**
1137 * <p>Return {@link Listener} registered with this {@link Unmarshaller}.
1138 *
1139 * @return registered {@link Listener} or <code>null</code> if no Listener is registered with this Unmarshaller.
1140 * @since JAXB2.0
1141 */
1142 public Listener getListener();
1143 }