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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2003, 2011, 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 javax.xml.bind.annotation.XmlRootElement;
ohair@286 29 import javax.xml.bind.annotation.adapters.XmlAdapter;
ohair@286 30 import javax.xml.bind.attachment.AttachmentMarshaller;
ohair@286 31 import javax.xml.validation.Schema;
ohair@286 32 import java.io.File;
ohair@286 33
ohair@286 34 /**
ohair@286 35 * <p>
ohair@286 36 * The <tt>Marshaller</tt> class is responsible for governing the process
ohair@286 37 * of serializing Java content trees back into XML data. It provides the basic
ohair@286 38 * marshalling methods:
ohair@286 39 *
ohair@286 40 * <p>
ohair@286 41 * <i>Assume the following setup code for all following code fragments:</i>
ohair@286 42 * <blockquote>
ohair@286 43 * <pre>
ohair@286 44 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
ohair@286 45 * Unmarshaller u = jc.createUnmarshaller();
ohair@286 46 * Object element = u.unmarshal( new File( "foo.xml" ) );
ohair@286 47 * Marshaller m = jc.createMarshaller();
ohair@286 48 * </pre>
ohair@286 49 * </blockquote>
ohair@286 50 *
ohair@286 51 * <p>
ohair@286 52 * Marshalling to a File:
ohair@286 53 * <blockquote>
ohair@286 54 * <pre>
ohair@286 55 * OutputStream os = new FileOutputStream( "nosferatu.xml" );
ohair@286 56 * m.marshal( element, os );
ohair@286 57 * </pre>
ohair@286 58 * </blockquote>
ohair@286 59 *
ohair@286 60 * <p>
ohair@286 61 * Marshalling to a SAX ContentHandler:
ohair@286 62 * <blockquote>
ohair@286 63 * <pre>
ohair@286 64 * // assume MyContentHandler instanceof ContentHandler
ohair@286 65 * m.marshal( element, new MyContentHandler() );
ohair@286 66 * </pre>
ohair@286 67 * </blockquote>
ohair@286 68 *
ohair@286 69 * <p>
ohair@286 70 * Marshalling to a DOM Node:
ohair@286 71 * <blockquote>
ohair@286 72 * <pre>
ohair@286 73 * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
ohair@286 74 * dbf.setNamespaceAware(true);
ohair@286 75 * DocumentBuilder db = dbf.newDocumentBuilder();
ohair@286 76 * Document doc = db.newDocument();
ohair@286 77 *
ohair@286 78 * m.marshal( element, doc );
ohair@286 79 * </pre>
ohair@286 80 * </blockquote>
ohair@286 81 *
ohair@286 82 * <p>
ohair@286 83 * Marshalling to a java.io.OutputStream:
ohair@286 84 * <blockquote>
ohair@286 85 * <pre>
ohair@286 86 * m.marshal( element, System.out );
ohair@286 87 * </pre>
ohair@286 88 * </blockquote>
ohair@286 89 *
ohair@286 90 * <p>
ohair@286 91 * Marshalling to a java.io.Writer:
ohair@286 92 * <blockquote>
ohair@286 93 * <pre>
ohair@286 94 * m.marshal( element, new PrintWriter( System.out ) );
ohair@286 95 * </pre>
ohair@286 96 * </blockquote>
ohair@286 97 *
ohair@286 98 * <p>
ohair@286 99 * Marshalling to a javax.xml.transform.SAXResult:
ohair@286 100 * <blockquote>
ohair@286 101 * <pre>
ohair@286 102 * // assume MyContentHandler instanceof ContentHandler
ohair@286 103 * SAXResult result = new SAXResult( new MyContentHandler() );
ohair@286 104 *
ohair@286 105 * m.marshal( element, result );
ohair@286 106 * </pre>
ohair@286 107 * </blockquote>
ohair@286 108 *
ohair@286 109 * <p>
ohair@286 110 * Marshalling to a javax.xml.transform.DOMResult:
ohair@286 111 * <blockquote>
ohair@286 112 * <pre>
ohair@286 113 * DOMResult result = new DOMResult();
ohair@286 114 *
ohair@286 115 * m.marshal( element, result );
ohair@286 116 * </pre>
ohair@286 117 * </blockquote>
ohair@286 118 *
ohair@286 119 * <p>
ohair@286 120 * Marshalling to a javax.xml.transform.StreamResult:
ohair@286 121 * <blockquote>
ohair@286 122 * <pre>
ohair@286 123 * StreamResult result = new StreamResult( System.out );
ohair@286 124 *
ohair@286 125 * m.marshal( element, result );
ohair@286 126 * </pre>
ohair@286 127 * </blockquote>
ohair@286 128 *
ohair@286 129 * <p>
ohair@286 130 * Marshalling to a javax.xml.stream.XMLStreamWriter:
ohair@286 131 * <blockquote>
ohair@286 132 * <pre>
ohair@286 133 * XMLStreamWriter xmlStreamWriter =
ohair@286 134 * XMLOutputFactory.newInstance().createXMLStreamWriter( ... );
ohair@286 135 *
ohair@286 136 * m.marshal( element, xmlStreamWriter );
ohair@286 137 * </pre>
ohair@286 138 * </blockquote>
ohair@286 139 *
ohair@286 140 * <p>
ohair@286 141 * Marshalling to a javax.xml.stream.XMLEventWriter:
ohair@286 142 * <blockquote>
ohair@286 143 * <pre>
ohair@286 144 * XMLEventWriter xmlEventWriter =
ohair@286 145 * XMLOutputFactory.newInstance().createXMLEventWriter( ... );
ohair@286 146 *
ohair@286 147 * m.marshal( element, xmlEventWriter );
ohair@286 148 * </pre>
ohair@286 149 * </blockquote>
ohair@286 150 *
ohair@286 151 * <p>
ohair@286 152 * <a name="elementMarshalling"></a>
ohair@286 153 * <b>Marshalling content tree rooted by a JAXB element</b><br>
ohair@286 154 * <blockquote>
ohair@286 155 * The first parameter of the overloaded
ohair@286 156 * <tt>Marshaller.marshal(java.lang.Object, ...)</tt> methods must be a
ohair@286 157 * JAXB element as computed by
ohair@286 158 * {@link JAXBIntrospector#isElement(java.lang.Object)};
ohair@286 159 * otherwise, a <tt>Marshaller.marshal</tt> method must throw a
ohair@286 160 * {@link MarshalException}. There exist two mechanisms
ohair@286 161 * to enable marshalling an instance that is not a JAXB element.
ohair@286 162 * One method is to wrap the instance as a value of a {@link JAXBElement},
ohair@286 163 * and pass the wrapper element as the first parameter to
ohair@286 164 * a <tt>Marshaller.marshal</tt> method. For java to schema binding, it
ohair@286 165 * is also possible to simply annotate the instance's class with
ohair@286 166 * &#64;{@link XmlRootElement}.
ohair@286 167 * </blockquote>
ohair@286 168 *
ohair@286 169 * <p>
ohair@286 170 * <b>Encoding</b><br>
ohair@286 171 * <blockquote>
ohair@286 172 * By default, the Marshaller will use UTF-8 encoding when generating XML data
ohair@286 173 * to a <tt>java.io.OutputStream</tt>, or a <tt>java.io.Writer</tt>. Use the
ohair@286 174 * {@link #setProperty(String,Object) setProperty} API to change the output
ohair@286 175 * encoding used during these marshal operations. Client applications are
ohair@286 176 * expected to supply a valid character encoding name as defined in the
ohair@286 177 * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#charencoding">W3C XML 1.0
ohair@286 178 * Recommendation</a> and supported by your
ohair@286 179 * <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">
ohair@286 180 * Java Platform</a>.
ohair@286 181 * </blockquote>
ohair@286 182 *
ohair@286 183 * <p>
ohair@286 184 * <b>Validation and Well-Formedness</b><br>
ohair@286 185 * <blockquote>
ohair@286 186 * <p>
ohair@286 187 * Client applications are not required to validate the Java content tree prior
ohair@286 188 * to calling any of the marshal API's. Furthermore, there is no requirement
ohair@286 189 * that the Java content tree be valid with respect to its original schema in
ohair@286 190 * order to marshal it back into XML data. Different JAXB Providers will
ohair@286 191 * support marshalling invalid Java content trees at varying levels, however
ohair@286 192 * all JAXB Providers must be able to marshal a valid content tree back to
ohair@286 193 * XML data. A JAXB Provider must throw a <tt>MarshalException</tt> when it
ohair@286 194 * is unable to complete the marshal operation due to invalid content. Some
ohair@286 195 * JAXB Providers will fully allow marshalling invalid content, others will fail
ohair@286 196 * on the first validation error.
ohair@286 197 * <p>
ohair@286 198 * Even when schema validation is not explictly enabled for the marshal operation,
ohair@286 199 * it is possible that certain types of validation events will be detected
ohair@286 200 * during the operation. Validation events will be reported to the registered
ohair@286 201 * event handler. If the client application has not registered an event handler
ohair@286 202 * prior to invoking one of the marshal API's, then events will be delivered to
ohair@286 203 * a default event handler which will terminate the marshal operation after
ohair@286 204 * encountering the first error or fatal error. Note that for JAXB 2.0 and
ohair@286 205 * later versions, {@link javax.xml.bind.helpers.DefaultValidationEventHandler} is
ohair@286 206 * no longer used.
ohair@286 207 *
ohair@286 208 * </blockquote>
ohair@286 209 *
ohair@286 210 * <p>
ohair@286 211 * <a name="supportedProps"></a>
ohair@286 212 * <b>Supported Properties</b><br>
ohair@286 213 * <blockquote>
ohair@286 214 * <p>
ohair@286 215 * All JAXB Providers are required to support the following set of properties.
ohair@286 216 * Some providers may support additional properties.
ohair@286 217 * <dl>
ohair@286 218 * <dt><tt>jaxb.encoding</tt> - value must be a java.lang.String</dt>
ohair@286 219 * <dd>The output encoding to use when marshalling the XML data. The
ohair@286 220 * Marshaller will use "UTF-8" by default if this property is not
ohair@286 221 * specified.</dd>
ohair@286 222 * <dt><tt>jaxb.formatted.output</tt> - value must be a java.lang.Boolean</dt>
ohair@286 223 * <dd>This property controls whether or not the Marshaller will format
ohair@286 224 * the resulting XML data with line breaks and indentation. A
ohair@286 225 * true value for this property indicates human readable indented
ohair@286 226 * xml data, while a false value indicates unformatted xml data.
ohair@286 227 * The Marshaller will default to false (unformatted) if this
ohair@286 228 * property is not specified.</dd>
ohair@286 229 * <dt><tt>jaxb.schemaLocation</tt> - value must be a java.lang.String</dt>
ohair@286 230 * <dd>This property allows the client application to specify an
ohair@286 231 * xsi:schemaLocation attribute in the generated XML data. The format of
ohair@286 232 * the schemaLocation attribute value is discussed in an easy to
ohair@286 233 * understand, non-normative form in
ohair@286 234 * <a href="http://www.w3.org/TR/xmlschema-0/#schemaLocation">Section 5.6
ohair@286 235 * of the W3C XML Schema Part 0: Primer</a> and specified in
ohair@286 236 * <a href="http://www.w3.org/TR/xmlschema-1/#Instance_Document_Constructions">
ohair@286 237 * Section 2.6 of the W3C XML Schema Part 1: Structures</a>.</dd>
ohair@286 238 * <dt><tt>jaxb.noNamespaceSchemaLocation</tt> - value must be a java.lang.String</dt>
ohair@286 239 * <dd>This property allows the client application to specify an
ohair@286 240 * xsi:noNamespaceSchemaLocation attribute in the generated XML
ohair@286 241 * data. The format of the schemaLocation attribute value is discussed in
ohair@286 242 * an easy to understand, non-normative form in
ohair@286 243 * <a href="http://www.w3.org/TR/xmlschema-0/#schemaLocation">Section 5.6
ohair@286 244 * of the W3C XML Schema Part 0: Primer</a> and specified in
ohair@286 245 * <a href="http://www.w3.org/TR/xmlschema-1/#Instance_Document_Constructions">
ohair@286 246 * Section 2.6 of the W3C XML Schema Part 1: Structures</a>.</dd>
ohair@286 247 * <dt><tt>jaxb.fragment</tt> - value must be a java.lang.Boolean</dt>
ohair@286 248 * <dd>This property determines whether or not document level events will be
ohair@286 249 * generated by the Marshaller. If the property is not specified, the
ohair@286 250 * default is <tt>false</tt>. This property has different implications depending
ohair@286 251 * on which marshal api you are using - when this property is set to true:<br>
ohair@286 252 * <ul>
ohair@286 253 * <li>{@link #marshal(Object,org.xml.sax.ContentHandler) marshal(Object,ContentHandler)} - the Marshaller won't
ohair@286 254 * invoke {@link org.xml.sax.ContentHandler#startDocument()} and
ohair@286 255 * {@link org.xml.sax.ContentHandler#endDocument()}.</li>
ohair@286 256 * <li>{@link #marshal(Object,org.w3c.dom.Node) marshal(Object,Node)} - the property has no effect on this
ohair@286 257 * API.</li>
ohair@286 258 * <li>{@link #marshal(Object,java.io.OutputStream) marshal(Object,OutputStream)} - the Marshaller won't
ohair@286 259 * generate an xml declaration.</li>
ohair@286 260 * <li>{@link #marshal(Object,java.io.Writer) marshal(Object,Writer)} - the Marshaller won't
ohair@286 261 * generate an xml declaration.</li>
ohair@286 262 * <li>{@link #marshal(Object,javax.xml.transform.Result) marshal(Object,Result)} - depends on the kind of
ohair@286 263 * Result object, see semantics for Node, ContentHandler, and Stream APIs</li>
ohair@286 264 * <li>{@link #marshal(Object,javax.xml.stream.XMLEventWriter) marshal(Object,XMLEventWriter)} - the
ohair@286 265 * Marshaller will not generate {@link javax.xml.stream.events.XMLEvent#START_DOCUMENT} and
ohair@286 266 * {@link javax.xml.stream.events.XMLEvent#END_DOCUMENT} events.</li>
ohair@286 267 * <li>{@link #marshal(Object,javax.xml.stream.XMLStreamWriter) marshal(Object,XMLStreamWriter)} - the
ohair@286 268 * Marshaller will not generate {@link javax.xml.stream.events.XMLEvent#START_DOCUMENT} and
ohair@286 269 * {@link javax.xml.stream.events.XMLEvent#END_DOCUMENT} events.</li>
ohair@286 270 * </ul>
ohair@286 271 * </dd>
ohair@286 272 * </dl>
ohair@286 273 * </blockquote>
ohair@286 274 *
ohair@286 275 * <p>
ohair@286 276 * <a name="marshalEventCallback"></a>
ohair@286 277 * <b>Marshal Event Callbacks</b><br>
ohair@286 278 * <blockquote>
ohair@286 279 * "The {@link Marshaller} provides two styles of callback mechanisms
ohair@286 280 * that allow application specific processing during key points in the
ohair@286 281 * unmarshalling process. In 'class defined' event callbacks, application
ohair@286 282 * specific code placed in JAXB mapped classes is triggered during
ohair@286 283 * marshalling. 'External listeners' allow for centralized processing
ohair@286 284 * of marshal events in one callback method rather than by type event callbacks.
ohair@286 285 *
ohair@286 286 * <p>
ohair@286 287 * Class defined event callback methods allow any JAXB mapped class to specify
ohair@286 288 * its own specific callback methods by defining methods with the following method signatures:
ohair@286 289 * <blockquote>
ohair@286 290 * <pre>
ohair@286 291 * // Invoked by Marshaller after it has created an instance of this object.
ohair@286 292 * boolean beforeMarshal(Marshaller);
ohair@286 293 *
ohair@286 294 * // Invoked by Marshaller after it has marshalled all properties of this object.
ohair@286 295 * void afterMmarshal(Marshaller);
ohair@286 296 * </pre>
ohair@286 297 * </blockquote>
ohair@286 298 * The class defined event callback methods should be used when the callback method requires
ohair@286 299 * access to non-public methods and/or fields of the class.
ohair@286 300 * <p>
ohair@286 301 * The external listener callback mechanism enables the registration of a {@link Listener}
ohair@286 302 * instance with a {@link Marshaller#setListener(Listener)}. The external listener receives all callback events,
ohair@286 303 * allowing for more centralized processing than per class defined callback methods.
ohair@286 304 * <p>
ohair@286 305 * The 'class defined' and external listener event callback methods are independent of each other,
ohair@286 306 * both can be called for one event. The invocation ordering when both listener callback methods exist is
ohair@286 307 * defined in {@link Listener#beforeMarshal(Object)} and {@link Listener#afterMarshal(Object)}.
ohair@286 308 * <p>
ohair@286 309 * An event callback method throwing an exception terminates the current marshal process.
ohair@286 310 * </blockquote>
ohair@286 311 *
ohair@286 312 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
ohair@286 313 * @see JAXBContext
ohair@286 314 * @see Validator
ohair@286 315 * @see Unmarshaller
ohair@286 316 * @since JAXB1.0
ohair@286 317 */
ohair@286 318 public interface Marshaller {
ohair@286 319
ohair@286 320 /**
ohair@286 321 * The name of the property used to specify the output encoding in
ohair@286 322 * the marshalled XML data.
ohair@286 323 */
ohair@286 324 public static final String JAXB_ENCODING =
ohair@286 325 "jaxb.encoding";
ohair@286 326
ohair@286 327 /**
ohair@286 328 * The name of the property used to specify whether or not the marshalled
ohair@286 329 * XML data is formatted with linefeeds and indentation.
ohair@286 330 */
ohair@286 331 public static final String JAXB_FORMATTED_OUTPUT =
ohair@286 332 "jaxb.formatted.output";
ohair@286 333
ohair@286 334 /**
ohair@286 335 * The name of the property used to specify the xsi:schemaLocation
ohair@286 336 * attribute value to place in the marshalled XML output.
ohair@286 337 */
ohair@286 338 public static final String JAXB_SCHEMA_LOCATION =
ohair@286 339 "jaxb.schemaLocation";
ohair@286 340
ohair@286 341 /**
ohair@286 342 * The name of the property used to specify the
ohair@286 343 * xsi:noNamespaceSchemaLocation attribute value to place in the marshalled
ohair@286 344 * XML output.
ohair@286 345 */
ohair@286 346 public static final String JAXB_NO_NAMESPACE_SCHEMA_LOCATION =
ohair@286 347 "jaxb.noNamespaceSchemaLocation";
ohair@286 348
ohair@286 349 /**
ohair@286 350 * The name of the property used to specify whether or not the marshaller
ohair@286 351 * will generate document level events (ie calling startDocument or endDocument).
ohair@286 352 */
ohair@286 353 public static final String JAXB_FRAGMENT =
ohair@286 354 "jaxb.fragment";
ohair@286 355
ohair@286 356 /**
ohair@286 357 * Marshal the content tree rooted at <tt>jaxbElement</tt> into the specified
ohair@286 358 * <tt>javax.xml.transform.Result</tt>.
ohair@286 359 *
ohair@286 360 * <p>
ohair@286 361 * All JAXB Providers must at least support
ohair@286 362 * {@link javax.xml.transform.dom.DOMResult},
ohair@286 363 * {@link javax.xml.transform.sax.SAXResult}, and
ohair@286 364 * {@link javax.xml.transform.stream.StreamResult}. It can
ohair@286 365 * support other derived classes of <tt>Result</tt> as well.
ohair@286 366 *
ohair@286 367 * @param jaxbElement
ohair@286 368 * The root of content tree to be marshalled.
ohair@286 369 * @param result
ohair@286 370 * XML will be sent to this Result
ohair@286 371 *
ohair@286 372 * @throws JAXBException
ohair@286 373 * If any unexpected problem occurs during the marshalling.
ohair@286 374 * @throws MarshalException
ohair@286 375 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 376 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 377 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
ohair@286 378 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
ohair@286 379 * Marshalling a JAXB element</a>.
ohair@286 380 * @throws IllegalArgumentException
ohair@286 381 * If any of the method parameters are null
ohair@286 382 */
ohair@286 383 public void marshal( Object jaxbElement, javax.xml.transform.Result result )
ohair@286 384 throws JAXBException;
ohair@286 385
ohair@286 386 /**
ohair@286 387 * Marshal the content tree rooted at <tt>jaxbElement</tt> into an output stream.
ohair@286 388 *
ohair@286 389 * @param jaxbElement
ohair@286 390 * The root of content tree to be marshalled.
ohair@286 391 * @param os
ohair@286 392 * XML will be added to this stream.
ohair@286 393 *
ohair@286 394 * @throws JAXBException
ohair@286 395 * If any unexpected problem occurs during the marshalling.
ohair@286 396 * @throws MarshalException
ohair@286 397 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 398 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 399 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
ohair@286 400 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
ohair@286 401 * Marshalling a JAXB element</a>.
ohair@286 402 * @throws IllegalArgumentException
ohair@286 403 * If any of the method parameters are null
ohair@286 404 */
ohair@286 405 public void marshal( Object jaxbElement, java.io.OutputStream os )
ohair@286 406 throws JAXBException;
ohair@286 407
ohair@286 408 /**
ohair@286 409 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a file.
ohair@286 410 *
ohair@286 411 * @param jaxbElement
ohair@286 412 * The root of content tree to be marshalled.
ohair@286 413 * @param output
ohair@286 414 * File to be written. If this file already exists, it will be overwritten.
ohair@286 415 *
ohair@286 416 * @throws JAXBException
ohair@286 417 * If any unexpected problem occurs during the marshalling.
ohair@286 418 * @throws MarshalException
ohair@286 419 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 420 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 421 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
ohair@286 422 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
ohair@286 423 * Marshalling a JAXB element</a>.
ohair@286 424 * @throws IllegalArgumentException
ohair@286 425 * If any of the method parameters are null
ohair@286 426 * @since JAXB2.1
ohair@286 427 */
ohair@286 428 public void marshal( Object jaxbElement, File output )
ohair@286 429 throws JAXBException;
ohair@286 430
ohair@286 431 /**
ohair@286 432 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a Writer.
ohair@286 433 *
ohair@286 434 * @param jaxbElement
ohair@286 435 * The root of content tree to be marshalled.
ohair@286 436 * @param writer
ohair@286 437 * XML will be sent to this writer.
ohair@286 438 *
ohair@286 439 * @throws JAXBException
ohair@286 440 * If any unexpected problem occurs during the marshalling.
ohair@286 441 * @throws MarshalException
ohair@286 442 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 443 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 444 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
ohair@286 445 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
ohair@286 446 * Marshalling a JAXB element</a>.
ohair@286 447 * @throws IllegalArgumentException
ohair@286 448 * If any of the method parameters are null
ohair@286 449 */
ohair@286 450 public void marshal( Object jaxbElement, java.io.Writer writer )
ohair@286 451 throws JAXBException;
ohair@286 452
ohair@286 453 /**
ohair@286 454 * Marshal the content tree rooted at <tt>jaxbElement</tt> into SAX2 events.
ohair@286 455 *
ohair@286 456 * @param jaxbElement
ohair@286 457 * The root of content tree to be marshalled.
ohair@286 458 * @param handler
ohair@286 459 * XML will be sent to this handler as SAX2 events.
ohair@286 460 *
ohair@286 461 * @throws JAXBException
ohair@286 462 * If any unexpected problem occurs during the marshalling.
ohair@286 463 * @throws MarshalException
ohair@286 464 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 465 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 466 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
ohair@286 467 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
ohair@286 468 * Marshalling a JAXB element</a>.
ohair@286 469 * @throws IllegalArgumentException
ohair@286 470 * If any of the method parameters are null
ohair@286 471 */
ohair@286 472 public void marshal( Object jaxbElement, org.xml.sax.ContentHandler handler )
ohair@286 473 throws JAXBException;
ohair@286 474
ohair@286 475 /**
ohair@286 476 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a DOM tree.
ohair@286 477 *
ohair@286 478 * @param jaxbElement
ohair@286 479 * The content tree to be marshalled.
ohair@286 480 * @param node
ohair@286 481 * DOM nodes will be added as children of this node.
ohair@286 482 * This parameter must be a Node that accepts children
ohair@286 483 * ({@link org.w3c.dom.Document},
ohair@286 484 * {@link org.w3c.dom.DocumentFragment}, or
ohair@286 485 * {@link org.w3c.dom.Element})
ohair@286 486 *
ohair@286 487 * @throws JAXBException
ohair@286 488 * If any unexpected problem occurs during the marshalling.
ohair@286 489 * @throws MarshalException
ohair@286 490 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 491 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 492 * <tt>Marshaller</tt> is unable to marshal <tt>jaxbElement</tt> (or any
ohair@286 493 * object reachable from <tt>jaxbElement</tt>). See <a href="#elementMarshalling">
ohair@286 494 * Marshalling a JAXB element</a>.
ohair@286 495 * @throws IllegalArgumentException
ohair@286 496 * If any of the method parameters are null
ohair@286 497 */
ohair@286 498 public void marshal( Object jaxbElement, org.w3c.dom.Node node )
ohair@286 499 throws JAXBException;
ohair@286 500
ohair@286 501 /**
ohair@286 502 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a
ohair@286 503 * {@link javax.xml.stream.XMLStreamWriter}.
ohair@286 504 *
ohair@286 505 * @param jaxbElement
ohair@286 506 * The content tree to be marshalled.
ohair@286 507 * @param writer
ohair@286 508 * XML will be sent to this writer.
ohair@286 509 *
ohair@286 510 * @throws JAXBException
ohair@286 511 * If any unexpected problem occurs during the marshalling.
ohair@286 512 * @throws MarshalException
ohair@286 513 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 514 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 515 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
ohair@286 516 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
ohair@286 517 * Marshalling a JAXB element</a>.
ohair@286 518 * @throws IllegalArgumentException
ohair@286 519 * If any of the method parameters are null
ohair@286 520 * @since JAXB 2.0
ohair@286 521 */
ohair@286 522 public void marshal( Object jaxbElement, javax.xml.stream.XMLStreamWriter writer )
ohair@286 523 throws JAXBException;
ohair@286 524
ohair@286 525 /**
ohair@286 526 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a
ohair@286 527 * {@link javax.xml.stream.XMLEventWriter}.
ohair@286 528 *
ohair@286 529 * @param jaxbElement
ohair@286 530 * The content tree rooted at jaxbElement to be marshalled.
ohair@286 531 * @param writer
ohair@286 532 * XML will be sent to this writer.
ohair@286 533 *
ohair@286 534 * @throws JAXBException
ohair@286 535 * If any unexpected problem occurs during the marshalling.
ohair@286 536 * @throws MarshalException
ohair@286 537 * If the {@link ValidationEventHandler ValidationEventHandler}
ohair@286 538 * returns false from its <tt>handleEvent</tt> method or the
ohair@286 539 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
ohair@286 540 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
ohair@286 541 * Marshalling a JAXB element</a>.
ohair@286 542 * @throws IllegalArgumentException
ohair@286 543 * If any of the method parameters are null
ohair@286 544 * @since JAXB 2.0
ohair@286 545 */
ohair@286 546 public void marshal( Object jaxbElement, javax.xml.stream.XMLEventWriter writer )
ohair@286 547 throws JAXBException;
ohair@286 548
ohair@286 549 /**
ohair@286 550 * Get a DOM tree view of the content tree(Optional).
ohair@286 551 *
ohair@286 552 * If the returned DOM tree is updated, these changes are also
ohair@286 553 * visible in the content tree.
ohair@286 554 * Use {@link #marshal(Object, org.w3c.dom.Node)} to force
ohair@286 555 * a deep copy of the content tree to a DOM representation.
ohair@286 556 *
ohair@286 557 * @param contentTree - JAXB Java representation of XML content
ohair@286 558 *
ohair@286 559 * @return the DOM tree view of the contentTree
ohair@286 560 *
ohair@286 561 * @throws UnsupportedOperationException
ohair@286 562 * If the JAXB provider implementation does not support a
ohair@286 563 * DOM view of the content tree
ohair@286 564 *
ohair@286 565 * @throws IllegalArgumentException
ohair@286 566 * If any of the method parameters are null
ohair@286 567 *
ohair@286 568 * @throws JAXBException
ohair@286 569 * If any unexpected problem occurs
ohair@286 570 *
ohair@286 571 */
ohair@286 572 public org.w3c.dom.Node getNode( java.lang.Object contentTree )
ohair@286 573 throws JAXBException;
ohair@286 574
ohair@286 575 /**
ohair@286 576 * Set the particular property in the underlying implementation of
ohair@286 577 * <tt>Marshaller</tt>. This method can only be used to set one of
ohair@286 578 * the standard JAXB defined properties above or a provider specific
ohair@286 579 * property. Attempting to set an undefined property will result in
ohair@286 580 * a PropertyException being thrown. See <a href="#supportedProps">
ohair@286 581 * Supported Properties</a>.
ohair@286 582 *
ohair@286 583 * @param name the name of the property to be set. This value can either
ohair@286 584 * be specified using one of the constant fields or a user
ohair@286 585 * supplied string.
ohair@286 586 * @param value the value of the property to be set
ohair@286 587 *
ohair@286 588 * @throws PropertyException when there is an error processing the given
ohair@286 589 * property or value
ohair@286 590 * @throws IllegalArgumentException
ohair@286 591 * If the name parameter is null
ohair@286 592 */
ohair@286 593 public void setProperty( String name, Object value )
ohair@286 594 throws PropertyException;
ohair@286 595
ohair@286 596 /**
ohair@286 597 * Get the particular property in the underlying implementation of
ohair@286 598 * <tt>Marshaller</tt>. This method can only be used to get one of
ohair@286 599 * the standard JAXB defined properties above or a provider specific
ohair@286 600 * property. Attempting to get an undefined property will result in
ohair@286 601 * a PropertyException being thrown. See <a href="#supportedProps">
ohair@286 602 * Supported Properties</a>.
ohair@286 603 *
ohair@286 604 * @param name the name of the property to retrieve
ohair@286 605 * @return the value of the requested property
ohair@286 606 *
ohair@286 607 * @throws PropertyException
ohair@286 608 * when there is an error retrieving the given property or value
ohair@286 609 * property name
ohair@286 610 * @throws IllegalArgumentException
ohair@286 611 * If the name parameter is null
ohair@286 612 */
ohair@286 613 public Object getProperty( String name ) throws PropertyException;
ohair@286 614
ohair@286 615 /**
ohair@286 616 * Allow an application to register a validation event handler.
ohair@286 617 * <p>
ohair@286 618 * The validation event handler will be called by the JAXB Provider if any
ohair@286 619 * validation errors are encountered during calls to any of the marshal
ohair@286 620 * API's. If the client application does not register a validation event
ohair@286 621 * handler before invoking one of the marshal methods, then validation
ohair@286 622 * events will be handled by the default event handler which will terminate
ohair@286 623 * the marshal operation after the first error or fatal error is encountered.
ohair@286 624 * <p>
ohair@286 625 * Calling this method with a null parameter will cause the Marshaller
ohair@286 626 * to revert back to the default default event handler.
ohair@286 627 *
ohair@286 628 * @param handler the validation event handler
ohair@286 629 * @throws JAXBException if an error was encountered while setting the
ohair@286 630 * event handler
ohair@286 631 */
ohair@286 632 public void setEventHandler( ValidationEventHandler handler )
ohair@286 633 throws JAXBException;
ohair@286 634
ohair@286 635 /**
ohair@286 636 * Return the current event handler or the default event handler if one
ohair@286 637 * hasn't been set.
ohair@286 638 *
ohair@286 639 * @return the current ValidationEventHandler or the default event handler
ohair@286 640 * if it hasn't been set
ohair@286 641 * @throws JAXBException if an error was encountered while getting the
ohair@286 642 * current event handler
ohair@286 643 */
ohair@286 644 public ValidationEventHandler getEventHandler()
ohair@286 645 throws JAXBException;
ohair@286 646
ohair@286 647
ohair@286 648
ohair@286 649 /**
ohair@286 650 * Associates a configured instance of {@link XmlAdapter} with this marshaller.
ohair@286 651 *
ohair@286 652 * <p>
ohair@286 653 * This is a convenience method that invokes <code>setAdapter(adapter.getClass(),adapter);</code>.
ohair@286 654 *
ohair@286 655 * @see #setAdapter(Class,XmlAdapter)
ohair@286 656 * @throws IllegalArgumentException
ohair@286 657 * if the adapter parameter is null.
ohair@286 658 * @throws UnsupportedOperationException
ohair@286 659 * if invoked agains a JAXB 1.0 implementation.
ohair@286 660 * @since JAXB 2.0
ohair@286 661 */
ohair@286 662 public void setAdapter( XmlAdapter adapter );
ohair@286 663
ohair@286 664 /**
ohair@286 665 * Associates a configured instance of {@link XmlAdapter} with this marshaller.
ohair@286 666 *
ohair@286 667 * <p>
ohair@286 668 * Every marshaller internally maintains a
ohair@286 669 * {@link java.util.Map}&lt;{@link Class},{@link XmlAdapter}>,
ohair@286 670 * which it uses for marshalling classes whose fields/methods are annotated
ohair@286 671 * with {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}.
ohair@286 672 *
ohair@286 673 * <p>
ohair@286 674 * This method allows applications to use a configured instance of {@link XmlAdapter}.
ohair@286 675 * When an instance of an adapter is not given, a marshaller will create
ohair@286 676 * one by invoking its default constructor.
ohair@286 677 *
ohair@286 678 * @param type
ohair@286 679 * The type of the adapter. The specified instance will be used when
ohair@286 680 * {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter#value()}
ohair@286 681 * refers to this type.
ohair@286 682 * @param adapter
ohair@286 683 * The instance of the adapter to be used. If null, it will un-register
ohair@286 684 * the current adapter set for this type.
ohair@286 685 * @throws IllegalArgumentException
ohair@286 686 * if the type parameter is null.
ohair@286 687 * @throws UnsupportedOperationException
ohair@286 688 * if invoked agains a JAXB 1.0 implementation.
ohair@286 689 * @since JAXB 2.0
ohair@286 690 */
ohair@286 691 public <A extends XmlAdapter> void setAdapter( Class<A> type, A adapter );
ohair@286 692
ohair@286 693 /**
ohair@286 694 * Gets the adapter associated with the specified type.
ohair@286 695 *
ohair@286 696 * This is the reverse operation of the {@link #setAdapter} method.
ohair@286 697 *
ohair@286 698 * @throws IllegalArgumentException
ohair@286 699 * if the type parameter is null.
ohair@286 700 * @throws UnsupportedOperationException
ohair@286 701 * if invoked agains a JAXB 1.0 implementation.
ohair@286 702 * @since JAXB 2.0
ohair@286 703 */
ohair@286 704 public <A extends XmlAdapter> A getAdapter( Class<A> type );
ohair@286 705
ohair@286 706
ohair@286 707 /**
ohair@286 708 * <p>Associate a context that enables binary data within an XML document
ohair@286 709 * to be transmitted as XML-binary optimized attachment.
ohair@286 710 * The attachment is referenced from the XML document content model
ohair@286 711 * by content-id URIs(cid) references stored within the xml document.
ohair@286 712 *
ohair@286 713 * @throws IllegalStateException if attempt to concurrently call this
ohair@286 714 * method during a marshal operation.
ohair@286 715 */
ohair@286 716 void setAttachmentMarshaller(AttachmentMarshaller am);
ohair@286 717
ohair@286 718 AttachmentMarshaller getAttachmentMarshaller();
ohair@286 719
ohair@286 720 /**
ohair@286 721 * Specify the JAXP 1.3 {@link javax.xml.validation.Schema Schema}
ohair@286 722 * object that should be used to validate subsequent marshal operations
ohair@286 723 * against. Passing null into this method will disable validation.
ohair@286 724 *
ohair@286 725 * <p>
ohair@286 726 * This method allows the caller to validate the marshalled XML as it's marshalled.
ohair@286 727 *
ohair@286 728 * <p>
ohair@286 729 * Initially this property is set to <tt>null</tt>.
ohair@286 730 *
ohair@286 731 * @param schema Schema object to validate marshal operations against or null to disable validation
ohair@286 732 * @throws UnsupportedOperationException could be thrown if this method is
ohair@286 733 * invoked on an Marshaller created from a JAXBContext referencing
ohair@286 734 * JAXB 1.0 mapped classes
ohair@286 735 * @since JAXB2.0
ohair@286 736 */
ohair@286 737 public void setSchema( Schema schema );
ohair@286 738
ohair@286 739 /**
ohair@286 740 * Get the JAXP 1.3 {@link javax.xml.validation.Schema Schema} object
ohair@286 741 * being used to perform marshal-time validation. If there is no
ohair@286 742 * Schema set on the marshaller, then this method will return null
ohair@286 743 * indicating that marshal-time validation will not be performed.
ohair@286 744 *
ohair@286 745 * @return the Schema object being used to perform marshal-time
ohair@286 746 * validation or null if not present.
ohair@286 747 * @throws UnsupportedOperationException could be thrown if this method is
ohair@286 748 * invoked on an Marshaller created from a JAXBContext referencing
ohair@286 749 * JAXB 1.0 mapped classes
ohair@286 750 * @since JAXB2.0
ohair@286 751 */
ohair@286 752 public Schema getSchema();
ohair@286 753
ohair@286 754 /**
ohair@286 755 * <p/>
ohair@286 756 * Register an instance of an implementation of this class with a {@link Marshaller} to externally listen
ohair@286 757 * for marshal events.
ohair@286 758 * <p/>
ohair@286 759 * <p/>
ohair@286 760 * This class enables pre and post processing of each marshalled object.
ohair@286 761 * The event callbacks are called when marshalling from an instance that maps to an xml element or
ohair@286 762 * complex type definition. The event callbacks are not called when marshalling from an instance of a
ohair@286 763 * Java datatype that represents a simple type definition.
ohair@286 764 * <p/>
ohair@286 765 * <p/>
ohair@286 766 * External listener is one of two different mechanisms for defining marshal event callbacks.
ohair@286 767 * See <a href="Marshaller.html#marshalEventCallback">Marshal Event Callbacks</a> for an overview.
ohair@286 768 *
ohair@286 769 * @see Marshaller#setListener(Listener)
ohair@286 770 * @see Marshaller#getListener()
ohair@286 771 * @since JAXB2.0
ohair@286 772 */
ohair@286 773 public static abstract class Listener {
ohair@286 774 /**
ohair@286 775 * <p/>
ohair@286 776 * Callback method invoked before marshalling from <tt>source</tt> to XML.
ohair@286 777 * <p/>
ohair@286 778 * <p/>
ohair@286 779 * This method is invoked just before marshalling process starts to marshal <tt>source</tt>.
ohair@286 780 * Note that if the class of <tt>source</tt> defines its own <tt>beforeMarshal</tt> method,
ohair@286 781 * the class specific callback method is invoked just before this method is invoked.
ohair@286 782 *
ohair@286 783 * @param source instance of JAXB mapped class prior to marshalling from it.
ohair@286 784 */
ohair@286 785 public void beforeMarshal(Object source) {
ohair@286 786 }
ohair@286 787
ohair@286 788 /**
ohair@286 789 * <p/>
ohair@286 790 * Callback method invoked after marshalling <tt>source</tt> to XML.
ohair@286 791 * <p/>
ohair@286 792 * <p/>
ohair@286 793 * This method is invoked after <tt>source</tt> and all its descendants have been marshalled.
ohair@286 794 * Note that if the class of <tt>source</tt> defines its own <tt>afterMarshal</tt> method,
ohair@286 795 * the class specific callback method is invoked just before this method is invoked.
ohair@286 796 *
ohair@286 797 * @param source instance of JAXB mapped class after marshalling it.
ohair@286 798 */
ohair@286 799 public void afterMarshal(Object source) {
ohair@286 800 }
ohair@286 801 }
ohair@286 802
ohair@286 803 /**
ohair@286 804 * <p>
ohair@286 805 * Register marshal event callback {@link Listener} with this {@link Marshaller}.
ohair@286 806 *
ohair@286 807 * <p>
ohair@286 808 * There is only one Listener per Marshaller. Setting a Listener replaces the previous set Listener.
ohair@286 809 * One can unregister current Listener by setting listener to <tt>null</tt>.
ohair@286 810 *
ohair@286 811 * @param listener an instance of a class that implements {@link Listener}
ohair@286 812 * @since JAXB2.0
ohair@286 813 */
ohair@286 814 public void setListener(Listener listener);
ohair@286 815
ohair@286 816 /**
ohair@286 817 * <p>Return {@link Listener} registered with this {@link Marshaller}.
ohair@286 818 *
ohair@286 819 * @return registered {@link Listener} or <code>null</code> if no Listener is registered with this Marshaller.
ohair@286 820 * @since JAXB2.0
ohair@286 821 */
ohair@286 822 public Listener getListener();
ohair@286 823 }

mercurial