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