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

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

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

mercurial