Skip to main content
Jump to: navigation, search

Difference between revisions of "EclipseLink/Development/2.1/DynamicMOXy/296967/BootstrapFromXSD"

Line 7: Line 7:
 
Support for bootstrapping a <tt>DynamicJAXBContext</tt> from an XML Schema file (XSD) will be implemented in multiple phases.  For each phase, test schemas will be created that would result in the following annotations being generated by XJC.  A <tt>DynamicJAXBContext</tt> will then be created from each schema, and an example object will be marshalled.  The resulting document will be inspected to ensure that it was marshalled properly, according to the annotations being tested.
 
Support for bootstrapping a <tt>DynamicJAXBContext</tt> from an XML Schema file (XSD) will be implemented in multiple phases.  For each phase, test schemas will be created that would result in the following annotations being generated by XJC.  A <tt>DynamicJAXBContext</tt> will then be created from each schema, and an example object will be marshalled.  The resulting document will be inspected to ensure that it was marshalled properly, according to the annotations being tested.
  
== Generating a <tt>DynamicProject</tt> with XJC ==
+
== Generating a DynamicProject with XJC ==
  
 
A <tt>DynamicProject</tt> is created from an XSD in the following way:
 
A <tt>DynamicProject</tt> is created from an XSD in the following way:
Line 16: Line 16:
 
# A Dynamic Project is then created from the standard project via <tt>DynamicTypeBuilder.loadDynamicProject()</tt>.
 
# A Dynamic Project is then created from the standard project via <tt>DynamicTypeBuilder.loadDynamicProject()</tt>.
 
# An <tt>org.eclipse.persistence.oxm.XMLContext</tt> is then created from the Dynamic Project, and a <tt>DynamicJAXBContext</tt> is returned backed by this <tt>XMLContext</tt>.
 
# An <tt>org.eclipse.persistence.oxm.XMLContext</tt> is then created from the Dynamic Project, and a <tt>DynamicJAXBContext</tt> is returned backed by this <tt>XMLContext</tt>.
 +
 +
<br><br>
  
 
== Development Phases ==
 
== Development Phases ==

Revision as of 15:48, 19 March 2010

Dynamic MOXy - Bootstrapping from XML Schema

Overview

Support for bootstrapping a DynamicJAXBContext from an XML Schema file (XSD) will be implemented in multiple phases. For each phase, test schemas will be created that would result in the following annotations being generated by XJC. A DynamicJAXBContext will then be created from each schema, and an example object will be marshalled. The resulting document will be inspected to ensure that it was marshalled properly, according to the annotations being tested.

Generating a DynamicProject with XJC

A DynamicProject is created from an XSD in the following way:

  1. The schema is parsed using com.sun.tools.xjc.api.SchemaCompiler, resulting in a com.sun.codemodel.JCodeModel being generated.
  2. JCodeModel contains XJC representations (com.sun.codemodel.JDefinedClass) of the classes to be generated. We iterate over the collection of JDefinedClasses to create EclipseLink representations of the same classes (org.eclipse.persistence.jaxb.javamodel.xjc.*)
  3. This collection of EclipseLink javamodel classes is then passed to org.eclipse.persistence.jaxb.compiler.Generator, which generates a standard EclipseLink project.
  4. A Dynamic Project is then created from the standard project via DynamicTypeBuilder.loadDynamicProject().
  5. An org.eclipse.persistence.oxm.XMLContext is then created from the Dynamic Project, and a DynamicJAXBContext is returned backed by this XMLContext.



Development Phases

Phase 1

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlNs xml-ns         N/A ?

Only used for package-info.java

XmlSchema xml-schema X       N/A ?

Only used for package-info.java

@javax.xml.bind.annotation.XmlSchema (
   namespace = "http://www.example.com/MYPO1"
)
 
<schema
   xmlns=...
   xmlns:po=....
   targetNamespace="http://www.example.com/MYPO1">
 
 
@javax.xml.bind.annotation.XmlSchema (
   xmlns = { 
      @javax.xml.bind.annotation.XmlNs(prefix = "po", 
         namespaceURI="http://www.example.com/myPO1"),
      @javax.xml.bind.annotation.XmlNs(prefix="xs",
         namespaceURI="http://www.w3.org/2001/XMLSchema")
   )
)
 
<schema
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:po="http://www.example.com/PO1"
   targetNamespace="http://www.example.com/PO1">
 
 
@javax.xml.bind.annotation.XmlSchema (
   elementFormDefault=XmlNsForm.UNQUALIFIED
   ...
)
 
<schema
   xmlns="http://www.w3.org/2001/XMLSchema"
   xmlns:po="http://www.example.com/PO1"
   elementFormDefault="unqualified">
XmlSeeAlso xml-see-also   X     N/A

XmlSeeAlso will be generated if there is an inheritance relationship in the schema's complex types.

XmlTransient xml-transient   X X X N/A



Limitations

  • XJC does not interpret the "attributeFormDefault" property during schema parsing, and will therefore not generate this property in the XmlSchema annotation.
  • There is no XML Schema construct that will result in an XmlTransient annotation being generated.



Phase 2

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlAccessorOrder xml-accessor-order X X     N/A
XmlAccessorType xml-accessor-type X X     N/A
XmlRootElement xml-root-element   X    
@XmlRootElement(name="PriceElement")
public class USPrice {
}
 
<!-- Example: XML schema definition -->
<xs:element name="PriceElement" type="USPrice"/>
   <xs:complexType name="USPrice">
      <xs:sequence>
         <xs:element name="price" type="xs:decimal"/>
      </sequence>
   </xs:complexType>
XmlType xml-type   X    
@XmlType(propOrder={"street", "city" , "state", "zip", "name" })
public class USAddress {
}
 
<xs:complexType name="USAddress">
   <xs:sequence>
      <xs:element name="street" type="xs:string"/>
      <xs:element name="city" type="xs:string"/>
      <xs:element name="state" type="xs:string"/>
      <xs:element name="zip" type="xs:decimal"/>
      <xs:element name="name" type="xs:string"/>
   </xs:all>
</xs:complexType>



Phase 3

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlAttribute xml-attribute     X X
public class USPrice { 
   @XmlAttribute
   public java.math.BigDecimal price;
}
 
<xs:complexType name="USPrice">
   <xs:sequence>
   </xs:sequence>
   <xs:attribute name="price" type="xs:decimal"/>
</xs:complexType>
XmlElement xml-element     X X
public class USPrice {
   @XmlElement(name="itemprice")
   public java.math.BigDecimal price;
}
 
<xs:complexType name="USPrice"/>
   <xs:sequence>
      <xs:element name="itemprice" type="xs:decimal" minOccurs="0"/>
   </sequence>
</xs:complexType>
XmlAdapter xml-java-type-adapter X X X X N/A ?

Referenced in XJC code but unsure how it is specified from Schema



Phase 4

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlElementWrapper xml-element-wrapper     X X N/A
XmlList xml-list     X X N/A
XmlValue xml-value X X
public class USPrice {
   @XmlValue
   public java.math.BigDecimal price;
}
 
<xs:simpleType name="USPrice">
   <xs:restriction base="xs:decimal"/>
</xs:simpleType>
 
 
public class InternationalPrice {
   @XmlValue
   public java.math.BigDecimal price;
   @XmlAttribute
   public String currency;
}
 
<xs:complexType name="InternationalPrice">
   <xs:simpleContent>
      <xs:extension base="xs:decimal">
         <xs:attribute name="currency" type="xs:string"/>
      </xs:extension>
   </xs:simpleContent>
</xs:complexType>
XmlAnyElement xml-any-element     X X
class Foo {
   int a;
   int b;
   @XmlAnyElement
   List<Element> any;
}
 
<xs:complexType name="foo">
   <xs:sequence>
      <xs:element name="a" type="xs:int" />
      <xs:element name="b" type="xs:int" />
      <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" />
   </xs:sequence>
</xs:complexType>
XmlAnyAttribute xml-any-attribute     X X N/A ?

Referenced in XJC code but unsure how it is specified from Schema

XmlMixed xml-mixed     X X
public class LetterBody {
   @XmlMixed 
   @XmlElementRefs({
      @XmlElementRef(name="productName", type=JAXBElement.class),
      @XmlElementRef(name="quantity", type=JAXBElement.class),
      @XmlElementRef(name="name", type=JAXBElement.class)})
   List getContent() {...}
}
 
<xs:complexType name="letterBody" mixed="true">
   <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="quantity" type="xs:positiveInteger"/>
      <xs:element name="productName" type="xs:string"/>
   </xs:sequence>
</xs:complexType>



Phase 5

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlID
xml-id     X X
public class Customer {
   @XmlAttribute
   @XmlID
   public String getCustomerID();
   public void setCustomerID(String id);
}
 
<xs:complexType name="Customer">
   <xs:complexContent>
      <xs:sequence>
         ....
      </xs:sequence>
      <xs:attribute name="customerID" type="xs:ID"/>
   </xs:complexContent>
</xs:complexType>
XmlIDREF
xml-idref   X
X
public class Shipping {
   @XmlIDREF
   public Customer getCustomer();
   public void setCustomer(Customer customer);
   ....
}
 
<xs:complexType name="Shipping">
   <xs:complexContent>
      <xs:sequence>
         <xs:element name="customer" type="xs:IDREF"/>
         ....
      </xs:sequence>
   </xs:complexContent>
</xs:complexType>



Phase 6

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlElements xml-elements     X X
public class Foo {
   @XmlElements(
      @XmlElement(name="A", type=Integer.class),
      @XmlElement(name="B", type=Float.class)
   }
   public List items;
}
 
<xs:complexType name="Foo">
   <xs:sequence>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
         <xs:element name="A" type="xs:int"/>
         <xs:element name="B" type="xs:float"/>
      <xs:choice>
   </xs:sequence>
</xs:complexType>
XmlElementRef xml-element-ref     X X
@XmlRootElement(name="target")
class Target {
   // The presence of @XmlElementRef indicates that the XML
   // element name will be derived from the @XmlRootElement 
   // annotation on the type (for e.g. "jar" for JarTask). 
   @XmlElementRef
   List<Task> tasks;
}
 
abstract class Task {}
 
@XmlRootElement(name="jar")
class JarTask extends Task {
   ...
}
 
@XmlRootElement(name="javac")
class JavacTask extends Task {
   ...
}
 
<xs:element name="target" type="Target">
   <xs:complexType name="Target">
      <xs:sequence>
         <xs:choice maxOccurs="unbounded">
            <xs:element ref="jar">
            <xs:element ref="javac">
         </xs:choice>
      </xs:sequence>
   </xs:complexType>
XmlElementRefs xml-element-refs X
X
N/A ?

Referenced in XJC code but unsure how it is specified from Schema



Phase 7

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlSchemaType xml-schema-type X   X X
public class USPrice {
   @XmlElement
   @XmlSchemaType(name="date")
   public XMLGregorianCalendar date;
}
 
<xs:complexType name="USPrice"/>
   <xs:sequence>
      <xs:element name="date" type="xs:date"/>
   </sequence>
</xs:complexType>
XmlSchemaTypes xml-schema-types X    
 
N/A



Phase 8

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlEnum xml-enum   X    
@XmlEnum(String.class)
public enum Card { CLUBS, DIAMONDS, HEARTS, SPADES }
 
<xs:simpleType name="Card">
   <xs:restriction base="xs:string"/>
      <xs:enumeration value="CLUBS"/>
      <xs:enumeration value="DIAMONDS"/>
      <xs:enumeration value="HEARTS"/>
      <xs:enumeration value="SPADES"/>
</xs:simpleType>
XmlEnumValue xml-enum-value     X
X
N/A



Phase 9

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlInlineBinaryData xml-inline-binary-data   X X X N/A



Phase 10

Annotation XML Metadata Tag Package Type Field Method Schema Representation
XmlElementDecl xml-element-decl     X X N/A ?

Only used for ObjectFactory.java

XmlRegistry xml-registry   X     N/A ?

Only used for ObjectFactory.java

Back to the top