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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial