ohair@286: /* mkos@397: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package javax.xml.bind; ohair@286: ohair@286: import javax.xml.bind.annotation.adapters.XmlAdapter; ohair@286: import javax.xml.bind.attachment.AttachmentUnmarshaller; ohair@286: import javax.xml.validation.Schema; ohair@286: import java.io.Reader; ohair@286: ohair@286: /** ohair@286: * The Unmarshaller class governs the process of deserializing XML ohair@286: * data into newly created Java content trees, optionally validating the XML ohair@286: * data as it is unmarshalled. It provides an overloading of unmarshal methods ohair@286: * for many different input kinds. ohair@286: * ohair@286: *
ohair@286: * Unmarshalling from a File: ohair@286: *
ohair@286: *ohair@286: * ohair@286: * ohair@286: *ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * Object o = u.unmarshal( new File( "nosferatu.xml" ) ); ohair@286: *ohair@286: *
ohair@286: * Unmarshalling from an InputStream: ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * InputStream is = new FileInputStream( "nosferatu.xml" ); ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * Object o = u.unmarshal( is ); ohair@286: *ohair@286: *
ohair@286: * Unmarshalling from a URL: ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * URL url = new URL( "http://beaker.east/nosferatu.xml" ); ohair@286: * Object o = u.unmarshal( url ); ohair@286: *ohair@286: *
ohair@286: * Unmarshalling from a StringBuffer using a ohair@286: * javax.xml.transform.stream.StreamSource: ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * StringBuffer xmlStr = new StringBuffer( "<?xml version="1.0"?>..." ); ohair@286: * Object o = u.unmarshal( new StreamSource( new StringReader( xmlStr.toString() ) ) ); ohair@286: *ohair@286: *
ohair@286: * Unmarshalling from a org.w3c.dom.Node: ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * ohair@286: * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); ohair@286: * dbf.setNamespaceAware(true); ohair@286: * DocumentBuilder db = dbf.newDocumentBuilder(); ohair@286: * Document doc = db.parse(new File( "nosferatu.xml")); ohair@286: ohair@286: * Object o = u.unmarshal( doc ); ohair@286: *ohair@286: *
ohair@286: * Unmarshalling from a javax.xml.transform.sax.SAXSource using a ohair@286: * client specified validating SAX2.0 parser: ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * // configure a validating SAX2.0 parser (Xerces2) ohair@286: * static final String JAXP_SCHEMA_LANGUAGE = ohair@286: * "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; ohair@286: * static final String JAXP_SCHEMA_LOCATION = ohair@286: * "http://java.sun.com/xml/jaxp/properties/schemaSource"; ohair@286: * static final String W3C_XML_SCHEMA = ohair@286: * "http://www.w3.org/2001/XMLSchema"; ohair@286: * ohair@286: * System.setProperty( "javax.xml.parsers.SAXParserFactory", ohair@286: * "org.apache.xerces.jaxp.SAXParserFactoryImpl" ); ohair@286: * ohair@286: * SAXParserFactory spf = SAXParserFactory.newInstance(); ohair@286: * spf.setNamespaceAware(true); ohair@286: * spf.setValidating(true); ohair@286: * SAXParser saxParser = spf.newSAXParser(); ohair@286: * ohair@286: * try { ohair@286: * saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); ohair@286: * saxParser.setProperty(JAXP_SCHEMA_LOCATION, "http://...."); ohair@286: * } catch (SAXNotRecognizedException x) { ohair@286: * // exception handling omitted ohair@286: * } ohair@286: * ohair@286: * XMLReader xmlReader = saxParser.getXMLReader(); ohair@286: * SAXSource source = ohair@286: * new SAXSource( xmlReader, new InputSource( "http://..." ) ); ohair@286: * ohair@286: * // Setup JAXB to unmarshal ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * ValidationEventCollector vec = new ValidationEventCollector(); ohair@286: * u.setEventHandler( vec ); ohair@286: * ohair@286: * // turn off the JAXB provider's default validation mechanism to ohair@286: * // avoid duplicate validation ohair@286: * u.setValidating( false ) ohair@286: * ohair@286: * // unmarshal ohair@286: * Object o = u.unmarshal( source ); ohair@286: * ohair@286: * // check for events ohair@286: * if( vec.hasEvents() ) { ohair@286: * // iterate over events ohair@286: * } ohair@286: *ohair@286: *
ohair@286: * Unmarshalling from a StAX XMLStreamReader: ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * ohair@286: * javax.xml.stream.XMLStreamReader xmlStreamReader = ohair@286: * javax.xml.stream.XMLInputFactory().newInstance().createXMLStreamReader( ... ); ohair@286: * ohair@286: * Object o = u.unmarshal( xmlStreamReader ); ohair@286: *ohair@286: *
ohair@286: * Unmarshalling from a StAX XMLEventReader: ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * ohair@286: * javax.xml.stream.XMLEventReader xmlEventReader = ohair@286: * javax.xml.stream.XMLInputFactory().newInstance().createXMLEventReader( ... ); ohair@286: * ohair@286: * Object o = u.unmarshal( xmlEventReader ); ohair@286: *ohair@286: *
ohair@286: *
ohair@286: * Unmarshalling XML Data
ohair@286: *
ohair@286: * Unmarshalling can deserialize XML data that represents either an entire XML document ohair@286: * or a subtree of an XML document. Typically, it is sufficient to use the ohair@286: * unmarshalling methods described by ohair@286: * Unmarshal root element that is declared globally. ohair@286: * These unmarshal methods utilize {@link JAXBContext}'s mapping of global XML element ohair@286: * declarations and type definitions to JAXB mapped classes to initiate the ohair@286: * unmarshalling of the root element of XML data. When the {@link JAXBContext}'s ohair@286: * mappings are not sufficient to unmarshal the root element of XML data, ohair@286: * the application can assist the unmarshalling process by using the ohair@286: * unmarshal by declaredType methods. ohair@286: * These methods are useful for unmarshalling XML data where ohair@286: * the root element corresponds to a local element declaration in the schema. ohair@286: *ohair@286: * ohair@286: *
ohair@286: * An unmarshal method never returns null. If the unmarshal process is unable to unmarshal ohair@286: * the root of XML content to a JAXB mapped object, a fatal error is reported that ohair@286: * terminates processing by throwing JAXBException. ohair@286: *ohair@286: * ohair@286: *
ohair@286: *
ohair@286: * Unmarshal a root element that is globally declared
ohair@286: *
ohair@286: * The unmarshal methods that do not have an declaredType parameter use ohair@286: * {@link JAXBContext} to unmarshal the root element of an XML data. The {@link JAXBContext} ohair@286: * instance is the one that was used to create this Unmarshaller. The {@link JAXBContext} ohair@286: * instance maintains a mapping of globally declared XML element and type definition names to ohair@286: * JAXB mapped classes. The unmarshal method checks if {@link JAXBContext} has a mapping ohair@286: * from the root element's XML name and/or @xsi:type to a JAXB mapped class. If it does, it umarshalls the ohair@286: * XML data using the appropriate JAXB mapped class. Note that when the root element name is unknown and the root ohair@286: * element has an @xsi:type, the XML data is unmarshalled ohair@286: * using that JAXB mapped class as the value of a {@link JAXBElement}. ohair@286: * When the {@link JAXBContext} object does not have a mapping for the root element's name ohair@286: * nor its @xsi:type, if it exists, ohair@286: * then the unmarshal operation will abort immediately by throwing a {@link UnmarshalException ohair@286: * UnmarshalException}. This exception scenario can be worked around by using the unmarshal by ohair@286: * declaredType methods described in the next subsection. ohair@286: *ohair@286: * ohair@286: *
ohair@286: *
ohair@286: * Unmarshal by Declared Type
ohair@286: *
ohair@286: * The unmarshal methods with aohair@286: * ohair@286: *declaredType
parameter enable an ohair@286: * application to deserialize a root element of XML data, even when ohair@286: * there is no mapping in {@link JAXBContext} of the root element's XML name. ohair@286: * The unmarshaller unmarshals the root element using the application provided ohair@286: * mapping specified as the declaredType parameter. ohair@286: * Note that even when the root element's element name is mapped by {@link JAXBContext}, ohair@286: * thedeclaredType
parameter overrides that mapping for ohair@286: * deserializing the root element when using these unmarshal methods. ohair@286: * Additionally, when the root element of XML data has an xsi:type attribute and ohair@286: * that attribute's value references a type definition that is mapped ohair@286: * to a JAXB mapped class by {@link JAXBContext}, that the root ohair@286: * element's xsi:type attribute takes ohair@286: * precedence over the unmarshal methods declaredType parameter. ohair@286: * These methods always return a JAXBElement<declaredType> ohair@286: * instance. The table below shows how the properties of the returned JAXBElement ohair@286: * instance are set. ohair@286: * ohair@286: * ohair@286: *ohair@286: *
ohair@286: * ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * Unmarshal By Declared Type returned JAXBElement ohair@286: * ohair@286: * ohair@286: *JAXBElement Property ohair@286: *Value ohair@286: *ohair@286: * ohair@286: * ohair@286: * ohair@286: *name ohair@286: *ohair@286: * xml element name
ohair@286: * ohair@286: *value ohair@286: *ohair@286: * instanceof declaredType
ohair@286: * ohair@286: *declaredType ohair@286: *unmarshal method ohair@286: *declaredType
parameterohair@286: * ohair@286: * ohair@286: *scope ohair@286: *ohair@286: * null
(actual scope is unknown)
ohair@286: * The following is an example of ohair@286: * unmarshal by declaredType method. ohair@286: *
ohair@286: * Unmarshal by declaredType from a org.w3c.dom.Node: ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * Schema fragment for example ohair@286: * <xs:schema> ohair@286: * <xs:complexType name="FooType">...<\xs:complexType> ohair@286: * <!-- global element declaration "PurchaseOrder" --> ohair@286: * <xs:element name="PurchaseOrder"> ohair@286: * <xs:complexType> ohair@286: * <xs:sequence> ohair@286: * <!-- local element declaration "foo" --> ohair@286: * <xs:element name="foo" type="FooType"/> ohair@286: * ... ohair@286: * </xs:sequence> ohair@286: * </xs:complexType> ohair@286: * </xs:element> ohair@286: * </xs:schema> ohair@286: * ohair@286: * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); ohair@286: * Unmarshaller u = jc.createUnmarshaller(); ohair@286: * ohair@286: * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); ohair@286: * dbf.setNamespaceAware(true); ohair@286: * DocumentBuilder db = dbf.newDocumentBuilder(); ohair@286: * Document doc = db.parse(new File( "nosferatu.xml")); ohair@286: * Element fooSubtree = ...; // traverse DOM till reach xml element foo, constrained by a ohair@286: * // local element declaration in schema. ohair@286: * ohair@286: * // FooType is the JAXB mapping of the type of local element declaration foo. ohair@286: * JAXBElement<FooType> foo = u.unmarshal( fooSubtree, FooType.class); ohair@286: *ohair@286: *
ohair@286: * Support for SAX2.0 Compliant Parsers
ohair@286: *
ohair@286: * A client application has the ability to select the SAX2.0 compliant parser ohair@286: * of their choice. If a SAX parser is not selected, then the JAXB Provider's ohair@286: * default parser will be used. Even though the JAXB Provider's default parser ohair@286: * is not required to be SAX2.0 compliant, all providers are required to allow ohair@286: * a client application to specify their own SAX2.0 parser. Some providers may ohair@286: * require the client application to specify the SAX2.0 parser at schema compile ohair@286: * time. See {@link #unmarshal(javax.xml.transform.Source) unmarshal(Source)} ohair@286: * for more detail. ohair@286: *ohair@286: * ohair@286: *
ohair@286: * Validation and Well-Formedness
ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * A client application can enable or disable JAXP 1.3 validation ohair@286: * mechanism via the setSchema(javax.xml.validation.Schema) API. ohair@286: * Sophisticated clients can specify their own validating SAX 2.0 compliant ohair@286: * parser and bypass the JAXP 1.3 validation mechanism using the ohair@286: * {@link #unmarshal(javax.xml.transform.Source) unmarshal(Source)} API. ohair@286: * ohair@286: *
ohair@286: * Since unmarshalling invalid XML content is defined in JAXB 2.0, ohair@286: * the Unmarshaller default validation event handler was made more lenient ohair@286: * than in JAXB 1.0. When schema-derived code generated ohair@286: * by JAXB 1.0 binding compiler is registered with {@link JAXBContext}, ohair@286: * the default unmarshal validation handler is ohair@286: * {@link javax.xml.bind.helpers.DefaultValidationEventHandler} and it ohair@286: * terminates the marshal operation after encountering either a fatal error or an error. ohair@286: * For a JAXB 2.0 client application, there is no explicitly defined default ohair@286: * validation handler and the default event handling only alanb@368: * terminates the unmarshal operation after encountering a fatal error. ohair@286: * ohair@286: *
ohair@286: *
ohair@286: * Supported Properties
ohair@286: *
ohair@286: *ohair@286: * ohair@286: *ohair@286: * There currently are not any properties required to be supported by all ohair@286: * JAXB Providers on Unmarshaller. However, some providers may support ohair@286: * their own set of provider specific properties. ohair@286: *
ohair@286: *
ohair@286: * Unmarshal Event Callbacks
ohair@286: *
ohair@286: * The {@link Unmarshaller} provides two styles of callback mechanisms ohair@286: * that allow application specific processing during key points in the ohair@286: * unmarshalling process. In 'class defined' event callbacks, application ohair@286: * specific code placed in JAXB mapped classes is triggered during ohair@286: * unmarshalling. 'External listeners' allow for centralized processing ohair@286: * of unmarshal events in one callback method rather than by type event callbacks. ohair@286: *ohair@286: * ohair@286: * @authorohair@286: * 'Class defined' event callback methods allow any JAXB mapped class to specify ohair@286: * its own specific callback methods by defining methods with the following method signature: ohair@286: *
ohair@286: *ohair@286: * The class defined callback methods should be used when the callback method requires ohair@286: * access to non-public methods and/or fields of the class. ohair@286: *ohair@286: * // This method is called immediately after the object is created and before the unmarshalling of this ohair@286: * // object begins. The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling. ohair@286: * void beforeUnmarshal(Unmarshaller, Object parent); ohair@286: * ohair@286: * //This method is called after all the properties (except IDREF) are unmarshalled for this object, ohair@286: * //but before this object is set to the parent object. ohair@286: * void afterUnmarshal(Unmarshaller, Object parent); ohair@286: *ohair@286: *ohair@286: * The external listener callback mechanism enables the registration of a {@link Listener} ohair@286: * instance with an {@link Unmarshaller#setListener(Listener)}. The external listener receives all callback events, ohair@286: * allowing for more centralized processing than per class defined callback methods. The external listener ohair@286: * receives events when unmarshalling proces is marshalling to a JAXB element or to JAXB mapped class. ohair@286: *
ohair@286: * The 'class defined' and external listener event callback methods are independent of each other, ohair@286: * both can be called for one event. The invocation ordering when both listener callback methods exist is ohair@286: * defined in {@link Listener#beforeUnmarshal(Object, Object)} and {@link Listener#afterUnmarshal(Object, Object)}. ohair@286: *
ohair@286: * An event callback method throwing an exception terminates the current unmarshal process. ohair@286: * ohair@286: *
ohair@286: * Implements Unmarshal Global Root Element. ohair@286: * ohair@286: * @param f the file to unmarshal XML data from ohair@286: * @return the newly created root object of the java content tree ohair@286: * ohair@286: * @throws JAXBException ohair@286: * If any unexpected errors occur while unmarshalling ohair@286: * @throws UnmarshalException ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler} ohair@286: * returns false from its handleEvent method or the ohair@286: * Unmarshaller is unable to perform the XML to Java ohair@286: * binding. See Unmarshalling XML Data ohair@286: * @throws IllegalArgumentException ohair@286: * If the file parameter is null ohair@286: */ ohair@286: public Object unmarshal( java.io.File f ) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshal XML data from the specified InputStream and return the ohair@286: * resulting content tree. Validation event location information may ohair@286: * be incomplete when using this form of the unmarshal API. ohair@286: * ohair@286: *
ohair@286: * Implements Unmarshal Global Root Element. ohair@286: * ohair@286: * @param is the InputStream to unmarshal XML data from ohair@286: * @return the newly created root object of the java content tree ohair@286: * ohair@286: * @throws JAXBException ohair@286: * If any unexpected errors occur while unmarshalling ohair@286: * @throws UnmarshalException ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler} ohair@286: * returns false from its handleEvent method or the ohair@286: * Unmarshaller is unable to perform the XML to Java ohair@286: * binding. See Unmarshalling XML Data ohair@286: * @throws IllegalArgumentException ohair@286: * If the InputStream parameter is null ohair@286: */ ohair@286: public Object unmarshal( java.io.InputStream is ) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshal XML data from the specified Reader and return the ohair@286: * resulting content tree. Validation event location information may ohair@286: * be incomplete when using this form of the unmarshal API, ohair@286: * because a Reader does not provide the system ID. ohair@286: * ohair@286: *
ohair@286: * Implements Unmarshal Global Root Element. ohair@286: * ohair@286: * @param reader the Reader to unmarshal XML data from ohair@286: * @return the newly created root object of the java content tree ohair@286: * ohair@286: * @throws JAXBException ohair@286: * If any unexpected errors occur while unmarshalling ohair@286: * @throws UnmarshalException ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler} ohair@286: * returns false from its handleEvent method or the ohair@286: * Unmarshaller is unable to perform the XML to Java ohair@286: * binding. See Unmarshalling XML Data ohair@286: * @throws IllegalArgumentException ohair@286: * If the InputStream parameter is null ohair@286: * @since JAXB2.0 ohair@286: */ ohair@286: public Object unmarshal( Reader reader ) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshal XML data from the specified URL and return the resulting ohair@286: * content tree. ohair@286: * ohair@286: *
ohair@286: * Implements Unmarshal Global Root Element. ohair@286: * ohair@286: * @param url the url to unmarshal XML data from ohair@286: * @return the newly created root object of the java content tree ohair@286: * ohair@286: * @throws JAXBException ohair@286: * If any unexpected errors occur while unmarshalling ohair@286: * @throws UnmarshalException ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler} ohair@286: * returns false from its handleEvent method or the ohair@286: * Unmarshaller is unable to perform the XML to Java ohair@286: * binding. See Unmarshalling XML Data ohair@286: * @throws IllegalArgumentException ohair@286: * If the URL parameter is null ohair@286: */ ohair@286: public Object unmarshal( java.net.URL url ) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshal XML data from the specified SAX InputSource and return the ohair@286: * resulting content tree. ohair@286: * ohair@286: *
ohair@286: * Implements Unmarshal Global Root Element. ohair@286: * ohair@286: * @param source the input source to unmarshal XML data from ohair@286: * @return the newly created root object of the java content tree ohair@286: * ohair@286: * @throws JAXBException ohair@286: * If any unexpected errors occur while unmarshalling ohair@286: * @throws UnmarshalException ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler} ohair@286: * returns false from its handleEvent method or the ohair@286: * Unmarshaller is unable to perform the XML to Java ohair@286: * binding. See Unmarshalling XML Data ohair@286: * @throws IllegalArgumentException ohair@286: * If the InputSource parameter is null ohair@286: */ ohair@286: public Object unmarshal( org.xml.sax.InputSource source ) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshal global XML data from the specified DOM tree and return the resulting ohair@286: * content tree. ohair@286: * ohair@286: *
ohair@286: * Implements Unmarshal Global Root Element. ohair@286: * ohair@286: * @param node ohair@286: * the document/element to unmarshal XML data from. ohair@286: * The caller must support at least Document and Element. ohair@286: * @return the newly created root object of the java content tree ohair@286: * ohair@286: * @throws JAXBException ohair@286: * If any unexpected errors occur while unmarshalling ohair@286: * @throws UnmarshalException ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler} ohair@286: * returns false from its handleEvent method or the ohair@286: * Unmarshaller is unable to perform the XML to Java ohair@286: * binding. See Unmarshalling XML Data ohair@286: * @throws IllegalArgumentException ohair@286: * If the Node parameter is null ohair@286: * @see #unmarshal(org.w3c.dom.Node, Class) ohair@286: */ ohair@286: public Object unmarshal( org.w3c.dom.Node node ) throws JAXBException; ohair@286: ohair@286: /** ohair@286: * Unmarshal XML data by JAXB mapped declaredType ohair@286: * and return the resulting content tree. ohair@286: * ohair@286: *
ohair@286: * Implements Unmarshal by Declared Type
ohair@286: *
ohair@286: * @param node
ohair@286: * the document/element to unmarshal XML data from.
ohair@286: * The caller must support at least Document and Element.
ohair@286: * @param declaredType
ohair@286: * appropriate JAXB mapped class to hold node's XML data.
ohair@286: *
ohair@286: * @return JAXB Element representation of node
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected errors occur while unmarshalling
ohair@286: * @throws UnmarshalException
ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286: * returns false from its handleEvent method or the
ohair@286: * Unmarshaller is unable to perform the XML to Java
ohair@286: * binding. See Unmarshalling XML Data
ohair@286: * @throws IllegalArgumentException
ohair@286: * If any parameter is null
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public
ohair@286: * Implements Unmarshal Global Root Element.
ohair@286: *
ohair@286: *
ohair@286: *
ohair@286: * SAX 2.0 Parser Pluggability
ohair@286: *
ohair@286: * A client application can choose not to use the default parser mechanism
ohair@286: * supplied with their JAXB provider. Any SAX 2.0 compliant parser can be
ohair@286: * substituted for the JAXB provider's default mechanism. To do so, the
ohair@286: * client application must properly configure a SAXSource containing
ohair@286: * an XMLReader implemented by the SAX 2.0 parser provider. If the
ohair@286: * XMLReader has an org.xml.sax.ErrorHandler registered
ohair@286: * on it, it will be replaced by the JAXB Provider so that validation errors
ohair@286: * can be reported via the ValidationEventHandler mechanism of
ohair@286: * JAXB. If the SAXSource does not contain an XMLReader,
ohair@286: * then the JAXB provider's default parser mechanism will be used.
ohair@286: *
ohair@286: * This parser replacement mechanism can also be used to replace the JAXB
ohair@286: * provider's unmarshal-time validation engine. The client application
ohair@286: * must properly configure their SAX 2.0 compliant parser to perform
ohair@286: * validation (as shown in the example above). Any SAXParserExceptions
ohair@286: * encountered by the parser during the unmarshal operation will be
ohair@286: * processed by the JAXB provider and converted into JAXB
ohair@286: * ValidationEvent objects which will be reported back to the
ohair@286: * client via the ValidationEventHandler registered with the
ohair@286: * Unmarshaller. Note: specifying a substitute validating
ohair@286: * SAX 2.0 parser for unmarshalling does not necessarily replace the
ohair@286: * validation engine used by the JAXB provider for performing on-demand
ohair@286: * validation.
ohair@286: *
ohair@286: * The only way for a client application to specify an alternate parser
ohair@286: * mechanism to be used during unmarshal is via the
ohair@286: * unmarshal(SAXSource) API. All other forms of the unmarshal
ohair@286: * method (File, URL, Node, etc) will use the JAXB provider's default
ohair@286: * parser and validator mechanisms.
ohair@286: *
ohair@286: * @param source the XML Source to unmarshal XML data from (providers are
ohair@286: * only required to support SAXSource, DOMSource, and StreamSource)
ohair@286: * @return the newly created root object of the java content tree
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected errors occur while unmarshalling
ohair@286: * @throws UnmarshalException
ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286: * returns false from its handleEvent method or the
ohair@286: * Unmarshaller is unable to perform the XML to Java
ohair@286: * binding. See Unmarshalling XML Data
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the Source parameter is null
ohair@286: * @see #unmarshal(javax.xml.transform.Source, Class)
ohair@286: */
ohair@286: public Object unmarshal( javax.xml.transform.Source source )
ohair@286: throws JAXBException;
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Unmarshal XML data from the specified XML Source by declaredType and return the
ohair@286: * resulting content tree.
ohair@286: *
ohair@286: *
ohair@286: * Implements Unmarshal by Declared Type
ohair@286: *
ohair@286: *
ohair@286: * See SAX 2.0 Parser Pluggability
ohair@286: *
ohair@286: * @param source the XML Source to unmarshal XML data from (providers are
ohair@286: * only required to support SAXSource, DOMSource, and StreamSource)
ohair@286: * @param declaredType
ohair@286: * appropriate JAXB mapped class to hold source's xml root element
ohair@286: * @return Java content rooted by JAXB Element
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected errors occur while unmarshalling
ohair@286: * @throws UnmarshalException
ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286: * returns false from its handleEvent method or the
ohair@286: * Unmarshaller is unable to perform the XML to Java
ohair@286: * binding. See Unmarshalling XML Data
ohair@286: * @throws IllegalArgumentException
ohair@286: * If any parameter is null
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public
ohair@286: * Implements Unmarshal Global Root Element.
ohair@286: *
ohair@286: *
ohair@286: * This method assumes that the parser is on a START_DOCUMENT or
ohair@286: * START_ELEMENT event. Unmarshalling will be done from this
ohair@286: * start event to the corresponding end event. If this method
ohair@286: * returns successfully, the reader will be pointing at
ohair@286: * the token right after the end event.
ohair@286: *
ohair@286: * @param reader
ohair@286: * The parser to be read.
ohair@286: * @return
ohair@286: * the newly created root object of the java content tree.
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected errors occur while unmarshalling
ohair@286: * @throws UnmarshalException
ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286: * returns false from its handleEvent method or the
ohair@286: * Unmarshaller is unable to perform the XML to Java
ohair@286: * binding. See Unmarshalling XML Data
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the reader parameter is null
ohair@286: * @throws IllegalStateException
ohair@286: * If reader is not pointing to a START_DOCUMENT or
ohair@286: * START_ELEMENT event.
ohair@286: * @since JAXB2.0
ohair@286: * @see #unmarshal(javax.xml.stream.XMLStreamReader, Class)
ohair@286: */
ohair@286: public Object unmarshal( javax.xml.stream.XMLStreamReader reader )
ohair@286: throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Unmarshal root element to JAXB mapped declaredType
ohair@286: * and return the resulting content tree.
ohair@286: *
ohair@286: *
ohair@286: * This method implements unmarshal by declaredType.
ohair@286: *
ohair@286: * This method assumes that the parser is on a START_DOCUMENT or
ohair@286: * START_ELEMENT event. Unmarshalling will be done from this
ohair@286: * start event to the corresponding end event. If this method
ohair@286: * returns successfully, the reader will be pointing at
ohair@286: * the token right after the end event.
ohair@286: *
ohair@286: * @param reader
ohair@286: * The parser to be read.
ohair@286: * @param declaredType
ohair@286: * appropriate JAXB mapped class to hold reader's START_ELEMENT XML data.
ohair@286: *
ohair@286: * @return content tree rooted by JAXB Element representation
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected errors occur while unmarshalling
ohair@286: * @throws UnmarshalException
ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286: * returns false from its handleEvent method or the
ohair@286: * Unmarshaller is unable to perform the XML to Java
ohair@286: * binding. See Unmarshalling XML Data
ohair@286: * @throws IllegalArgumentException
ohair@286: * If any parameter is null
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public
ohair@286: * This method is an Unmarshal Global Root method.
ohair@286: *
ohair@286: *
ohair@286: * This method assumes that the parser is on a START_DOCUMENT or
ohair@286: * START_ELEMENT event. Unmarshalling will be done from this
ohair@286: * start event to the corresponding end event. If this method
ohair@286: * returns successfully, the reader will be pointing at
ohair@286: * the token right after the end event.
ohair@286: *
ohair@286: * @param reader
ohair@286: * The parser to be read.
ohair@286: * @return
ohair@286: * the newly created root object of the java content tree.
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected errors occur while unmarshalling
ohair@286: * @throws UnmarshalException
ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286: * returns false from its handleEvent method or the
ohair@286: * Unmarshaller is unable to perform the XML to Java
ohair@286: * binding. See Unmarshalling XML Data
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the reader parameter is null
ohair@286: * @throws IllegalStateException
ohair@286: * If reader is not pointing to a START_DOCUMENT or
ohair@286: * START_ELEMENT event.
ohair@286: * @since JAXB2.0
ohair@286: * @see #unmarshal(javax.xml.stream.XMLEventReader, Class)
ohair@286: */
ohair@286: public Object unmarshal( javax.xml.stream.XMLEventReader reader )
ohair@286: throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Unmarshal root element to JAXB mapped declaredType
ohair@286: * and return the resulting content tree.
ohair@286: *
ohair@286: *
ohair@286: * This method implements unmarshal by declaredType.
ohair@286: *
ohair@286: *
ohair@286: * This method assumes that the parser is on a START_DOCUMENT or
ohair@286: * START_ELEMENT event. Unmarshalling will be done from this
ohair@286: * start event to the corresponding end event. If this method
ohair@286: * returns successfully, the reader will be pointing at
ohair@286: * the token right after the end event.
ohair@286: *
ohair@286: * @param reader
ohair@286: * The parser to be read.
ohair@286: * @param declaredType
ohair@286: * appropriate JAXB mapped class to hold reader's START_ELEMENT XML data.
ohair@286: *
ohair@286: * @return content tree rooted by JAXB Element representation
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected errors occur while unmarshalling
ohair@286: * @throws UnmarshalException
ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286: * returns false from its handleEvent method or the
ohair@286: * Unmarshaller is unable to perform the XML to Java
ohair@286: * binding. See Unmarshalling XML Data
ohair@286: * @throws IllegalArgumentException
ohair@286: * If any parameter is null
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public
ohair@286: * The JAXB Provider can return the same handler object for multiple
ohair@286: * invocations of this method. In other words, this method does not
ohair@286: * necessarily create a new instance of UnmarshallerHandler. If the
ohair@286: * application needs to use more than one UnmarshallerHandler, it
ohair@286: * should create more than one Unmarshaller.
ohair@286: *
ohair@286: * @return the unmarshaller handler object
ohair@286: * @see UnmarshallerHandler
ohair@286: */
ohair@286: public UnmarshallerHandler getUnmarshallerHandler();
ohair@286:
ohair@286: /**
ohair@286: * Specifies whether or not the default validation mechanism of the
ohair@286: * Unmarshaller should validate during unmarshal operations.
ohair@286: * By default, the Unmarshaller does not validate.
ohair@286: *
ohair@286: * This method may only be invoked before or after calling one of the
ohair@286: * unmarshal methods.
ohair@286: *
ohair@286: * This method only controls the JAXB Provider's default unmarshal-time
ohair@286: * validation mechanism - it has no impact on clients that specify their
ohair@286: * own validating SAX 2.0 compliant parser. Clients that specify their
ohair@286: * own unmarshal-time validation mechanism may wish to turn off the JAXB
ohair@286: * Provider's default validation mechanism via this API to avoid "double
ohair@286: * validation".
ohair@286: *
ohair@286: * This method is deprecated as of JAXB 2.0 - please use the new
ohair@286: * {@link #setSchema(javax.xml.validation.Schema)} API.
ohair@286: *
ohair@286: * @param validating true if the Unmarshaller should validate during
ohair@286: * unmarshal, false otherwise
ohair@286: * @throws JAXBException if an error occurred while enabling or disabling
ohair@286: * validation at unmarshal time
ohair@286: * @throws UnsupportedOperationException could be thrown if this method is
ohair@286: * invoked on an Unmarshaller created from a JAXBContext referencing
ohair@286: * JAXB 2.0 mapped classes
ohair@286: * @deprecated since JAXB2.0, please see {@link #setSchema(javax.xml.validation.Schema)}
ohair@286: */
ohair@286: public void setValidating( boolean validating )
ohair@286: throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Indicates whether or not the Unmarshaller is configured to
ohair@286: * validate during unmarshal operations.
ohair@286: *
ohair@286: * This API returns the state of the JAXB Provider's default unmarshal-time
ohair@286: * validation mechanism.
ohair@286: *
ohair@286: * This method is deprecated as of JAXB 2.0 - please use the new
ohair@286: * {@link #getSchema()} API.
ohair@286: *
ohair@286: * @return true if the Unmarshaller is configured to validate during
ohair@286: * unmarshal operations, false otherwise
ohair@286: * @throws JAXBException if an error occurs while retrieving the validating
ohair@286: * flag
ohair@286: * @throws UnsupportedOperationException could be thrown if this method is
ohair@286: * invoked on an Unmarshaller created from a JAXBContext referencing
ohair@286: * JAXB 2.0 mapped classes
ohair@286: * @deprecated since JAXB2.0, please see {@link #getSchema()}
ohair@286: */
ohair@286: public boolean isValidating()
ohair@286: throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Allow an application to register a ValidationEventHandler.
ohair@286: *
ohair@286: * The ValidationEventHandler will be called by the JAXB Provider
ohair@286: * if any validation errors are encountered during calls to any of the
ohair@286: * unmarshal methods. If the client application does not register a
ohair@286: * ValidationEventHandler before invoking the unmarshal methods,
ohair@286: * then ValidationEvents will be handled by the default event
ohair@286: * handler which will terminate the unmarshal operation after the first
ohair@286: * error or fatal error is encountered.
ohair@286: *
ohair@286: * Calling this method with a null parameter will cause the Unmarshaller
ohair@286: * to revert back to the default event handler.
ohair@286: *
ohair@286: * @param handler the validation event handler
ohair@286: * @throws JAXBException if an error was encountered while setting the
ohair@286: * event handler
ohair@286: */
ohair@286: public void setEventHandler( ValidationEventHandler handler )
ohair@286: throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Return the current event handler or the default event handler if one
ohair@286: * hasn't been set.
ohair@286: *
ohair@286: * @return the current ValidationEventHandler or the default event handler
ohair@286: * if it hasn't been set
ohair@286: * @throws JAXBException if an error was encountered while getting the
ohair@286: * current event handler
ohair@286: */
ohair@286: public ValidationEventHandler getEventHandler()
ohair@286: throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Set the particular property in the underlying implementation of
ohair@286: * Unmarshaller. This method can only be used to set one of
ohair@286: * the standard JAXB defined properties above or a provider specific
ohair@286: * property. Attempting to set an undefined property will result in
ohair@286: * a PropertyException being thrown. See
ohair@286: * Supported Properties.
ohair@286: *
ohair@286: * @param name the name of the property to be set. This value can either
ohair@286: * be specified using one of the constant fields or a user
ohair@286: * supplied string.
ohair@286: * @param value the value of the property to be set
ohair@286: *
ohair@286: * @throws PropertyException when there is an error processing the given
ohair@286: * property or value
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the name parameter is null
ohair@286: */
ohair@286: public void setProperty( String name, Object value )
ohair@286: throws PropertyException;
ohair@286:
ohair@286: /**
ohair@286: * Get the particular property in the underlying implementation of
ohair@286: * Unmarshaller. This method can only be used to get one of
ohair@286: * the standard JAXB defined properties above or a provider specific
ohair@286: * property. Attempting to get an undefined property will result in
ohair@286: * a PropertyException being thrown. See
ohair@286: * Supported Properties.
ohair@286: *
ohair@286: * @param name the name of the property to retrieve
ohair@286: * @return the value of the requested property
ohair@286: *
ohair@286: * @throws PropertyException
ohair@286: * when there is an error retrieving the given property or value
ohair@286: * property name
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the name parameter is null
ohair@286: */
ohair@286: public Object getProperty( String name ) throws PropertyException;
ohair@286:
ohair@286: /**
ohair@286: * Specify the JAXP 1.3 {@link javax.xml.validation.Schema Schema}
ohair@286: * object that should be used to validate subsequent unmarshal operations
ohair@286: * against. Passing null into this method will disable validation.
ohair@286: *
ohair@286: * This method replaces the deprecated {@link #setValidating(boolean) setValidating(boolean)}
ohair@286: * API.
ohair@286: *
ohair@286: *
ohair@286: * Initially this property is set to null.
ohair@286: *
ohair@286: * @param schema Schema object to validate unmarshal operations against or null to disable validation
ohair@286: * @throws UnsupportedOperationException could be thrown if this method is
ohair@286: * invoked on an Unmarshaller created from a JAXBContext referencing
ohair@286: * JAXB 1.0 mapped classes
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public void setSchema( javax.xml.validation.Schema schema );
ohair@286:
ohair@286: /**
ohair@286: * Get the JAXP 1.3 {@link javax.xml.validation.Schema Schema} object
ohair@286: * being used to perform unmarshal-time validation. If there is no
ohair@286: * Schema set on the unmarshaller, then this method will return null
ohair@286: * indicating that unmarshal-time validation will not be performed.
ohair@286: *
ohair@286: * This method provides replacement functionality for the deprecated
ohair@286: * {@link #isValidating()} API as well as access to the Schema object.
ohair@286: * To determine if the Unmarshaller has validation enabled, simply
ohair@286: * test the return type for null:
ohair@286: *
ohair@286: *
ohair@286: * This is a convenience method that invokes
ohair@286: * Every unmarshaller internally maintains a
ohair@286: * {@link java.util.Map}<{@link Class},{@link XmlAdapter}>,
ohair@286: * which it uses for unmarshalling classes whose fields/methods are annotated
ohair@286: * with {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}.
ohair@286: *
ohair@286: *
ohair@286: * This method allows applications to use a configured instance of {@link XmlAdapter}.
ohair@286: * When an instance of an adapter is not given, an unmarshaller will create
ohair@286: * one by invoking its default constructor.
ohair@286: *
ohair@286: * @param type
ohair@286: * The type of the adapter. The specified instance will be used when
ohair@286: * {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter#value()}
ohair@286: * refers to this type.
ohair@286: * @param adapter
ohair@286: * The instance of the adapter to be used. If null, it will un-register
ohair@286: * the current adapter set for this type.
ohair@286: * @throws IllegalArgumentException
ohair@286: * if the type parameter is null.
ohair@286: * @throws UnsupportedOperationException
ohair@286: * if invoked agains a JAXB 1.0 implementation.
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public void setAdapter( Class type, A adapter );
ohair@286:
ohair@286: /**
ohair@286: * Gets the adapter associated with the specified type.
ohair@286: *
ohair@286: * This is the reverse operation of the {@link #setAdapter} method.
ohair@286: *
ohair@286: * @throws IllegalArgumentException
ohair@286: * if the type parameter is null.
ohair@286: * @throws UnsupportedOperationException
ohair@286: * if invoked agains a JAXB 1.0 implementation.
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public A getAdapter( Class type );
ohair@286:
ohair@286: /**
ohair@286: * Associate a context that resolves cid's, content-id URIs, to
ohair@286: * binary data passed as attachments. Unmarshal time validation, enabled via {@link #setSchema(Schema)},
ohair@286: * must be supported even when unmarshaller is performing XOP processing.
ohair@286: *
ohair@286: * Register unmarshal event callback {@link Listener} with this {@link Unmarshaller}.
ohair@286: *
ohair@286: *
ohair@286: * There is only one Listener per Unmarshaller. Setting a Listener replaces the previous set Listener.
ohair@286: * One can unregister current Listener by setting listener to null.
ohair@286: *
ohair@286: * @param listener provides unmarshal event callbacks for this {@link Unmarshaller}
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public void setListener(Listener listener);
ohair@286:
ohair@286: /**
ohair@286: * Return {@link Listener} registered with this {@link Unmarshaller}.
ohair@286: *
ohair@286: * @return registered {@link Listener} or
ohair@286: * boolean isValidating = u.getSchema()!=null;
ohair@286: *
ohair@286: *
ohair@286: * @return the Schema object being used to perform unmarshal-time
ohair@286: * validation or null if not present
ohair@286: * @throws UnsupportedOperationException could be thrown if this method is
ohair@286: * invoked on an Unmarshaller created from a JAXBContext referencing
ohair@286: * JAXB 1.0 mapped classes
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public javax.xml.validation.Schema getSchema();
ohair@286:
ohair@286: /**
ohair@286: * Associates a configured instance of {@link XmlAdapter} with this unmarshaller.
ohair@286: *
ohair@286: * setAdapter(adapter.getClass(),adapter);
.
ohair@286: *
ohair@286: * @see #setAdapter(Class,XmlAdapter)
ohair@286: * @throws IllegalArgumentException
ohair@286: * if the adapter parameter is null.
ohair@286: * @throws UnsupportedOperationException
ohair@286: * if invoked agains a JAXB 1.0 implementation.
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public void setAdapter( XmlAdapter adapter );
ohair@286:
ohair@286: /**
ohair@286: * Associates a configured instance of {@link XmlAdapter} with this unmarshaller.
ohair@286: *
ohair@286: * null
if no Listener is registered with this Unmarshaller.
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public Listener getListener();
ohair@286: }