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 org.w3c.dom.Node;
325N/A
325N/Aimport java.util.Collections;
325N/Aimport java.util.Map;
325N/Aimport java.util.Properties;
325N/Aimport java.io.IOException;
325N/Aimport java.io.InputStream;
325N/A
325N/A/**
325N/A * <p>
325N/A * The <tt>JAXBContext</tt> class provides the client's entry point to the
325N/A * JAXB API. It provides an abstraction for managing the XML/Java binding
325N/A * information necessary to implement the JAXB binding framework operations:
325N/A * unmarshal, marshal and validate.
325N/A *
325N/A * <p>A client application normally obtains new instances of this class using
325N/A * one of these two styles for newInstance methods, although there are other
325N/A * specialized forms of the method available:
325N/A *
325N/A * <ul>
325N/A * <li>{@link #newInstance(String,ClassLoader) JAXBContext.newInstance( "com.acme.foo:com.acme.bar" )} <br/>
325N/A * The JAXBContext instance is initialized from a list of colon
325N/A * separated Java package names. Each java package contains
325N/A * JAXB mapped classes, schema-derived classes and/or user annotated
325N/A * classes. Additionally, the java package may contain JAXB package annotations
325N/A * that must be processed. (see JLS, Section 7.4.1 "Named Packages").
325N/A * </li>
325N/A * <li>{@link #newInstance(Class...) JAXBContext.newInstance( com.acme.foo.Foo.class )} <br/>
325N/A * The JAXBContext instance is intialized with class(es)
325N/A * passed as parameter(s) and classes that are statically reachable from
325N/A * these class(es). See {@link #newInstance(Class...)} for details.
325N/A * </li>
325N/A * </ul>
325N/A *
325N/A * <p>
325N/A * <i><B>SPEC REQUIREMENT:</B> the provider must supply an implementation
325N/A * class containing the following method signatures:</i>
325N/A *
325N/A * <pre>
325N/A * public static JAXBContext createContext( String contextPath, ClassLoader classLoader, Map&lt;String,Object> properties ) throws JAXBException
325N/A * public static JAXBContext createContext( Class[] classes, Map&lt;String,Object> properties ) throws JAXBException
325N/A * </pre>
325N/A *
325N/A * <p><i>
325N/A * The following JAXB 1.0 requirement is only required for schema to
325N/A * java interface/implementation binding. It does not apply to JAXB annotated
325N/A * classes. JAXB Providers must generate a <tt>jaxb.properties</tt> file in
325N/A * each package containing schema derived classes. The property file must
325N/A * contain a property named <tt>javax.xml.bind.context.factory</tt> whose
325N/A * value is the name of the class that implements the <tt>createContext</tt>
325N/A * APIs.</i>
325N/A *
325N/A * <p><i>
325N/A * The class supplied by the provider does not have to be assignable to
325N/A * <tt>javax.xml.bind.JAXBContext</tt>, it simply has to provide a class that
325N/A * implements the <tt>createContext</tt> APIs.</i>
325N/A *
325N/A * <p><i>
325N/A * In addition, the provider must call the
325N/A * {@link DatatypeConverter#setDatatypeConverter(DatatypeConverterInterface)
325N/A * DatatypeConverter.setDatatypeConverter} api prior to any client
325N/A * invocations of the marshal and unmarshal methods. This is necessary to
325N/A * configure the datatype converter that will be used during these operations.</i>
325N/A *
325N/A * <a name="Unmarshalling"></a>
325N/A * <h3>Unmarshalling</h3>
325N/A * <p>
325N/A * The {@link Unmarshaller} class provides the client application the ability
325N/A * to convert XML data into a tree of Java content objects.
325N/A * The unmarshal method allows for
325N/A * any global XML element declared in the schema to be unmarshalled as
325N/A * the root of an instance document.
325N/A * Additionally, the unmarshal method allows for an unrecognized root element that
325N/A * has an xsi:type attribute's value that references a type definition declared in
325N/A * the schema to be unmarshalled as the root of an instance document.
325N/A * The <tt>JAXBContext</tt> object
325N/A * allows the merging of global elements and type definitions across a set of schemas (listed
325N/A * in the <tt>contextPath</tt>). Since each schema in the schema set can belong
325N/A * to distinct namespaces, the unification of schemas to an unmarshalling
325N/A * context should be namespace independent. This means that a client
325N/A * application is able to unmarshal XML documents that are instances of
325N/A * any of the schemas listed in the <tt>contextPath</tt>. For example:
325N/A *
325N/A * <pre>
325N/A * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo:com.acme.bar" );
325N/A * Unmarshaller u = jc.createUnmarshaller();
325N/A * FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) ); // ok
325N/A * BarObject barObj = (BarObject)u.unmarshal( new File( "bar.xml" ) ); // ok
325N/A * BazObject bazObj = (BazObject)u.unmarshal( new File( "baz.xml" ) ); // error, "com.acme.baz" not in contextPath
325N/A * </pre>
325N/A *
325N/A * <p>
325N/A * The client application may also generate Java content trees explicitly rather
325N/A * than unmarshalling existing XML data. For all JAXB-annotated value classes,
325N/A * an application can create content using constructors.
325N/A * For schema-derived interface/implementation classes and for the
325N/A * creation of elements that are not bound to a JAXB-annotated
325N/A * class, an application needs to have access and knowledge about each of
325N/A * the schema derived <tt> ObjectFactory</tt> classes that exist in each of
325N/A * java packages contained in the <tt>contextPath</tt>. For each schema
325N/A * derived java class, there is a static factory method that produces objects
325N/A * of that type. For example,
325N/A * assume that after compiling a schema, you have a package <tt>com.acme.foo</tt>
325N/A * that contains a schema derived interface named <tt>PurchaseOrder</tt>. In
325N/A * order to create objects of that type, the client application would use the
325N/A * factory method like this:
325N/A *
325N/A * <pre>
325N/A * com.acme.foo.PurchaseOrder po =
325N/A * com.acme.foo.ObjectFactory.createPurchaseOrder();
325N/A * </pre>
325N/A *
325N/A * <p>
325N/A * Once the client application has an instance of the the schema derived object,
325N/A * it can use the mutator methods to set content on it.
325N/A *
325N/A * <p>
325N/A * For more information on the generated <tt>ObjectFactory</tt> classes, see
325N/A * Section 4.2 <i>Java Package</i> of the specification.
325N/A *
325N/A * <p>
325N/A * <i><B>SPEC REQUIREMENT:</B> the provider must generate a class in each
325N/A * package that contains all of the necessary object factory methods for that
325N/A * package named ObjectFactory as well as the static
325N/A * <tt>newInstance( javaContentInterface )</tt> method</i>
325N/A *
325N/A * <h3>Marshalling</h3>
325N/A * <p>
325N/A * The {@link Marshaller} class provides the client application the ability
325N/A * to convert a Java content tree back into XML data. There is no difference
325N/A * between marshalling a content tree that is created manually using the factory
325N/A * methods and marshalling a content tree that is the result an <tt>unmarshal
325N/A * </tt> operation. Clients can marshal a java content tree back to XML data
325N/A * to a <tt>java.io.OutputStream</tt> or a <tt>java.io.Writer</tt>. The
325N/A * marshalling process can alternatively produce SAX2 event streams to a
325N/A * registered <tt>ContentHandler</tt> or produce a DOM Node object.
325N/A * Client applications have control over the output encoding as well as
325N/A * whether or not to marshal the XML data as a complete document or
325N/A * as a fragment.
325N/A *
325N/A * <p>
325N/A * Here is a simple example that unmarshals an XML document and then marshals
325N/A * it back out:
325N/A *
325N/A * <pre>
325N/A * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
325N/A *
325N/A * // unmarshal from foo.xml
325N/A * Unmarshaller u = jc.createUnmarshaller();
325N/A * FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) );
325N/A *
325N/A * // marshal to System.out
325N/A * Marshaller m = jc.createMarshaller();
325N/A * m.marshal( fooObj, System.out );
325N/A * </pre>
325N/A *
325N/A *
325N/A * <h3>Validation</h3>
325N/A * <p>
325N/A * Validation has been changed significantly since JAXB 1.0. The {@link Validator}
325N/A * class has been deprecated and made optional. This means that you are advised
325N/A * not to use this class and, in fact, it may not even be available depending on
325N/A * your JAXB provider. JAXB 1.0 client applications that rely on <tt>Validator</tt>
325N/A * will still work properly when deployed with the JAXB 1.0 runtime system.
325N/A *
325N/A * In JAXB 2.0, the {@link Unmarshaller} has included convenince methods that expose
325N/A * the JAXP 1.3 {@link javax.xml.validation} framework. Please refer to the
325N/A * {@link Unmarshaller#setSchema(javax.xml.validation.Schema)} API for more
325N/A * information.
325N/A *
325N/A *
325N/A * <h3>JAXB Runtime Binding Framework Compatibility</h3>
325N/A * <p>
325N/A * The following JAXB 1.0 restriction only applies to binding schema to
325N/A * interfaces/implementation classes.
325N/A * Since this binding does not require a common runtime system, a JAXB
325N/A * client application must not attempt to mix runtime objects (<tt>JAXBContext,
325N/A * Marshaller</tt>, etc. ) from different providers. This does not
325N/A * mean that the client application isn't portable, it simply means that a
325N/A * client has to use a runtime system provided by the same provider that was
325N/A * used to compile the schema.
325N/A *
325N/A *
325N/A * <h3>Discovery of JAXB implementation</h3>
325N/A * <p>
325N/A * When one of the <tt>newInstance</tt> methods is called, a JAXB implementation is discovered
325N/A * by the following steps.
325N/A *
325N/A * <ol>
325N/A * <li>
325N/A * For each package/class explicitly passed in to the {@link #newInstance} method, in the order they are specified,
325N/A * <tt>jaxb.properties</tt> file is looked up in its package, by using the associated classloader &mdash;
325N/A * this is {@link Class#getClassLoader() the owner class loader} for a {@link Class} argument, and for a package
325N/A * the speified {@link ClassLoader}.
325N/A *
325N/A * <p>
325N/A * If such a file is discovered, it is {@link Properties#load(InputStream) loaded} as a property file, and
325N/A * the value of the {@link #JAXB_CONTEXT_FACTORY} key will be assumed to be the provider factory class.
325N/A * This class is then loaded by the associated classloader discussed above.
325N/A *
325N/A * <p>
325N/A * This phase of the look up allows some packages to force the use of a certain JAXB implementation.
325N/A * (For example, perhaps the schema compiler has generated some vendor extension in the code.)
325N/A *
325N/A * <li>
325N/A * If the system property {@link #JAXB_CONTEXT_FACTORY} exists, then its value is assumed to be the provider
325N/A * factory class. This phase of the look up enables per-JVM override of the JAXB implementation.
325N/A *
325N/A * <li>
325N/A * Look for <tt>/META-INF/services/javax.xml.bind.JAXBContext</tt> file in the associated classloader.
325N/A * This file follows the standard service descriptor convention, and if such a file exists, its content
325N/A * is assumed to be the provider factory class. This phase of the look up is for automatic discovery.
325N/A * It allows users to just put a JAXB implementation in a classpath and use it without any furhter configuration.
325N/A *
325N/A * <li>
325N/A * Finally, if all the steps above fail, then the rest of the look up is unspecified. That said,
325N/A * the recommended behavior is to simply look for some hard-coded platform default JAXB implementation.
325N/A * This phase of the look up is so that JavaSE can have its own JAXB implementation as the last resort.
325N/A * </ol>
325N/A *
325N/A * <p>
325N/A * Once the provider factory class is discovered, its
325N/A * <tt>public static JAXBContext createContext(String,ClassLoader,Map)</tt> method
325N/A * (see {@link #newInstance(String, ClassLoader, Map)} for the parameter semantics.)
325N/A * or <tt>public static JAXBContext createContet(Class[],Map)</tt> method
325N/A * (see {@link #newInstance(Class[], Map)} for the parameter semantics) are invoked
325N/A * to create a {@link JAXBContext}.
325N/A *
325N/A * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
325N/A * @see Marshaller
325N/A * @see Unmarshaller
325N/A * @see S 7.4.1 "Named Packages" in Java Language Specification</a>
325N/A * @since JAXB1.0
325N/A */
325N/Apublic abstract class JAXBContext {
325N/A
325N/A /**
325N/A * The name of the property that contains the name of the class capable
325N/A * of creating new <tt>JAXBContext</tt> objects.
325N/A */
325N/A public static final String JAXB_CONTEXT_FACTORY =
325N/A "javax.xml.bind.context.factory";
325N/A
325N/A
325N/A protected JAXBContext() {
325N/A }
325N/A
325N/A
325N/A /**
325N/A * <p>
325N/A * Obtain a new instance of a <tt>JAXBContext</tt> class.
325N/A *
325N/A * <p>
325N/A * This is a convenience method to invoke the
325N/A * {@link #newInstance(String,ClassLoader)} method with
325N/A * the context class loader of the current thread.
325N/A *
325N/A * @throws JAXBException if an error was encountered while creating the
325N/A * <tt>JAXBContext</tt> such as
325N/A * <ol>
325N/A * <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
325N/A * <li>an ambiguity among global elements contained in the contextPath</li>
325N/A * <li>failure to locate a value for the context factory provider property</li>
325N/A * <li>mixing schema derived packages from different providers on the same contextPath</li>
325N/A * </ol>
325N/A */
325N/A public static JAXBContext newInstance( String contextPath )
325N/A throws JAXBException {
325N/A
325N/A //return newInstance( contextPath, JAXBContext.class.getClassLoader() );
325N/A return newInstance( contextPath, Thread.currentThread().getContextClassLoader() );
325N/A }
325N/A
325N/A /**
325N/A * <p>
325N/A * Obtain a new instance of a <tt>JAXBContext</tt> class.
325N/A *
325N/A * <p>
325N/A * The client application must supply a context path which is a list of
325N/A * colon (':', \u005Cu003A) separated java package names that contain
325N/A * schema-derived classes and/or fully qualified JAXB-annotated classes.
325N/A * Schema-derived
325N/A * code is registered with the JAXBContext by the
325N/A * ObjectFactory.class generated per package.
325N/A * Alternatively than being listed in the context path, programmer
325N/A * annotated JAXB mapped classes can be listed in a
325N/A * <tt>jaxb.index</tt> resource file, format described below.
325N/A * Note that a java package can contain both schema-derived classes and
325N/A * user annotated JAXB classes. Additionally, the java package may
325N/A * contain JAXB package annotations that must be processed. (see JLS,
325N/A * Section 7.4.1 "Named Packages").
325N/A * </p>
325N/A *
325N/A * <p>
325N/A * Every package listed on the contextPath must meet <b>one or both</b> of the
325N/A * following conditions otherwise a <tt>JAXBException</tt> will be thrown:
325N/A * </p>
325N/A * <ol>
325N/A * <li>it must contain ObjectFactory.class</li>
325N/A * <li>it must contain jaxb.index</li>
325N/A * </ol>
325N/A *
325N/A * <p>
325N/A * <b>Format for jaxb.index</b>
325N/A * <p>
325N/A * The file contains a newline-separated list of class names.
325N/A * Space and tab characters, as well as blank
325N/A * lines, are ignored. The comment character
325N/A * is '#' (0x23); on each line all characters following the first comment
325N/A * character are ignored. The file must be encoded in UTF-8. Classes that
325N/A * are reachable, as defined in {@link #newInstance(Class...)}, from the
325N/A * listed classes are also registered with JAXBContext.
325N/A * <p>
325N/A * Constraints on class name occuring in a <tt>jaxb.index</tt> file are:
325N/A * <ul>
325N/A * <li>Must not end with ".class".</li>
325N/A * <li>Class names are resolved relative to package containing
325N/A * <tt>jaxb.index</tt> file. Only classes occuring directly in package
325N/A * containing <tt>jaxb.index</tt> file are allowed.</li>
325N/A * <li>Fully qualified class names are not allowed.
325N/A * A qualified class name,relative to current package,
325N/A * is only allowed to specify a nested or inner class.</li>
325N/A * </ul>
325N/A *
325N/A * <p>
325N/A * To maintain compatibility with JAXB 1.0 schema to java
325N/A * interface/implementation binding, enabled by schema customization
325N/A * <tt>&lt;jaxb:globalBindings valueClass="false"></tt>,
325N/A * the JAXB provider will ensure that each package on the context path
325N/A * has a <tt>jaxb.properties</tt> file which contains a value for the
325N/A * <tt>javax.xml.bind.context.factory</tt> property and that all values
325N/A * resolve to the same provider. This requirement does not apply to
325N/A * JAXB annotated classes.
325N/A *
325N/A * <p>
325N/A * If there are any global XML element name collisions across the various
325N/A * packages listed on the <tt>contextPath</tt>, a <tt>JAXBException</tt>
325N/A * will be thrown.
325N/A *
325N/A * <p>
325N/A * Mixing generated interface/impl bindings from multiple JAXB Providers
325N/A * in the same context path may result in a <tt>JAXBException</tt>
325N/A * being thrown.
325N/A *
325N/A * <p>
325N/A * The steps involved in discovering the JAXB implementation is discussed in the class javadoc.
325N/A *
325N/A * @param contextPath list of java package names that contain schema
325N/A * derived class and/or java to schema (JAXB-annotated)
325N/A * mapped classes
325N/A * @param classLoader
325N/A * This class loader will be used to locate the implementation
325N/A * classes.
325N/A *
325N/A * @return a new instance of a <tt>JAXBContext</tt>
325N/A * @throws JAXBException if an error was encountered while creating the
325N/A * <tt>JAXBContext</tt> such as
325N/A * <ol>
325N/A * <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
325N/A * <li>an ambiguity among global elements contained in the contextPath</li>
325N/A * <li>failure to locate a value for the context factory provider property</li>
325N/A * <li>mixing schema derived packages from different providers on the same contextPath</li>
325N/A * </ol>
325N/A */
325N/A public static JAXBContext newInstance( String contextPath, ClassLoader classLoader ) throws JAXBException {
325N/A
325N/A return newInstance(contextPath,classLoader,Collections.<String,Object>emptyMap());
325N/A }
325N/A
325N/A /**
325N/A * <p>
325N/A * Obtain a new instance of a <tt>JAXBContext</tt> class.
325N/A *
325N/A * <p>
325N/A * This is mostly the same as {@link JAXBContext#newInstance(String, ClassLoader)},
325N/A * but this version allows you to pass in provider-specific properties to configure
325N/A * the instantiation of {@link JAXBContext}.
325N/A *
325N/A * <p>
325N/A * The interpretation of properties is up to implementations. Implementations should
325N/A * throw <tt>JAXBException</tt> if it finds properties that it doesn't understand.
325N/A *
325N/A * @param contextPath list of java package names that contain schema derived classes
325N/A * @param classLoader
325N/A * This class loader will be used to locate the implementation classes.
325N/A * @param properties
325N/A * provider-specific properties. Can be null, which means the same thing as passing
325N/A * in an empty map.
325N/A *
325N/A * @return a new instance of a <tt>JAXBContext</tt>
325N/A * @throws JAXBException if an error was encountered while creating the
325N/A * <tt>JAXBContext</tt> such as
325N/A * <ol>
325N/A * <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
325N/A * <li>an ambiguity among global elements contained in the contextPath</li>
325N/A * <li>failure to locate a value for the context factory provider property</li>
325N/A * <li>mixing schema derived packages from different providers on the same contextPath</li>
325N/A * </ol>
325N/A * @since JAXB2.0
325N/A */
325N/A public static JAXBContext newInstance( String contextPath, ClassLoader classLoader, Map<String,?> properties )
325N/A throws JAXBException {
325N/A
325N/A return ContextFinder.find(
325N/A /* The default property name according to the JAXB spec */
325N/A JAXB_CONTEXT_FACTORY,
325N/A
325N/A /* the context path supplied by the client app */
325N/A contextPath,
325N/A
325N/A /* class loader to be used */
325N/A classLoader,
325N/A properties );
325N/A }
325N/A
325N/A// TODO: resurrect this once we introduce external annotations
325N/A// /**
325N/A// * <p>
325N/A// * Obtain a new instance of a <tt>JAXBContext</tt> class.
325N/A// *
325N/A// * <p>
325N/A// * The client application must supply a list of classes that the new
325N/A// * context object needs to recognize.
325N/A// *
325N/A// * Not only the new context will recognize all the classes specified,
325N/A// * but it will also recognize any classes that are directly/indirectly
325N/A// * referenced statically from the specified classes.
325N/A// *
325N/A// * For example, in the following Java code, if you do
325N/A// * <tt>newInstance(Foo.class)</tt>, the newly created {@link JAXBContext}
325N/A// * will recognize both <tt>Foo</tt> and <tt>Bar</tt>, but not <tt>Zot</tt>:
325N/A// * <pre>
325N/A// * class Foo {
325N/A// * Bar b;
325N/A// * }
325N/A// * class Bar { int x; }
325N/A// * class Zot extends Bar { int y; }
325N/A// * </pre>
325N/A// *
325N/A// * Therefore, a typical client application only needs to specify the
325N/A// * top-level classes, but it needs to be careful.
325N/A// *
325N/A// * TODO: if we are to define other mechanisms, refer to them.
325N/A// *
325N/A// * @param externalBindings
325N/A// * list of external binding files. Can be null or empty if none is used.
325N/A// * when specified, those files determine how the classes are bound.
325N/A// *
325N/A// * @param classesToBeBound
325N/A// * list of java classes to be recognized by the new {@link JAXBContext}.
325N/A// * Can be empty, in which case a {@link JAXBContext} that only knows about
325N/A// * spec-defined classes will be returned.
325N/A// *
325N/A// * @return
325N/A// * A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
325N/A// *
325N/A// * @throws JAXBException
325N/A// * if an error was encountered while creating the
325N/A// * <tt>JAXBContext</tt>, such as (but not limited to):
325N/A// * <ol>
325N/A// * <li>No JAXB implementation was discovered
325N/A// * <li>Classes use JAXB annotations incorrectly
325N/A// * <li>Classes have colliding annotations (i.e., two classes with the same type name)
325N/A// * <li>Specified external bindings are incorrect
325N/A// * <li>The JAXB implementation was unable to locate
325N/A// * provider-specific out-of-band information (such as additional
325N/A// * files generated at the development time.)
325N/A// * </ol>
325N/A// *
325N/A// * @throws IllegalArgumentException
325N/A// * if the parameter contains {@code null} (i.e., {@code newInstance(null);})
325N/A// *
325N/A// * @since JAXB2.0
325N/A// */
325N/A// public static JAXBContext newInstance( Source[] externalBindings, Class... classesToBeBound )
325N/A// throws JAXBException {
325N/A//
325N/A// // empty class list is not an error, because the context will still include
325N/A// // spec-specified classes like String and Integer.
325N/A// // if(classesToBeBound.length==0)
325N/A// // throw new IllegalArgumentException();
325N/A//
325N/A// // but it is an error to have nulls in it.
325N/A// for( int i=classesToBeBound.length-1; i>=0; i-- )
325N/A// if(classesToBeBound[i]==null)
325N/A// throw new IllegalArgumentException();
325N/A//
325N/A// return ContextFinder.find(externalBindings,classesToBeBound);
325N/A// }
325N/A
325N/A /**
325N/A * <p>
325N/A * Obtain a new instance of a <tt>JAXBContext</tt> class.
325N/A *
325N/A * <p>
325N/A * The client application must supply a list of classes that the new
325N/A * context object needs to recognize.
325N/A *
325N/A * Not only the new context will recognize all the classes specified,
325N/A * but it will also recognize any classes that are directly/indirectly
325N/A * referenced statically from the specified classes. Subclasses of
325N/A * referenced classes nor <tt>&#64;XmlTransient</tt> referenced classes
325N/A * are not registered with JAXBContext.
325N/A *
325N/A * For example, in the following Java code, if you do
325N/A * <tt>newInstance(Foo.class)</tt>, the newly created {@link JAXBContext}
325N/A * will recognize both <tt>Foo</tt> and <tt>Bar</tt>, but not <tt>Zot</tt> or <tt>FooBar</tt>:
325N/A * <pre>
325N/A * class Foo {
325N/A * &#64;XmlTransient FooBar c;
325N/A * Bar b;
325N/A * }
325N/A * class Bar { int x; }
325N/A * class Zot extends Bar { int y; }
325N/A * class FooBar { }
325N/A * </pre>
325N/A *
325N/A * Therefore, a typical client application only needs to specify the
325N/A * top-level classes, but it needs to be careful.
325N/A *
325N/A * <p>
325N/A * Note that for each java package registered with JAXBContext,
325N/A * when the optional package annotations exist, they must be processed.
325N/A * (see JLS, Section 7.4.1 "Named Packages").
325N/A *
325N/A * <p>
325N/A * The steps involved in discovering the JAXB implementation is discussed in the class javadoc.
325N/A *
325N/A * @param classesToBeBound
325N/A * list of java classes to be recognized by the new {@link JAXBContext}.
325N/A * Can be empty, in which case a {@link JAXBContext} that only knows about
325N/A * spec-defined classes will be returned.
325N/A *
325N/A * @return
325N/A * A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
325N/A *
325N/A * @throws JAXBException
325N/A * if an error was encountered while creating the
325N/A * <tt>JAXBContext</tt>, such as (but not limited to):
325N/A * <ol>
325N/A * <li>No JAXB implementation was discovered
325N/A * <li>Classes use JAXB annotations incorrectly
325N/A * <li>Classes have colliding annotations (i.e., two classes with the same type name)
325N/A * <li>The JAXB implementation was unable to locate
325N/A * provider-specific out-of-band information (such as additional
325N/A * files generated at the development time.)
325N/A * </ol>
325N/A *
325N/A * @throws IllegalArgumentException
325N/A * if the parameter contains {@code null} (i.e., {@code newInstance(null);})
325N/A *
325N/A * @since JAXB2.0
325N/A */
325N/A public static JAXBContext newInstance( Class... classesToBeBound )
325N/A throws JAXBException {
325N/A
325N/A return newInstance(classesToBeBound,Collections.<String,Object>emptyMap());
325N/A }
325N/A
325N/A /**
325N/A * <p>
325N/A * Obtain a new instance of a <tt>JAXBContext</tt> class.
325N/A *
325N/A * <p>
325N/A * An overloading of {@link JAXBContext#newInstance(Class...)}
325N/A * to configure 'properties' for this instantiation of {@link JAXBContext}.
325N/A *
325N/A * <p>
325N/A * The interpretation of properties is up to implementations. Implementations should
325N/A * throw <tt>JAXBException</tt> if it finds properties that it doesn't understand.
325N/A *
325N/A * @param classesToBeBound
325N/A * list of java classes to be recognized by the new {@link JAXBContext}.
325N/A * Can be empty, in which case a {@link JAXBContext} that only knows about
325N/A * spec-defined classes will be returned.
325N/A * @param properties
325N/A * provider-specific properties. Can be null, which means the same thing as passing
325N/A * in an empty map.
325N/A *
325N/A * @return
325N/A * A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
325N/A *
325N/A * @throws JAXBException
325N/A * if an error was encountered while creating the
325N/A * <tt>JAXBContext</tt>, such as (but not limited to):
325N/A * <ol>
325N/A * <li>No JAXB implementation was discovered
325N/A * <li>Classes use JAXB annotations incorrectly
325N/A * <li>Classes have colliding annotations (i.e., two classes with the same type name)
325N/A * <li>The JAXB implementation was unable to locate
325N/A * provider-specific out-of-band information (such as additional
325N/A * files generated at the development time.)
325N/A * </ol>
325N/A *
325N/A * @throws IllegalArgumentException
325N/A * if the parameter contains {@code null} (i.e., {@code newInstance(null,someMap);})
325N/A *
325N/A * @since JAXB2.0
325N/A */
325N/A public static JAXBContext newInstance( Class[] classesToBeBound, Map<String,?> properties )
325N/A throws JAXBException {
325N/A
325N/A if (classesToBeBound == null) throw new IllegalArgumentException();
325N/A
325N/A // but it is an error to have nulls in it.
325N/A for( int i=classesToBeBound.length-1; i>=0; i-- )
325N/A if(classesToBeBound[i]==null)
325N/A throw new IllegalArgumentException();
325N/A
325N/A return ContextFinder.find(classesToBeBound,properties);
325N/A }
325N/A
325N/A /**
325N/A * Create an <tt>Unmarshaller</tt> object that can be used to convert XML
325N/A * data into a java content tree.
325N/A *
325N/A * @return an <tt>Unmarshaller</tt> object
325N/A *
325N/A * @throws JAXBException if an error was encountered while creating the
325N/A * <tt>Unmarshaller</tt> object
325N/A */
325N/A public abstract Unmarshaller createUnmarshaller() throws JAXBException;
325N/A
325N/A
325N/A /**
325N/A * Create a <tt>Marshaller</tt> object that can be used to convert a
325N/A * java content tree into XML data.
325N/A *
325N/A * @return a <tt>Marshaller</tt> object
325N/A *
325N/A * @throws JAXBException if an error was encountered while creating the
325N/A * <tt>Marshaller</tt> object
325N/A */
325N/A public abstract Marshaller createMarshaller() throws JAXBException;
325N/A
325N/A
325N/A /**
325N/A * {@link Validator} has been made optional and deprecated in JAXB 2.0. Please
325N/A * refer to the javadoc for {@link Validator} for more detail.
325N/A * <p>
325N/A * Create a <tt>Validator</tt> object that can be used to validate a
325N/A * java content tree against its source schema.
325N/A *
325N/A * @return a <tt>Validator</tt> object
325N/A *
325N/A * @throws JAXBException if an error was encountered while creating the
325N/A * <tt>Validator</tt> object
325N/A * @deprecated since JAXB2.0
325N/A */
325N/A public abstract Validator createValidator() throws JAXBException;
325N/A
325N/A /**
325N/A * Creates a <tt>Binder</tt> object that can be used for
325N/A * associative/in-place unmarshalling/marshalling.
325N/A *
325N/A * @param domType select the DOM API to use by passing in its DOM Node class.
325N/A *
325N/A * @return always a new valid <tt>Binder</tt> object.
325N/A *
325N/A * @throws UnsupportedOperationException
325N/A * if DOM API corresponding to <tt>domType</tt> is not supported by
325N/A * the implementation.
325N/A *
325N/A * @since JAXB2.0
325N/A */
325N/A public <T> Binder<T> createBinder(Class<T> domType) {
325N/A // to make JAXB 1.0 implementations work, this method must not be
325N/A // abstract
325N/A throw new UnsupportedOperationException();
325N/A }
325N/A
325N/A /**
325N/A * Creates a <tt>Binder</tt> for W3C DOM.
325N/A *
325N/A * @return always a new valid <tt>Binder</tt> object.
325N/A *
325N/A * @since JAXB2.0
325N/A */
325N/A public Binder<Node> createBinder() {
325N/A return createBinder(Node.class);
325N/A }
325N/A
325N/A /**
325N/A * Creates a <tt>JAXBIntrospector</tt> object that can be used to
325N/A * introspect JAXB objects.
325N/A *
325N/A * @return
325N/A * always return a non-null valid <tt>JAXBIntrospector</tt> object.
325N/A *
325N/A * @throws UnsupportedOperationException
325N/A * Calling this method on JAXB 1.0 implementations will throw
325N/A * an UnsupportedOperationException.
325N/A *
325N/A * @since JAXB2.0
325N/A */
325N/A public JAXBIntrospector createJAXBIntrospector() {
325N/A // to make JAXB 1.0 implementations work, this method must not be
325N/A // abstract
325N/A throw new UnsupportedOperationException();
325N/A }
325N/A
325N/A /**
325N/A * Generates the schema documents for this context.
325N/A *
325N/A * @param outputResolver
325N/A * this object controls the output to which schemas
325N/A * will be sent.
325N/A *
325N/A * @throws IOException
325N/A * if {@link SchemaOutputResolver} throws an {@link IOException}.
325N/A *
325N/A * @throws UnsupportedOperationException
325N/A * Calling this method on JAXB 1.0 implementations will throw
325N/A * an UnsupportedOperationException.
325N/A *
325N/A * @since JAXB 2.0
325N/A */
325N/A public void generateSchema(SchemaOutputResolver outputResolver) throws IOException {
325N/A // to make JAXB 1.0 implementations work, this method must not be
325N/A // abstract
325N/A throw new UnsupportedOperationException();
325N/A }
325N/A}