通过模式生成默认绑定和代码
通过 XML 模式定义生成 JiBX 绑定定义及相应的 Java 代码十分简单。您将在本节中了解具体操作。
简单示例模式简介
我将使用 第 1 部分 中生成的一个模式作为一个简单示例。清单 1 显示了该模式的简短版本,用于表示在线商店的订单。样例代码的 dwcode2 目录中的 starter.xsd 提供了完整的模式。
清单 1. 第一个示例模式
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://jibx.org/starter" elementFormDefault="qualified"
targetNamespace="http://jibx.org/starter">
<xs:simpleType name="shipping">
<xs:annotation>
<xs:documentation>Supported shipment methods. The "INTERNATIONAL" shipment
methods can only be used for orders with shipping addresses outside the U.S., and
one of these methods is required in this case.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:enumeration value="STANDARD_MAIL"/>
<xs:enumeration value="PRIORITY_MAIL"/>
<xs:enumeration value="INTERNATIONAL_MAIL"/>
...
</xs:restriction>
</xs:simpleType>
<xs:complexType name="item">
<xs:annotation>
<xs:documentation>Order line item information.</xs:documentation>
</xs:annotation>
<xs:sequence/>
<xs:attribute type="xs:string" use="required" name="id">
<xs:annotation>
<xs:documentation>Stock identifier. This is expected to be 12 characters in
length, with two leading alpha characters followed by ten decimal digits.
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute type="xs:int" use="required" name="quantity">
<xs:annotation>
<xs:documentation>Number of units ordered.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute type="xs:float" use="required" name="price">
<xs:annotation>
<xs:documentation>Price per unit.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="address">
<xs:annotation>
<xs:documentation>Address information.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element type="xs:string" name="street1">
<xs:annotation>
<xs:documentation>First line of street information (required).
</xs:documentation>
</xs:annotation>
</xs:element>
...
</xs:sequence>
<xs:attribute type="xs:string" name="state">
<xs:annotation>
<xs:documentation>State abbreviation (required for the U.S. and Canada,
optional otherwise).</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute type="xs:string" name="postCode">
<xs:annotation>
<xs:documentation>Postal code (required for the U.S. and Canada, optional
otherwise).</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="customer">
<xs:annotation>
<xs:documentation>Customer information.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element type="xs:long" name="customerNumber"/>
...
</xs:sequence>
</xs:complexType>
<xs:element type="tns:order" name="order"/>
<xs:complexType name="order">
<xs:annotation>
<xs:documentation>Order information.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element type="xs:long" name="orderNumber"/>
<xs:element type="tns:customer" name="customer"/>
<xs:element type="tns:address" name="billTo">
<xs:annotation>
<xs:documentation>Billing address information.</xs:documentation>
</xs:annotation>
</xs:element>
...
<xs:element type="tns:item" name="item" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute type="xs:date" use="required" name="orderDate">
<xs:annotation>
<xs:documentation>Date order was placed with server.</xs:documentation>
</xs:annotation>
</xs:attribute>
...
</xs:complexType>
</xs:schema>
|

 |

|
生成默认绑定和代码
要通过 XML 模式生成 JiBX 绑定和 Java 类,您只需运行 JiBX 发行版中的 jibx-tools.jar 附带的 org.jibx.schema.codegen.CodeGen 工具。您可以通过命令行直接运行该工具,也可以通过 Apache Ant 之类的构建工具间接运行该工具。
本教程的下载部分包括 Ant build.xml 脚本,其中 codegen 目标用于运行生成。
要尝试使用此脚本,请打开已安装下载的 dwcode2 目录中的控制台,然后输入 ant codegen。如果系统中安装了 Ant 并且根据说明安装了下载代码,则应当会看到类似图 1 所示的输出:
图 1. 使用 Ant 构建
您还可以直接从控制台运行 CodeGen。为此,您需要:
- 在 Java 类路径中包括 jibx-tools.jar。
- 指定
org.jibx.schema.codegen.CodeGen 为要运行的类。
- 列出要生成的模式定义。
提供的 Ant codegen 目标将使用几个附加参数告诉 CodeGen 使用 gen/src 目录作为生成的数据模型包结构的根目录,并且在运行生成前删除该目录中的所有现有文件。下面是用于从 dwcode2 目录的控制台中复制 Ant codegen 目标的 Java 命令行(假定您遵循了推荐安装说明):
java -cp ../lib/jibx-tools.jar org.jibx.schema.codegen.CodeGen -t gen/src -w starter.xsd |
对于 Windows,该命令为:
java -cp ..\lib\jibx-tools.jar org.jibx.schema.codegen.CodeGen -t gen\src -w starter.xsd |
您可以通过命令行向 CodeGen 传递许多其他选项。您稍后将在教程中看到这些选项。现在,让我们看一看生成的 Java 代码。
生成的工件
生成的代码由五个类组成,分别对应于 清单 1 模式中的五个全局类型定义。清单 2 显示了生成的代码的一些样例,其中包括 org.jibx.starter.Order 类的摘录及整个 org.jibx.starter.Shipping 类:
清单 2. 生成的代码
/**
* Order information.
*
* Schema fragment(s) for this class:
* <pre>
* <xs:complexType xmlns:ns="http://jibx.org/starter"
xmlns:xs="http://www.w3.org/2001/XMLSchema" name="order">
* <xs:sequence>
* <xs:element type="xs:long" name="orderNumber"/>
* <xs:element type="ns:customer" name="customer"/>
* <xs:element type="ns:address" name="billTo"/>
* <xs:element type="ns:shipping" name="shipping"/>
* <xs:element type="ns:address" name="shipTo" minOccurs="0"/>
* <xs:element type="ns:item" name="item" minOccurs="0" maxOccurs="unbounded"/>
* </xs:sequence>
* <xs:attribute type="xs:date" use="required" name="orderDate"/>
* <xs:attribute type="xs:date" name="shipDate"/>
* <xs:attribute type="xs:float" name="total"/>
* </xs:complexType>
* </pre>
*/
public class Order
{
private long orderNumber;
private Customer customer;
private Address billTo;
private Shipping shipping;
private Address shipTo;
private List<Item> itemList = new ArrayList<Item>();
private Date orderDate;
private Date shipDate;
private Float total;
...
/**
* Get the 'shipTo' element value. Shipping address information. If missing, the
* billing address is also used as the shipping address.
*/
public Address getShipTo() {
return shipTo;
}
/**
* Set the 'shipTo' element value. Shipping address information. If missing, the
* billing address is also used as the shipping address.
*/
public void setShipTo(Address shipTo) {
this.shipTo = shipTo;
}
/**
* Get the list of 'item' element items.
*/
public List<Item> getItems() {
return itemList;
}
/**
* Set the list of 'item' element items.
*/
public void setItems(List<Item> list) {
itemList = list;
}
...
}
/**
* Supported shipment methods. The "INTERNATIONAL" shipment methods can only be used
for orders with shipping addresses outside the U.S., and one of these methods is
required in this case.
*
* Schema fragment(s) for this class:
* <pre>
* <xs:simpleType xmlns:xs="http://www.w3.org/2001/XMLSchema" name="shipping">
* <xs:restriction base="xs:string">
* <xs:enumeration value="STANDARD_MAIL"/>
* <xs:enumeration value="PRIORITY_MAIL"/>
* <xs:enumeration value="INTERNATIONAL_MAIL"/>
* <xs:enumeration value="DOMESTIC_EXPRESS"/>
* <xs:enumeration value="INTERNATIONAL_EXPRESS"/>
* </xs:restriction>
* </xs:simpleType>
* </pre>
*/
public enum Shipping {
STANDARD_MAIL, PRIORITY_MAIL, INTERNATIONAL_MAIL, DOMESTIC_EXPRESS,
INTERNATIONAL_EXPRESS
} |
通过 清单 2 可以看到,CodeGen 在生成的代码中将模式文档自动转换为 Javadoc(在此处显示为每个类 Javadoc 中的前导注释,以及显示为 getShipTo() 和 setShipTo() 方法的注释部分)。默认情况下,CodeGen 还合并类 Javadoc 中的实际模式定义,而对于 get/set 属性访问方法,它将描述与属性对应的模式组件。
对于重复值,例如清单 1 订单 complexType 定义中的重复项目元素,CodeGen 将默认生成 Java 5 类型的列表。对于 simpleType 限制枚举,例如清单 1 中的送货类型,CodeGen 将默认生成 Java 5 枚举类型。清单 2 中显示了为这两个实例生成的代码。
生成的 JiBX 绑定
除了生成的代码之外,CodeGen 还将产生 JiBX 绑定定义(在本例中为 binding.xml 文件),该绑定定义将告诉 JiBX 绑定编译器如何在 Java 类与 XML 之间进行转换。绑定定义包含 JiBX 所完成的转换的完整信息,因此它们必然非常复杂。幸运的是,使用 CodeGen 绑定和代码生成,您无需了解绑定定义即可使用 JiBX,因此本教程不会介绍这部分的详细信息。
|