All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.xml.bind.Marshaller Maven / Gradle / Ivy

There is a newer version: 2.4.0-b180830.0359
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2001-2011 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package javax.xml.bind;

/**
 * 

* The Marshaller class is responsible for governing the process * of serializing Java content trees back into XML data. It provides the basic * marshalling methods: * *

* Assume the following setup code in all following code fragments: *

*
 *       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
 *       Unmarshaller u = jc.createUnmarshaller();
 *       FooObject obj = (FooObject)u.unmarshal( new File( "foo.xml" ) );
 *       Marshaller m = jc.createMarshaller();
 *    
*
* *

* Marshalling to a File: *

*
 *       OutputStream os = new FileOutputStream( "nosferatu.xml" );
 *       m.marshal( obj, os );
 *    
*
* *

* Marshalling to a SAX ContentHandler: *

*
 *       // assume MyContentHandler instanceof ContentHandler
 *       m.marshal( obj, new MyContentHandler() );  
 *    
*
* *

* Marshalling to a DOM Node: *

*
 *       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 *       dbf.setNamespaceAware(true);
 *       DocumentBuilder db = dbf.newDocumentBuilder();
 *       Document doc = db.newDocument();
 *
 *       m.marshal( obj, doc );
 *    
*
* *

* Marshalling to a java.io.OutputStream: *

*
 *       m.marshal( obj, System.out );
 *    
*
* *

* Marshalling to a java.io.Writer: *

*
 *       m.marshal( obj, new PrintWriter( System.out ) );
 *    
*
* *

* Marshalling to a javax.xml.transform.SAXResult: *

*
 *       // assume MyContentHandler instanceof ContentHandler
 *       SAXResult result = new SAXResult( new MyContentHandler() );
 *
 *       m.marshal( obj, result );
 *    
*
* *

* Marshalling to a javax.xml.transform.DOMResult: *

*
 *       DOMResult result = new DOMResult();
 *       
 *       m.marshal( obj, result );
 *    
*
* *

* Marshalling to a javax.xml.transform.StreamResult: *

*
 *       StreamResult result = new StreamResult( System.out );
 * 
 *       m.marshal( obj, result );
 *    
*
* *

* * Marshalling java.lang.Object Objects
*

* Although each of the marshal methods accepts a java.lang.Object as * its first parameter, JAXB Providers are not required to be able to marshal * any arbitrary java.lang.Object. If the JAXBContext * object that was used to create this Marshaller does not have enough * information to know how to marshal the object parameter (or any objects * reachable from it), then the marshal operation will throw a * {@link MarshalException MarshalException}. Even though JAXB Providers are not * required to be able to marshal arbitrary java.lang.Object objects, * some providers may allow it. *
* *

* Encoding
*

* By default, the Marshaller will use UTF-8 encoding when generating XML data * to a java.io.OutputStream, or a java.io.Writer. Use the * {@link #setProperty(String,Object) setProperty} API to change the ouput * encoding used during these marshal operations. Client applications are * expected to supply a valid character encoding name as defined in the * W3C XML 1.0 * Recommendation and supported by your * * Java Platform. *
* *

* Validation and Well-Formedness
*

*

* Client applications are not required to validate the Java content tree prior * to calling any of the marshal API's. Furthermore, there is no requirement * that the Java content tree be valid with respect to its original schema in * order to marshal it back into XML data. Different JAXB Providers will * support marshalling invalid Java content trees at varying levels, however * all JAXB Providers must be able to marshal a valid content tree back to * XML data. A JAXB Provider must throw a MarshalException when it * is unable to complete the marshal operation due to invalid content. Some * JAXB Providers will fully allow marshalling invalid content, others will fail * on the first validation error. *

* Although there is no way to enable validation during the marshal operation, * it is possible that certain types of validation events will be detected * during the operation. These events will be reported to the registered * event handler. If the client application has not registered an event handler * prior to invoking one of the marshal API's, then events will be delivered to * the default event handler which will terminate the marshal operation after * encountering the first error or fatal error. *

* *

* * Supported Properties
*

*

* All JAXB Providers are required to support the following set of properties. * Some providers may support additional properties. *

*
jaxb.encoding - value must be a java.lang.String *
The output encoding to use when marshalling the XML data. The * Marshaller will use "UTF-8" by default if this property is not * specified.
*
jaxb.formatted.output - value must be a java.lang.Boolean *
This property controls whether or not the Marshaller will format * the resulting XML data with line breaks and indentation. A * true value for this property indicates human readable indented * xml data, while a false value indicates unformatted xml data. * The Marshaller will default to false (unformatted) if this * property is not specified.
*
jaxb.schemaLocation - value must be a java.lang.String *
This property allows the client application to specify an * xsi:schemaLocation attribute in the generated XML data. The format of * the schemaLocation attribute value is discussed in an easy to * understand, non-normative form in * Section 5.6 * of the W3C XML Schema Part 0: Primer and specified in * * Section 2.6 of the W3C XML Schema Part 1: Structures.
*
jaxb.noNamespaceSchemaLocation - value must be a java.lang.String *
This property allows the client application to specify an * xsi:noNamespaceSchemaLocation attribute in the generated XML * data. The format of the schemaLocation attribute value is discussed in * an easy to understand, non-normative form in * Section 5.6 * of the W3C XML Schema Part 0: Primer and specified in * * Section 2.6 of the W3C XML Schema Part 1: Structures.
*
*
* * @author
  • Kohsuke Kawaguchi, Sun Microsystems, Inc.
  • Ryan Shoemaker, Sun Microsystems, Inc.
  • Joe Fialli, Sun Microsystems, Inc.
* @version $Revision: 1.28 $ $Date: 2003/01/13 21:15:52 $ * @see JAXBContext * @see Validator * @see Unmarshaller * @since JAXB1.0 */ public interface Marshaller { /** * The name of the property used to specify the output encoding in * the marshalled XML data. */ public static final String JAXB_ENCODING = "jaxb.encoding"; /** * The name of the property used to specify whether or not the marshalled * XML data is formated with linefeeds and indentation. */ public static final String JAXB_FORMATTED_OUTPUT = "jaxb.formatted.output"; /** * The name of the property used to specify the xsi:schemaLocation * attribute value to place in the marshalled XML output. */ public static final String JAXB_SCHEMA_LOCATION = "jaxb.schemaLocation"; /** * The name of the property used to specify the the * xsi:noNamespaceSchemaLocation attribute value to place in the marshalled * XML output. */ public static final String JAXB_NO_NAMESPACE_SCHEMA_LOCATION = "jaxb.noNamespaceSchemaLocation"; /** * Marshal the content tree rooted at obj into the specified * javax.xml.transform.Result. * *

* All JAXB Providers must at least support * {@link javax.xml.transform.dom.DOMResult}, * {@link javax.xml.transform.sax.SAXResult}, and * {@link javax.xml.transform.stream.StreamResult}. It can * support other derived classes of Result as well. * * @param obj * The content tree to be marshalled. * @param result * XML will be sent to this Result * * @throws JAXBException * If any unexpected problem occurs during the marshalling. * @throws MarshalException * If the {@link ValidationEventHandler ValidationEventHandler} * returns false from its handleEvent method or the * Marshaller is unable to marshal obj (or any * object reachable from obj). See * Marshalling objects. * @throws IllegalArgumentException * If any of the method parameters are null */ public void marshal( Object obj, javax.xml.transform.Result result ) throws JAXBException; /** * Marshal the content tree rooted at obj into an output stream. * * @param obj * The content tree to be marshalled. * @param os * XML will be added to this stream. * * @throws JAXBException * If any unexpected problem occurs during the marshalling. * @throws MarshalException * If the {@link ValidationEventHandler ValidationEventHandler} * returns false from its handleEvent method or the * Marshaller is unable to marshal obj (or any * object reachable from obj). See * Marshalling objects. * @throws IllegalArgumentException * If any of the method parameters are null */ public void marshal( Object obj, java.io.OutputStream os ) throws JAXBException; /** * Marshal the content tree rooted at obj into a Writer. * * @param obj * The content tree to be marshalled. * @param writer * XML will be sent to this writer. * * @throws JAXBException * If any unexpected problem occurs during the marshalling. * @throws MarshalException * If the {@link ValidationEventHandler ValidationEventHandler} * returns false from its handleEvent method or the * Marshaller is unable to marshal obj (or any * object reachable from obj). See * Marshalling objects. * @throws IllegalArgumentException * If any of the method parameters are null */ public void marshal( Object obj, java.io.Writer writer ) throws JAXBException; /** * Marshal the content tree rooted at obj into SAX2 events. * * @param obj * The content tree to be marshalled. * @param handler * XML will be sent to this handler as SAX2 events. * * @throws JAXBException * If any unexpected problem occurs during the marshalling. * @throws MarshalException * If the {@link ValidationEventHandler ValidationEventHandler} * returns false from its handleEvent method or the * Marshaller is unable to marshal obj (or any * object reachable from obj). See * Marshalling objects. * @throws IllegalArgumentException * If any of the method parameters are null */ public void marshal( Object obj, org.xml.sax.ContentHandler handler ) throws JAXBException; /** * Marshal the content tree rooted at obj into a DOM tree. * * @param obj * The content tree to be marshalled. * @param node * DOM nodes will be added as children of this node. * This parameter must be a Node that accepts children * ({@link org.w3c.dom.Document}, * {@link org.w3c.dom.DocumentFragment}, or * {@link org.w3c.dom.Element}) * * @throws JAXBException * If any unexpected problem occurs during the marshalling. * @throws MarshalException * If the {@link ValidationEventHandler ValidationEventHandler} * returns false from its handleEvent method or the * Marshaller is unable to marshal obj (or any * object reachable from obj). See * Marshalling objects. * @throws IllegalArgumentException * If any of the method parameters are null */ public void marshal( Object obj, org.w3c.dom.Node node ) throws JAXBException; /** * Get a DOM tree view of the content tree(Optional). * * If the returned DOM tree is updated, these changes are also * visible in the content tree. * Use {@link #marshal(Object, org.w3c.dom.Node)} to force * a deep copy of the content tree to a DOM representation. * * @param contentTree - JAXB Java representation of XML content * * @return the DOM tree view of the contentTree * * @throws UnsupportedOperationException * If the JAXB provider implementation does not support a * DOM view of the content tree * * @throws IllegalArgumentException * If any of the method parameters are null * * @throws JAXBException * If any unexpected problem occurs * */ public org.w3c.dom.Node getNode( java.lang.Object contentTree ) throws JAXBException; /** * Set the particular property in the underlying implementation of * Marshaller. This method can only be used to set one of * the standard JAXB defined properties above or a provider specific * property. Attempting to set an undefined property will result in * a PropertyException being thrown. See * Supported Properties. * * @param name the name of the property to be set. This value can either * be specified using one of the constant fields or a user * supplied string. * @param value the value of the property to be set * * @throws PropertyException when there is an error processing the given * property or value * @throws IllegalArgumentException * If the name parameter is null */ public void setProperty( String name, Object value ) throws PropertyException; /** * Get the particular property in the underlying implementation of * Marshaller. This method can only be used to get one of * the standard JAXB defined properties above or a provider specific * property. Attempting to get an undefined property will result in * a PropertyException being thrown. See * Supported Properties. * * @param name the name of the property to retrieve * @return the value of the requested property * * @throws PropertyException * when there is an error retrieving the given property or value * property name * @throws IllegalArgumentException * If the name parameter is null */ public Object getProperty( String name ) throws PropertyException; /** * Allow an application to register a validation event handler. *

* The validation event handler will be called by the JAXB Provider if any * validation errors are encountered during calls to any of the marshal * API's. If the client application does not register a validation event * handler before invoking one of the marshal methods, then validation * events will be handled by the default event handler which will terminate * the marshal operation after the first error or fatal error is encountered. *

* Calling this method with a null parameter will cause the Marshaller * to revert back to the default vefault event handler. * * @param handler the validation event handler * @throws JAXBException if an error was encountered while setting the * event handler */ public void setEventHandler( ValidationEventHandler handler ) throws JAXBException; /** * Return the current event handler or the default event handler if one * hasn't been set. * * @return the current ValidationEventHandler or the default event handler * if it hasn't been set * @throws JAXBException if an error was encountered while getting the * current event handler */ public ValidationEventHandler getEventHandler() throws JAXBException; }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy