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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial