Save This Page
Home » openjdk-7 » javax » xml » bind » [javadoc | source]
    1   /*
    2    * Copyright 2005-2006 Sun Microsystems, Inc.  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.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   package javax.xml.bind;
   27   
   28   import org.w3c.dom.Node;
   29   
   30   import java.util.Collections;
   31   import java.util.Map;
   32   import java.io.IOException;
   33   
   34   /**
   35    * <p>
   36    * The <tt>JAXBContext</tt> class provides the client's entry point to the
   37    * JAXB API. It provides an abstraction for managing the XML/Java binding
   38    * information necessary to implement the JAXB binding framework operations:
   39    * unmarshal, marshal and validate.
   40    *
   41    * <p>A client application normally obtains new instances of this class using
   42    * one of these two styles for newInstance methods, although there are other
   43    * specialized forms of the method available:
   44    *
   45    * <ul>
   46    *   <li>{@link #newInstance(String,ClassLoader) JAXBContext.newInstance( "com.acme.foo:com.acme.bar" )} <br/>
   47    *   The JAXBContext instance is initialized from a list of colon
   48    *   separated Java package names. Each java package contains
   49    *   JAXB mapped classes, schema-derived classes and/or user annotated
   50    *   classes. Additionally, the java package may contain JAXB package annotations
   51    *   that must be processed. (see JLS 3rd Edition, Section 7.4.1. Package Annotations).
   52    *   </li>
   53    *   <li>{@link #newInstance(Class...) JAXBContext.newInstance( com.acme.foo.Foo.class )} <br/>
   54    *    The JAXBContext instance is intialized with class(es)
   55    *    passed as parameter(s) and classes that are statically reachable from
   56    *    these class(es). See {@link #newInstance(Class...)} for details.
   57    *   </li>
   58    * </ul>
   59    *
   60    * <p>
   61    * <blockquote>
   62    * <i><B>SPEC REQUIREMENT:</B> the provider must supply an implementation
   63    * class containing the following method signatures:</i>
   64    *
   65    * <pre>
   66    * public static JAXBContext createContext( String contextPath, ClassLoader classLoader, Map<String,Object> properties ) throws JAXBException
   67    * public static JAXBContext createContext( Class[] classes, Map<String,Object> properties ) throws JAXBException
   68    * </pre>
   69    *
   70    * <p><i>
   71    * The following JAXB 1.0 requirement is only required for schema to
   72    * java interface/implementation binding. It does not apply to JAXB annotated
   73    * classes. JAXB Providers must generate a <tt>jaxb.properties</tt> file in
   74    * each package containing schema derived classes.  The property file must
   75    * contain a property named <tt>javax.xml.bind.context.factory</tt> whose
   76    * value is the name of the class that implements the <tt>createContext</tt>
   77    * APIs.</i>
   78    *
   79    * <p><i>
   80    * The class supplied by the provider does not have to be assignable to
   81    * <tt>javax.xml.bind.JAXBContext</tt>, it simply has to provide a class that
   82    * implements the <tt>createContext</tt> APIs.</i>
   83    *
   84    * <p><i>
   85    * In addition, the provider must call the
   86    * {@link DatatypeConverter#setDatatypeConverter(DatatypeConverterInterface)
   87    * DatatypeConverter.setDatatypeConverter} api prior to any client
   88    * invocations of the marshal and unmarshal methods.  This is necessary to
   89    * configure the datatype converter that will be used during these operations.</i>
   90    * </blockquote>
   91    *
   92    * <p>
   93    * <a name="Unmarshalling"></a>
   94    * <b>Unmarshalling</b>
   95    * <p>
   96    * <blockquote>
   97    * The {@link Unmarshaller} class provides the client application the ability
   98    * to convert XML data into a tree of Java content objects.
   99    * The unmarshal method allows for
  100    * any global XML element declared in the schema to be unmarshalled as
  101    * the root of an instance document.
  102    * Additionally, the unmarshal method allows for an unrecognized root element that
  103    * has  an xsi:type attribute's value that references a type definition declared in
  104    * the schema  to be unmarshalled as the root of an instance document.
  105    * The <tt>JAXBContext</tt> object
  106    * allows the merging of global elements and type definitions across a set of schemas (listed
  107    * in the <tt>contextPath</tt>). Since each schema in the schema set can belong
  108    * to distinct namespaces, the unification of schemas to an unmarshalling
  109    * context should be namespace independent.  This means that a client
  110    * application is able to unmarshal XML documents that are instances of
  111    * any of the schemas listed in the <tt>contextPath</tt>.  For example:
  112    *
  113    * <pre>
  114    *        JAXBContext jc = JAXBContext.newInstance( "com.acme.foo:com.acme.bar" );
  115    *        Unmarshaller u = jc.createUnmarshaller();
  116    *        FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) ); // ok
  117    *        BarObject barObj = (BarObject)u.unmarshal( new File( "bar.xml" ) ); // ok
  118    *        BazObject bazObj = (BazObject)u.unmarshal( new File( "baz.xml" ) ); // error, "com.acme.baz" not in contextPath
  119    * </pre>
  120    *
  121    * <p>
  122    * The client application may also generate Java content trees explicitly rather
  123    * than unmarshalling existing XML data.  For all JAXB-annotated value classes,
  124    * an application can create content using constructors.
  125    * For schema-derived interface/implementation classes and for the
  126    * creation of elements that are not bound to a JAXB-annotated
  127    * class, an application needs to have access and knowledge about each of
  128    * the schema derived <tt> ObjectFactory</tt> classes that exist in each of
  129    * java packages contained in the <tt>contextPath</tt>.  For each schema
  130    * derived java class, there is a static factory method that produces objects
  131    * of that type.  For example,
  132    * assume that after compiling a schema, you have a package <tt>com.acme.foo</tt>
  133    * that contains a schema derived interface named <tt>PurchaseOrder</tt>.  In
  134    * order to create objects of that type, the client application would use the
  135    * factory method like this:
  136    *
  137    * <pre>
  138    *       com.acme.foo.PurchaseOrder po =
  139    *           com.acme.foo.ObjectFactory.createPurchaseOrder();
  140    * </pre>
  141    *
  142    * <p>
  143    * Once the client application has an instance of the the schema derived object,
  144    * it can use the mutator methods to set content on it.
  145    *
  146    * <p>
  147    * For more information on the generated <tt>ObjectFactory</tt> classes, see
  148    * Section 4.2 <i>Java Package</i> of the specification.
  149    *
  150    * <p>
  151    * <i><B>SPEC REQUIREMENT:</B> the provider must generate a class in each
  152    * package that contains all of the necessary object factory methods for that
  153    * package named ObjectFactory as well as the static
  154    * <tt>newInstance( javaContentInterface )</tt> method</i>
  155    * </blockquote>
  156    *
  157    * <p>
  158    * <b>Marshalling</b>
  159    * <p>
  160    * <blockquote>
  161    * The {@link Marshaller} class provides the client application the ability
  162    * to convert a Java content tree back into XML data.  There is no difference
  163    * between marshalling a content tree that is created manually using the factory
  164    * methods and marshalling a content tree that is the result an <tt>unmarshal
  165    * </tt> operation.  Clients can marshal a java content tree back to XML data
  166    * to a <tt>java.io.OutputStream</tt> or a <tt>java.io.Writer</tt>.  The
  167    * marshalling process can alternatively produce SAX2 event streams to a
  168    * registered <tt>ContentHandler</tt> or produce a DOM Node object.
  169    * Client applications have control over the output encoding as well as
  170    * whether or not to marshal the XML data as a complete document or
  171    * as a fragment.
  172    *
  173    * <p>
  174    * Here is a simple example that unmarshals an XML document and then marshals
  175    * it back out:
  176    *
  177    * <pre>
  178    *        JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
  179    *
  180    *        // unmarshal from foo.xml
  181    *        Unmarshaller u = jc.createUnmarshaller();
  182    *        FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) );
  183    *
  184    *        // marshal to System.out
  185    *        Marshaller m = jc.createMarshaller();
  186    *        m.marshal( fooObj, System.out );
  187    * </pre>
  188    * </blockquote>
  189    *
  190    * <p>
  191    * <b>Validation</b>
  192    * <p>
  193    * <blockquote>
  194    * Validation has been changed significantly since JAXB 1.0.  The {@link Validator}
  195    * class has been deprecated and made optional.  This means that you are advised
  196    * not to use this class and, in fact, it may not even be available depending on
  197    * your JAXB provider.  JAXB 1.0 client applications that rely on <tt>Validator</tt>
  198    * will still work properly when deployed with the JAXB 1.0 runtime system.
  199    *
  200    * In JAXB 2.0, the {@link Unmarshaller} has included convenince methods that expose
  201    * the JAXP 1.3 {@link javax.xml.validation} framework.  Please refer to the
  202    * {@link Unmarshaller#setSchema(javax.xml.validation.Schema)} API for more
  203    * information.
  204    * </blockquote>
  205    *
  206    * <p>
  207    * <b>JAXB Runtime Binding Framework Compatibility</b><br>
  208    * <blockquote>
  209    * The following JAXB 1.0 restriction only applies to binding schema to
  210    * interfaces/implementation classes.
  211    * Since this binding does not require a common runtime system, a JAXB
  212    * client application must not attempt to mix runtime objects (<tt>JAXBContext,
  213    * Marshaller</tt>, etc. ) from different providers.  This does not
  214    * mean that the client application isn't portable, it simply means that a
  215    * client has to use a runtime system provided by the same provider that was
  216    * used to compile the schema.
  217    * </blockquote>
  218    *
  219    * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
  220    * @see Marshaller
  221    * @see Unmarshaller
  222    * @see <a href="http://java.sun.com/docs/books/jls">S 7.4.1.1 "Package Annotations" in Java Language Specification, 3rd Edition</a>
  223    * @since JAXB1.0
  224    */
  225   public abstract class JAXBContext {
  226   
  227       /**
  228        * The name of the property that contains the name of the class capable
  229        * of creating new <tt>JAXBContext</tt> objects.
  230        */
  231       public static final String JAXB_CONTEXT_FACTORY =
  232           "javax.xml.bind.context.factory";
  233   
  234   
  235       protected JAXBContext() {
  236       }
  237   
  238   
  239       /**
  240        * <p>
  241        * Obtain a new instance of a <tt>JAXBContext</tt> class.
  242        *
  243        * <p>
  244        * This is a convenience method for the
  245        * {@link #newInstance(String,ClassLoader) newInstance} method.  It uses
  246        * the context class loader of the current thread.  To specify the use of
  247        * a different class loader, either set it via the
  248        * <tt>Thread.setContextClassLoader()</tt> api or use the
  249        * {@link #newInstance(String,ClassLoader) newInstance} method.
  250        * @throws JAXBException if an error was encountered while creating the
  251        *                       <tt>JAXBContext</tt> such as
  252        * <ol>
  253        *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
  254        *   <li>an ambiguity among global elements contained in the contextPath</li>
  255        *   <li>failure to locate a value for the context factory provider property</li>
  256        *   <li>mixing schema derived packages from different providers on the same contextPath</li>
  257        * </ol>
  258        */
  259       public static JAXBContext newInstance( String contextPath )
  260           throws JAXBException {
  261   
  262           //return newInstance( contextPath, JAXBContext.class.getClassLoader() );
  263           return newInstance( contextPath, Thread.currentThread().getContextClassLoader() );
  264       }
  265   
  266       /**
  267        * <p>
  268        * Obtain a new instance of a <tt>JAXBContext</tt> class.
  269        *
  270        * <p>
  271        * The client application must supply a context path which is a list of
  272        * colon (':', \u005Cu003A) separated java package names that contain
  273        * schema-derived classes and/or fully qualified JAXB-annotated classes.
  274        * Schema-derived
  275        * code is registered with the JAXBContext by the
  276        * ObjectFactory.class generated per package.
  277        * Alternatively than being listed in the context path, programmer
  278        * annotated JAXB mapped classes can be listed in a
  279        * <tt>jaxb.index</tt> resource file, format described below.
  280        * Note that a java package can contain both schema-derived classes and
  281        * user annotated JAXB classes. Additionally, the java package may
  282        * contain JAXB package annotations  that must be processed. (see JLS 3rd Edition,
  283        * Section 7.4.1. "Package Annotations").
  284        * </p>
  285        *
  286        * <p>
  287        * Every package listed on the contextPath must meet <b>one or both</b> of the
  288        * following conditions otherwise a <tt>JAXBException</tt> will be thrown:
  289        * </p>
  290        * <ol>
  291        *   <li>it must contain ObjectFactory.class</li>
  292        *   <li>it must contain jaxb.index</li>
  293        * </ol>
  294        *
  295        * <p>
  296        * <b>Format for jaxb.index</b>
  297        * <p>
  298        * The file contains a newline-separated list of class names.
  299        * Space and tab characters, as well as blank
  300        * lines, are ignored. The comment character
  301        * is '#' (0x23); on each line all characters following the first comment
  302        * character are ignored. The file must be encoded in UTF-8. Classes that
  303        * are reachable, as defined in {@link #newInstance(Class...)}, from the
  304        * listed classes are also registered with JAXBContext.
  305        * <p>
  306        * Constraints on class name occuring in a <tt>jaxb.index</tt> file are:
  307        * <ul>
  308        *   <li>Must not end with ".class".</li>
  309        *   <li>Class names are resolved relative to package containing
  310        *       <tt>jaxb.index</tt> file. Only classes occuring directly in package
  311        *       containing <tt>jaxb.index</tt> file are allowed.</li>
  312        *   <li>Fully qualified class names are not allowed.
  313        *       A qualified class name,relative to current package,
  314        *       is only allowed to specify a nested or inner class.</li>
  315        * </ul>
  316        *
  317        * <p>
  318        * To maintain compatibility with JAXB 1.0 schema to java
  319        * interface/implementation binding, enabled by schema customization
  320        * <tt><jaxb:globalBindings valueClass="false"></tt>,
  321        * the JAXB provider will ensure that each package on the context path
  322        * has a <tt>jaxb.properties</tt> file which contains a value for the
  323        * <tt>javax.xml.bind.context.factory</tt> property and that all values
  324        * resolve to the same provider.  This requirement does not apply to
  325        * JAXB annotated classes.
  326        *
  327        * <p>
  328        * If there are any global XML element name collisions across the various
  329        * packages listed on the <tt>contextPath</tt>, a <tt>JAXBException</tt>
  330        * will be thrown.
  331        *
  332        * <p>
  333        * Mixing generated interface/impl bindings from multiple JAXB Providers
  334        * in the same context path may result in a <tt>JAXBException</tt>
  335        * being thrown.
  336        *
  337        * @param contextPath list of java package names that contain schema
  338        *                    derived class and/or java to schema (JAXB-annotated)
  339        *                    mapped classes
  340        * @param classLoader
  341        *      This class loader will be used to locate the implementation
  342        *      classes.
  343        *
  344        * @return a new instance of a <tt>JAXBContext</tt>
  345        * @throws JAXBException if an error was encountered while creating the
  346        *                       <tt>JAXBContext</tt> such as
  347        * <ol>
  348        *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
  349        *   <li>an ambiguity among global elements contained in the contextPath</li>
  350        *   <li>failure to locate a value for the context factory provider property</li>
  351        *   <li>mixing schema derived packages from different providers on the same contextPath</li>
  352        * </ol>
  353        */
  354       public static JAXBContext newInstance( String contextPath, ClassLoader classLoader ) throws JAXBException {
  355   
  356           return newInstance(contextPath,classLoader,Collections.<String,Object>emptyMap());
  357       }
  358   
  359       /**
  360        * <p>
  361        * Obtain a new instance of a <tt>JAXBContext</tt> class.
  362        *
  363        * <p>
  364        * This is mostly the same as {@link JAXBContext#newInstance(String, ClassLoader)},
  365        * but this version allows you to pass in provider-specific properties to configure
  366        * the instanciation of {@link JAXBContext}.
  367        *
  368        * <p>
  369        * The interpretation of properties is up to implementations.
  370        *
  371        * @param contextPath list of java package names that contain schema derived classes
  372        * @param classLoader
  373        *      This class loader will be used to locate the implementation classes.
  374        * @param properties
  375        *      provider-specific properties
  376        *
  377        * @return a new instance of a <tt>JAXBContext</tt>
  378        * @throws JAXBException if an error was encountered while creating the
  379        *                       <tt>JAXBContext</tt> such as
  380        * <ol>
  381        *   <li>failure to locate either ObjectFactory.class or jaxb.index in the packages</li>
  382        *   <li>an ambiguity among global elements contained in the contextPath</li>
  383        *   <li>failure to locate a value for the context factory provider property</li>
  384        *   <li>mixing schema derived packages from different providers on the same contextPath</li>
  385        * </ol>
  386        * @since JAXB2.0
  387        */
  388       public static JAXBContext newInstance( String contextPath, ClassLoader classLoader, Map<String,?>  properties  )
  389           throws JAXBException {
  390   
  391           return ContextFinder.find(
  392                           /* The default property name according to the JAXB spec */
  393                           JAXB_CONTEXT_FACTORY,
  394   
  395                           /* the context path supplied by the client app */
  396                           contextPath,
  397   
  398                           /* class loader to be used */
  399                           classLoader,
  400                           properties );
  401       }
  402   
  403   // TODO: resurrect this once we introduce external annotations
  404   //    /**
  405   //     * <p>
  406   //     * Obtain a new instance of a <tt>JAXBContext</tt> class.
  407   //     *
  408   //     * <p>
  409   //     * The client application must supply a list of classes that the new
  410   //     * context object needs to recognize.
  411   //     *
  412   //     * Not only the new context will recognize all the classes specified,
  413   //     * but it will also recognize any classes that are directly/indirectly
  414   //     * referenced statically from the specified classes.
  415   //     *
  416   //     * For example, in the following Java code, if you do
  417   //     * <tt>newInstance(Foo.class)</tt>, the newly created {@link JAXBContext}
  418   //     * will recognize both <tt>Foo</tt> and <tt>Bar</tt>, but not <tt>Zot</tt>:
  419   //     * <pre><xmp>
  420   //     * class Foo {
  421   //     *      Bar b;
  422   //     * }
  423   //     * class Bar { int x; }
  424   //     * class Zot extends Bar { int y; }
  425   //     * </xmp></pre>
  426   //     *
  427   //     * Therefore, a typical client application only needs to specify the
  428   //     * top-level classes, but it needs to be careful.
  429   //     *
  430   //     * TODO: if we are to define other mechanisms, refer to them.
  431   //     *
  432   //     * @param externalBindings
  433   //     *      list of external binding files. Can be null or empty if none is used.
  434   //     *      when specified, those files determine how the classes are bound.
  435   //     *
  436   //     * @param classesToBeBound
  437   //     *      list of java classes to be recognized by the new {@link JAXBContext}.
  438   //     *      Can be empty, in which case a {@link JAXBContext} that only knows about
  439   //     *      spec-defined classes will be returned.
  440   //     *
  441   //     * @return
  442   //     *      A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
  443   //     *
  444   //     * @throws JAXBException
  445   //     *      if an error was encountered while creating the
  446   //     *      <tt>JAXBContext</tt>, such as (but not limited to):
  447   //     * <ol>
  448   //     *  <li>No JAXB implementation was discovered
  449   //     *  <li>Classes use JAXB annotations incorrectly
  450   //     *  <li>Classes have colliding annotations (i.e., two classes with the same type name)
  451   //     *  <li>Specified external bindings are incorrect
  452   //     *  <li>The JAXB implementation was unable to locate
  453   //     *      provider-specific out-of-band information (such as additional
  454   //     *      files generated at the development time.)
  455   //     * </ol>
  456   //     *
  457   //     * @throws IllegalArgumentException
  458   //     *      if the parameter contains {@code null} (i.e., {@code newInstance(null);})
  459   //     *
  460   //     * @since JAXB2.0
  461   //     */
  462   //    public static JAXBContext newInstance( Source[] externalBindings, Class... classesToBeBound )
  463   //        throws JAXBException {
  464   //
  465   //        // empty class list is not an error, because the context will still include
  466   //        // spec-specified classes like String and Integer.
  467   //        // if(classesToBeBound.length==0)
  468   //        //    throw new IllegalArgumentException();
  469   //
  470   //        // but it is an error to have nulls in it.
  471   //        for( int i=classesToBeBound.length-1; i>=0; i-- )
  472   //            if(classesToBeBound[i]==null)
  473   //                throw new IllegalArgumentException();
  474   //
  475   //        return ContextFinder.find(externalBindings,classesToBeBound);
  476   //    }
  477   
  478       /**
  479        * <p>
  480        * Obtain a new instance of a <tt>JAXBContext</tt> class.
  481        *
  482        * <p>
  483        * The client application must supply a list of classes that the new
  484        * context object needs to recognize.
  485        *
  486        * Not only the new context will recognize all the classes specified,
  487        * but it will also recognize any classes that are directly/indirectly
  488        * referenced statically from the specified classes. Subclasses of
  489        * referenced classes nor <tt>&#64;XmlTransient</tt> referenced classes
  490        * are not registered with JAXBContext.
  491        *
  492        * For example, in the following Java code, if you do
  493        * <tt>newInstance(Foo.class)</tt>, the newly created {@link JAXBContext}
  494        * will recognize both <tt>Foo</tt> and <tt>Bar</tt>, but not <tt>Zot</tt> or <tt>FooBar</tt>:
  495        * <pre>
  496        * class Foo {
  497        *      &#64;XmlTransient FooBar c;
  498        *      Bar b;
  499        * }
  500        * class Bar { int x; }
  501        * class Zot extends Bar { int y; }
  502        * class FooBar { }
  503        * </pre>
  504        *
  505        * Therefore, a typical client application only needs to specify the
  506        * top-level classes, but it needs to be careful.
  507        *
  508        * <p>
  509        * Note that for each java package registered with JAXBContext,
  510        * when the optional package annotations exist, they must be processed.
  511        * (see JLS 3rd Edition, Section 7.4.1. "Package Annotations").
  512        *
  513        * @param classesToBeBound
  514        *      list of java classes to be recognized by the new {@link JAXBContext}.
  515        *      Can be empty, in which case a {@link JAXBContext} that only knows about
  516        *      spec-defined classes will be returned.
  517        *
  518        * @return
  519        *      A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
  520        *
  521        * @throws JAXBException
  522        *      if an error was encountered while creating the
  523        *      <tt>JAXBContext</tt>, such as (but not limited to):
  524        * <ol>
  525        *  <li>No JAXB implementation was discovered
  526        *  <li>Classes use JAXB annotations incorrectly
  527        *  <li>Classes have colliding annotations (i.e., two classes with the same type name)
  528        *  <li>The JAXB implementation was unable to locate
  529        *      provider-specific out-of-band information (such as additional
  530        *      files generated at the development time.)
  531        * </ol>
  532        *
  533        * @throws IllegalArgumentException
  534        *      if the parameter contains {@code null} (i.e., {@code newInstance(null);})
  535        *
  536        * @since JAXB2.0
  537        */
  538       public static JAXBContext newInstance( Class... classesToBeBound )
  539           throws JAXBException {
  540   
  541           return newInstance(classesToBeBound,Collections.<String,Object>emptyMap());
  542       }
  543   
  544       /**
  545        * <p>
  546        * Obtain a new instance of a <tt>JAXBContext</tt> class.
  547        *
  548        * <p>
  549        * An overloading of {@link JAXBContext#newInstance(Class...)}
  550        * to configure 'properties' for this instantiation of {@link JAXBContext}.
  551        *
  552        * <p>
  553        * The interpretation of properties is implementation specific.
  554        *
  555        * @param classesToBeBound
  556        *      list of java classes to be recognized by the new {@link JAXBContext}.
  557        *      Can be empty, in which case a {@link JAXBContext} that only knows about
  558        *      spec-defined classes will be returned.
  559        *
  560        * @return
  561        *      A new instance of a <tt>JAXBContext</tt>. Always non-null valid object.
  562        *
  563        * @throws JAXBException
  564        *      if an error was encountered while creating the
  565        *      <tt>JAXBContext</tt>, such as (but not limited to):
  566        * <ol>
  567        *  <li>No JAXB implementation was discovered
  568        *  <li>Classes use JAXB annotations incorrectly
  569        *  <li>Classes have colliding annotations (i.e., two classes with the same type name)
  570        *  <li>The JAXB implementation was unable to locate
  571        *      provider-specific out-of-band information (such as additional
  572        *      files generated at the development time.)
  573        * </ol>
  574        *
  575        * @throws IllegalArgumentException
  576        *      if the parameter contains {@code null} (i.e., {@code newInstance(null);})
  577        *
  578        * @since JAXB2.0
  579        */
  580       public static JAXBContext newInstance( Class[] classesToBeBound, Map<String,?> properties )
  581           throws JAXBException {
  582   
  583           // empty class list is not an error, because the context will still include
  584           // spec-specified classes like String and Integer.
  585           // if(classesToBeBound.length==0)
  586           //    throw new IllegalArgumentException();
  587   
  588           // but it is an error to have nulls in it.
  589           for( int i=classesToBeBound.length-1; i>=0; i-- )
  590               if(classesToBeBound[i]==null)
  591                   throw new IllegalArgumentException();
  592   
  593           return ContextFinder.find(classesToBeBound,properties);
  594       }
  595   
  596       /**
  597        * Create an <tt>Unmarshaller</tt> object that can be used to convert XML
  598        * data into a java content tree.
  599        *
  600        * @return an <tt>Unmarshaller</tt> object
  601        *
  602        * @throws JAXBException if an error was encountered while creating the
  603        *                       <tt>Unmarshaller</tt> object
  604        */
  605       public abstract Unmarshaller createUnmarshaller() throws JAXBException;
  606   
  607   
  608       /**
  609        * Create a <tt>Marshaller</tt> object that can be used to convert a
  610        * java content tree into XML data.
  611        *
  612        * @return a <tt>Marshaller</tt> object
  613        *
  614        * @throws JAXBException if an error was encountered while creating the
  615        *                       <tt>Marshaller</tt> object
  616        */
  617       public abstract Marshaller createMarshaller() throws JAXBException;
  618   
  619   
  620       /**
  621        * {@link Validator} has been made optional and deprecated in JAXB 2.0.  Please
  622        * refer to the javadoc for {@link Validator} for more detail.
  623        * <p>
  624        * Create a <tt>Validator</tt> object that can be used to validate a
  625        * java content tree against its source schema.
  626        *
  627        * @return a <tt>Validator</tt> object
  628        *
  629        * @throws JAXBException if an error was encountered while creating the
  630        *                       <tt>Validator</tt> object
  631        * @deprecated since JAXB2.0
  632        */
  633       public abstract Validator createValidator() throws JAXBException;
  634   
  635       /**
  636        * Creates a <tt>Binder</tt> object that can be used for
  637        * associative/in-place unmarshalling/marshalling.
  638        *
  639        * @param domType select the DOM API to use by passing in its DOM Node class.
  640        *
  641        * @return always a new valid <tt>Binder</tt> object.
  642        *
  643        * @throws UnsupportedOperationException
  644        *      if DOM API corresponding to <tt>domType</tt> is not supported by
  645        *      the implementation.
  646        *
  647        * @since JAXB2.0
  648        */
  649       public <T> Binder<T> createBinder(Class<T> domType) {
  650           // to make JAXB 1.0 implementations work, this method must not be
  651           // abstract
  652           throw new UnsupportedOperationException();
  653       }
  654   
  655       /**
  656        * Creates a <tt>Binder</tt> for W3C DOM.
  657        *
  658        * @return always a new valid <tt>Binder</tt> object.
  659        *
  660        * @since JAXB2.0
  661        */
  662       public Binder<Node> createBinder() {
  663           return createBinder(Node.class);
  664       }
  665   
  666       /**
  667        * Creates a <tt>JAXBIntrospector</tt> object that can be used to
  668        * introspect JAXB objects.
  669        *
  670        * @return
  671        *      always return a non-null valid <tt>JAXBIntrospector</tt> object.
  672        *
  673        * @throws UnsupportedOperationException
  674        *      Calling this method on JAXB 1.0 implementations will throw
  675        *      an UnsupportedOperationException.
  676        *
  677        * @since JAXB2.0
  678        */
  679       public JAXBIntrospector createJAXBIntrospector() {
  680           // to make JAXB 1.0 implementations work, this method must not be
  681           // abstract
  682           throw new UnsupportedOperationException();
  683       }
  684   
  685       /**
  686        * Generates the schema documents for this context.
  687        *
  688        * @param outputResolver
  689        *      this object controls the output to which schemas
  690        *      will be sent.
  691        *
  692        * @throws IOException
  693        *      if {@link SchemaOutputResolver} throws an {@link IOException}.
  694        *
  695        * @throws UnsupportedOperationException
  696        *      Calling this method on JAXB 1.0 implementations will throw
  697        *      an UnsupportedOperationException.
  698        *
  699        * @since JAXB 2.0
  700        */
  701       public void generateSchema(SchemaOutputResolver outputResolver) throws IOException  {
  702           // to make JAXB 1.0 implementations work, this method must not be
  703           // abstract
  704           throw new UnsupportedOperationException();
  705       }
  706   }

Save This Page
Home » openjdk-7 » javax » xml » bind » [javadoc | source]