ohair@286: /* mkos@397: * Copyright (c) 2005, 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 org.w3c.dom.Node; ohair@286: ohair@286: import javax.xml.validation.Schema; ohair@286: ohair@286: /** ohair@286: * Enable synchronization between XML infoset nodes and JAXB objects ohair@286: * representing same XML document. ohair@286: * ohair@286: *
ohair@286: * An instance of this class maintains the association between XML nodes of ohair@286: * an infoset preserving view and a JAXB representation of an XML document. ohair@286: * Navigation between the two views is provided by the methods ohair@286: * {@link #getXMLNode(Object)} and {@link #getJAXBNode(Object)}. ohair@286: * ohair@286: *
ohair@286: * Modifications can be made to either the infoset preserving view or the ohair@286: * JAXB representation of the document while the other view remains ohair@286: * unmodified. The binder is able to synchronize the changes made in the ohair@286: * modified view back into the other view using the appropriate ohair@286: * Binder update methods, {@link #updateXML(Object, Object)} or ohair@286: * {@link #updateJAXB(Object)}. ohair@286: * ohair@286: *
ohair@286: * A typical usage scenario is the following: ohair@286: *
ohair@286: * A Binder instance is created using the factory method ohair@286: * {@link JAXBContext#createBinder()} or {@link JAXBContext#createBinder(Class)}. ohair@286: * ohair@286: *
ohair@286: * The template parameter,
ohair@286: * This method is similar to {@link Unmarshaller#unmarshal(Node)}
ohair@286: * with the addition of maintaining the association between XML nodes
ohair@286: * and the produced JAXB objects, enabling future update operations,
ohair@286: * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
ohair@286: *
ohair@286: *
ohair@286: * When {@link #getSchema()} is non-null,
ohair@286: * This method throws {@link UnmarshalException} when the Binder's
ohair@286: * {@link JAXBContext} does not have a mapping for the XML element name
ohair@286: * or the type, specifiable via @xsi:type, of xmlNode
ohair@286: * to a JAXB mapped class. The method {@link #unmarshal(Object, Class)}
ohair@286: * enables an application to specify the JAXB mapped class that
ohair@286: * the xmlNode should be mapped to.
ohair@286: *
ohair@286: * @param xmlNode
ohair@286: * the document/element to unmarshal XML data from.
ohair@286: *
ohair@286: * @return
ohair@286: * the newly created root object of the JAXB object 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: * Binder is unable to perform the XML to Java
ohair@286: * binding.
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the node parameter is null
ohair@286: */
ohair@286: public abstract Object unmarshal( XmlNode xmlNode ) throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Unmarshal XML root element by provided declaredType
ohair@286: * to a JAXB object tree.
ohair@286: *
ohair@286: *
ohair@286: * Implements Unmarshal by Declared Type
ohair@286: *
ohair@286: *
ohair@286: * This method is similar to {@link Unmarshaller#unmarshal(Node, Class)}
ohair@286: * with the addition of maintaining the association between XML nodes
ohair@286: * and the produced JAXB objects, enabling future update operations,
ohair@286: * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
ohair@286: *
ohair@286: *
ohair@286: * When {@link #getSchema()} is non-null,
ohair@286: * This method is similar to {@link Marshaller#marshal(Object, Node)}
ohair@286: * with the addition of maintaining the association between JAXB objects
ohair@286: * and the produced XML nodes,
ohair@286: * enabling future update operations such as
ohair@286: * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
ohair@286: *
ohair@286: *
ohair@286: * When {@link #getSchema()} is non-null, the marshalled
ohair@286: * xml content is validated during this operation.
ohair@286: *
ohair@286: * @param jaxbObject
ohair@286: * The content tree to be marshalled.
ohair@286: * @param xmlNode
ohair@286: * The parameter must be a Node that accepts children.
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected problem occurs during the marshalling.
ohair@286: * @throws MarshalException
ohair@286: * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286: * returns false from its handleEvent method or the
ohair@286: * Binder is unable to marshal jaxbObject (or any
ohair@286: * object reachable from jaxbObject).
ohair@286: *
ohair@286: * @throws IllegalArgumentException
ohair@286: * If any of the method parameters are null
ohair@286: */
ohair@286: public abstract void marshal( Object jaxbObject, XmlNode xmlNode ) throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Gets the XML element associated with the given JAXB object.
ohair@286: *
ohair@286: *
ohair@286: * Once a JAXB object tree is associated with an XML fragment,
ohair@286: * this method enables navigation between the two trees.
ohair@286: *
ohair@286: *
ohair@286: * An association between an XML element and a JAXB object is
ohair@286: * established by the bind methods and the update methods.
ohair@286: * Note that this association is partial; not all XML elements
ohair@286: * have associated JAXB objects, and not all JAXB objects have
ohair@286: * associated XML elements.
ohair@286: *
ohair@286: * @param jaxbObject An instance that is reachable from a prior
ohair@286: * call to a bind or update method that returned
ohair@286: * a JAXB object tree.
ohair@286: *
ohair@286: * @return
ohair@286: * null if the specified JAXB object is not known to this
ohair@286: * {@link Binder}, or if it is not associated with an
ohair@286: * XML element.
ohair@286: *
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the jaxbObject parameter is null
ohair@286: */
ohair@286: public abstract XmlNode getXMLNode( Object jaxbObject );
ohair@286:
ohair@286: /**
ohair@286: * Gets the JAXB object associated with the given XML element.
ohair@286: *
ohair@286: *
ohair@286: * Once a JAXB object tree is associated with an XML fragment,
ohair@286: * this method enables navigation between the two trees.
ohair@286: *
ohair@286: *
ohair@286: * An association between an XML element and a JAXB object is
ohair@286: * established by the unmarshal, marshal and update methods.
ohair@286: * Note that this association is partial; not all XML elements
ohair@286: * have associated JAXB objects, and not all JAXB objects have
ohair@286: * associated XML elements.
ohair@286: *
ohair@286: * @return
ohair@286: * null if the specified XML node is not known to this
ohair@286: * {@link Binder}, or if it is not associated with a
ohair@286: * JAXB object.
ohair@286: *
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the node parameter is null
ohair@286: */
ohair@286: public abstract Object getJAXBNode( XmlNode xmlNode );
ohair@286:
ohair@286: /**
ohair@286: * Takes an JAXB object and updates
ohair@286: * its associated XML node and its descendants.
ohair@286: *
ohair@286: *
ohair@286: * This is a convenience method of:
ohair@286: *
ohair@286: * This operation can be thought of as an "in-place" marshalling.
ohair@286: * The difference is that instead of creating a whole new XML tree,
ohair@286: * this operation updates an existing tree while trying to preserve
ohair@286: * the XML as much as possible.
ohair@286: *
ohair@286: *
ohair@286: * For example, unknown elements/attributes in XML that were not bound
ohair@286: * to JAXB will be left untouched (whereas a marshalling operation
ohair@286: * would create a new tree that doesn't contain any of those.)
ohair@286: *
ohair@286: *
ohair@286: * As a side-effect, this operation updates the association between
ohair@286: * XML nodes and JAXB objects.
ohair@286: *
ohair@286: * @param jaxbObject root of potentially modified JAXB object tree
ohair@286: * @param xmlNode root of update target XML parse tree
ohair@286: *
ohair@286: * @return
ohair@286: * Returns the updated XML node. Typically, this is the same
ohair@286: * node you passed in as xmlNode, but it maybe
ohair@286: * a different object, for example when the tag name of the object
ohair@286: * has changed.
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected problem occurs updating corresponding XML content.
ohair@286: * @throws IllegalArgumentException
ohair@286: * If any of the input parameters are null
ohair@286: */
ohair@286: public abstract XmlNode updateXML( Object jaxbObject, XmlNode xmlNode ) throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Takes an XML node and updates its associated JAXB object and its descendants.
ohair@286: *
ohair@286: *
ohair@286: * This operation can be thought of as an "in-place" unmarshalling.
ohair@286: * The difference is that instead of creating a whole new JAXB tree,
ohair@286: * this operation updates an existing tree, reusing as much JAXB objects
ohair@286: * as possible.
ohair@286: *
ohair@286: *
ohair@286: * As a side-effect, this operation updates the association between
ohair@286: * XML nodes and JAXB objects.
ohair@286: *
ohair@286: * @return
ohair@286: * Returns the updated JAXB object. Typically, this is the same
ohair@286: * object that was returned from earlier
ohair@286: * {@link #marshal(Object,Object)} or
ohair@286: * {@link #updateJAXB(Object)} method invocation,
ohair@286: * but it maybe
ohair@286: * a different object, for example when the name of the XML
ohair@286: * element has changed.
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected problem occurs updating corresponding JAXB mapped content.
ohair@286: * @throws IllegalArgumentException
ohair@286: * If node parameter is null
ohair@286: */
ohair@286: public abstract Object updateJAXB( XmlNode xmlNode ) throws JAXBException;
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Specifies whether marshal, unmarshal and update methods
ohair@286: * performs validation on their XML content.
ohair@286: *
ohair@286: * @param schema set to null to disable validation.
ohair@286: *
ohair@286: * @see Unmarshaller#setSchema(Schema)
ohair@286: */
ohair@286: public abstract void setSchema( Schema schema );
ohair@286:
ohair@286: /**
ohair@286: * Gets the last {@link Schema} object (including null) set by the
ohair@286: * {@link #setSchema(Schema)} method.
ohair@286: *
ohair@286: * @return the Schema object for validation or null if not present
ohair@286: */
ohair@286: public abstract Schema getSchema();
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: * Binder unmarshal, marshal and update methods.
ohair@286: *
ohair@286: *
ohair@286: * Calling this method with a null parameter will cause the Binder
ohair@286: * to revert back to the default 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 abstract void setEventHandler( ValidationEventHandler handler ) 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 abstract ValidationEventHandler getEventHandler() throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: *
ohair@286: * Set the particular property in the underlying implementation of
ohair@286: * Binder. This method can only be used to set one of
ohair@286: * the standard JAXB defined unmarshal/marshal properties
ohair@286: * or a provider specific property for binder, unmarshal or marshal.
ohair@286: * Attempting to set an undefined property will result in
ohair@286: * a PropertyException being thrown. See
ohair@286: * Supported Unmarshal Properties
ohair@286: * and
ohair@286: * Supported Marshal 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: abstract public void setProperty( String name, Object value ) throws PropertyException;
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Get the particular property in the underlying implementation of
ohair@286: * Binder. This method can only
ohair@286: * be used to get one of
ohair@286: * the standard JAXB defined unmarshal/marshal properties
ohair@286: * or a provider specific property for binder, unmarshal or marshal.
ohair@286: * Attempting to get an undefined property will result in
ohair@286: * a PropertyException being thrown. See
ohair@286: * Supported Unmarshal Properties
ohair@286: * and
ohair@286: * Supported Marshal 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: abstract public Object getProperty( String name ) throws PropertyException;
ohair@286:
ohair@286: }
XmlNode
, is the
ohair@286: * root interface/class for the XML infoset preserving representation.
ohair@286: * A Binder implementation is required to minimally support
ohair@286: * an XmlNode
value of org.w3c.dom.Node.class
.
ohair@286: * A Binder implementation can support alternative XML infoset
ohair@286: * preserving representations.
ohair@286: *
ohair@286: * @author
ohair@286: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286: * Joseph Fialli
ohair@286: *
ohair@286: * @since JAXB 2.0
ohair@286: */
ohair@286: public abstract class BinderxmlNode
ohair@286: * and its descendants is validated during this operation.
ohair@286: *
ohair@286: * xmlNode
ohair@286: * and its descendants is validated during this operation.
ohair@286: *
ohair@286: * @param xmlNode
ohair@286: * the document/element to unmarshal XML data from.
ohair@286: * @param declaredType
ohair@286: * appropriate JAXB mapped class to hold node's XML data.
ohair@286: *
ohair@286: * @return
ohair@286: * JAXB Element representation
ohair@286: * 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: * Binder is unable to perform the XML to Java
ohair@286: * binding.
ohair@286: * @throws IllegalArgumentException
ohair@286: * If any of the input parameters are null
ohair@286: * @since JAXB2.0
ohair@286: */
ohair@286: public abstract
ohair@286: * updateXML( jaxbObject, getXMLNode(jaxbObject));
ohair@286: *
ohair@286: *
ohair@286: * @throws JAXBException
ohair@286: * If any unexpected problem occurs updating corresponding XML content.
ohair@286: * @throws IllegalArgumentException
ohair@286: * If the jaxbObject parameter is null
ohair@286: */
ohair@286: public abstract XmlNode updateXML( Object jaxbObject ) throws JAXBException;
ohair@286:
ohair@286: /**
ohair@286: * Changes in JAXB object tree are updated in its associated XML parse tree.
ohair@286: *
ohair@286: *