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

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

mercurial