325N/A/*
325N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage javax.xml.bind;
325N/A
325N/Aimport javax.xml.bind.annotation.XmlRootElement;
325N/Aimport javax.xml.bind.annotation.adapters.XmlAdapter;
325N/Aimport javax.xml.bind.attachment.AttachmentMarshaller;
325N/Aimport javax.xml.validation.Schema;
325N/Aimport java.io.File;
325N/A
325N/A/**
325N/A * <p>
325N/A * The <tt>Marshaller</tt> class is responsible for governing the process
325N/A * of serializing Java content trees back into XML data. It provides the basic
325N/A * marshalling methods:
325N/A *
325N/A * <p>
325N/A * <i>Assume the following setup code for all following code fragments:</i>
325N/A * <blockquote>
325N/A * <pre>
325N/A * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
325N/A * Unmarshaller u = jc.createUnmarshaller();
325N/A * Object element = u.unmarshal( new File( "foo.xml" ) );
325N/A * Marshaller m = jc.createMarshaller();
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a File:
325N/A * <blockquote>
325N/A * <pre>
325N/A * OutputStream os = new FileOutputStream( "nosferatu.xml" );
325N/A * m.marshal( element, os );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a SAX ContentHandler:
325N/A * <blockquote>
325N/A * <pre>
325N/A * // assume MyContentHandler instanceof ContentHandler
325N/A * m.marshal( element, new MyContentHandler() );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a DOM Node:
325N/A * <blockquote>
325N/A * <pre>
325N/A * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
325N/A * dbf.setNamespaceAware(true);
325N/A * DocumentBuilder db = dbf.newDocumentBuilder();
325N/A * Document doc = db.newDocument();
325N/A *
325N/A * m.marshal( element, doc );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a java.io.OutputStream:
325N/A * <blockquote>
325N/A * <pre>
325N/A * m.marshal( element, System.out );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a java.io.Writer:
325N/A * <blockquote>
325N/A * <pre>
325N/A * m.marshal( element, new PrintWriter( System.out ) );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a javax.xml.transform.SAXResult:
325N/A * <blockquote>
325N/A * <pre>
325N/A * // assume MyContentHandler instanceof ContentHandler
325N/A * SAXResult result = new SAXResult( new MyContentHandler() );
325N/A *
325N/A * m.marshal( element, result );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a javax.xml.transform.DOMResult:
325N/A * <blockquote>
325N/A * <pre>
325N/A * DOMResult result = new DOMResult();
325N/A *
325N/A * m.marshal( element, result );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a javax.xml.transform.StreamResult:
325N/A * <blockquote>
325N/A * <pre>
325N/A * StreamResult result = new StreamResult( System.out );
325N/A *
325N/A * m.marshal( element, result );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a javax.xml.stream.XMLStreamWriter:
325N/A * <blockquote>
325N/A * <pre>
325N/A * XMLStreamWriter xmlStreamWriter =
325N/A * XMLOutputFactory.newInstance().createXMLStreamWriter( ... );
325N/A *
325N/A * m.marshal( element, xmlStreamWriter );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * Marshalling to a javax.xml.stream.XMLEventWriter:
325N/A * <blockquote>
325N/A * <pre>
325N/A * XMLEventWriter xmlEventWriter =
325N/A * XMLOutputFactory.newInstance().createXMLEventWriter( ... );
325N/A *
325N/A * m.marshal( element, xmlEventWriter );
325N/A * </pre>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * <a name="elementMarshalling"></a>
325N/A * <b>Marshalling content tree rooted by a JAXB element</b><br>
325N/A * <blockquote>
325N/A * The first parameter of the overloaded
325N/A * <tt>Marshaller.marshal(java.lang.Object, ...)</tt> methods must be a
325N/A * JAXB element as computed by
325N/A * {@link JAXBIntrospector#isElement(java.lang.Object)};
325N/A * otherwise, a <tt>Marshaller.marshal</tt> method must throw a
325N/A * {@link MarshalException}. There exist two mechanisms
325N/A * to enable marshalling an instance that is not a JAXB element.
325N/A * One method is to wrap the instance as a value of a {@link JAXBElement},
325N/A * and pass the wrapper element as the first parameter to
325N/A * a <tt>Marshaller.marshal</tt> method. For java to schema binding, it
325N/A * is also possible to simply annotate the instance's class with
325N/A * &#64;{@link XmlRootElement}.
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * <b>Encoding</b><br>
325N/A * <blockquote>
325N/A * By default, the Marshaller will use UTF-8 encoding when generating XML data
325N/A * to a <tt>java.io.OutputStream</tt>, or a <tt>java.io.Writer</tt>. Use the
325N/A * {@link #setProperty(String,Object) setProperty} API to change the output
325N/A * encoding used during these marshal operations. Client applications are
325N/A * expected to supply a valid character encoding name as defined in the
325N/A * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#charencoding">W3C XML 1.0
325N/A * Recommendation</a> and supported by your
325N/A * <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">
325N/A * Java Platform</a>.
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * <b>Validation and Well-Formedness</b><br>
325N/A * <blockquote>
325N/A * <p>
325N/A * Client applications are not required to validate the Java content tree prior
325N/A * to calling any of the marshal API's. Furthermore, there is no requirement
325N/A * that the Java content tree be valid with respect to its original schema in
325N/A * order to marshal it back into XML data. Different JAXB Providers will
325N/A * support marshalling invalid Java content trees at varying levels, however
325N/A * all JAXB Providers must be able to marshal a valid content tree back to
325N/A * XML data. A JAXB Provider must throw a <tt>MarshalException</tt> when it
325N/A * is unable to complete the marshal operation due to invalid content. Some
325N/A * JAXB Providers will fully allow marshalling invalid content, others will fail
325N/A * on the first validation error.
325N/A * <p>
325N/A * Even when schema validation is not explictly enabled for the marshal operation,
325N/A * it is possible that certain types of validation events will be detected
325N/A * during the operation. Validation events will be reported to the registered
325N/A * event handler. If the client application has not registered an event handler
325N/A * prior to invoking one of the marshal API's, then events will be delivered to
325N/A * a default event handler which will terminate the marshal operation after
325N/A * encountering the first error or fatal error. Note that for JAXB 2.0 and
325N/A * later versions, {@link javax.xml.bind.helpers.DefaultValidationEventHandler} is
325N/A * no longer used.
325N/A *
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * <a name="supportedProps"></a>
325N/A * <b>Supported Properties</b><br>
325N/A * <blockquote>
325N/A * <p>
325N/A * All JAXB Providers are required to support the following set of properties.
325N/A * Some providers may support additional properties.
325N/A * <dl>
325N/A * <dt><tt>jaxb.encoding</tt> - value must be a java.lang.String</dt>
325N/A * <dd>The output encoding to use when marshalling the XML data. The
325N/A * Marshaller will use "UTF-8" by default if this property is not
325N/A * specified.</dd>
325N/A * <dt><tt>jaxb.formatted.output</tt> - value must be a java.lang.Boolean</dt>
325N/A * <dd>This property controls whether or not the Marshaller will format
325N/A * the resulting XML data with line breaks and indentation. A
325N/A * true value for this property indicates human readable indented
325N/A * xml data, while a false value indicates unformatted xml data.
325N/A * The Marshaller will default to false (unformatted) if this
325N/A * property is not specified.</dd>
325N/A * <dt><tt>jaxb.schemaLocation</tt> - value must be a java.lang.String</dt>
325N/A * <dd>This property allows the client application to specify an
325N/A * xsi:schemaLocation attribute in the generated XML data. The format of
325N/A * the schemaLocation attribute value is discussed in an easy to
325N/A * understand, non-normative form in
325N/A * <a href="http://www.w3.org/TR/xmlschema-0/#schemaLocation">Section 5.6
325N/A * of the W3C XML Schema Part 0: Primer</a> and specified in
325N/A * <a href="http://www.w3.org/TR/xmlschema-1/#Instance_Document_Constructions">
325N/A * Section 2.6 of the W3C XML Schema Part 1: Structures</a>.</dd>
325N/A * <dt><tt>jaxb.noNamespaceSchemaLocation</tt> - value must be a java.lang.String</dt>
325N/A * <dd>This property allows the client application to specify an
325N/A * xsi:noNamespaceSchemaLocation attribute in the generated XML
325N/A * data. The format of the schemaLocation attribute value is discussed in
325N/A * an easy to understand, non-normative form in
325N/A * <a href="http://www.w3.org/TR/xmlschema-0/#schemaLocation">Section 5.6
325N/A * of the W3C XML Schema Part 0: Primer</a> and specified in
325N/A * <a href="http://www.w3.org/TR/xmlschema-1/#Instance_Document_Constructions">
325N/A * Section 2.6 of the W3C XML Schema Part 1: Structures</a>.</dd>
325N/A * <dt><tt>jaxb.fragment</tt> - value must be a java.lang.Boolean</dt>
325N/A * <dd>This property determines whether or not document level events will be
325N/A * generated by the Marshaller. If the property is not specified, the
325N/A * default is <tt>false</tt>. This property has different implications depending
325N/A * on which marshal api you are using - when this property is set to true:<br>
325N/A * <ul>
325N/A * <li>{@link #marshal(Object,org.xml.sax.ContentHandler) marshal(Object,ContentHandler)} - the Marshaller won't
325N/A * invoke {@link org.xml.sax.ContentHandler#startDocument()} and
325N/A * {@link org.xml.sax.ContentHandler#endDocument()}.</li>
325N/A * <li>{@link #marshal(Object,org.w3c.dom.Node) marshal(Object,Node)} - the property has no effect on this
325N/A * API.</li>
325N/A * <li>{@link #marshal(Object,java.io.OutputStream) marshal(Object,OutputStream)} - the Marshaller won't
325N/A * generate an xml declaration.</li>
325N/A * <li>{@link #marshal(Object,java.io.Writer) marshal(Object,Writer)} - the Marshaller won't
325N/A * generate an xml declaration.</li>
325N/A * <li>{@link #marshal(Object,javax.xml.transform.Result) marshal(Object,Result)} - depends on the kind of
325N/A * Result object, see semantics for Node, ContentHandler, and Stream APIs</li>
325N/A * <li>{@link #marshal(Object,javax.xml.stream.XMLEventWriter) marshal(Object,XMLEventWriter)} - the
325N/A * Marshaller will not generate {@link javax.xml.stream.events.XMLEvent#START_DOCUMENT} and
325N/A * {@link javax.xml.stream.events.XMLEvent#END_DOCUMENT} events.</li>
325N/A * <li>{@link #marshal(Object,javax.xml.stream.XMLStreamWriter) marshal(Object,XMLStreamWriter)} - the
325N/A * Marshaller will not generate {@link javax.xml.stream.events.XMLEvent#START_DOCUMENT} and
325N/A * {@link javax.xml.stream.events.XMLEvent#END_DOCUMENT} events.</li>
325N/A * </ul>
325N/A * </dd>
325N/A * </dl>
325N/A * </blockquote>
325N/A *
325N/A * <p>
325N/A * <a name="marshalEventCallback"></a>
325N/A * <b>Marshal Event Callbacks</b><br>
325N/A * <blockquote>
325N/A * "The {@link Marshaller} provides two styles of callback mechanisms
325N/A * that allow application specific processing during key points in the
325N/A * unmarshalling process. In 'class defined' event callbacks, application
325N/A * specific code placed in JAXB mapped classes is triggered during
325N/A * marshalling. 'External listeners' allow for centralized processing
325N/A * of marshal events in one callback method rather than by type event callbacks.
325N/A *
325N/A * <p>
325N/A * Class defined event callback methods allow any JAXB mapped class to specify
325N/A * its own specific callback methods by defining methods with the following method signatures:
325N/A * <blockquote>
325N/A * <pre>
325N/A * // Invoked by Marshaller after it has created an instance of this object.
325N/A * boolean beforeMarshal(Marshaller);
325N/A *
325N/A * // Invoked by Marshaller after it has marshalled all properties of this object.
325N/A * void afterMmarshal(Marshaller);
325N/A * </pre>
325N/A * </blockquote>
325N/A * The class defined event callback methods should be used when the callback method requires
325N/A * access to non-public methods and/or fields of the class.
325N/A * <p>
325N/A * The external listener callback mechanism enables the registration of a {@link Listener}
325N/A * instance with a {@link Marshaller#setListener(Listener)}. The external listener receives all callback events,
325N/A * allowing for more centralized processing than per class defined callback methods.
325N/A * <p>
325N/A * The 'class defined' and external listener event callback methods are independent of each other,
325N/A * both can be called for one event. The invocation ordering when both listener callback methods exist is
325N/A * defined in {@link Listener#beforeMarshal(Object)} and {@link Listener#afterMarshal(Object)}.
325N/A * <p>
325N/A * An event callback method throwing an exception terminates the current marshal process.
325N/A * </blockquote>
325N/A *
325N/A * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
325N/A * @see JAXBContext
325N/A * @see Validator
325N/A * @see Unmarshaller
325N/A * @since JAXB1.0
325N/A */
325N/Apublic interface Marshaller {
325N/A
325N/A /**
325N/A * The name of the property used to specify the output encoding in
325N/A * the marshalled XML data.
325N/A */
325N/A public static final String JAXB_ENCODING =
325N/A "jaxb.encoding";
325N/A
325N/A /**
325N/A * The name of the property used to specify whether or not the marshalled
325N/A * XML data is formatted with linefeeds and indentation.
325N/A */
325N/A public static final String JAXB_FORMATTED_OUTPUT =
325N/A "jaxb.formatted.output";
325N/A
325N/A /**
325N/A * The name of the property used to specify the xsi:schemaLocation
325N/A * attribute value to place in the marshalled XML output.
325N/A */
325N/A public static final String JAXB_SCHEMA_LOCATION =
325N/A "jaxb.schemaLocation";
325N/A
325N/A /**
325N/A * The name of the property used to specify the
325N/A * xsi:noNamespaceSchemaLocation attribute value to place in the marshalled
325N/A * XML output.
325N/A */
325N/A public static final String JAXB_NO_NAMESPACE_SCHEMA_LOCATION =
325N/A "jaxb.noNamespaceSchemaLocation";
325N/A
325N/A /**
325N/A * The name of the property used to specify whether or not the marshaller
325N/A * will generate document level events (ie calling startDocument or endDocument).
325N/A */
325N/A public static final String JAXB_FRAGMENT =
325N/A "jaxb.fragment";
325N/A
325N/A /**
325N/A * Marshal the content tree rooted at <tt>jaxbElement</tt> into the specified
325N/A * <tt>javax.xml.transform.Result</tt>.
325N/A *
325N/A * <p>
325N/A * All JAXB Providers must at least support
325N/A * {@link javax.xml.transform.dom.DOMResult},
325N/A * {@link javax.xml.transform.sax.SAXResult}, and
325N/A * {@link javax.xml.transform.stream.StreamResult}. It can
325N/A * support other derived classes of <tt>Result</tt> as well.
325N/A *
325N/A * @param jaxbElement
325N/A * The root of content tree to be marshalled.
325N/A * @param result
325N/A * XML will be sent to this Result
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs during the marshalling.
325N/A * @throws MarshalException
325N/A * If the {@link ValidationEventHandler ValidationEventHandler}
325N/A * returns false from its <tt>handleEvent</tt> method or the
325N/A * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
325N/A * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
325N/A * Marshalling a JAXB element</a>.
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A */
325N/A public void marshal( Object jaxbElement, javax.xml.transform.Result result )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Marshal the content tree rooted at <tt>jaxbElement</tt> into an output stream.
325N/A *
325N/A * @param jaxbElement
325N/A * The root of content tree to be marshalled.
325N/A * @param os
325N/A * XML will be added to this stream.
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs during the marshalling.
325N/A * @throws MarshalException
325N/A * If the {@link ValidationEventHandler ValidationEventHandler}
325N/A * returns false from its <tt>handleEvent</tt> method or the
325N/A * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
325N/A * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
325N/A * Marshalling a JAXB element</a>.
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A */
325N/A public void marshal( Object jaxbElement, java.io.OutputStream os )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Marshal the content tree rooted at <tt>jaxbElement</tt> into a file.
325N/A *
325N/A * @param jaxbElement
325N/A * The root of content tree to be marshalled.
325N/A * @param output
325N/A * File to be written. If this file already exists, it will be overwritten.
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs during the marshalling.
325N/A * @throws MarshalException
325N/A * If the {@link ValidationEventHandler ValidationEventHandler}
325N/A * returns false from its <tt>handleEvent</tt> method or the
325N/A * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
325N/A * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
325N/A * Marshalling a JAXB element</a>.
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A * @since JAXB2.1
325N/A */
325N/A public void marshal( Object jaxbElement, File output )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Marshal the content tree rooted at <tt>jaxbElement</tt> into a Writer.
325N/A *
325N/A * @param jaxbElement
325N/A * The root of content tree to be marshalled.
325N/A * @param writer
325N/A * XML will be sent to this writer.
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs during the marshalling.
325N/A * @throws MarshalException
325N/A * If the {@link ValidationEventHandler ValidationEventHandler}
325N/A * returns false from its <tt>handleEvent</tt> method or the
325N/A * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
325N/A * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
325N/A * Marshalling a JAXB element</a>.
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A */
325N/A public void marshal( Object jaxbElement, java.io.Writer writer )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Marshal the content tree rooted at <tt>jaxbElement</tt> into SAX2 events.
325N/A *
325N/A * @param jaxbElement
325N/A * The root of content tree to be marshalled.
325N/A * @param handler
325N/A * XML will be sent to this handler as SAX2 events.
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs during the marshalling.
325N/A * @throws MarshalException
325N/A * If the {@link ValidationEventHandler ValidationEventHandler}
325N/A * returns false from its <tt>handleEvent</tt> method or the
325N/A * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
325N/A * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
325N/A * Marshalling a JAXB element</a>.
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A */
325N/A public void marshal( Object jaxbElement, org.xml.sax.ContentHandler handler )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Marshal the content tree rooted at <tt>jaxbElement</tt> into a DOM tree.
325N/A *
325N/A * @param jaxbElement
325N/A * The content tree to be marshalled.
325N/A * @param node
325N/A * DOM nodes will be added as children of this node.
325N/A * This parameter must be a Node that accepts children
325N/A * ({@link org.w3c.dom.Document},
325N/A * {@link org.w3c.dom.DocumentFragment}, or
325N/A * {@link org.w3c.dom.Element})
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs during the marshalling.
325N/A * @throws MarshalException
325N/A * If the {@link ValidationEventHandler ValidationEventHandler}
325N/A * returns false from its <tt>handleEvent</tt> method or the
325N/A * <tt>Marshaller</tt> is unable to marshal <tt>jaxbElement</tt> (or any
325N/A * object reachable from <tt>jaxbElement</tt>). See <a href="#elementMarshalling">
325N/A * Marshalling a JAXB element</a>.
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A */
325N/A public void marshal( Object jaxbElement, org.w3c.dom.Node node )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Marshal the content tree rooted at <tt>jaxbElement</tt> into a
325N/A * {@link javax.xml.stream.XMLStreamWriter}.
325N/A *
325N/A * @param jaxbElement
325N/A * The content tree to be marshalled.
325N/A * @param writer
325N/A * XML will be sent to this writer.
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs during the marshalling.
325N/A * @throws MarshalException
325N/A * If the {@link ValidationEventHandler ValidationEventHandler}
325N/A * returns false from its <tt>handleEvent</tt> method or the
325N/A * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
325N/A * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
325N/A * Marshalling a JAXB element</a>.
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A * @since JAXB 2.0
325N/A */
325N/A public void marshal( Object jaxbElement, javax.xml.stream.XMLStreamWriter writer )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Marshal the content tree rooted at <tt>jaxbElement</tt> into a
325N/A * {@link javax.xml.stream.XMLEventWriter}.
325N/A *
325N/A * @param jaxbElement
325N/A * The content tree rooted at jaxbElement to be marshalled.
325N/A * @param writer
325N/A * XML will be sent to this writer.
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs during the marshalling.
325N/A * @throws MarshalException
325N/A * If the {@link ValidationEventHandler ValidationEventHandler}
325N/A * returns false from its <tt>handleEvent</tt> method or the
325N/A * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
325N/A * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
325N/A * Marshalling a JAXB element</a>.
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A * @since JAXB 2.0
325N/A */
325N/A public void marshal( Object jaxbElement, javax.xml.stream.XMLEventWriter writer )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Get a DOM tree view of the content tree(Optional).
325N/A *
325N/A * If the returned DOM tree is updated, these changes are also
325N/A * visible in the content tree.
325N/A * Use {@link #marshal(Object, org.w3c.dom.Node)} to force
325N/A * a deep copy of the content tree to a DOM representation.
325N/A *
325N/A * @param contentTree - JAXB Java representation of XML content
325N/A *
325N/A * @return the DOM tree view of the contentTree
325N/A *
325N/A * @throws UnsupportedOperationException
325N/A * If the JAXB provider implementation does not support a
325N/A * DOM view of the content tree
325N/A *
325N/A * @throws IllegalArgumentException
325N/A * If any of the method parameters are null
325N/A *
325N/A * @throws JAXBException
325N/A * If any unexpected problem occurs
325N/A *
325N/A */
325N/A public org.w3c.dom.Node getNode( java.lang.Object contentTree )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Set the particular property in the underlying implementation of
325N/A * <tt>Marshaller</tt>. This method can only be used to set one of
325N/A * the standard JAXB defined properties above or a provider specific
325N/A * property. Attempting to set an undefined property will result in
325N/A * a PropertyException being thrown. See <a href="#supportedProps">
325N/A * Supported Properties</a>.
325N/A *
325N/A * @param name the name of the property to be set. This value can either
325N/A * be specified using one of the constant fields or a user
325N/A * supplied string.
325N/A * @param value the value of the property to be set
325N/A *
325N/A * @throws PropertyException when there is an error processing the given
325N/A * property or value
325N/A * @throws IllegalArgumentException
325N/A * If the name parameter is null
325N/A */
325N/A public void setProperty( String name, Object value )
325N/A throws PropertyException;
325N/A
325N/A /**
325N/A * Get the particular property in the underlying implementation of
325N/A * <tt>Marshaller</tt>. This method can only be used to get one of
325N/A * the standard JAXB defined properties above or a provider specific
325N/A * property. Attempting to get an undefined property will result in
325N/A * a PropertyException being thrown. See <a href="#supportedProps">
325N/A * Supported Properties</a>.
325N/A *
325N/A * @param name the name of the property to retrieve
325N/A * @return the value of the requested property
325N/A *
325N/A * @throws PropertyException
325N/A * when there is an error retrieving the given property or value
325N/A * property name
325N/A * @throws IllegalArgumentException
325N/A * If the name parameter is null
325N/A */
325N/A public Object getProperty( String name ) throws PropertyException;
325N/A
325N/A /**
325N/A * Allow an application to register a validation event handler.
325N/A * <p>
325N/A * The validation event handler will be called by the JAXB Provider if any
325N/A * validation errors are encountered during calls to any of the marshal
325N/A * API's. If the client application does not register a validation event
325N/A * handler before invoking one of the marshal methods, then validation
325N/A * events will be handled by the default event handler which will terminate
325N/A * the marshal operation after the first error or fatal error is encountered.
325N/A * <p>
325N/A * Calling this method with a null parameter will cause the Marshaller
325N/A * to revert back to the default default event handler.
325N/A *
325N/A * @param handler the validation event handler
325N/A * @throws JAXBException if an error was encountered while setting the
325N/A * event handler
325N/A */
325N/A public void setEventHandler( ValidationEventHandler handler )
325N/A throws JAXBException;
325N/A
325N/A /**
325N/A * Return the current event handler or the default event handler if one
325N/A * hasn't been set.
325N/A *
325N/A * @return the current ValidationEventHandler or the default event handler
325N/A * if it hasn't been set
325N/A * @throws JAXBException if an error was encountered while getting the
325N/A * current event handler
325N/A */
325N/A public ValidationEventHandler getEventHandler()
325N/A throws JAXBException;
325N/A
325N/A
325N/A
325N/A /**
325N/A * Associates a configured instance of {@link XmlAdapter} with this marshaller.
325N/A *
325N/A * <p>
325N/A * This is a convenience method that invokes <code>setAdapter(adapter.getClass(),adapter);</code>.
325N/A *
325N/A * @see #setAdapter(Class,XmlAdapter)
325N/A * @throws IllegalArgumentException
325N/A * if the adapter parameter is null.
325N/A * @throws UnsupportedOperationException
325N/A * if invoked agains a JAXB 1.0 implementation.
325N/A * @since JAXB 2.0
325N/A */
325N/A public void setAdapter( XmlAdapter adapter );
325N/A
325N/A /**
325N/A * Associates a configured instance of {@link XmlAdapter} with this marshaller.
325N/A *
325N/A * <p>
325N/A * Every marshaller internally maintains a
325N/A * {@link java.util.Map}&lt;{@link Class},{@link XmlAdapter}>,
325N/A * which it uses for marshalling classes whose fields/methods are annotated
325N/A * with {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}.
325N/A *
325N/A * <p>
325N/A * This method allows applications to use a configured instance of {@link XmlAdapter}.
325N/A * When an instance of an adapter is not given, a marshaller will create
325N/A * one by invoking its default constructor.
325N/A *
325N/A * @param type
325N/A * The type of the adapter. The specified instance will be used when
325N/A * {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter#value()}
325N/A * refers to this type.
325N/A * @param adapter
325N/A * The instance of the adapter to be used. If null, it will un-register
325N/A * the current adapter set for this type.
325N/A * @throws IllegalArgumentException
325N/A * if the type parameter is null.
325N/A * @throws UnsupportedOperationException
325N/A * if invoked agains a JAXB 1.0 implementation.
325N/A * @since JAXB 2.0
325N/A */
325N/A public <A extends XmlAdapter> void setAdapter( Class<A> type, A adapter );
325N/A
325N/A /**
325N/A * Gets the adapter associated with the specified type.
325N/A *
325N/A * This is the reverse operation of the {@link #setAdapter} method.
325N/A *
325N/A * @throws IllegalArgumentException
325N/A * if the type parameter is null.
325N/A * @throws UnsupportedOperationException
325N/A * if invoked agains a JAXB 1.0 implementation.
325N/A * @since JAXB 2.0
325N/A */
325N/A public <A extends XmlAdapter> A getAdapter( Class<A> type );
325N/A
325N/A
325N/A /**
325N/A * <p>Associate a context that enables binary data within an XML document
325N/A * to be transmitted as XML-binary optimized attachment.
325N/A * The attachment is referenced from the XML document content model
325N/A * by content-id URIs(cid) references stored within the xml document.
325N/A *
325N/A * @throws IllegalStateException if attempt to concurrently call this
325N/A * method during a marshal operation.
325N/A */
325N/A void setAttachmentMarshaller(AttachmentMarshaller am);
325N/A
325N/A AttachmentMarshaller getAttachmentMarshaller();
325N/A
325N/A /**
325N/A * Specify the JAXP 1.3 {@link javax.xml.validation.Schema Schema}
325N/A * object that should be used to validate subsequent marshal operations
325N/A * against. Passing null into this method will disable validation.
325N/A *
325N/A * <p>
325N/A * This method allows the caller to validate the marshalled XML as it's marshalled.
325N/A *
325N/A * <p>
325N/A * Initially this property is set to <tt>null</tt>.
325N/A *
325N/A * @param schema Schema object to validate marshal operations against or null to disable validation
325N/A * @throws UnsupportedOperationException could be thrown if this method is
325N/A * invoked on an Marshaller created from a JAXBContext referencing
325N/A * JAXB 1.0 mapped classes
325N/A * @since JAXB2.0
325N/A */
325N/A public void setSchema( Schema schema );
325N/A
325N/A /**
325N/A * Get the JAXP 1.3 {@link javax.xml.validation.Schema Schema} object
325N/A * being used to perform marshal-time validation. If there is no
325N/A * Schema set on the marshaller, then this method will return null
325N/A * indicating that marshal-time validation will not be performed.
325N/A *
325N/A * @return the Schema object being used to perform marshal-time
325N/A * validation or null if not present.
325N/A * @throws UnsupportedOperationException could be thrown if this method is
325N/A * invoked on an Marshaller created from a JAXBContext referencing
325N/A * JAXB 1.0 mapped classes
325N/A * @since JAXB2.0
325N/A */
325N/A public Schema getSchema();
325N/A
325N/A /**
325N/A * <p/>
325N/A * Register an instance of an implementation of this class with a {@link Marshaller} to externally listen
325N/A * for marshal events.
325N/A * <p/>
325N/A * <p/>
325N/A * This class enables pre and post processing of each marshalled object.
325N/A * The event callbacks are called when marshalling from an instance that maps to an xml element or
325N/A * complex type definition. The event callbacks are not called when marshalling from an instance of a
325N/A * Java datatype that represents a simple type definition.
325N/A * <p/>
325N/A * <p/>
325N/A * External listener is one of two different mechanisms for defining marshal event callbacks.
325N/A * See <a href="Marshaller.html#marshalEventCallback">Marshal Event Callbacks</a> for an overview.
325N/A *
325N/A * @see Marshaller#setListener(Listener)
325N/A * @see Marshaller#getListener()
325N/A * @since JAXB2.0
325N/A */
325N/A public static abstract class Listener {
325N/A /**
325N/A * <p/>
325N/A * Callback method invoked before marshalling from <tt>source</tt> to XML.
325N/A * <p/>
325N/A * <p/>
325N/A * This method is invoked just before marshalling process starts to marshal <tt>source</tt>.
325N/A * Note that if the class of <tt>source</tt> defines its own <tt>beforeMarshal</tt> method,
325N/A * the class specific callback method is invoked just before this method is invoked.
325N/A *
325N/A * @param source instance of JAXB mapped class prior to marshalling from it.
325N/A */
325N/A public void beforeMarshal(Object source) {
325N/A }
325N/A
325N/A /**
325N/A * <p/>
325N/A * Callback method invoked after marshalling <tt>source</tt> to XML.
325N/A * <p/>
325N/A * <p/>
325N/A * This method is invoked after <tt>source</tt> and all its descendants have been marshalled.
325N/A * Note that if the class of <tt>source</tt> defines its own <tt>afterMarshal</tt> method,
325N/A * the class specific callback method is invoked just before this method is invoked.
325N/A *
325N/A * @param source instance of JAXB mapped class after marshalling it.
325N/A */
325N/A public void afterMarshal(Object source) {
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * <p>
325N/A * Register marshal event callback {@link Listener} with this {@link Marshaller}.
325N/A *
325N/A * <p>
325N/A * There is only one Listener per Marshaller. Setting a Listener replaces the previous set Listener.
325N/A * One can unregister current Listener by setting listener to <tt>null</tt>.
325N/A *
325N/A * @param listener an instance of a class that implements {@link Listener}
325N/A * @since JAXB2.0
325N/A */
325N/A public void setListener(Listener listener);
325N/A
325N/A /**
325N/A * <p>Return {@link Listener} registered with this {@link Marshaller}.
325N/A *
325N/A * @return registered {@link Listener} or <code>null</code> if no Listener is registered with this Marshaller.
325N/A * @since JAXB2.0
325N/A */
325N/A public Listener getListener();
325N/A}