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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 397
b99d7e355d4b
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

ohair@286 1 /*
mkos@397 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind;
ohair@286 27
ohair@286 28 import org.w3c.dom.Node;
ohair@286 29
ohair@286 30 import javax.xml.validation.Schema;
ohair@286 31
ohair@286 32 /**
ohair@286 33 * Enable synchronization between XML infoset nodes and JAXB objects
ohair@286 34 * representing same XML document.
ohair@286 35 *
ohair@286 36 * <p>
ohair@286 37 * An instance of this class maintains the association between XML nodes of
ohair@286 38 * an infoset preserving view and a JAXB representation of an XML document.
ohair@286 39 * Navigation between the two views is provided by the methods
ohair@286 40 * {@link #getXMLNode(Object)} and {@link #getJAXBNode(Object)}.
ohair@286 41 *
ohair@286 42 * <p>
ohair@286 43 * Modifications can be made to either the infoset preserving view or the
ohair@286 44 * JAXB representation of the document while the other view remains
ohair@286 45 * unmodified. The binder is able to synchronize the changes made in the
ohair@286 46 * modified view back into the other view using the appropriate
ohair@286 47 * Binder update methods, {@link #updateXML(Object, Object)} or
ohair@286 48 * {@link #updateJAXB(Object)}.
ohair@286 49 *
ohair@286 50 * <p>
ohair@286 51 * A typical usage scenario is the following:
ohair@286 52 * <ul>
ohair@286 53 * <li>load XML document into an XML infoset representation</li>
ohair@286 54 * <li>{@link #unmarshal(Object)} XML infoset view to JAXB view.
ohair@286 55 * (Note to conserve resources, it is possible to only unmarshal a
ohair@286 56 * subtree of the XML infoset view to the JAXB view.)</li>
ohair@286 57 * <li>application access/updates JAXB view of XML document.</li>
ohair@286 58 * <li>{@link #updateXML(Object)} synchronizes modifications to JAXB view
ohair@286 59 * back into the XML infoset view. Update operation preserves as
ohair@286 60 * much of original XML infoset as possible (i.e. comments, PI, ...)</li>
ohair@286 61 * </ul>
ohair@286 62 *
ohair@286 63 * <p>
ohair@286 64 * A Binder instance is created using the factory method
ohair@286 65 * {@link JAXBContext#createBinder()} or {@link JAXBContext#createBinder(Class)}.
ohair@286 66 *
ohair@286 67 * <p>
ohair@286 68 * The template parameter, <code>XmlNode</code>, is the
ohair@286 69 * root interface/class for the XML infoset preserving representation.
ohair@286 70 * A Binder implementation is required to minimally support
ohair@286 71 * an <code>XmlNode</code> value of <code>org.w3c.dom.Node.class</code>.
ohair@286 72 * A Binder implementation can support alternative XML infoset
ohair@286 73 * preserving representations.
ohair@286 74 *
ohair@286 75 * @author
ohair@286 76 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 77 * Joseph Fialli
ohair@286 78 *
ohair@286 79 * @since JAXB 2.0
ohair@286 80 */
ohair@286 81 public abstract class Binder<XmlNode> {
ohair@286 82 /**
ohair@286 83 * Unmarshal XML infoset view to a JAXB object tree.
ohair@286 84 *
ohair@286 85 * <p>
ohair@286 86 * This method is similar to {@link Unmarshaller#unmarshal(Node)}
ohair@286 87 * with the addition of maintaining the association between XML nodes
ohair@286 88 * and the produced JAXB objects, enabling future update operations,
ohair@286 89 * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
ohair@286 90 *
ohair@286 91 * <p>
ohair@286 92 * When {@link #getSchema()} is non-null, <code>xmlNode</code>
ohair@286 93 * and its descendants is validated during this operation.
ohair@286 94 *
ohair@286 95 * <p>
ohair@286 96 * This method throws {@link UnmarshalException} when the Binder's
ohair@286 97 * {@link JAXBContext} does not have a mapping for the XML element name
ohair@286 98 * or the type, specifiable via <tt>@xsi:type</tt>, of <tt>xmlNode</tt>
ohair@286 99 * to a JAXB mapped class. The method {@link #unmarshal(Object, Class)}
ohair@286 100 * enables an application to specify the JAXB mapped class that
ohair@286 101 * the <tt>xmlNode</tt> should be mapped to.
ohair@286 102 *
ohair@286 103 * @param xmlNode
ohair@286 104 * the document/element to unmarshal XML data from.
ohair@286 105 *
ohair@286 106 * @return
ohair@286 107 * the newly created root object of the JAXB object tree.
ohair@286 108 *
ohair@286 109 * @throws JAXBException
ohair@286 110 * If any unexpected errors occur while unmarshalling
ohair@286 111 * @throws UnmarshalException
ohair@286 112 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 113 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 114 * <tt>Binder</tt> is unable to perform the XML to Java
ohair@286 115 * binding.
ohair@286 116 * @throws IllegalArgumentException
ohair@286 117 * If the node parameter is null
ohair@286 118 */
ohair@286 119 public abstract Object unmarshal( XmlNode xmlNode ) throws JAXBException;
ohair@286 120
ohair@286 121 /**
ohair@286 122 * Unmarshal XML root element by provided <tt>declaredType</tt>
ohair@286 123 * to a JAXB object tree.
ohair@286 124 *
ohair@286 125 * <p>
ohair@286 126 * Implements <a href="Unmarshaller.html#unmarshalByDeclaredType">Unmarshal by Declared Type</a>
ohair@286 127 *
ohair@286 128 * <p>
ohair@286 129 * This method is similar to {@link Unmarshaller#unmarshal(Node, Class)}
ohair@286 130 * with the addition of maintaining the association between XML nodes
ohair@286 131 * and the produced JAXB objects, enabling future update operations,
ohair@286 132 * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
ohair@286 133 *
ohair@286 134 * <p>
ohair@286 135 * When {@link #getSchema()} is non-null, <code>xmlNode</code>
ohair@286 136 * and its descendants is validated during this operation.
ohair@286 137 *
ohair@286 138 * @param xmlNode
ohair@286 139 * the document/element to unmarshal XML data from.
ohair@286 140 * @param declaredType
ohair@286 141 * appropriate JAXB mapped class to hold <tt>node</tt>'s XML data.
ohair@286 142 *
ohair@286 143 * @return
ohair@286 144 * <a href="JAXBElement.html">JAXB Element</a> representation
ohair@286 145 * of <tt>node</tt>
ohair@286 146 *
ohair@286 147 * @throws JAXBException
ohair@286 148 * If any unexpected errors occur while unmarshalling
ohair@286 149 * @throws UnmarshalException
ohair@286 150 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 151 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 152 * <tt>Binder</tt> is unable to perform the XML to Java
ohair@286 153 * binding.
ohair@286 154 * @throws IllegalArgumentException
ohair@286 155 * If any of the input parameters are null
ohair@286 156 * @since JAXB2.0
ohair@286 157 */
ohair@286 158 public abstract <T> JAXBElement<T>
ohair@286 159 unmarshal( XmlNode xmlNode, Class<T> declaredType )
ohair@286 160 throws JAXBException;
ohair@286 161
ohair@286 162 /**
ohair@286 163 * Marshal a JAXB object tree to a new XML document.
ohair@286 164 *
ohair@286 165 * <p>
ohair@286 166 * This method is similar to {@link Marshaller#marshal(Object, Node)}
ohair@286 167 * with the addition of maintaining the association between JAXB objects
ohair@286 168 * and the produced XML nodes,
ohair@286 169 * enabling future update operations such as
ohair@286 170 * {@link #updateXML(Object, Object)} or {@link #updateJAXB(Object)}.
ohair@286 171 *
ohair@286 172 * <p>
ohair@286 173 * When {@link #getSchema()} is non-null, the marshalled
ohair@286 174 * xml content is validated during this operation.
ohair@286 175 *
ohair@286 176 * @param jaxbObject
ohair@286 177 * The content tree to be marshalled.
ohair@286 178 * @param xmlNode
ohair@286 179 * The parameter must be a Node that accepts children.
ohair@286 180 *
ohair@286 181 * @throws JAXBException
ohair@286 182 * If any unexpected problem occurs during the marshalling.
ohair@286 183 * @throws MarshalException
ohair@286 184 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 185 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 186 * <tt>Binder</tt> is unable to marshal <tt>jaxbObject</tt> (or any
ohair@286 187 * object reachable from <tt>jaxbObject</tt>).
ohair@286 188 *
ohair@286 189 * @throws IllegalArgumentException
ohair@286 190 * If any of the method parameters are null
ohair@286 191 */
ohair@286 192 public abstract void marshal( Object jaxbObject, XmlNode xmlNode ) throws JAXBException;
ohair@286 193
ohair@286 194 /**
ohair@286 195 * Gets the XML element associated with the given JAXB object.
ohair@286 196 *
ohair@286 197 * <p>
ohair@286 198 * Once a JAXB object tree is associated with an XML fragment,
ohair@286 199 * this method enables navigation between the two trees.
ohair@286 200 *
ohair@286 201 * <p>
ohair@286 202 * An association between an XML element and a JAXB object is
ohair@286 203 * established by the bind methods and the update methods.
ohair@286 204 * Note that this association is partial; not all XML elements
ohair@286 205 * have associated JAXB objects, and not all JAXB objects have
ohair@286 206 * associated XML elements.
ohair@286 207 *
ohair@286 208 * @param jaxbObject An instance that is reachable from a prior
ohair@286 209 * call to a bind or update method that returned
ohair@286 210 * a JAXB object tree.
ohair@286 211 *
ohair@286 212 * @return
ohair@286 213 * null if the specified JAXB object is not known to this
ohair@286 214 * {@link Binder}, or if it is not associated with an
ohair@286 215 * XML element.
ohair@286 216 *
ohair@286 217 * @throws IllegalArgumentException
ohair@286 218 * If the jaxbObject parameter is null
ohair@286 219 */
ohair@286 220 public abstract XmlNode getXMLNode( Object jaxbObject );
ohair@286 221
ohair@286 222 /**
ohair@286 223 * Gets the JAXB object associated with the given XML element.
ohair@286 224 *
ohair@286 225 * <p>
ohair@286 226 * Once a JAXB object tree is associated with an XML fragment,
ohair@286 227 * this method enables navigation between the two trees.
ohair@286 228 *
ohair@286 229 * <p>
ohair@286 230 * An association between an XML element and a JAXB object is
ohair@286 231 * established by the unmarshal, marshal and update methods.
ohair@286 232 * Note that this association is partial; not all XML elements
ohair@286 233 * have associated JAXB objects, and not all JAXB objects have
ohair@286 234 * associated XML elements.
ohair@286 235 *
ohair@286 236 * @return
ohair@286 237 * null if the specified XML node is not known to this
ohair@286 238 * {@link Binder}, or if it is not associated with a
ohair@286 239 * JAXB object.
ohair@286 240 *
ohair@286 241 * @throws IllegalArgumentException
ohair@286 242 * If the node parameter is null
ohair@286 243 */
ohair@286 244 public abstract Object getJAXBNode( XmlNode xmlNode );
ohair@286 245
ohair@286 246 /**
ohair@286 247 * Takes an JAXB object and updates
ohair@286 248 * its associated XML node and its descendants.
ohair@286 249 *
ohair@286 250 * <p>
ohair@286 251 * This is a convenience method of:
ohair@286 252 * <pre>
ohair@286 253 * updateXML( jaxbObject, getXMLNode(jaxbObject));
ohair@286 254 * </pre>
ohair@286 255 *
ohair@286 256 * @throws JAXBException
ohair@286 257 * If any unexpected problem occurs updating corresponding XML content.
ohair@286 258 * @throws IllegalArgumentException
ohair@286 259 * If the jaxbObject parameter is null
ohair@286 260 */
ohair@286 261 public abstract XmlNode updateXML( Object jaxbObject ) throws JAXBException;
ohair@286 262
ohair@286 263 /**
ohair@286 264 * Changes in JAXB object tree are updated in its associated XML parse tree.
ohair@286 265 *
ohair@286 266 * <p>
ohair@286 267 * This operation can be thought of as an "in-place" marshalling.
ohair@286 268 * The difference is that instead of creating a whole new XML tree,
ohair@286 269 * this operation updates an existing tree while trying to preserve
ohair@286 270 * the XML as much as possible.
ohair@286 271 *
ohair@286 272 * <p>
ohair@286 273 * For example, unknown elements/attributes in XML that were not bound
ohair@286 274 * to JAXB will be left untouched (whereas a marshalling operation
ohair@286 275 * would create a new tree that doesn't contain any of those.)
ohair@286 276 *
ohair@286 277 * <p>
ohair@286 278 * As a side-effect, this operation updates the association between
ohair@286 279 * XML nodes and JAXB objects.
ohair@286 280 *
ohair@286 281 * @param jaxbObject root of potentially modified JAXB object tree
ohair@286 282 * @param xmlNode root of update target XML parse tree
ohair@286 283 *
ohair@286 284 * @return
ohair@286 285 * Returns the updated XML node. Typically, this is the same
ohair@286 286 * node you passed in as <i>xmlNode</i>, but it maybe
ohair@286 287 * a different object, for example when the tag name of the object
ohair@286 288 * has changed.
ohair@286 289 *
ohair@286 290 * @throws JAXBException
ohair@286 291 * If any unexpected problem occurs updating corresponding XML content.
ohair@286 292 * @throws IllegalArgumentException
ohair@286 293 * If any of the input parameters are null
ohair@286 294 */
ohair@286 295 public abstract XmlNode updateXML( Object jaxbObject, XmlNode xmlNode ) throws JAXBException;
ohair@286 296
ohair@286 297 /**
ohair@286 298 * Takes an XML node and updates its associated JAXB object and its descendants.
ohair@286 299 *
ohair@286 300 * <p>
ohair@286 301 * This operation can be thought of as an "in-place" unmarshalling.
ohair@286 302 * The difference is that instead of creating a whole new JAXB tree,
ohair@286 303 * this operation updates an existing tree, reusing as much JAXB objects
ohair@286 304 * as possible.
ohair@286 305 *
ohair@286 306 * <p>
ohair@286 307 * As a side-effect, this operation updates the association between
ohair@286 308 * XML nodes and JAXB objects.
ohair@286 309 *
ohair@286 310 * @return
ohair@286 311 * Returns the updated JAXB object. Typically, this is the same
ohair@286 312 * object that was returned from earlier
ohair@286 313 * {@link #marshal(Object,Object)} or
ohair@286 314 * {@link #updateJAXB(Object)} method invocation,
ohair@286 315 * but it maybe
ohair@286 316 * a different object, for example when the name of the XML
ohair@286 317 * element has changed.
ohair@286 318 *
ohair@286 319 * @throws JAXBException
ohair@286 320 * If any unexpected problem occurs updating corresponding JAXB mapped content.
ohair@286 321 * @throws IllegalArgumentException
ohair@286 322 * If node parameter is null
ohair@286 323 */
ohair@286 324 public abstract Object updateJAXB( XmlNode xmlNode ) throws JAXBException;
ohair@286 325
ohair@286 326
ohair@286 327 /**
ohair@286 328 * Specifies whether marshal, unmarshal and update methods
ohair@286 329 * performs validation on their XML content.
ohair@286 330 *
ohair@286 331 * @param schema set to null to disable validation.
ohair@286 332 *
ohair@286 333 * @see Unmarshaller#setSchema(Schema)
ohair@286 334 */
ohair@286 335 public abstract void setSchema( Schema schema );
ohair@286 336
ohair@286 337 /**
ohair@286 338 * Gets the last {@link Schema} object (including null) set by the
ohair@286 339 * {@link #setSchema(Schema)} method.
ohair@286 340 *
ohair@286 341 * @return the Schema object for validation or null if not present
ohair@286 342 */
ohair@286 343 public abstract Schema getSchema();
ohair@286 344
ohair@286 345 /**
ohair@286 346 * Allow an application to register a <tt>ValidationEventHandler</tt>.
ohair@286 347 * <p>
ohair@286 348 * The <tt>ValidationEventHandler</tt> will be called by the JAXB Provider
ohair@286 349 * if any validation errors are encountered during calls to any of the
ohair@286 350 * Binder unmarshal, marshal and update methods.
ohair@286 351 *
ohair@286 352 * <p>
ohair@286 353 * Calling this method with a null parameter will cause the Binder
ohair@286 354 * to revert back to the default default event handler.
ohair@286 355 *
ohair@286 356 * @param handler the validation event handler
ohair@286 357 * @throws JAXBException if an error was encountered while setting the
ohair@286 358 * event handler
ohair@286 359 */
ohair@286 360 public abstract void setEventHandler( ValidationEventHandler handler ) throws JAXBException;
ohair@286 361
ohair@286 362 /**
ohair@286 363 * Return the current event handler or the default event handler if one
ohair@286 364 * hasn't been set.
ohair@286 365 *
ohair@286 366 * @return the current ValidationEventHandler or the default event handler
ohair@286 367 * if it hasn't been set
ohair@286 368 * @throws JAXBException if an error was encountered while getting the
ohair@286 369 * current event handler
ohair@286 370 */
ohair@286 371 public abstract ValidationEventHandler getEventHandler() throws JAXBException;
ohair@286 372
ohair@286 373 /**
ohair@286 374 *
ohair@286 375 * Set the particular property in the underlying implementation of
ohair@286 376 * <tt>Binder</tt>. This method can only be used to set one of
ohair@286 377 * the standard JAXB defined unmarshal/marshal properties
ohair@286 378 * or a provider specific property for binder, unmarshal or marshal.
ohair@286 379 * Attempting to set an undefined property will result in
ohair@286 380 * a PropertyException being thrown. See
ohair@286 381 * <a href="Unmarshaller.html#supportedProps">Supported Unmarshal Properties</a>
ohair@286 382 * and
ohair@286 383 * <a href="Marshaller.html#supportedProps">Supported Marshal Properties</a>.
ohair@286 384 *
ohair@286 385 * @param name the name of the property to be set. This value can either
ohair@286 386 * be specified using one of the constant fields or a user
ohair@286 387 * supplied string.
ohair@286 388 * @param value the value of the property to be set
ohair@286 389 *
ohair@286 390 * @throws PropertyException when there is an error processing the given
ohair@286 391 * property or value
ohair@286 392 * @throws IllegalArgumentException
ohair@286 393 * If the name parameter is null
ohair@286 394 */
ohair@286 395 abstract public void setProperty( String name, Object value ) throws PropertyException;
ohair@286 396
ohair@286 397
ohair@286 398 /**
ohair@286 399 * Get the particular property in the underlying implementation of
ohair@286 400 * <tt>Binder</tt>. This method can only
ohair@286 401 * be used to get one of
ohair@286 402 * the standard JAXB defined unmarshal/marshal properties
ohair@286 403 * or a provider specific property for binder, unmarshal or marshal.
ohair@286 404 * Attempting to get an undefined property will result in
ohair@286 405 * a PropertyException being thrown. See
ohair@286 406 * <a href="Unmarshaller.html#supportedProps">Supported Unmarshal Properties</a>
ohair@286 407 * and
ohair@286 408 * <a href="Marshaller.html#supportedProps">Supported Marshal Properties</a>.
ohair@286 409 *
ohair@286 410 * @param name the name of the property to retrieve
ohair@286 411 * @return the value of the requested property
ohair@286 412 *
ohair@286 413 * @throws PropertyException
ohair@286 414 * when there is an error retrieving the given property or value
ohair@286 415 * property name
ohair@286 416 * @throws IllegalArgumentException
ohair@286 417 * If the name parameter is null
ohair@286 418 */
ohair@286 419 abstract public Object getProperty( String name ) throws PropertyException;
ohair@286 420
ohair@286 421 }

mercurial