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