src/share/jaxws_classes/javax/xml/bind/Binder.java

changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/javax/xml/bind/Binder.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,421 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package javax.xml.bind;
    1.30 +
    1.31 +import org.w3c.dom.Node;
    1.32 +
    1.33 +import javax.xml.validation.Schema;
    1.34 +
    1.35 +/**
    1.36 + * Enable synchronization between XML infoset nodes and JAXB objects
    1.37 + * representing same XML document.
    1.38 + *
    1.39 + * <p>
    1.40 + * An instance of this class maintains the association between XML nodes of
    1.41 + * an infoset preserving view and a JAXB representation of an XML document.
    1.42 + * Navigation between the two views is provided by the methods
    1.43 + * {@link #getXMLNode(Object)} and {@link #getJAXBNode(Object)}.
    1.44 + *
    1.45 + * <p>
    1.46 + * Modifications can be made to either the infoset preserving view or the
    1.47 + * JAXB representation of the document while the other view remains
    1.48 + * unmodified. The binder is able to synchronize the changes made in the
    1.49 + * modified view back into the other view using the appropriate
    1.50 + * Binder update methods, {@link #updateXML(Object, Object)} or
    1.51 + * {@link #updateJAXB(Object)}.
    1.52 + *
    1.53 + * <p>
    1.54 + * A typical usage scenario is the following:
    1.55 + * <ul>
    1.56 + *   <li>load XML document into an XML infoset representation</li>
    1.57 + *   <li>{@link #unmarshal(Object)} XML infoset view to JAXB view.
    1.58 + *       (Note to conserve resources, it is possible to only unmarshal a
    1.59 + *       subtree of the XML infoset view to the JAXB view.)</li>
    1.60 + *   <li>application access/updates JAXB view of XML document.</li>
    1.61 + *   <li>{@link #updateXML(Object)} synchronizes modifications to JAXB view
    1.62 + *       back into the XML infoset view. Update operation preserves as
    1.63 + *       much of original XML infoset as possible (i.e. comments, PI, ...)</li>
    1.64 + * </ul>
    1.65 + *
    1.66 + * <p>
    1.67 + * A Binder instance is created using the factory method
    1.68 + * {@link JAXBContext#createBinder()} or {@link JAXBContext#createBinder(Class)}.
    1.69 + *
    1.70 + * <p>
    1.71 + * The template parameter, <code>XmlNode</code>, is the
    1.72 + * root interface/class for the XML infoset preserving representation.
    1.73 + * A Binder implementation is required to minimally support
    1.74 + * an <code>XmlNode</code> value of <code>org.w3c.dom.Node.class</code>.
    1.75 + * A Binder implementation can support alternative XML infoset
    1.76 + * preserving representations.
    1.77 + *
    1.78 + * @author
    1.79 + *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.80 + *     Joseph Fialli
    1.81 + *
    1.82 + * @since JAXB 2.0
    1.83 + */
    1.84 +public abstract class Binder<XmlNode> {
    1.85 +    /**
    1.86 +     * Unmarshal XML infoset view to a JAXB object tree.
    1.87 +     *
    1.88 +     * <p>
    1.89 +     * This method is similar to {@link Unmarshaller#unmarshal(Node)}
    1.90 +     * with the addition of maintaining the association between XML nodes
    1.91 +     * and the produced JAXB objects, enabling future update operations,
    1.92 +     * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
    1.93 +     *
    1.94 +     * <p>
    1.95 +     * When {@link #getSchema()} is non-null, <code>xmlNode</code>
    1.96 +     * and its descendants is validated during this operation.
    1.97 +     *
    1.98 +     * <p>
    1.99 +     * This method throws {@link UnmarshalException} when the Binder's
   1.100 +     * {@link JAXBContext} does not have a mapping for the XML element name
   1.101 +     * or the type, specifiable via <tt>@xsi:type</tt>, of <tt>xmlNode</tt>
   1.102 +     * to a JAXB mapped class. The method {@link #unmarshal(Object, Class)}
   1.103 +     * enables an application to specify the JAXB mapped class that
   1.104 +     * the <tt>xmlNode</tt> should be mapped to.
   1.105 +     *
   1.106 +     * @param xmlNode
   1.107 +     *      the document/element to unmarshal XML data from.
   1.108 +     *
   1.109 +     * @return
   1.110 +     *      the newly created root object of the JAXB object tree.
   1.111 +     *
   1.112 +     * @throws JAXBException
   1.113 +     *      If any unexpected errors occur while unmarshalling
   1.114 +     * @throws UnmarshalException
   1.115 +     *     If the {@link ValidationEventHandler ValidationEventHandler}
   1.116 +     *     returns false from its <tt>handleEvent</tt> method or the
   1.117 +     *     <tt>Binder</tt> is unable to perform the XML to Java
   1.118 +     *     binding.
   1.119 +     * @throws IllegalArgumentException
   1.120 +     *      If the node parameter is null
   1.121 +     */
   1.122 +    public abstract Object unmarshal( XmlNode xmlNode ) throws JAXBException;
   1.123 +
   1.124 +    /**
   1.125 +     * Unmarshal XML root element by provided <tt>declaredType</tt>
   1.126 +     * to a JAXB object tree.
   1.127 +     *
   1.128 +     * <p>
   1.129 +     * Implements <a href="Unmarshaller.html#unmarshalByDeclaredType">Unmarshal by Declared Type</a>
   1.130 +     *
   1.131 +     * <p>
   1.132 +     * This method is similar to {@link Unmarshaller#unmarshal(Node, Class)}
   1.133 +     * with the addition of maintaining the association between XML nodes
   1.134 +     * and the produced JAXB objects, enabling future update operations,
   1.135 +     * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
   1.136 +     *
   1.137 +     * <p>
   1.138 +     * When {@link #getSchema()} is non-null, <code>xmlNode</code>
   1.139 +     * and its descendants is validated during this operation.
   1.140 +     *
   1.141 +     * @param xmlNode
   1.142 +     *      the document/element to unmarshal XML data from.
   1.143 +     * @param declaredType
   1.144 +     *      appropriate JAXB mapped class to hold <tt>node</tt>'s XML data.
   1.145 +     *
   1.146 +     * @return
   1.147 +     * <a href="JAXBElement.html">JAXB Element</a> representation
   1.148 +     * of <tt>node</tt>
   1.149 +     *
   1.150 +     * @throws JAXBException
   1.151 +     *      If any unexpected errors occur while unmarshalling
   1.152 +     * @throws UnmarshalException
   1.153 +     *     If the {@link ValidationEventHandler ValidationEventHandler}
   1.154 +     *     returns false from its <tt>handleEvent</tt> method or the
   1.155 +     *     <tt>Binder</tt> is unable to perform the XML to Java
   1.156 +     *     binding.
   1.157 +     * @throws IllegalArgumentException
   1.158 +     *      If any of the input parameters are null
   1.159 +     * @since JAXB2.0
   1.160 +     */
   1.161 +    public abstract <T> JAXBElement<T>
   1.162 +        unmarshal( XmlNode xmlNode, Class<T> declaredType )
   1.163 +        throws JAXBException;
   1.164 +
   1.165 +    /**
   1.166 +     * Marshal a JAXB object tree to a new XML document.
   1.167 +     *
   1.168 +     * <p>
   1.169 +     * This method is similar to {@link Marshaller#marshal(Object, Node)}
   1.170 +     * with the addition of maintaining the association between JAXB objects
   1.171 +     * and the produced XML nodes,
   1.172 +     * enabling future update operations such as
   1.173 +     * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
   1.174 +     *
   1.175 +     * <p>
   1.176 +     * When {@link #getSchema()} is non-null, the marshalled
   1.177 +     * xml content is validated during this operation.
   1.178 +     *
   1.179 +     * @param jaxbObject
   1.180 +     *      The content tree to be marshalled.
   1.181 +     * @param xmlNode
   1.182 +     *      The parameter must be a Node that accepts children.
   1.183 +     *
   1.184 +     * @throws JAXBException
   1.185 +     *      If any unexpected problem occurs during the marshalling.
   1.186 +     * @throws MarshalException
   1.187 +     *      If the {@link ValidationEventHandler ValidationEventHandler}
   1.188 +     *      returns false from its <tt>handleEvent</tt> method or the
   1.189 +     *      <tt>Binder</tt> is unable to marshal <tt>jaxbObject</tt> (or any
   1.190 +     *      object reachable from <tt>jaxbObject</tt>).
   1.191 +     *
   1.192 +     * @throws IllegalArgumentException
   1.193 +     *      If any of the method parameters are null
   1.194 +     */
   1.195 +    public abstract void marshal( Object jaxbObject, XmlNode xmlNode ) throws JAXBException;
   1.196 +
   1.197 +    /**
   1.198 +     * Gets the XML element associated with the given JAXB object.
   1.199 +     *
   1.200 +     * <p>
   1.201 +     * Once a JAXB object tree is associated with an XML fragment,
   1.202 +     * this method enables navigation between the two trees.
   1.203 +     *
   1.204 +     * <p>
   1.205 +     * An association between an XML element and a JAXB object is
   1.206 +     * established by the bind methods and the update methods.
   1.207 +     * Note that this association is partial; not all XML elements
   1.208 +     * have associated JAXB objects, and not all JAXB objects have
   1.209 +     * associated XML elements.
   1.210 +     *
   1.211 +     * @param jaxbObject An instance that is reachable from a prior
   1.212 +     *                   call to a bind or update method that returned
   1.213 +     *                   a JAXB object tree.
   1.214 +     *
   1.215 +     * @return
   1.216 +     *      null if the specified JAXB object is not known to this
   1.217 +     *      {@link Binder}, or if it is not associated with an
   1.218 +     *      XML element.
   1.219 +     *
   1.220 +     * @throws IllegalArgumentException
   1.221 +     *      If the jaxbObject parameter is null
   1.222 +     */
   1.223 +    public abstract XmlNode getXMLNode( Object jaxbObject );
   1.224 +
   1.225 +    /**
   1.226 +     * Gets the JAXB object associated with the given XML element.
   1.227 +     *
   1.228 +     * <p>
   1.229 +     * Once a JAXB object tree is associated with an XML fragment,
   1.230 +     * this method enables navigation between the two trees.
   1.231 +     *
   1.232 +     * <p>
   1.233 +     * An association between an XML element and a JAXB object is
   1.234 +     * established by the unmarshal, marshal and update methods.
   1.235 +     * Note that this association is partial; not all XML elements
   1.236 +     * have associated JAXB objects, and not all JAXB objects have
   1.237 +     * associated XML elements.
   1.238 +     *
   1.239 +     * @return
   1.240 +     *      null if the specified XML node is not known to this
   1.241 +     *      {@link Binder}, or if it is not associated with a
   1.242 +     *      JAXB object.
   1.243 +     *
   1.244 +     * @throws IllegalArgumentException
   1.245 +     *      If the node parameter is null
   1.246 +     */
   1.247 +    public abstract Object getJAXBNode( XmlNode xmlNode );
   1.248 +
   1.249 +    /**
   1.250 +     * Takes an JAXB object and updates
   1.251 +     * its associated XML node and its descendants.
   1.252 +     *
   1.253 +     * <p>
   1.254 +     * This is a convenience method of:
   1.255 +     * <pre>
   1.256 +     * updateXML( jaxbObject, getXMLNode(jaxbObject));
   1.257 +     * </pre>
   1.258 +     *
   1.259 +     * @throws JAXBException
   1.260 +     *      If any unexpected problem occurs updating corresponding XML content.
   1.261 +     * @throws IllegalArgumentException
   1.262 +     *      If the jaxbObject parameter is null
   1.263 +     */
   1.264 +    public abstract XmlNode updateXML( Object jaxbObject ) throws JAXBException;
   1.265 +
   1.266 +    /**
   1.267 +     * Changes in JAXB object tree are updated in its associated XML parse tree.
   1.268 +     *
   1.269 +     * <p>
   1.270 +     * This operation can be thought of as an "in-place" marshalling.
   1.271 +     * The difference is that instead of creating a whole new XML tree,
   1.272 +     * this operation updates an existing tree while trying to preserve
   1.273 +     * the XML as much as possible.
   1.274 +     *
   1.275 +     * <p>
   1.276 +     * For example, unknown elements/attributes in XML that were not bound
   1.277 +     * to JAXB will be left untouched (whereas a marshalling operation
   1.278 +     * would create a new tree that doesn't contain any of those.)
   1.279 +     *
   1.280 +     * <p>
   1.281 +     * As a side-effect, this operation updates the association between
   1.282 +     * XML nodes and JAXB objects.
   1.283 +     *
   1.284 +     * @param jaxbObject root of potentially modified JAXB object tree
   1.285 +     * @param xmlNode    root of update target XML parse tree
   1.286 +     *
   1.287 +     * @return
   1.288 +     *      Returns the updated XML node. Typically, this is the same
   1.289 +     *      node you passed in as <i>xmlNode</i>, but it maybe
   1.290 +     *      a different object, for example when the tag name of the object
   1.291 +     *      has changed.
   1.292 +     *
   1.293 +     * @throws JAXBException
   1.294 +     *      If any unexpected problem occurs updating corresponding XML content.
   1.295 +     * @throws IllegalArgumentException
   1.296 +     *      If any of the input parameters are null
   1.297 +     */
   1.298 +    public abstract XmlNode updateXML( Object jaxbObject, XmlNode xmlNode ) throws JAXBException;
   1.299 +
   1.300 +    /**
   1.301 +     * Takes an XML node and updates its associated JAXB object and its descendants.
   1.302 +     *
   1.303 +     * <p>
   1.304 +     * This operation can be thought of as an "in-place" unmarshalling.
   1.305 +     * The difference is that instead of creating a whole new JAXB tree,
   1.306 +     * this operation updates an existing tree, reusing as much JAXB objects
   1.307 +     * as possible.
   1.308 +     *
   1.309 +     * <p>
   1.310 +     * As a side-effect, this operation updates the association between
   1.311 +     * XML nodes and JAXB objects.
   1.312 +     *
   1.313 +     * @return
   1.314 +     *      Returns the updated JAXB object. Typically, this is the same
   1.315 +     *      object that was returned from earlier
   1.316 +     *      {@link #marshal(Object,Object)} or
   1.317 +     *      {@link #updateJAXB(Object)} method invocation,
   1.318 +     *      but it maybe
   1.319 +     *      a different object, for example when the name of the XML
   1.320 +     *      element has changed.
   1.321 +     *
   1.322 +     * @throws JAXBException
   1.323 +     *      If any unexpected problem occurs updating corresponding JAXB mapped content.
   1.324 +     * @throws IllegalArgumentException
   1.325 +     *      If node parameter is null
   1.326 +     */
   1.327 +    public abstract Object updateJAXB( XmlNode xmlNode ) throws JAXBException;
   1.328 +
   1.329 +
   1.330 +    /**
   1.331 +     * Specifies whether marshal, unmarshal and update methods
   1.332 +     * performs validation on their XML content.
   1.333 +     *
   1.334 +     * @param schema set to null to disable validation.
   1.335 +     *
   1.336 +     * @see Unmarshaller#setSchema(Schema)
   1.337 +     */
   1.338 +    public abstract void setSchema( Schema schema );
   1.339 +
   1.340 +    /**
   1.341 +     * Gets the last {@link Schema} object (including null) set by the
   1.342 +     * {@link #setSchema(Schema)} method.
   1.343 +     *
   1.344 +     * @return the Schema object for validation or null if not present
   1.345 +     */
   1.346 +    public abstract Schema getSchema();
   1.347 +
   1.348 +    /**
   1.349 +     * Allow an application to register a <tt>ValidationEventHandler</tt>.
   1.350 +     * <p>
   1.351 +     * The <tt>ValidationEventHandler</tt> will be called by the JAXB Provider
   1.352 +     * if any validation errors are encountered during calls to any of the
   1.353 +     * Binder unmarshal, marshal and update methods.
   1.354 +     *
   1.355 +     * <p>
   1.356 +     * Calling this method with a null parameter will cause the Binder
   1.357 +     * to revert back to the default default event handler.
   1.358 +     *
   1.359 +     * @param handler the validation event handler
   1.360 +     * @throws JAXBException if an error was encountered while setting the
   1.361 +     *         event handler
   1.362 +     */
   1.363 +    public abstract void setEventHandler( ValidationEventHandler handler ) throws JAXBException;
   1.364 +
   1.365 +    /**
   1.366 +     * Return the current event handler or the default event handler if one
   1.367 +     * hasn't been set.
   1.368 +     *
   1.369 +     * @return the current ValidationEventHandler or the default event handler
   1.370 +     *         if it hasn't been set
   1.371 +     * @throws JAXBException if an error was encountered while getting the
   1.372 +     *         current event handler
   1.373 +     */
   1.374 +    public abstract ValidationEventHandler getEventHandler() throws JAXBException;
   1.375 +
   1.376 +    /**
   1.377 +     *
   1.378 +     * Set the particular property in the underlying implementation of
   1.379 +     * <tt>Binder</tt>.  This method can only be used to set one of
   1.380 +     * the standard JAXB defined unmarshal/marshal properties
   1.381 +     * or a provider specific property for binder, unmarshal or marshal.
   1.382 +     * Attempting to set an undefined property will result in
   1.383 +     * a PropertyException being thrown.  See
   1.384 +     * <a href="Unmarshaller.html#supportedProps">Supported Unmarshal Properties</a>
   1.385 +     * and
   1.386 +     * <a href="Marshaller.html#supportedProps">Supported Marshal Properties</a>.
   1.387 +     *
   1.388 +     * @param name the name of the property to be set. This value can either
   1.389 +     *              be specified using one of the constant fields or a user
   1.390 +     *              supplied string.
   1.391 +     * @param value the value of the property to be set
   1.392 +     *
   1.393 +     * @throws PropertyException when there is an error processing the given
   1.394 +     *                            property or value
   1.395 +     * @throws IllegalArgumentException
   1.396 +     *      If the name parameter is null
   1.397 +     */
   1.398 +    abstract public void setProperty( String name, Object value ) throws PropertyException;
   1.399 +
   1.400 +
   1.401 +    /**
   1.402 +     * Get the particular property in the underlying implementation of
   1.403 +     * <tt>Binder</tt>.  This method can only
   1.404 +     * be used to get one of
   1.405 +     * the standard JAXB defined unmarshal/marshal properties
   1.406 +     * or a provider specific property for binder, unmarshal or marshal.
   1.407 +     * Attempting to get an undefined property will result in
   1.408 +     * a PropertyException being thrown.  See
   1.409 +     * <a href="Unmarshaller.html#supportedProps">Supported Unmarshal Properties</a>
   1.410 +     * and
   1.411 +     * <a href="Marshaller.html#supportedProps">Supported Marshal Properties</a>.
   1.412 +     *
   1.413 +     * @param name the name of the property to retrieve
   1.414 +     * @return the value of the requested property
   1.415 +     *
   1.416 +     * @throws PropertyException
   1.417 +     *      when there is an error retrieving the given property or value
   1.418 +     *      property name
   1.419 +     * @throws IllegalArgumentException
   1.420 +     *      If the name parameter is null
   1.421 +     */
   1.422 +    abstract public Object getProperty( String name ) throws PropertyException;
   1.423 +
   1.424 +}

mercurial