General Usage Instructions
This section provides general usage instructions for the examples used in this chapter, including how to build and run the applications using the Ant build tool, and provides details about the default schema-to-JAXB bindings used in these examples.
Description
This chapter describes three sets of examples:
- The Basic examples (Modify Marshal, Unmarshal Validate, Pull Parser) demonstrate basic JAXB concepts like ummarshalling, marshalling, validating XML content, and parsing XML data.
- The Customize examples (Customize Inline, Datatype Converter, External Customize) demonstrate various ways of customizing the binding of XML schemas to Java objects.
- The Java-to-Schema examples show how to use annotations to map Java classes to XML schema.
The Basic and Customize examples are based on a Purchase Order scenario. With the exception of the Fix Collides example, each uses an XML document,
po.xml
, written against an XML schema,po.xsd
.Table 16-9 briefly describes the Basic examples.
Table 16-10 briefly describes the Customize examples.
Table 16-11 briefly describes the Java-to-Schema examples.
Each Basic and Customize example directory contains several base files:
po.xsd
is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations. Note that the Fix Collides example usesexample.xsd
rather thanpo.xsd
.po.xml
is the Purchase Order XML file containing sample XML content, and is the file you will unmarshal into a Java content tree in each example. This file is almost exactly the same in each example, with minor content differences to highlight different JAXB concepts. Note that the Fix Collides example usesexample.xml
rather thanpo.xml
.Main.java
is the main Java class for each example.build.xml
is an Ant project file provided for your convenience. Use Ant to generate, compile, and run the schema-derived JAXB classes automatically. Thebuild.xml
file varies across the examples.MyDatatypeConverter.java
in theinline-customize
example is a Java class used to provide custom datatype conversions.binding.xjb
in the External Customize and Fix Collides examples is an external binding declarations file that is passed to the JAXB binding compiler to customize the default JAXB bindings.example.xsd
in the Fix Collides example is a short schema file that contains deliberate naming conflicts, to show how to resolve such conflicts with custom JAXB bindings.Using the Examples
As with all applications that implement schema-derived JAXB classes, as described above, there are two distinct phases in using JAXB:
You perform these steps by using
ant
with thebuild.xml
project file included in each example directory. Use the "runapp" target to build, compile, and run each example and, when you are done examining everything that gets created and/or consumed, use the "clean" target to clean out the generated files and directories.Configuring and Running the Samples
The
build.xml
file included in each example directory is an ant project file that, when run, automatically performs the following steps:
- Updates your
CLASSPATH
to include the necessary schema-derived JAXB classes.- For the Basic and Customize examples, runs the JAXB binding compiler to generate JAXB Java classes from the XML source schema,
po.xsd
, and puts the classes in a package namedprimer.po
. For the Java-to-Schema examples runsschemagen
, the schema generator, to generate XML schema from the annotated Java classes.- Compiles the schema-derived JAXB classes or the annotated Java code.
- Runs the
Main
class for the example.The schema-derived JAXB classes and how they are bound to the source schema is described in About the Schema-to-Java Bindings. The methods used for building and processing the Java content tree are described in Basic Examples.
JAXB Compiler Options
The JAXB XJC schema binding compiler transforms, or binds, a source XML schema to a set of JAXB content classes in the Java programming language. The compiler,
xjc
, is provided in two flavors in the Application Server:xjc.sh
(Solaris/Linux) andxjc.bat
(Windows). Bothxjc.sh
andxjc.bat
take the same command-line options. You can display quick usage instructions by invoking the scripts without any options, or with the-help
switch. The syntax is as follows:The
xjc
command-line options are listed in Table 16-12.
JAXB Schema Generator Options
The JAXB Schema Generator,
schemagen
, creates a schema file for each namespace referenced in your Java classes. The schema generator can be launched using the appropriate schemagen shell script in thebin
directory for your platform. The schema generator processes Java source files only. If your Java sources reference other classes, those sources must be accessible from your system CLASSPATH environment variable or errors will occur when the schema is generated. There is no way to control the name of the generated schema files.You can display quick usage instructions by invoking the scripts without any options, or with the
-help
switch. The syntax is as follows:The
schemagen
command-line options are listed in Table 16-13.
Table 16-13 schemagen Command-Line Options Option or Argument Description -d path Specifies the location of the processor- and javac-generated class files.
About the Schema-to-Java Bindings
When you run the JAXB binding compiler against the
po.xsd
XML schema used in the basic examples (Unmarshal Read, Modify Marshal, Unmarshal Validate), the JAXB binding compiler generates a Java package namedprimer.po
containing eleven classes, making a total of twelve classes in each of the basic examples:
Note: You should never directly use the generated implementation classes--that is,
*Impl.java
in the <packagename
>/impl
directory. These classes are not directly referenceable because the class names in this directory are not standardized by the JAXB specification. TheObjectFactory
method is the only portable means to create an instance of a schema-derived interface. There is also anObjectFactory.newInstance(Class JAXBinterface)
method that enables you to create instances of interfaces.
These classes and their specific bindings to the source XML schema for the basic examples are described below.
Schema-Derived JAXB Classes
The code for the individual classes generated by the JAXB binding compiler for the Basic examples is listed below, followed by brief explanations of its functions. The classes listed here are:
Comment.java
In
Comment.java
:
- The
Comment.java
class is part of theprimer.po
package.Comment
is a public interface that extendsjavax.xml.bind.Element
.- Content in instantiations of this class bind to the XML schema element named
comment
.- The
getValue()
andsetValue()
methods are used to get and set strings representing XMLcomment
elements in the Java content tree.The
Comment.java
code looks like this:package primer.po; public interface Comment extends javax.xml.bind.Element { String getValue(); void setValue(String value); }Items.java
In
Items.java
, below:
- The
Items.java
class is part of theprimer.po
package.- The class provides public interfaces for
Items
andItemType
.- Content in instantiations of this class bind to the XML ComplexTypes
Items
and its child elementItemType
.Item
provides thegetItem()
method.ItemType
provides methods for:
getPartNum();
setPartNum(String value);
getComment();
setComment(java.lang.String value);
getUSPrice();
setUSPrice(java.math.BigDecimal value);
getProductName();
setProductName(String value);
getShipDate();
setShipDate(java.util.Calendar value);
getQuantity();
setQuantity(java.math.BigInteger value);
The
Items.java
code looks like this:package primer.po; public interface Items { java.util.List getItem(); public interface ItemType { String getPartNum(); void setPartNum(String value); java.lang.String getComment(); void setComment(java.lang.String value); java.math.BigDecimal getUSPrice(); void setUSPrice(java.math.BigDecimal value); String getProductName(); void setProductName(String value); java.util.Calendar getShipDate(); void setShipDate(java.util.Calendar value); java.math.BigInteger getQuantity(); void setQuantity(java.math.BigInteger value); } }ObjectFactory.java
In
ObjectFactory.java
, below:For example, in this case, for the Java interface
primer.po.Items.ItemType
,ObjectFactory
creates the methodcreateItemsItemType()
.The
ObjectFactory.java
code looks like this:package primer.po; public class ObjectFactory extends com.sun.xml.bind.DefaultJAXBContextImpl { /** * Create a new ObjectFactory that can be used to create * new instances of schema derived classes for package: * primer.po */ public ObjectFactory() { super(new primer.po.ObjectFactory.GrammarInfoImpl()); } /** * Create an instance of the specified Java content * interface. */ public Object newInstance(Class javaContentInterface) throws javax.xml.bind.JAXBException { return super.newInstance(javaContentInterface); } /** * Get the specified property. This method can only be * used to get provider specific properties. * Attempting to get an undefined property will result * in a PropertyException being thrown. */ public Object getProperty(String name) throws javax.xml.bind.PropertyException { return super.getProperty(name); } /** * Set the specified property. This method can only be * used to set provider specific properties. * Attempting to set an undefined property will result * in a PropertyException being thrown. */ public void setProperty(String name, Object value) throws javax.xml.bind.PropertyException { super.setProperty(name, value); } /** * Create an instance of PurchaseOrder */ public primer.po.PurchaseOrder createPurchaseOrder() throws javax.xml.bind.JAXBException { return ((primer.po.PurchaseOrder) newInstance((primer.po.PurchaseOrder.class))); } /** * Create an instance of ItemsItemType */ public primer.po.Items.ItemType createItemsItemType() throws javax.xml.bind.JAXBException { return ((primer.po.Items.ItemType) newInstance((primer.po.Items.ItemType.class))); } /** * Create an instance of USAddress */ public primer.po.USAddress createUSAddress() throws javax.xml.bind.JAXBException { return ((primer.po.USAddress) newInstance((primer.po.USAddress.class))); } /** * Create an instance of Comment */ public primer.po.Comment createComment() throws javax.xml.bind.JAXBException { return ((primer.po.Comment) newInstance((primer.po.Comment.class))); } /** * Create an instance of Comment */ public primer.po.Comment createComment(String value) throws javax.xml.bind.JAXBException { return new primer.po.impl.CommentImpl(value); } /** * Create an instance of Items */ public primer.po.Items createItems() throws javax.xml.bind.JAXBException { return ((primer.po.Items) newInstance((primer.po.Items.class))); } /** * Create an instance of PurchaseOrderType */ public primer.po.PurchaseOrderType createPurchaseOrderType() throws javax.xml.bind.JAXBException { return ((primer.po.PurchaseOrderType) newInstance((primer.po.PurchaseOrderType.class))); } }PurchaseOrder.java
In
PurchaseOrder.java
, below:The
PurchaseOrder.java
code looks like this:package primer.po; public interface PurchaseOrder extends javax.xml.bind.Element, primer.po.PurchaseOrderType{ }PurchaseOrderType.java
In
PurchaseOrderType.java
, below:The
PurchaseOrderType.java
code looks like this:package primer.po; public interface PurchaseOrderType { primer.po.Items getItems(); void setItems(primer.po.Items value); java.util.Calendar getOrderDate(); void setOrderDate(java.util.Calendar value); java.lang.String getComment(); void setComment(java.lang.String value); primer.po.USAddress getBillTo(); void setBillTo(primer.po.USAddress value); primer.po.USAddress getShipTo(); void setShipTo(primer.po.USAddress value); }USAddress.java
In
USAddress.java
, below:The
USAddress.java
code looks like this:package primer.po; public interface USAddress { String getState(); void setState(String value); java.math.BigDecimal getZip(); void setZip(java.math.BigDecimal value); String getCountry(); void setCountry(String value); String getCity(); void setCity(String value); String getStreet(); void setStreet(String value); String getName(); void setName(String value); }