prev table of contents next

5.2 Defining Package Names

Some Java entities don't have a counterpart in an XML schema. One of these things is the package name. By default, JAXB uses the somewhat unimaginative package name generated. You can set a more appropriate package name in the call of the JAXB compiler, in addition to the option defining the root directory for the generated source files.:

xjc -p jess.ruleml -d gen-src RuleML.xsd
This, however, is neither flexible enough nor easy to maintain. A better place would be in the schema file itself where you may write an xsd:annotation element containing an xsd:appinfo sub-element:
<xsd:annotation>
  <xsd:appinfo>
    <jaxb:schemaBindings>
      <jaxb:package name="jess.ruleml"/>
    </jaxb:schemaBindings>
  </xsd:appinfo>
</xsd:annotation>
If you don't want to burden your XML schema with these annotations you can collect this and other customizing directives in a separate file, ususally of file type .xjb. This XML file uses elements from the jaxb namespace. Its outermost element is jaxb:bindings, where you would indicate the namespace URI:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               version="2.0">
  ...
</jaxb:bindings>
You pass the file name to the schema compiler:
xjc -b RuleML.xjb -d gen-src RuleML.xsd
To put all of your classes into the same package you define the package name in a jaxb:schemaBindings element at the outermost level:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               version="2.0">
  <jaxb:schemaBindings>
    <jaxb:package name="jess.ruleml"/>
  </jaxb:schemaBindings>
</jaxb:bindings>
A slightly more complicated element structure is necessary if you want to compile several schema files in one run and the classes resulting from different schemata should emerge in separate packages. Here is an example:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               jaxb:version="2.0">
  <jaxb:bindings schemaLocation="Flight.xsd"  node="/xsd:schema">
    <jaxb:schemaBindings>
      <jaxb:package name="travel.flight"/>
    </jaxb:schemaBindings>
  </jaxb:bindings>
  <jaxb:bindings schemaLocation="Hotel.xsd" node="/xsd:schema">
    <jaxb:schemaBindings>
      <jaxb:package name="travel.hotel"/>
    </jaxb:schemaBindings>
  </jaxb:bindings>
</jaxb:bindings>
Now we have nested jaxb:bindings elements, with the inner ones being associated with some xsd:schema element, the outermost element of an XML schema. The value of the node attribute is an
XPath expression referring to that outermost element. Notice that it's necessary to define the mapping of the XML Schema namespace prefix (here: xsd) to its URI in the top-level jaxb:bindings element.


prev table of contents next