Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaxws_classes/javax/xml/bind/Binder.java
38890 views
/*1* Copyright (c) 2005, 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 org.w3c.dom.Node;2829import javax.xml.validation.Schema;3031/**32* Enable synchronization between XML infoset nodes and JAXB objects33* representing same XML document.34*35* <p>36* An instance of this class maintains the association between XML nodes of37* an infoset preserving view and a JAXB representation of an XML document.38* Navigation between the two views is provided by the methods39* {@link #getXMLNode(Object)} and {@link #getJAXBNode(Object)}.40*41* <p>42* Modifications can be made to either the infoset preserving view or the43* JAXB representation of the document while the other view remains44* unmodified. The binder is able to synchronize the changes made in the45* modified view back into the other view using the appropriate46* Binder update methods, {@link #updateXML(Object, Object)} or47* {@link #updateJAXB(Object)}.48*49* <p>50* A typical usage scenario is the following:51* <ul>52* <li>load XML document into an XML infoset representation</li>53* <li>{@link #unmarshal(Object)} XML infoset view to JAXB view.54* (Note to conserve resources, it is possible to only unmarshal a55* subtree of the XML infoset view to the JAXB view.)</li>56* <li>application access/updates JAXB view of XML document.</li>57* <li>{@link #updateXML(Object)} synchronizes modifications to JAXB view58* back into the XML infoset view. Update operation preserves as59* much of original XML infoset as possible (i.e. comments, PI, ...)</li>60* </ul>61*62* <p>63* A Binder instance is created using the factory method64* {@link JAXBContext#createBinder()} or {@link JAXBContext#createBinder(Class)}.65*66* <p>67* The template parameter, <code>XmlNode</code>, is the68* root interface/class for the XML infoset preserving representation.69* A Binder implementation is required to minimally support70* an <code>XmlNode</code> value of <code>org.w3c.dom.Node.class</code>.71* A Binder implementation can support alternative XML infoset72* preserving representations.73*74* @author75* Kohsuke Kawaguchi ([email protected])76* Joseph Fialli77*78* @since JAXB 2.079*/80public abstract class Binder<XmlNode> {81/**82* Unmarshal XML infoset view to a JAXB object tree.83*84* <p>85* This method is similar to {@link Unmarshaller#unmarshal(Node)}86* with the addition of maintaining the association between XML nodes87* and the produced JAXB objects, enabling future update operations,88* {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.89*90* <p>91* When {@link #getSchema()} is non-null, <code>xmlNode</code>92* and its descendants is validated during this operation.93*94* <p>95* This method throws {@link UnmarshalException} when the Binder's96* {@link JAXBContext} does not have a mapping for the XML element name97* or the type, specifiable via <tt>@xsi:type</tt>, of <tt>xmlNode</tt>98* to a JAXB mapped class. The method {@link #unmarshal(Object, Class)}99* enables an application to specify the JAXB mapped class that100* the <tt>xmlNode</tt> should be mapped to.101*102* @param xmlNode103* the document/element to unmarshal XML data from.104*105* @return106* the newly created root object of the JAXB object tree.107*108* @throws JAXBException109* If any unexpected errors occur while unmarshalling110* @throws UnmarshalException111* If the {@link ValidationEventHandler ValidationEventHandler}112* returns false from its <tt>handleEvent</tt> method or the113* <tt>Binder</tt> is unable to perform the XML to Java114* binding.115* @throws IllegalArgumentException116* If the node parameter is null117*/118public abstract Object unmarshal( XmlNode xmlNode ) throws JAXBException;119120/**121* Unmarshal XML root element by provided <tt>declaredType</tt>122* to a JAXB object tree.123*124* <p>125* Implements <a href="Unmarshaller.html#unmarshalByDeclaredType">Unmarshal by Declared Type</a>126*127* <p>128* This method is similar to {@link Unmarshaller#unmarshal(Node, Class)}129* with the addition of maintaining the association between XML nodes130* and the produced JAXB objects, enabling future update operations,131* {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.132*133* <p>134* When {@link #getSchema()} is non-null, <code>xmlNode</code>135* and its descendants is validated during this operation.136*137* @param xmlNode138* the document/element to unmarshal XML data from.139* @param declaredType140* appropriate JAXB mapped class to hold <tt>node</tt>'s XML data.141*142* @return143* <a href="JAXBElement.html">JAXB Element</a> representation144* of <tt>node</tt>145*146* @throws JAXBException147* If any unexpected errors occur while unmarshalling148* @throws UnmarshalException149* If the {@link ValidationEventHandler ValidationEventHandler}150* returns false from its <tt>handleEvent</tt> method or the151* <tt>Binder</tt> is unable to perform the XML to Java152* binding.153* @throws IllegalArgumentException154* If any of the input parameters are null155* @since JAXB2.0156*/157public abstract <T> JAXBElement<T>158unmarshal( XmlNode xmlNode, Class<T> declaredType )159throws JAXBException;160161/**162* Marshal a JAXB object tree to a new XML document.163*164* <p>165* This method is similar to {@link Marshaller#marshal(Object, Node)}166* with the addition of maintaining the association between JAXB objects167* and the produced XML nodes,168* enabling future update operations such as169* {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.170*171* <p>172* When {@link #getSchema()} is non-null, the marshalled173* xml content is validated during this operation.174*175* @param jaxbObject176* The content tree to be marshalled.177* @param xmlNode178* The parameter must be a Node that accepts children.179*180* @throws JAXBException181* If any unexpected problem occurs during the marshalling.182* @throws MarshalException183* If the {@link ValidationEventHandler ValidationEventHandler}184* returns false from its <tt>handleEvent</tt> method or the185* <tt>Binder</tt> is unable to marshal <tt>jaxbObject</tt> (or any186* object reachable from <tt>jaxbObject</tt>).187*188* @throws IllegalArgumentException189* If any of the method parameters are null190*/191public abstract void marshal( Object jaxbObject, XmlNode xmlNode ) throws JAXBException;192193/**194* Gets the XML element associated with the given JAXB object.195*196* <p>197* Once a JAXB object tree is associated with an XML fragment,198* this method enables navigation between the two trees.199*200* <p>201* An association between an XML element and a JAXB object is202* established by the bind methods and the update methods.203* Note that this association is partial; not all XML elements204* have associated JAXB objects, and not all JAXB objects have205* associated XML elements.206*207* @param jaxbObject An instance that is reachable from a prior208* call to a bind or update method that returned209* a JAXB object tree.210*211* @return212* null if the specified JAXB object is not known to this213* {@link Binder}, or if it is not associated with an214* XML element.215*216* @throws IllegalArgumentException217* If the jaxbObject parameter is null218*/219public abstract XmlNode getXMLNode( Object jaxbObject );220221/**222* Gets the JAXB object associated with the given XML element.223*224* <p>225* Once a JAXB object tree is associated with an XML fragment,226* this method enables navigation between the two trees.227*228* <p>229* An association between an XML element and a JAXB object is230* established by the unmarshal, marshal and update methods.231* Note that this association is partial; not all XML elements232* have associated JAXB objects, and not all JAXB objects have233* associated XML elements.234*235* @return236* null if the specified XML node is not known to this237* {@link Binder}, or if it is not associated with a238* JAXB object.239*240* @throws IllegalArgumentException241* If the node parameter is null242*/243public abstract Object getJAXBNode( XmlNode xmlNode );244245/**246* Takes an JAXB object and updates247* its associated XML node and its descendants.248*249* <p>250* This is a convenience method of:251* <pre>252* updateXML( jaxbObject, getXMLNode(jaxbObject));253* </pre>254*255* @throws JAXBException256* If any unexpected problem occurs updating corresponding XML content.257* @throws IllegalArgumentException258* If the jaxbObject parameter is null259*/260public abstract XmlNode updateXML( Object jaxbObject ) throws JAXBException;261262/**263* Changes in JAXB object tree are updated in its associated XML parse tree.264*265* <p>266* This operation can be thought of as an "in-place" marshalling.267* The difference is that instead of creating a whole new XML tree,268* this operation updates an existing tree while trying to preserve269* the XML as much as possible.270*271* <p>272* For example, unknown elements/attributes in XML that were not bound273* to JAXB will be left untouched (whereas a marshalling operation274* would create a new tree that doesn't contain any of those.)275*276* <p>277* As a side-effect, this operation updates the association between278* XML nodes and JAXB objects.279*280* @param jaxbObject root of potentially modified JAXB object tree281* @param xmlNode root of update target XML parse tree282*283* @return284* Returns the updated XML node. Typically, this is the same285* node you passed in as <i>xmlNode</i>, but it maybe286* a different object, for example when the tag name of the object287* has changed.288*289* @throws JAXBException290* If any unexpected problem occurs updating corresponding XML content.291* @throws IllegalArgumentException292* If any of the input parameters are null293*/294public abstract XmlNode updateXML( Object jaxbObject, XmlNode xmlNode ) throws JAXBException;295296/**297* Takes an XML node and updates its associated JAXB object and its descendants.298*299* <p>300* This operation can be thought of as an "in-place" unmarshalling.301* The difference is that instead of creating a whole new JAXB tree,302* this operation updates an existing tree, reusing as much JAXB objects303* as possible.304*305* <p>306* As a side-effect, this operation updates the association between307* XML nodes and JAXB objects.308*309* @return310* Returns the updated JAXB object. Typically, this is the same311* object that was returned from earlier312* {@link #marshal(Object,Object)} or313* {@link #updateJAXB(Object)} method invocation,314* but it maybe315* a different object, for example when the name of the XML316* element has changed.317*318* @throws JAXBException319* If any unexpected problem occurs updating corresponding JAXB mapped content.320* @throws IllegalArgumentException321* If node parameter is null322*/323public abstract Object updateJAXB( XmlNode xmlNode ) throws JAXBException;324325326/**327* Specifies whether marshal, unmarshal and update methods328* performs validation on their XML content.329*330* @param schema set to null to disable validation.331*332* @see Unmarshaller#setSchema(Schema)333*/334public abstract void setSchema( Schema schema );335336/**337* Gets the last {@link Schema} object (including null) set by the338* {@link #setSchema(Schema)} method.339*340* @return the Schema object for validation or null if not present341*/342public abstract Schema getSchema();343344/**345* Allow an application to register a <tt>ValidationEventHandler</tt>.346* <p>347* The <tt>ValidationEventHandler</tt> will be called by the JAXB Provider348* if any validation errors are encountered during calls to any of the349* Binder unmarshal, marshal and update methods.350*351* <p>352* Calling this method with a null parameter will cause the Binder353* to revert back to the default default event handler.354*355* @param handler the validation event handler356* @throws JAXBException if an error was encountered while setting the357* event handler358*/359public abstract void setEventHandler( ValidationEventHandler handler ) throws JAXBException;360361/**362* Return the current event handler or the default event handler if one363* hasn't been set.364*365* @return the current ValidationEventHandler or the default event handler366* if it hasn't been set367* @throws JAXBException if an error was encountered while getting the368* current event handler369*/370public abstract ValidationEventHandler getEventHandler() throws JAXBException;371372/**373*374* Set the particular property in the underlying implementation of375* <tt>Binder</tt>. This method can only be used to set one of376* the standard JAXB defined unmarshal/marshal properties377* or a provider specific property for binder, unmarshal or marshal.378* Attempting to set an undefined property will result in379* a PropertyException being thrown. See380* <a href="Unmarshaller.html#supportedProps">Supported Unmarshal Properties</a>381* and382* <a href="Marshaller.html#supportedProps">Supported Marshal Properties</a>.383*384* @param name the name of the property to be set. This value can either385* be specified using one of the constant fields or a user386* supplied string.387* @param value the value of the property to be set388*389* @throws PropertyException when there is an error processing the given390* property or value391* @throws IllegalArgumentException392* If the name parameter is null393*/394abstract public void setProperty( String name, Object value ) throws PropertyException;395396397/**398* Get the particular property in the underlying implementation of399* <tt>Binder</tt>. This method can only400* be used to get one of401* the standard JAXB defined unmarshal/marshal properties402* or a provider specific property for binder, unmarshal or marshal.403* Attempting to get an undefined property will result in404* a PropertyException being thrown. See405* <a href="Unmarshaller.html#supportedProps">Supported Unmarshal Properties</a>406* and407* <a href="Marshaller.html#supportedProps">Supported Marshal Properties</a>.408*409* @param name the name of the property to retrieve410* @return the value of the requested property411*412* @throws PropertyException413* when there is an error retrieving the given property or value414* property name415* @throws IllegalArgumentException416* If the name parameter is null417*/418abstract public Object getProperty( String name ) throws PropertyException;419420}421422423