src/share/jaxws_classes/javax/xml/bind/JAXBContext.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 397
b99d7e355d4b
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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

mercurial