Java Doc for JAXBContext.java in  » 6.0-JDK-Modules » jaxb-api » javax » xml » bind » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » jaxb api » javax.xml.bind 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   javax.xml.bind.JAXBContext

JAXBContext
abstract public class JAXBContext (Code)

The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate.

A client application normally obtains new instances of this class using one of these two styles for newInstance methods, although there are other specialized forms of the method available:

  • JAXBContext.newInstance(String,ClassLoader) JAXBContext.newInstance( "com.acme.foo:com.acme.bar" )
    The JAXBContext instance is initialized from a list of colon separated Java package names. Each java package contains JAXB mapped classes, schema-derived classes and/or user annotated classes. Additionally, the java package may contain JAXB package annotations that must be processed. (see JLS 3rd Edition, Section 7.4.1. Package Annotations).
  • JAXBContext.newInstance(Class) JAXBContext.newInstance( com.acme.foo.Foo.class )
    The JAXBContext instance is intialized with class(es) passed as parameter(s) and classes that are statically reachable from these class(es). See JAXBContext.newInstance(Class) for details.

SPEC REQUIREMENT: the provider must supply an implementation class containing the following method signatures:
 public static JAXBContext createContext( String contextPath, ClassLoader classLoader, Map properties ) throws JAXBException
 public static JAXBContext createContext( Class[] classes, Map properties ) throws JAXBException
 

The following JAXB 1.0 requirement is only required for schema to java interface/implementation binding. It does not apply to JAXB annotated classes. JAXB Providers must generate a jaxb.properties file in each package containing schema derived classes. The property file must contain a property named javax.xml.bind.context.factory whose value is the name of the class that implements the createContext APIs.

The class supplied by the provider does not have to be assignable to javax.xml.bind.JAXBContext, it simply has to provide a class that implements the createContext APIs.

In addition, the provider must call the DatatypeConverter.setDatatypeConverter(DatatypeConverterInterface) DatatypeConverter.setDatatypeConverter api prior to any client invocations of the marshal and unmarshal methods. This is necessary to configure the datatype converter that will be used during these operations.

Unmarshalling

The Unmarshaller class provides the client application the ability to convert XML data into a tree of Java content objects. The unmarshal method allows for any global XML element declared in the schema to be unmarshalled as the root of an instance document. Additionally, the unmarshal method allows for an unrecognized root element that has an xsi:type attribute's value that references a type definition declared in the schema to be unmarshalled as the root of an instance document. The JAXBContext object allows the merging of global elements and type definitions across a set of schemas (listed in the contextPath). Since each schema in the schema set can belong to distinct namespaces, the unification of schemas to an unmarshalling context should be namespace independent. This means that a client application is able to unmarshal XML documents that are instances of any of the schemas listed in the contextPath. For example:
 JAXBContext jc = JAXBContext.newInstance( "com.acme.foo:com.acme.bar" );
 Unmarshaller u = jc.createUnmarshaller();
 FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) ); // ok
 BarObject barObj = (BarObject)u.unmarshal( new File( "bar.xml" ) ); // ok
 BazObject bazObj = (BazObject)u.unmarshal( new File( "baz.xml" ) ); // error, "com.acme.baz" not in contextPath
 

The client application may also generate Java content trees explicitly rather than unmarshalling existing XML data. For all JAXB-annotated value classes, an application can create content using constructors. For schema-derived interface/implementation classes and for the creation of elements that are not bound to a JAXB-annotated class, an application needs to have access and knowledge about each of the schema derived ObjectFactory classes that exist in each of java packages contained in the contextPath. For each schema derived java class, there is a static factory method that produces objects of that type. For example, assume that after compiling a schema, you have a package com.acme.foo that contains a schema derived interface named PurchaseOrder. In order to create objects of that type, the client application would use the factory method like this:

 com.acme.foo.PurchaseOrder po = 
 com.acme.foo.ObjectFactory.createPurchaseOrder();
 

Once the client application has an instance of the the schema derived object, it can use the mutator methods to set content on it.

For more information on the generated ObjectFactory classes, see Section 4.2 Java Package of the specification.

SPEC REQUIREMENT: the provider must generate a class in each package that contains all of the necessary object factory methods for that package named ObjectFactory as well as the static newInstance( javaContentInterface ) method

Marshalling

The Marshaller class provides the client application the ability to convert a Java content tree back into XML data. There is no difference between marshalling a content tree that is created manually using the factory methods and marshalling a content tree that is the result an unmarshal operation. Clients can marshal a java content tree back to XML data to a java.io.OutputStream or a java.io.Writer. The marshalling process can alternatively produce SAX2 event streams to a registered ContentHandler or produce a DOM Node object. Client applications have control over the output encoding as well as whether or not to marshal the XML data as a complete document or as a fragment.

Here is a simple example that unmarshals an XML document and then marshals it back out:

 JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
 // unmarshal from foo.xml
 Unmarshaller u = jc.createUnmarshaller();
 FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) );
 // marshal to System.out
 Marshaller m = jc.createMarshaller();
 m.marshal( fooObj, System.out );
 

Validation

Validation has been changed significantly since JAXB 1.0. The Validator class has been deprecated and made optional. This means that you are advised not to use this class and, in fact, it may not even be available depending on your JAXB provider. JAXB 1.0 client applications that rely on Validator will still work properly when deployed with the JAXB 1.0 runtime system. In JAXB 2.0, the Unmarshaller has included convenince methods that expose the JAXP 1.3 javax.xml.validation framework. Please refer to the Unmarshaller.setSchema(javax.xml.validation.Schema) API for more information.

JAXB Runtime Binding Framework Compatibility

The following JAXB 1.0 restriction only applies to binding schema to interfaces/implementation classes. Since this binding does not require a common runtime system, a JAXB client application must not attempt to mix runtime objects (JAXBContext, Marshaller, etc. ) from different providers. This does not mean that the client application isn't portable, it simply means that a client has to use a runtime system provided by the same provider that was used to compile the schema.

author:
  
  • Ryan Shoemaker, Sun Microsystems, Inc.
  • Kohsuke Kawaguchi, Sun Microsystems, Inc.
  • Joe Fialli, Sun Microsystems, Inc.

version:
   $Revision: 1.24 $ $Date: 2006/03/08 17:05:01 $
See Also:   Marshaller
See Also:   Unmarshaller
See Also:    S 7.4.1.1 "Package Annotations" in Java Language Specification, 3rd Edition
since:
   JAXB1.0


Field Summary
final public static  StringJAXB_CONTEXT_FACTORY
     The name of the property that contains the name of the class capable of creating new JAXBContext objects.

Constructor Summary
protected  JAXBContext()
    

Method Summary
public  Binder<T>createBinder(Class<T> domType)
     Creates a Binder object that can be used for associative/in-place unmarshalling/marshalling.
Parameters:
  domType - select the DOM API to use by passing in its DOM Node class.
public  Binder<Node>createBinder()
     Creates a Binder for W3C DOM.
public  JAXBIntrospectorcreateJAXBIntrospector()
     Creates a JAXBIntrospector object that can be used to introspect JAXB objects.
abstract public  MarshallercreateMarshaller()
     Create a Marshaller object that can be used to convert a java content tree into XML data.
abstract public  UnmarshallercreateUnmarshaller()
     Create an Unmarshaller object that can be used to convert XML data into a java content tree.
abstract public  ValidatorcreateValidator()
    Validator has been made optional and deprecated in JAXB 2.0.
public  voidgenerateSchema(SchemaOutputResolver outputResolver)
     Generates the schema documents for this context.
public static  JAXBContextnewInstance(String contextPath)
    

Obtain a new instance of a JAXBContext class.

This is a convenience method for the JAXBContext.newInstance(String,ClassLoader) newInstance method.

public static  JAXBContextnewInstance(String contextPath, ClassLoader classLoader)
    

Obtain a new instance of a JAXBContext class.

The client application must supply a context path which is a list of colon (':', \u005Cu003A) separated java package names that contain schema-derived classes and/or fully qualified JAXB-annotated classes.

public static  JAXBContextnewInstance(String contextPath, ClassLoader classLoader, Map<String, ?> properties)
    

Obtain a new instance of a JAXBContext class.

public static  JAXBContextnewInstance(Class... classesToBeBound)
    

Obtain a new instance of a JAXBContext class.

The client application must supply a list of classes that the new context object needs to recognize. Not only the new context will recognize all the classes specified, but it will also recognize any classes that are directly/indirectly referenced statically from the specified classes.

public static  JAXBContextnewInstance(Class[] classesToBeBound, Map<String, ?> properties)
    

Obtain a new instance of a JAXBContext class.

An overloading of JAXBContext.newInstance(Class) to configure 'properties' for this instantiation of JAXBContext .

The interpretation of properties is implementation specific.
Parameters:
  classesToBeBound - list of java classes to be recognized by the new JAXBContext.Can be empty, in which case a JAXBContext that only knows aboutspec-defined classes will be returned.A new instance of a JAXBContext.


Field Detail
JAXB_CONTEXT_FACTORY
final public static String JAXB_CONTEXT_FACTORY(Code)
The name of the property that contains the name of the class capable of creating new JAXBContext objects.




Constructor Detail
JAXBContext
protected JAXBContext()(Code)




Method Detail
createBinder
public Binder<T> createBinder(Class<T> domType)(Code)
Creates a Binder object that can be used for associative/in-place unmarshalling/marshalling.
Parameters:
  domType - select the DOM API to use by passing in its DOM Node class. always a new valid Binder object.
throws:
  UnsupportedOperationException - if DOM API corresponding to domType is not supported by the implementation.
since:
   JAXB2.0



createBinder
public Binder<Node> createBinder()(Code)
Creates a Binder for W3C DOM. always a new valid Binder object.
since:
   JAXB2.0



createJAXBIntrospector
public JAXBIntrospector createJAXBIntrospector()(Code)
Creates a JAXBIntrospector object that can be used to introspect JAXB objects. always return a non-null valid JAXBIntrospector object.
throws:
  UnsupportedOperationException - Calling this method on JAXB 1.0 implementations will throwan UnsupportedOperationException.
since:
   JAXB2.0



createMarshaller
abstract public Marshaller createMarshaller() throws JAXBException(Code)
Create a Marshaller object that can be used to convert a java content tree into XML data. a Marshaller object
throws:
  JAXBException - if an error was encountered while creating theMarshaller object



createUnmarshaller
abstract public Unmarshaller createUnmarshaller() throws JAXBException(Code)
Create an Unmarshaller object that can be used to convert XML data into a java content tree. an Unmarshaller object
throws:
  JAXBException - if an error was encountered while creating theUnmarshaller object



createValidator
abstract public Validator createValidator() throws JAXBException(Code)
Validator has been made optional and deprecated in JAXB 2.0. Please refer to the javadoc for Validator for more detail.

Create a Validator object that can be used to validate a java content tree against its source schema. a Validator object
throws:
  JAXBException - if an error was encountered while creating theValidator object




generateSchema
public void generateSchema(SchemaOutputResolver outputResolver) throws IOException(Code)
Generates the schema documents for this context.
Parameters:
  outputResolver - this object controls the output to which schemaswill be sent.
throws:
  IOException - if SchemaOutputResolver throws an IOException.
throws:
  UnsupportedOperationException - Calling this method on JAXB 1.0 implementations will throwan UnsupportedOperationException.
since:
   JAXB 2.0



newInstance
public static JAXBContext newInstance(String contextPath) throws JAXBException(Code)

Obtain a new instance of a JAXBContext class.

This is a convenience method for the JAXBContext.newInstance(String,ClassLoader) newInstance method. It uses the context class loader of the current thread. To specify the use of a different class loader, either set it via the Thread.setContextClassLoader() api or use the JAXBContext.newInstance(String,ClassLoader) newInstance method.
throws:
  JAXBException - if an error was encountered while creating theJAXBContext such as

  1. failure to locate either ObjectFactory.class or jaxb.index in the packages
  2. an ambiguity among global elements contained in the contextPath
  3. failure to locate a value for the context factory provider property
  4. mixing schema derived packages from different providers on the same contextPath



newInstance
public static JAXBContext newInstance(String contextPath, ClassLoader classLoader) throws JAXBException(Code)

Obtain a new instance of a JAXBContext class.

The client application must supply a context path which is a list of colon (':', \u005Cu003A) separated java package names that contain schema-derived classes and/or fully qualified JAXB-annotated classes. Schema-derived code is registered with the JAXBContext by the ObjectFactory.class generated per package. Alternatively than being listed in the context path, programmer annotated JAXB mapped classes can be listed in a jaxb.index resource file, format described below. Note that a java package can contain both schema-derived classes and user annotated JAXB classes. Additionally, the java package may contain JAXB package annotations that must be processed. (see JLS 3rd Edition, Section 7.4.1. "Package Annotations").

Every package listed on the contextPath must meet one or both of the following conditions otherwise a JAXBException will be thrown:

  1. it must contain ObjectFactory.class
  2. it must contain jaxb.index

Format for jaxb.index

The file contains a newline-separated list of class names. Space and tab characters, as well as blank lines, are ignored. The comment character is '#' (0x23); on each line all characters following the first comment character are ignored. The file must be encoded in UTF-8. Classes that are reachable, as defined in JAXBContext.newInstance(Class) , from the listed classes are also registered with JAXBContext.

Constraints on class name occuring in a jaxb.index file are:

  • Must not end with ".class".
  • Class names are resolved relative to package containing jaxb.index file. Only classes occuring directly in package containing jaxb.index file are allowed.
  • Fully qualified class names are not allowed. A qualified class name,relative to current package, is only allowed to specify a nested or inner class.

To maintain compatibility with JAXB 1.0 schema to java interface/implementation binding, enabled by schema customization , the JAXB provider will ensure that each package on the context path has a jaxb.properties file which contains a value for the javax.xml.bind.context.factory property and that all values resolve to the same provider. This requirement does not apply to JAXB annotated classes.

If there are any global XML element name collisions across the various packages listed on the contextPath, a JAXBException will be thrown.

Mixing generated interface/impl bindings from multiple JAXB Providers in the same context path may result in a JAXBException being thrown.
Parameters:
  contextPath - list of java package names that contain schema derived class and/or java to schema (JAXB-annotated)mapped classes
Parameters:
  classLoader - This class loader will be used to locate the implementationclasses. a new instance of a JAXBContext
throws:
  JAXBException - if an error was encountered while creating theJAXBContext such as

  1. failure to locate either ObjectFactory.class or jaxb.index in the packages
  2. an ambiguity among global elements contained in the contextPath
  3. failure to locate a value for the context factory provider property
  4. mixing schema derived packages from different providers on the same contextPath



newInstance
public static JAXBContext newInstance(String contextPath, ClassLoader classLoader, Map<String, ?> properties) throws JAXBException(Code)

Obtain a new instance of a JAXBContext class.

This is mostly the same as JAXBContext.newInstance(StringClassLoader) , but this version allows you to pass in provider-specific properties to configure the instanciation of JAXBContext .

The interpretation of properties is up to implementations.
Parameters:
  contextPath - list of java package names that contain schema derived classes
Parameters:
  classLoader - This class loader will be used to locate the implementation classes.
Parameters:
  properties - provider-specific properties a new instance of a JAXBContext
throws:
  JAXBException - if an error was encountered while creating theJAXBContext such as

  1. failure to locate either ObjectFactory.class or jaxb.index in the packages
  2. an ambiguity among global elements contained in the contextPath
  3. failure to locate a value for the context factory provider property
  4. mixing schema derived packages from different providers on the same contextPath

since:
   JAXB2.0



newInstance
public static JAXBContext newInstance(Class... classesToBeBound) throws JAXBException(Code)

Obtain a new instance of a JAXBContext class.

The client application must supply a list of classes that the new context object needs to recognize. Not only the new context will recognize all the classes specified, but it will also recognize any classes that are directly/indirectly referenced statically from the specified classes. Subclasses of referenced classes nor @XmlTransient referenced classes are not registered with JAXBContext. For example, in the following Java code, if you do newInstance(Foo.class), the newly created JAXBContext will recognize both Foo and Bar, but not Zot or FooBar:

 class Foo {
 @XmlTransient FooBar c;
 Bar b;
 }
 class Bar { int x; }
 class Zot extends Bar { int y; }
 class FooBar { }
 
Therefore, a typical client application only needs to specify the top-level classes, but it needs to be careful.

Note that for each java package registered with JAXBContext, when the optional package annotations exist, they must be processed. (see JLS 3rd Edition, Section 7.4.1. "Package Annotations").
Parameters:
  classesToBeBound - list of java classes to be recognized by the new JAXBContext.Can be empty, in which case a JAXBContext that only knows aboutspec-defined classes will be returned.A new instance of a JAXBContext. Always non-null valid object.
throws:
  JAXBException - if an error was encountered while creating theJAXBContext, such as (but not limited to):

  1. No JAXB implementation was discovered
  2. Classes use JAXB annotations incorrectly
  3. Classes have colliding annotations (i.e., two classes with the same type name)
  4. The JAXB implementation was unable to locateprovider-specific out-of-band information (such as additionalfiles generated at the development time.)

throws:
  IllegalArgumentException - if the parameter contains null (i.e., newInstance(null); )
since:
   JAXB2.0



newInstance
public static JAXBContext newInstance(Class[] classesToBeBound, Map<String, ?> properties) throws JAXBException(Code)

Obtain a new instance of a JAXBContext class.

An overloading of JAXBContext.newInstance(Class) to configure 'properties' for this instantiation of JAXBContext .

The interpretation of properties is implementation specific.
Parameters:
  classesToBeBound - list of java classes to be recognized by the new JAXBContext.Can be empty, in which case a JAXBContext that only knows aboutspec-defined classes will be returned.A new instance of a JAXBContext. Always non-null valid object.
throws:
  JAXBException - if an error was encountered while creating theJAXBContext, such as (but not limited to):

  1. No JAXB implementation was discovered
  2. Classes use JAXB annotations incorrectly
  3. Classes have colliding annotations (i.e., two classes with the same type name)
  4. The JAXB implementation was unable to locateprovider-specific out-of-band information (such as additionalfiles generated at the development time.)

throws:
  IllegalArgumentException - if the parameter contains null (i.e., newInstance(null); )
since:
   JAXB2.0



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.