Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaxws_classes/javax/xml/bind/Unmarshaller.java
38890 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.bind;2627import javax.xml.bind.annotation.adapters.XmlAdapter;28import javax.xml.bind.attachment.AttachmentUnmarshaller;29import javax.xml.validation.Schema;30import java.io.Reader;3132/**33* The <tt>Unmarshaller</tt> class governs the process of deserializing XML34* data into newly created Java content trees, optionally validating the XML35* data as it is unmarshalled. It provides an overloading of unmarshal methods36* for many different input kinds.37*38* <p>39* Unmarshalling from a File:40* <blockquote>41* <pre>42* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );43* Unmarshaller u = jc.createUnmarshaller();44* Object o = u.unmarshal( new File( "nosferatu.xml" ) );45* </pre>46* </blockquote>47*48*49* <p>50* Unmarshalling from an InputStream:51* <blockquote>52* <pre>53* InputStream is = new FileInputStream( "nosferatu.xml" );54* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );55* Unmarshaller u = jc.createUnmarshaller();56* Object o = u.unmarshal( is );57* </pre>58* </blockquote>59*60* <p>61* Unmarshalling from a URL:62* <blockquote>63* <pre>64* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );65* Unmarshaller u = jc.createUnmarshaller();66* URL url = new URL( "http://beaker.east/nosferatu.xml" );67* Object o = u.unmarshal( url );68* </pre>69* </blockquote>70*71* <p>72* Unmarshalling from a StringBuffer using a73* <tt>javax.xml.transform.stream.StreamSource</tt>:74* <blockquote>75* <pre>76* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );77* Unmarshaller u = jc.createUnmarshaller();78* StringBuffer xmlStr = new StringBuffer( "<?xml version="1.0"?>..." );79* Object o = u.unmarshal( new StreamSource( new StringReader( xmlStr.toString() ) ) );80* </pre>81* </blockquote>82*83* <p>84* Unmarshalling from a <tt>org.w3c.dom.Node</tt>:85* <blockquote>86* <pre>87* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );88* Unmarshaller u = jc.createUnmarshaller();89*90* DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();91* dbf.setNamespaceAware(true);92* DocumentBuilder db = dbf.newDocumentBuilder();93* Document doc = db.parse(new File( "nosferatu.xml"));9495* Object o = u.unmarshal( doc );96* </pre>97* </blockquote>98*99* <p>100* Unmarshalling from a <tt>javax.xml.transform.sax.SAXSource</tt> using a101* client specified validating SAX2.0 parser:102* <blockquote>103* <pre>104* // configure a validating SAX2.0 parser (Xerces2)105* static final String JAXP_SCHEMA_LANGUAGE =106* "http://java.sun.com/xml/jaxp/properties/schemaLanguage";107* static final String JAXP_SCHEMA_LOCATION =108* "http://java.sun.com/xml/jaxp/properties/schemaSource";109* static final String W3C_XML_SCHEMA =110* "http://www.w3.org/2001/XMLSchema";111*112* System.setProperty( "javax.xml.parsers.SAXParserFactory",113* "org.apache.xerces.jaxp.SAXParserFactoryImpl" );114*115* SAXParserFactory spf = SAXParserFactory.newInstance();116* spf.setNamespaceAware(true);117* spf.setValidating(true);118* SAXParser saxParser = spf.newSAXParser();119*120* try {121* saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);122* saxParser.setProperty(JAXP_SCHEMA_LOCATION, "http://....");123* } catch (SAXNotRecognizedException x) {124* // exception handling omitted125* }126*127* XMLReader xmlReader = saxParser.getXMLReader();128* SAXSource source =129* new SAXSource( xmlReader, new InputSource( "http://..." ) );130*131* // Setup JAXB to unmarshal132* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );133* Unmarshaller u = jc.createUnmarshaller();134* ValidationEventCollector vec = new ValidationEventCollector();135* u.setEventHandler( vec );136*137* // turn off the JAXB provider's default validation mechanism to138* // avoid duplicate validation139* u.setValidating( false )140*141* // unmarshal142* Object o = u.unmarshal( source );143*144* // check for events145* if( vec.hasEvents() ) {146* // iterate over events147* }148* </pre>149* </blockquote>150*151* <p>152* Unmarshalling from a StAX XMLStreamReader:153* <blockquote>154* <pre>155* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );156* Unmarshaller u = jc.createUnmarshaller();157*158* javax.xml.stream.XMLStreamReader xmlStreamReader =159* javax.xml.stream.XMLInputFactory().newInstance().createXMLStreamReader( ... );160*161* Object o = u.unmarshal( xmlStreamReader );162* </pre>163* </blockquote>164*165* <p>166* Unmarshalling from a StAX XMLEventReader:167* <blockquote>168* <pre>169* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );170* Unmarshaller u = jc.createUnmarshaller();171*172* javax.xml.stream.XMLEventReader xmlEventReader =173* javax.xml.stream.XMLInputFactory().newInstance().createXMLEventReader( ... );174*175* Object o = u.unmarshal( xmlEventReader );176* </pre>177* </blockquote>178*179* <p>180* <a name="unmarshalEx"></a>181* <b>Unmarshalling XML Data</b><br>182* <blockquote>183* Unmarshalling can deserialize XML data that represents either an entire XML document184* or a subtree of an XML document. Typically, it is sufficient to use the185* unmarshalling methods described by186* <a href="#unmarshalGlobal">Unmarshal root element that is declared globally</a>.187* These unmarshal methods utilize {@link JAXBContext}'s mapping of global XML element188* declarations and type definitions to JAXB mapped classes to initiate the189* unmarshalling of the root element of XML data. When the {@link JAXBContext}'s190* mappings are not sufficient to unmarshal the root element of XML data,191* the application can assist the unmarshalling process by using the192* <a href="#unmarshalByDeclaredType">unmarshal by declaredType methods</a>.193* These methods are useful for unmarshalling XML data where194* the root element corresponds to a local element declaration in the schema.195* </blockquote>196*197* <blockquote>198* An unmarshal method never returns null. If the unmarshal process is unable to unmarshal199* the root of XML content to a JAXB mapped object, a fatal error is reported that200* terminates processing by throwing JAXBException.201* </blockquote>202*203* <p>204* <a name="unmarshalGlobal"></a>205* <b>Unmarshal a root element that is globally declared</b><br>206* <blockquote>207* The unmarshal methods that do not have an <tt>declaredType</tt> parameter use208* {@link JAXBContext} to unmarshal the root element of an XML data. The {@link JAXBContext}209* instance is the one that was used to create this <tt>Unmarshaller</tt>. The {@link JAXBContext}210* instance maintains a mapping of globally declared XML element and type definition names to211* JAXB mapped classes. The unmarshal method checks if {@link JAXBContext} has a mapping212* from the root element's XML name and/or <tt>@xsi:type</tt> to a JAXB mapped class. If it does, it umarshalls the213* XML data using the appropriate JAXB mapped class. Note that when the root element name is unknown and the root214* element has an <tt>@xsi:type</tt>, the XML data is unmarshalled215* using that JAXB mapped class as the value of a {@link JAXBElement}.216* When the {@link JAXBContext} object does not have a mapping for the root element's name217* nor its <tt>@xsi:type</tt>, if it exists,218* then the unmarshal operation will abort immediately by throwing a {@link UnmarshalException219* UnmarshalException}. This exception scenario can be worked around by using the unmarshal by220* declaredType methods described in the next subsection.221* </blockquote>222*223* <p>224* <a name="unmarshalByDeclaredType"></a>225* <b>Unmarshal by Declared Type</b><br>226* <blockquote>227* The unmarshal methods with a <code>declaredType</code> parameter enable an228* application to deserialize a root element of XML data, even when229* there is no mapping in {@link JAXBContext} of the root element's XML name.230* The unmarshaller unmarshals the root element using the application provided231* mapping specified as the <tt>declaredType</tt> parameter.232* Note that even when the root element's element name is mapped by {@link JAXBContext},233* the <code>declaredType</code> parameter overrides that mapping for234* deserializing the root element when using these unmarshal methods.235* Additionally, when the root element of XML data has an <tt>xsi:type</tt> attribute and236* that attribute's value references a type definition that is mapped237* to a JAXB mapped class by {@link JAXBContext}, that the root238* element's <tt>xsi:type</tt> attribute takes239* precedence over the unmarshal methods <tt>declaredType</tt> parameter.240* These methods always return a <tt>JAXBElement<declaredType></tt>241* instance. The table below shows how the properties of the returned JAXBElement242* instance are set.243*244* <a name="unmarshalDeclaredTypeReturn"></a>245* <p>246* <table border="2" rules="all" cellpadding="4">247* <thead>248* <tr>249* <th align="center" colspan="2">250* Unmarshal By Declared Type returned JAXBElement251* </tr>252* <tr>253* <th>JAXBElement Property</th>254* <th>Value</th>255* </tr>256* <tr>257* <td>name</td>258* <td><code>xml element name</code></td>259* </tr>260* </thead>261* <tbody>262* <tr>263* <td>value</td>264* <td><code>instanceof declaredType</code></td>265* </tr>266* <tr>267* <td>declaredType</td>268* <td>unmarshal method <code>declaredType</code> parameter</td>269* </tr>270* <tr>271* <td>scope</td>272* <td><code>null</code> <i>(actual scope is unknown)</i></td>273* </tr>274* </tbody>275* </table>276* </blockquote>277*278* <p>279* The following is an example of280* <a href="#unmarshalByDeclaredType">unmarshal by declaredType method</a>.281* <p>282* Unmarshal by declaredType from a <tt>org.w3c.dom.Node</tt>:283* <blockquote>284* <pre>285* Schema fragment for example286* <xs:schema>287* <xs:complexType name="FooType">...<\xs:complexType>288* <!-- global element declaration "PurchaseOrder" -->289* <xs:element name="PurchaseOrder">290* <xs:complexType>291* <xs:sequence>292* <!-- local element declaration "foo" -->293* <xs:element name="foo" type="FooType"/>294* ...295* </xs:sequence>296* </xs:complexType>297* </xs:element>298* </xs:schema>299*300* JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );301* Unmarshaller u = jc.createUnmarshaller();302*303* DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();304* dbf.setNamespaceAware(true);305* DocumentBuilder db = dbf.newDocumentBuilder();306* Document doc = db.parse(new File( "nosferatu.xml"));307* Element fooSubtree = ...; // traverse DOM till reach xml element foo, constrained by a308* // local element declaration in schema.309*310* // FooType is the JAXB mapping of the type of local element declaration foo.311* JAXBElement<FooType> foo = u.unmarshal( fooSubtree, FooType.class);312* </pre>313* </blockquote>314*315* <p>316* <b>Support for SAX2.0 Compliant Parsers</b><br>317* <blockquote>318* A client application has the ability to select the SAX2.0 compliant parser319* of their choice. If a SAX parser is not selected, then the JAXB Provider's320* default parser will be used. Even though the JAXB Provider's default parser321* is not required to be SAX2.0 compliant, all providers are required to allow322* a client application to specify their own SAX2.0 parser. Some providers may323* require the client application to specify the SAX2.0 parser at schema compile324* time. See {@link #unmarshal(javax.xml.transform.Source) unmarshal(Source)}325* for more detail.326* </blockquote>327*328* <p>329* <b>Validation and Well-Formedness</b><br>330* <blockquote>331* <p>332* A client application can enable or disable JAXP 1.3 validation333* mechanism via the <tt>setSchema(javax.xml.validation.Schema)</tt> API.334* Sophisticated clients can specify their own validating SAX 2.0 compliant335* parser and bypass the JAXP 1.3 validation mechanism using the336* {@link #unmarshal(javax.xml.transform.Source) unmarshal(Source)} API.337*338* <p>339* Since unmarshalling invalid XML content is defined in JAXB 2.0,340* the Unmarshaller default validation event handler was made more lenient341* than in JAXB 1.0. When schema-derived code generated342* by JAXB 1.0 binding compiler is registered with {@link JAXBContext},343* the default unmarshal validation handler is344* {@link javax.xml.bind.helpers.DefaultValidationEventHandler} and it345* terminates the marshal operation after encountering either a fatal error or an error.346* For a JAXB 2.0 client application, there is no explicitly defined default347* validation handler and the default event handling only348* terminates the unmarshal operation after encountering a fatal error.349*350* </blockquote>351*352* <p>353* <a name="supportedProps"></a>354* <b>Supported Properties</b><br>355* <blockquote>356* <p>357* There currently are not any properties required to be supported by all358* JAXB Providers on Unmarshaller. However, some providers may support359* their own set of provider specific properties.360* </blockquote>361*362* <p>363* <a name="unmarshalEventCallback"></a>364* <b>Unmarshal Event Callbacks</b><br>365* <blockquote>366* The {@link Unmarshaller} provides two styles of callback mechanisms367* that allow application specific processing during key points in the368* unmarshalling process. In 'class defined' event callbacks, application369* specific code placed in JAXB mapped classes is triggered during370* unmarshalling. 'External listeners' allow for centralized processing371* of unmarshal events in one callback method rather than by type event callbacks.372* <p>373* 'Class defined' event callback methods allow any JAXB mapped class to specify374* its own specific callback methods by defining methods with the following method signature:375* <blockquote>376* <pre>377* // This method is called immediately after the object is created and before the unmarshalling of this378* // object begins. The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.379* void beforeUnmarshal(Unmarshaller, Object parent);380*381* //This method is called after all the properties (except IDREF) are unmarshalled for this object,382* //but before this object is set to the parent object.383* void afterUnmarshal(Unmarshaller, Object parent);384* </pre>385* </blockquote>386* The class defined callback methods should be used when the callback method requires387* access to non-public methods and/or fields of the class.388* <p>389* The external listener callback mechanism enables the registration of a {@link Listener}390* instance with an {@link Unmarshaller#setListener(Listener)}. The external listener receives all callback events,391* allowing for more centralized processing than per class defined callback methods. The external listener392* receives events when unmarshalling proces is marshalling to a JAXB element or to JAXB mapped class.393* <p>394* The 'class defined' and external listener event callback methods are independent of each other,395* both can be called for one event. The invocation ordering when both listener callback methods exist is396* defined in {@link Listener#beforeUnmarshal(Object, Object)} and {@link Listener#afterUnmarshal(Object, Object)}.397* <p>398* An event callback method throwing an exception terminates the current unmarshal process.399*400* </blockquote>401*402* @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>403* @see JAXBContext404* @see Marshaller405* @see Validator406* @since JAXB1.0407*/408public interface Unmarshaller {409410/**411* Unmarshal XML data from the specified file and return the resulting412* content tree.413*414* <p>415* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.416*417* @param f the file to unmarshal XML data from418* @return the newly created root object of the java content tree419*420* @throws JAXBException421* If any unexpected errors occur while unmarshalling422* @throws UnmarshalException423* If the {@link ValidationEventHandler ValidationEventHandler}424* returns false from its <tt>handleEvent</tt> method or the425* <tt>Unmarshaller</tt> is unable to perform the XML to Java426* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>427* @throws IllegalArgumentException428* If the file parameter is null429*/430public Object unmarshal( java.io.File f ) throws JAXBException;431432/**433* Unmarshal XML data from the specified InputStream and return the434* resulting content tree. Validation event location information may435* be incomplete when using this form of the unmarshal API.436*437* <p>438* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.439*440* @param is the InputStream to unmarshal XML data from441* @return the newly created root object of the java content tree442*443* @throws JAXBException444* If any unexpected errors occur while unmarshalling445* @throws UnmarshalException446* If the {@link ValidationEventHandler ValidationEventHandler}447* returns false from its <tt>handleEvent</tt> method or the448* <tt>Unmarshaller</tt> is unable to perform the XML to Java449* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>450* @throws IllegalArgumentException451* If the InputStream parameter is null452*/453public Object unmarshal( java.io.InputStream is ) throws JAXBException;454455/**456* Unmarshal XML data from the specified Reader and return the457* resulting content tree. Validation event location information may458* be incomplete when using this form of the unmarshal API,459* because a Reader does not provide the system ID.460*461* <p>462* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.463*464* @param reader the Reader to unmarshal XML data from465* @return the newly created root object of the java content tree466*467* @throws JAXBException468* If any unexpected errors occur while unmarshalling469* @throws UnmarshalException470* If the {@link ValidationEventHandler ValidationEventHandler}471* returns false from its <tt>handleEvent</tt> method or the472* <tt>Unmarshaller</tt> is unable to perform the XML to Java473* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>474* @throws IllegalArgumentException475* If the InputStream parameter is null476* @since JAXB2.0477*/478public Object unmarshal( Reader reader ) throws JAXBException;479480/**481* Unmarshal XML data from the specified URL and return the resulting482* content tree.483*484* <p>485* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.486*487* @param url the url to unmarshal XML data from488* @return the newly created root object of the java content tree489*490* @throws JAXBException491* If any unexpected errors occur while unmarshalling492* @throws UnmarshalException493* If the {@link ValidationEventHandler ValidationEventHandler}494* returns false from its <tt>handleEvent</tt> method or the495* <tt>Unmarshaller</tt> is unable to perform the XML to Java496* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>497* @throws IllegalArgumentException498* If the URL parameter is null499*/500public Object unmarshal( java.net.URL url ) throws JAXBException;501502/**503* Unmarshal XML data from the specified SAX InputSource and return the504* resulting content tree.505*506* <p>507* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.508*509* @param source the input source to unmarshal XML data from510* @return the newly created root object of the java content tree511*512* @throws JAXBException513* If any unexpected errors occur while unmarshalling514* @throws UnmarshalException515* If the {@link ValidationEventHandler ValidationEventHandler}516* returns false from its <tt>handleEvent</tt> method or the517* <tt>Unmarshaller</tt> is unable to perform the XML to Java518* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>519* @throws IllegalArgumentException520* If the InputSource parameter is null521*/522public Object unmarshal( org.xml.sax.InputSource source ) throws JAXBException;523524/**525* Unmarshal global XML data from the specified DOM tree and return the resulting526* content tree.527*528* <p>529* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.530*531* @param node532* the document/element to unmarshal XML data from.533* The caller must support at least Document and Element.534* @return the newly created root object of the java content tree535*536* @throws JAXBException537* If any unexpected errors occur while unmarshalling538* @throws UnmarshalException539* If the {@link ValidationEventHandler ValidationEventHandler}540* returns false from its <tt>handleEvent</tt> method or the541* <tt>Unmarshaller</tt> is unable to perform the XML to Java542* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>543* @throws IllegalArgumentException544* If the Node parameter is null545* @see #unmarshal(org.w3c.dom.Node, Class)546*/547public Object unmarshal( org.w3c.dom.Node node ) throws JAXBException;548549/**550* Unmarshal XML data by JAXB mapped <tt>declaredType</tt>551* and return the resulting content tree.552*553* <p>554* Implements <a href="#unmarshalByDeclaredType">Unmarshal by Declared Type</a>555*556* @param node557* the document/element to unmarshal XML data from.558* The caller must support at least Document and Element.559* @param declaredType560* appropriate JAXB mapped class to hold <tt>node</tt>'s XML data.561*562* @return <a href="#unmarshalDeclaredTypeReturn">JAXB Element</a> representation of <tt>node</tt>563*564* @throws JAXBException565* If any unexpected errors occur while unmarshalling566* @throws UnmarshalException567* If the {@link ValidationEventHandler ValidationEventHandler}568* returns false from its <tt>handleEvent</tt> method or the569* <tt>Unmarshaller</tt> is unable to perform the XML to Java570* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>571* @throws IllegalArgumentException572* If any parameter is null573* @since JAXB2.0574*/575public <T> JAXBElement<T> unmarshal( org.w3c.dom.Node node, Class<T> declaredType ) throws JAXBException;576577/**578* Unmarshal XML data from the specified XML Source and return the579* resulting content tree.580*581* <p>582* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.583*584* <p>585* <a name="saxParserPlugable"></a>586* <b>SAX 2.0 Parser Pluggability</b>587* <p>588* A client application can choose not to use the default parser mechanism589* supplied with their JAXB provider. Any SAX 2.0 compliant parser can be590* substituted for the JAXB provider's default mechanism. To do so, the591* client application must properly configure a <tt>SAXSource</tt> containing592* an <tt>XMLReader</tt> implemented by the SAX 2.0 parser provider. If the593* <tt>XMLReader</tt> has an <tt>org.xml.sax.ErrorHandler</tt> registered594* on it, it will be replaced by the JAXB Provider so that validation errors595* can be reported via the <tt>ValidationEventHandler</tt> mechanism of596* JAXB. If the <tt>SAXSource</tt> does not contain an <tt>XMLReader</tt>,597* then the JAXB provider's default parser mechanism will be used.598* <p>599* This parser replacement mechanism can also be used to replace the JAXB600* provider's unmarshal-time validation engine. The client application601* must properly configure their SAX 2.0 compliant parser to perform602* validation (as shown in the example above). Any <tt>SAXParserExceptions603* </tt> encountered by the parser during the unmarshal operation will be604* processed by the JAXB provider and converted into JAXB605* <tt>ValidationEvent</tt> objects which will be reported back to the606* client via the <tt>ValidationEventHandler</tt> registered with the607* <tt>Unmarshaller</tt>. <i>Note:</i> specifying a substitute validating608* SAX 2.0 parser for unmarshalling does not necessarily replace the609* validation engine used by the JAXB provider for performing on-demand610* validation.611* <p>612* The only way for a client application to specify an alternate parser613* mechanism to be used during unmarshal is via the614* <tt>unmarshal(SAXSource)</tt> API. All other forms of the unmarshal615* method (File, URL, Node, etc) will use the JAXB provider's default616* parser and validator mechanisms.617*618* @param source the XML Source to unmarshal XML data from (providers are619* only required to support SAXSource, DOMSource, and StreamSource)620* @return the newly created root object of the java content tree621*622* @throws JAXBException623* If any unexpected errors occur while unmarshalling624* @throws UnmarshalException625* If the {@link ValidationEventHandler ValidationEventHandler}626* returns false from its <tt>handleEvent</tt> method or the627* <tt>Unmarshaller</tt> is unable to perform the XML to Java628* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>629* @throws IllegalArgumentException630* If the Source parameter is null631* @see #unmarshal(javax.xml.transform.Source, Class)632*/633public Object unmarshal( javax.xml.transform.Source source )634throws JAXBException;635636637/**638* Unmarshal XML data from the specified XML Source by <tt>declaredType</tt> and return the639* resulting content tree.640*641* <p>642* Implements <a href="#unmarshalByDeclaredType">Unmarshal by Declared Type</a>643*644* <p>645* See <a href="#saxParserPlugable">SAX 2.0 Parser Pluggability</a>646*647* @param source the XML Source to unmarshal XML data from (providers are648* only required to support SAXSource, DOMSource, and StreamSource)649* @param declaredType650* appropriate JAXB mapped class to hold <tt>source</tt>'s xml root element651* @return Java content rooted by <a href="#unmarshalDeclaredTypeReturn">JAXB Element</a>652*653* @throws JAXBException654* If any unexpected errors occur while unmarshalling655* @throws UnmarshalException656* If the {@link ValidationEventHandler ValidationEventHandler}657* returns false from its <tt>handleEvent</tt> method or the658* <tt>Unmarshaller</tt> is unable to perform the XML to Java659* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>660* @throws IllegalArgumentException661* If any parameter is null662* @since JAXB2.0663*/664public <T> JAXBElement<T> unmarshal( javax.xml.transform.Source source, Class<T> declaredType )665throws JAXBException;666667/**668* Unmarshal XML data from the specified pull parser and return the669* resulting content tree.670*671* <p>672* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.673*674* <p>675* This method assumes that the parser is on a START_DOCUMENT or676* START_ELEMENT event. Unmarshalling will be done from this677* start event to the corresponding end event. If this method678* returns successfully, the <tt>reader</tt> will be pointing at679* the token right after the end event.680*681* @param reader682* The parser to be read.683* @return684* the newly created root object of the java content tree.685*686* @throws JAXBException687* If any unexpected errors occur while unmarshalling688* @throws UnmarshalException689* If the {@link ValidationEventHandler ValidationEventHandler}690* returns false from its <tt>handleEvent</tt> method or the691* <tt>Unmarshaller</tt> is unable to perform the XML to Java692* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>693* @throws IllegalArgumentException694* If the <tt>reader</tt> parameter is null695* @throws IllegalStateException696* If <tt>reader</tt> is not pointing to a START_DOCUMENT or697* START_ELEMENT event.698* @since JAXB2.0699* @see #unmarshal(javax.xml.stream.XMLStreamReader, Class)700*/701public Object unmarshal( javax.xml.stream.XMLStreamReader reader )702throws JAXBException;703704/**705* Unmarshal root element to JAXB mapped <tt>declaredType</tt>706* and return the resulting content tree.707*708* <p>709* This method implements <a href="#unmarshalByDeclaredType">unmarshal by declaredType</a>.710* <p>711* This method assumes that the parser is on a START_DOCUMENT or712* START_ELEMENT event. Unmarshalling will be done from this713* start event to the corresponding end event. If this method714* returns successfully, the <tt>reader</tt> will be pointing at715* the token right after the end event.716*717* @param reader718* The parser to be read.719* @param declaredType720* appropriate JAXB mapped class to hold <tt>reader</tt>'s START_ELEMENT XML data.721*722* @return content tree rooted by <a href="#unmarshalDeclaredTypeReturn">JAXB Element representation</a>723*724* @throws JAXBException725* If any unexpected errors occur while unmarshalling726* @throws UnmarshalException727* If the {@link ValidationEventHandler ValidationEventHandler}728* returns false from its <tt>handleEvent</tt> method or the729* <tt>Unmarshaller</tt> is unable to perform the XML to Java730* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>731* @throws IllegalArgumentException732* If any parameter is null733* @since JAXB2.0734*/735public <T> JAXBElement<T> unmarshal( javax.xml.stream.XMLStreamReader reader, Class<T> declaredType ) throws JAXBException;736737/**738* Unmarshal XML data from the specified pull parser and return the739* resulting content tree.740*741* <p>742* This method is an <a href="#unmarshalGlobal">Unmarshal Global Root method</a>.743*744* <p>745* This method assumes that the parser is on a START_DOCUMENT or746* START_ELEMENT event. Unmarshalling will be done from this747* start event to the corresponding end event. If this method748* returns successfully, the <tt>reader</tt> will be pointing at749* the token right after the end event.750*751* @param reader752* The parser to be read.753* @return754* the newly created root object of the java content tree.755*756* @throws JAXBException757* If any unexpected errors occur while unmarshalling758* @throws UnmarshalException759* If the {@link ValidationEventHandler ValidationEventHandler}760* returns false from its <tt>handleEvent</tt> method or the761* <tt>Unmarshaller</tt> is unable to perform the XML to Java762* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>763* @throws IllegalArgumentException764* If the <tt>reader</tt> parameter is null765* @throws IllegalStateException766* If <tt>reader</tt> is not pointing to a START_DOCUMENT or767* START_ELEMENT event.768* @since JAXB2.0769* @see #unmarshal(javax.xml.stream.XMLEventReader, Class)770*/771public Object unmarshal( javax.xml.stream.XMLEventReader reader )772throws JAXBException;773774/**775* Unmarshal root element to JAXB mapped <tt>declaredType</tt>776* and return the resulting content tree.777*778* <p>779* This method implements <a href="#unmarshalByDeclaredType">unmarshal by declaredType</a>.780*781* <p>782* This method assumes that the parser is on a START_DOCUMENT or783* START_ELEMENT event. Unmarshalling will be done from this784* start event to the corresponding end event. If this method785* returns successfully, the <tt>reader</tt> will be pointing at786* the token right after the end event.787*788* @param reader789* The parser to be read.790* @param declaredType791* appropriate JAXB mapped class to hold <tt>reader</tt>'s START_ELEMENT XML data.792*793* @return content tree rooted by <a href="#unmarshalDeclaredTypeReturn">JAXB Element representation</a>794*795* @throws JAXBException796* If any unexpected errors occur while unmarshalling797* @throws UnmarshalException798* If the {@link ValidationEventHandler ValidationEventHandler}799* returns false from its <tt>handleEvent</tt> method or the800* <tt>Unmarshaller</tt> is unable to perform the XML to Java801* binding. See <a href="#unmarshalEx">Unmarshalling XML Data</a>802* @throws IllegalArgumentException803* If any parameter is null804* @since JAXB2.0805*/806public <T> JAXBElement<T> unmarshal( javax.xml.stream.XMLEventReader reader, Class<T> declaredType ) throws JAXBException;807808/**809* Get an unmarshaller handler object that can be used as a component in810* an XML pipeline.811*812* <p>813* The JAXB Provider can return the same handler object for multiple814* invocations of this method. In other words, this method does not815* necessarily create a new instance of <tt>UnmarshallerHandler</tt>. If the816* application needs to use more than one <tt>UnmarshallerHandler</tt>, it817* should create more than one <tt>Unmarshaller</tt>.818*819* @return the unmarshaller handler object820* @see UnmarshallerHandler821*/822public UnmarshallerHandler getUnmarshallerHandler();823824/**825* Specifies whether or not the default validation mechanism of the826* <tt>Unmarshaller</tt> should validate during unmarshal operations.827* By default, the <tt>Unmarshaller</tt> does not validate.828* <p>829* This method may only be invoked before or after calling one of the830* unmarshal methods.831* <p>832* This method only controls the JAXB Provider's default unmarshal-time833* validation mechanism - it has no impact on clients that specify their834* own validating SAX 2.0 compliant parser. Clients that specify their835* own unmarshal-time validation mechanism may wish to turn off the JAXB836* Provider's default validation mechanism via this API to avoid "double837* validation".838* <p>839* This method is deprecated as of JAXB 2.0 - please use the new840* {@link #setSchema(javax.xml.validation.Schema)} API.841*842* @param validating true if the Unmarshaller should validate during843* unmarshal, false otherwise844* @throws JAXBException if an error occurred while enabling or disabling845* validation at unmarshal time846* @throws UnsupportedOperationException could be thrown if this method is847* invoked on an Unmarshaller created from a JAXBContext referencing848* JAXB 2.0 mapped classes849* @deprecated since JAXB2.0, please see {@link #setSchema(javax.xml.validation.Schema)}850*/851public void setValidating( boolean validating )852throws JAXBException;853854/**855* Indicates whether or not the <tt>Unmarshaller</tt> is configured to856* validate during unmarshal operations.857* <p>858* This API returns the state of the JAXB Provider's default unmarshal-time859* validation mechanism.860* <p>861* This method is deprecated as of JAXB 2.0 - please use the new862* {@link #getSchema()} API.863*864* @return true if the Unmarshaller is configured to validate during865* unmarshal operations, false otherwise866* @throws JAXBException if an error occurs while retrieving the validating867* flag868* @throws UnsupportedOperationException could be thrown if this method is869* invoked on an Unmarshaller created from a JAXBContext referencing870* JAXB 2.0 mapped classes871* @deprecated since JAXB2.0, please see {@link #getSchema()}872*/873public boolean isValidating()874throws JAXBException;875876/**877* Allow an application to register a <tt>ValidationEventHandler</tt>.878* <p>879* The <tt>ValidationEventHandler</tt> will be called by the JAXB Provider880* if any validation errors are encountered during calls to any of the881* unmarshal methods. If the client application does not register a882* <tt>ValidationEventHandler</tt> before invoking the unmarshal methods,883* then <tt>ValidationEvents</tt> will be handled by the default event884* handler which will terminate the unmarshal operation after the first885* error or fatal error is encountered.886* <p>887* Calling this method with a null parameter will cause the Unmarshaller888* to revert back to the default event handler.889*890* @param handler the validation event handler891* @throws JAXBException if an error was encountered while setting the892* event handler893*/894public void setEventHandler( ValidationEventHandler handler )895throws JAXBException;896897/**898* Return the current event handler or the default event handler if one899* hasn't been set.900*901* @return the current ValidationEventHandler or the default event handler902* if it hasn't been set903* @throws JAXBException if an error was encountered while getting the904* current event handler905*/906public ValidationEventHandler getEventHandler()907throws JAXBException;908909/**910* Set the particular property in the underlying implementation of911* <tt>Unmarshaller</tt>. This method can only be used to set one of912* the standard JAXB defined properties above or a provider specific913* property. Attempting to set an undefined property will result in914* a PropertyException being thrown. See <a href="#supportedProps">915* Supported Properties</a>.916*917* @param name the name of the property to be set. This value can either918* be specified using one of the constant fields or a user919* supplied string.920* @param value the value of the property to be set921*922* @throws PropertyException when there is an error processing the given923* property or value924* @throws IllegalArgumentException925* If the name parameter is null926*/927public void setProperty( String name, Object value )928throws PropertyException;929930/**931* Get the particular property in the underlying implementation of932* <tt>Unmarshaller</tt>. This method can only be used to get one of933* the standard JAXB defined properties above or a provider specific934* property. Attempting to get an undefined property will result in935* a PropertyException being thrown. See <a href="#supportedProps">936* Supported Properties</a>.937*938* @param name the name of the property to retrieve939* @return the value of the requested property940*941* @throws PropertyException942* when there is an error retrieving the given property or value943* property name944* @throws IllegalArgumentException945* If the name parameter is null946*/947public Object getProperty( String name ) throws PropertyException;948949/**950* Specify the JAXP 1.3 {@link javax.xml.validation.Schema Schema}951* object that should be used to validate subsequent unmarshal operations952* against. Passing null into this method will disable validation.953* <p>954* This method replaces the deprecated {@link #setValidating(boolean) setValidating(boolean)}955* API.956*957* <p>958* Initially this property is set to <tt>null</tt>.959*960* @param schema Schema object to validate unmarshal operations against or null to disable validation961* @throws UnsupportedOperationException could be thrown if this method is962* invoked on an Unmarshaller created from a JAXBContext referencing963* JAXB 1.0 mapped classes964* @since JAXB2.0965*/966public void setSchema( javax.xml.validation.Schema schema );967968/**969* Get the JAXP 1.3 {@link javax.xml.validation.Schema Schema} object970* being used to perform unmarshal-time validation. If there is no971* Schema set on the unmarshaller, then this method will return null972* indicating that unmarshal-time validation will not be performed.973* <p>974* This method provides replacement functionality for the deprecated975* {@link #isValidating()} API as well as access to the Schema object.976* To determine if the Unmarshaller has validation enabled, simply977* test the return type for null:978* <p>979* <code>980* boolean isValidating = u.getSchema()!=null;981* </code>982*983* @return the Schema object being used to perform unmarshal-time984* validation or null if not present985* @throws UnsupportedOperationException could be thrown if this method is986* invoked on an Unmarshaller created from a JAXBContext referencing987* JAXB 1.0 mapped classes988* @since JAXB2.0989*/990public javax.xml.validation.Schema getSchema();991992/**993* Associates a configured instance of {@link XmlAdapter} with this unmarshaller.994*995* <p>996* This is a convenience method that invokes <code>setAdapter(adapter.getClass(),adapter);</code>.997*998* @see #setAdapter(Class,XmlAdapter)999* @throws IllegalArgumentException1000* if the adapter parameter is null.1001* @throws UnsupportedOperationException1002* if invoked agains a JAXB 1.0 implementation.1003* @since JAXB2.01004*/1005public void setAdapter( XmlAdapter adapter );10061007/**1008* Associates a configured instance of {@link XmlAdapter} with this unmarshaller.1009*1010* <p>1011* Every unmarshaller internally maintains a1012* {@link java.util.Map}<{@link Class},{@link XmlAdapter}>,1013* which it uses for unmarshalling classes whose fields/methods are annotated1014* with {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}.1015*1016* <p>1017* This method allows applications to use a configured instance of {@link XmlAdapter}.1018* When an instance of an adapter is not given, an unmarshaller will create1019* one by invoking its default constructor.1020*1021* @param type1022* The type of the adapter. The specified instance will be used when1023* {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter#value()}1024* refers to this type.1025* @param adapter1026* The instance of the adapter to be used. If null, it will un-register1027* the current adapter set for this type.1028* @throws IllegalArgumentException1029* if the type parameter is null.1030* @throws UnsupportedOperationException1031* if invoked agains a JAXB 1.0 implementation.1032* @since JAXB2.01033*/1034public <A extends XmlAdapter> void setAdapter( Class<A> type, A adapter );10351036/**1037* Gets the adapter associated with the specified type.1038*1039* This is the reverse operation of the {@link #setAdapter} method.1040*1041* @throws IllegalArgumentException1042* if the type parameter is null.1043* @throws UnsupportedOperationException1044* if invoked agains a JAXB 1.0 implementation.1045* @since JAXB2.01046*/1047public <A extends XmlAdapter> A getAdapter( Class<A> type );10481049/**1050* <p>Associate a context that resolves cid's, content-id URIs, to1051* binary data passed as attachments.</p>1052* <p/>1053* <p>Unmarshal time validation, enabled via {@link #setSchema(Schema)},1054* must be supported even when unmarshaller is performing XOP processing.1055* </p>1056*1057* @throws IllegalStateException if attempt to concurrently call this1058* method during a unmarshal operation.1059*/1060void setAttachmentUnmarshaller(AttachmentUnmarshaller au);10611062AttachmentUnmarshaller getAttachmentUnmarshaller();10631064/**1065* <p/>1066* Register an instance of an implementation of this class with {@link Unmarshaller} to externally listen1067* for unmarshal events.1068* <p/>1069* <p/>1070* This class enables pre and post processing of an instance of a JAXB mapped class1071* as XML data is unmarshalled into it. The event callbacks are called when unmarshalling1072* XML content into a JAXBElement instance or a JAXB mapped class that represents a complex type definition.1073* The event callbacks are not called when unmarshalling to an instance of a1074* Java datatype that represents a simple type definition.1075* <p/>1076* <p/>1077* External listener is one of two different mechanisms for defining unmarshal event callbacks.1078* See <a href="Unmarshaller.html#unmarshalEventCallback">Unmarshal Event Callbacks</a> for an overview.1079* <p/>1080* (@link #setListener(Listener)}1081* (@link #getListener()}1082*1083* @since JAXB2.01084*/1085public static abstract class Listener {1086/**1087* <p/>1088* Callback method invoked before unmarshalling into <tt>target</tt>.1089* <p/>1090* <p/>1091* This method is invoked immediately after <tt>target</tt> was created and1092* before the unmarshalling of this object begins. Note that1093* if the class of <tt>target</tt> defines its own <tt>beforeUnmarshal</tt> method,1094* the class specific callback method is invoked before this method is invoked.1095*1096* @param target non-null instance of JAXB mapped class prior to unmarshalling into it.1097* @param parent instance of JAXB mapped class that will eventually reference <tt>target</tt>.1098* <tt>null</tt> when <tt>target</tt> is root element.1099*/1100public void beforeUnmarshal(Object target, Object parent) {1101}11021103/**1104* <p/>1105* Callback method invoked after unmarshalling XML data into <tt>target</tt>.1106* <p/>1107* <p/>1108* This method is invoked after all the properties (except IDREF) are unmarshalled into <tt>target</tt>,1109* but before <tt>target</tt> is set into its <tt>parent</tt> object.1110* Note that if the class of <tt>target</tt> defines its own <tt>afterUnmarshal</tt> method,1111* the class specific callback method is invoked before this method is invoked.1112*1113* @param target non-null instance of JAXB mapped class prior to unmarshalling into it.1114* @param parent instance of JAXB mapped class that will reference <tt>target</tt>.1115* <tt>null</tt> when <tt>target</tt> is root element.1116*/1117public void afterUnmarshal(Object target, Object parent) {1118}1119}11201121/**1122* <p>1123* Register unmarshal event callback {@link Listener} with this {@link Unmarshaller}.1124*1125* <p>1126* There is only one Listener per Unmarshaller. Setting a Listener replaces the previous set Listener.1127* One can unregister current Listener by setting listener to <tt>null</tt>.1128*1129* @param listener provides unmarshal event callbacks for this {@link Unmarshaller}1130* @since JAXB2.01131*/1132public void setListener(Listener listener);11331134/**1135* <p>Return {@link Listener} registered with this {@link Unmarshaller}.1136*1137* @return registered {@link Listener} or <code>null</code> if no Listener is registered with this Unmarshaller.1138* @since JAXB2.01139*/1140public Listener getListener();1141}114211431144