org.eclipse.xsd.util
Class XSDPrototypicalSchema

java.lang.Object
  extended by org.eclipse.xsd.util.XSDPrototypicalSchema

public class XSDPrototypicalSchema
extends java.lang.Object

The Sample Code to construct the Purchase Order Schema and the Prototype Schema using the concrete APIs.

Additional examples include:

  1. saving the Purchase Order Schema to a URI,
  2. printing a schema loaded from a URI,
  3. printing a component's element using Xerces,
  4. creating a schema from an element,
  5. tracing a resource set's loading behavior when loading a URI
  6. cloning a component.
  7. and cross referencing.


Field Summary
protected static XSDPrototypicalSchema instance
          The one static instance.
 XSDSchema prototypeSchema
          The Prototype Schema.
 XSDSchema purchaseOrderSchema
          The Purchase Order Schema.
protected  java.lang.String someOtherSchemaURI
          The value "http://www.example.com/SomeOtherSchema".
 XSDFactory xsdFactory
          A cached XSDFactory.
 
Constructor Summary
XSDPrototypicalSchema()
          Create the Prototype Schema and the Purchase Order Schema.
 
Method Summary
 XSDConcreteComponent cloneComponent(XSDConcreteComponent xsdConcreteComponent, boolean preserveDOM)
          
 XSDSchema createSchema(org.w3c.dom.Element element)
          
 void crossReferenceTest(java.io.PrintStream out)
          
static XSDPrototypicalSchema getInstance()
          Return the one static instance.
static XSDSchema getPrototypicalSchema()
          Returns the Prototype Schema instance.
static XSDSchema getPurchaseOrderSchema()
          Returns the Purchase Order Schema instance.
 void initializeFancyListTypeDefinition()
          
 XSDSchema initializePrototypeSchema()
          
 XSDSchema initializePurchaseOrderSchema()
          
 void initializeSimpleAttributeDeclaration()
          
 void initializeSimpleAttributeGroupDefinition()
          
 void initializeSimpleContentComplexTypeDefinition()
          
 void initializeSimpleElementDeclarationWithAnonymousType()
          
 void initializeSimpleListTypeDefinition()
          
 void initializeSimpleRecursiveComplexTypeDefinition()
          
 void initializeSimpleRecursiveElementDeclaration()
          
 void initializeSimpleRecursiveModelGroupDefinition()
          
 void initializeSimpleTypeDefinition()
          
 void initializeSimpleUnionTypeDefinition()
          
 void initializeSomeOtherImport()
          
 void printComponent(java.io.OutputStream outputStream, XSDConcreteComponent xsdConcreteComponent)
          
 void printSchema(java.lang.String xsdSchemaURI)
          
 void savePurchaseOrderSchema(java.lang.String schemaURI)
          
 void traceLoading(java.lang.String xsdSchemaURI)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

protected static XSDPrototypicalSchema instance
The one static instance.


xsdFactory

public XSDFactory xsdFactory
A cached XSDFactory.


prototypeSchema

public XSDSchema prototypeSchema
The Prototype Schema.


purchaseOrderSchema

public XSDSchema purchaseOrderSchema
The Purchase Order Schema.


someOtherSchemaURI

protected java.lang.String someOtherSchemaURI
The value "http://www.example.com/SomeOtherSchema".

Constructor Detail

XSDPrototypicalSchema

public XSDPrototypicalSchema()
Create the Prototype Schema and the Purchase Order Schema.

Method Detail

getInstance

public static XSDPrototypicalSchema getInstance()
Return the one static instance.

Returns:
the one static instance.

getPrototypicalSchema

public static XSDSchema getPrototypicalSchema()
Returns the Prototype Schema instance.

Returns:
the Prototype Schema instance.

getPurchaseOrderSchema

public static XSDSchema getPurchaseOrderSchema()
Returns the Purchase Order Schema instance.

Returns:
the Purchase Order Schema instance.

initializePrototypeSchema

public XSDSchema initializePrototypeSchema()

{
  // Create the schema, give it a target namespace, and set its form defaults.
  //
  prototypeSchema =  xsdFactory.createXSDSchema();

  prototypeSchema.setTargetNamespace("http://www.example.com/PrototypicalSchema");
  prototypeSchema.setElementFormDefault(XSDForm.QUALIFIED_LITERAL);
  prototypeSchema.setAttributeFormDefault(XSDForm.QUALIFIED_LITERAL);

  // If you want schema tags and references to schema types to be qualified,
  // which is recommend, this is the recommended qualifier.
  //
  prototypeSchema.setSchemaForSchemaQNamePrefix("xsd");

  // Choose the prefix used for this schema's namespace,
  // the schema for schema's namespace,
  // and some other imported namespace.
  //
  Map qNamePrefixToNamespaceMap = prototypeSchema.getQNamePrefixToNamespaceMap();
  qNamePrefixToNamespaceMap.put("PTS", prototypeSchema.getTargetNamespace());
  qNamePrefixToNamespaceMap.put(prototypeSchema.getSchemaForSchemaQNamePrefix(), XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
  qNamePrefixToNamespaceMap.put("EXT", someOtherSchemaURI);

  // Initialize the other parts of this examples.
  //
  initializeSomeOtherImport();
  initializeSimpleAttributeGroupDefinition();
  initializeSimpleRecursiveModelGroupDefinition();
  initializeSimpleRecursiveComplexTypeDefinition();
  initializeSimpleRecursiveElementDeclaration();
  initializeSimpleAttributeDeclaration();
  initializeSimpleElementDeclarationWithAnonymousType();
  initializeSimpleTypeDefinition();
  initializeSimpleListTypeDefinition();
  initializeSimpleUnionTypeDefinition();
  initializeFancyListTypeDefinition();
  initializeSimpleContentComplexTypeDefinition();

  // This is the  result that is produced.
  //
  // <?xml version="1.0" encoding="UTF-8"?>
  // <xsd:schema attributeFormDefault="qualified"
  //     elementFormDefault="qualified"
  //     targetNamespace="http://www.example.com/PrototypicalSchema"
  //     xmlns:EXT="http://www.example.com/SomeOtherSchema"
  //     xmlns:PTS="http://www.example.com/PrototypicalSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  //     <xsd:import namespace="http://www.example.com/SomeOtherSchema" schemaLocation="http://www.example.com/SomeOtherSchema"/>
  //     <xsd:attributeGroup name="simpleAttributeGroupDefinition">
  //         <xsd:attribute default="defaultValue"
  //             name="simpleAttributeDeclarationGroupMember" type="EXT:someOtherTypeDefinition"/>
  //     </xsd:attributeGroup>
  //     <xsd:group name="simpleRecursiveModelGroupDefinition">
  //         <xsd:annotation/>
  //         <xsd:sequence>
  //             <xsd:element maxOccurs="unbounded" minOccurs="0" ref="PTS:simpleRecursiveElementDeclaration"/>
  //             <xsd:any namespace="##other" processContents="lax"/>
  //         </xsd:sequence>
  //     </xsd:group>
  //     <xsd:complexType abstract="false" block="#all" final="#all"
  //         mixed="true" name="SimpleRecursiveComplexTypeDefinition">
  //         <xsd:annotation>
  //             <xsd:appinfo source="http://www.example.com/appinfo"/>
  //             <xsd:documentation
  //                 source="http://www.example.com/documentation">A simple
  //                 recursive complex type definition.</xsd:documentation>
  //         </xsd:annotation>
  //         <xsd:group ref="PTS:simpleRecursiveModelGroupDefinition"/>
  //         <xsd:attribute ref="PTS:simpleAttributeDeclaration" use="optional"/>
  //         <xsd:attributeGroup ref="PTS:simpleAttributeGroupDefinition"/>
  //         <xsd:anyAttribute namespace="##other"/>
  //     </xsd:complexType>
  //     <xsd:element name="simpleRecursiveElementDeclaration" type="PTS:SimpleRecursiveComplexTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:unique name="unique">
  //             <xsd:selector xpath="simpleRecursiveElementDeclaration"/>
  //             <xsd:field xpath="simpleAttributeDeclarationGroupMember"/>
  //         </xsd:unique>
  //         <xsd:key name="key">
  //             <xsd:selector xpath="simpleRecursiveElementDeclaration"/>
  //             <xsd:field xpath="simpleAttributeDeclarationGroupMember"/>
  //         </xsd:key>
  //         <xsd:keyref name="keyref" refer="PTS:key">
  //             <xsd:selector xpath="simpleRecursiveElementDeclaration"/>
  //             <xsd:field xpath="simpleAttributeDeclaration"/>
  //         </xsd:keyref>
  //     </xsd:element>
  //     <xsd:attribute default="defaultValue"
  //         name="simpleAttributeDeclaration" type="EXT:someOtherTypeDefinition"/>
  //     <xsd:element fixed="12ab" name="simpleElementWithAnonymousType">
  //         <xsd:annotation/>
  //         <xsd:simpleType>
  //             <xsd:restriction base="xsd:string">
  //                 <xsd:pattern value="\d\d.."/>
  //             </xsd:restriction>
  //         </xsd:simpleType>
  //     </xsd:element>
  //     <xsd:simpleType name="SimpleTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:restriction base="xsd:positiveInteger">
  //             <xsd:maxInclusive value="100"/>
  //         </xsd:restriction>
  //     </xsd:simpleType>
  //     <xsd:simpleType name="SimpleListTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:list itemType="xsd:integer"/>
  //     </xsd:simpleType>
  //     <xsd:simpleType name="SimpleUnionTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:union memberTypes="xsd:integer"/>
  //     </xsd:simpleType>
  //     <xsd:simpleType name="FancyListTypeDefinition">
  //         <xsd:restriction>
  //             <xsd:simpleType>
  //                 <xsd:list>
  //                     <xsd:simpleType>
  //                         <xsd:union>
  //                             <xsd:simpleType>
  //                                 <xsd:restriction base="xsd:string">
  //                                     <xsd:enumeration value="unknown"/>
  //                                 </xsd:restriction>
  //                             </xsd:simpleType>
  //                             <xsd:simpleType>
  //                                 <xsd:restriction base="xsd:decimal">
  //                                     <xsd:fractionDigits value="2"/>
  //                                 </xsd:restriction>
  //                             </xsd:simpleType>
  //                         </xsd:union>
  //                     </xsd:simpleType>
  //                 </xsd:list>
  //             </xsd:simpleType>
  //             <xsd:maxLength value="4"/>
  //         </xsd:restriction>
  //     </xsd:simpleType>
  //     <xsd:complexType name="SimpleContentComplexTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:simpleContent>
  //             <xsd:extension base="xsd:string">
  //                 <xsd:attribute ref="PTS:simpleAttributeDeclaration" use="optional"/>
  //             </xsd:extension>
  //         </xsd:simpleContent>
  //     </xsd:complexType>
  // </xsd:schema>

  return prototypeSchema;
}


initializeSomeOtherImport

public void initializeSomeOtherImport()

{
  // Create an import and set its namespace and schema location.
  //
  XSDImport someOtherImport = xsdFactory.createXSDImport();
  someOtherImport.setNamespace(someOtherSchemaURI);
  someOtherImport.setSchemaLocation(someOtherSchemaURI);

  // Add the import to the schema contents.
  //
  prototypeSchema.getContents().add(someOtherImport);

  // This is the  result that is produced.
  //
  //     <xsd:import namespace="http://www.example.com/SomeOtherSchema" schemaLocation="http://www.example.com/SomeOtherSchema"/>
}


initializeSimpleAttributeGroupDefinition

public void initializeSimpleAttributeGroupDefinition()

{
  // Create an attribute group definition and set it's name to simpleAttributeGroupDefinition.
  //
  XSDAttributeGroupDefinition simpleAttributeGroupDefinition = xsdFactory.createXSDAttributeGroupDefinition();
  simpleAttributeGroupDefinition.setName("simpleAttributeGroupDefinition");

  // Create an attribute, name it simpleAttributeDeclarationGroupMember, 
  // set it's type to someOtherTypeDefinition in some other schema.
  // set its constraints to be default, set the lexical value of the constraint to be "defaultValue".
  //
  XSDAttributeDeclaration simpleAttributeDeclarationGroupMember = xsdFactory.createXSDAttributeDeclaration();
  simpleAttributeDeclarationGroupMember.setName("simpleAttributeDeclarationGroupMember");
  simpleAttributeDeclarationGroupMember.setTypeDefinition
    (prototypeSchema.resolveSimpleTypeDefinition(someOtherSchemaURI, "someOtherTypeDefinition"));

  // Create an attribute use to contain the attribute and add it the attribute group.
  //
  XSDAttributeUse simpleAttributeUseGroupMember = xsdFactory.createXSDAttributeUse();
  simpleAttributeUseGroupMember.setContent(simpleAttributeDeclarationGroupMember);
  simpleAttributeUseGroupMember.setConstraint(XSDConstraint.DEFAULT_LITERAL);
  simpleAttributeUseGroupMember.setLexicalValue("defaultValue");
  simpleAttributeGroupDefinition.getContents().add(simpleAttributeUseGroupMember);

  // Add the attribute group to the schema.
  //
  prototypeSchema.getContents().add(simpleAttributeGroupDefinition);

  // This is the  result that is produced.
  //
  //     <xsd:attributeGroup name="simpleAttributeGroupDefinition">
  //         <xsd:attribute default="defaultValue"
  //             name="simpleAttributeDeclarationGroupMember" type="EXT:someOtherTypeDefinition"/>
  //     </xsd:attributeGroup>
}


initializeSimpleRecursiveModelGroupDefinition

public void initializeSimpleRecursiveModelGroupDefinition()

{
  // Create a model group definition and name it simpleRecursiveModelGroupDefinition.
  //
  XSDModelGroupDefinition simpleRecursiveModelGroupDefinition = xsdFactory.createXSDModelGroupDefinition();
  simpleRecursiveModelGroupDefinition.setName("simpleRecursiveModelGroupDefinition");

  // Create an annotation placeholder and add it to the model group definition.
  //
  XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
  simpleRecursiveModelGroupDefinition.setAnnotation(annotation);

  // Create a sequence model group.
  //
  XSDModelGroup modelGroup = xsdFactory.createXSDModelGroup();
  modelGroup.setCompositor(XSDCompositor.SEQUENCE_LITERAL);

  // Create an element reference to the simpleRecursiveElementDeclaration of this schema.
  //
  XSDElementDeclaration simpleRecursiveElementDeclarationReference = xsdFactory.createXSDElementDeclaration();
  simpleRecursiveElementDeclarationReference.setResolvedElementDeclaration
    (prototypeSchema.resolveElementDeclaration("simpleRecursiveElementDeclaration"));

  // Create a particle to hold the element and set it to be optional and unbounded.
  //
  XSDParticle simpleRecursiveElementParticle = xsdFactory.createXSDParticle();
  simpleRecursiveElementParticle.setContent(simpleRecursiveElementDeclarationReference);
  simpleRecursiveElementParticle.setMinOccurs(0);
  simpleRecursiveElementParticle.setMaxOccurs(-1);

  // Add the element particle to the model group.
  //
  modelGroup.getContents().add(simpleRecursiveElementParticle);

  // Create an element wildcard, set the namespace constraint to be other, and the processing to be lax.
  //
  XSDWildcard elementWildcard = xsdFactory.createXSDWildcard();
  elementWildcard.getLexicalNamespaceConstraint().add("##other");
  elementWildcard.setProcessContents(XSDProcessContents.LAX_LITERAL);

  // Create a particle to hold the wildcard.
  //
  XSDParticle wildcardParticle = xsdFactory.createXSDParticle();
  wildcardParticle.setContent(elementWildcard);

  // Add the wildcard particle to the model group.
  //
  modelGroup.getContents().add(wildcardParticle);

  // Set the model group to be that of the simpleRecursiveModelGroupDefinition.
  //
  simpleRecursiveModelGroupDefinition.setModelGroup(modelGroup);

  // Add simpleRecursiveModelGroupDefinition to the schema.
  //
  prototypeSchema.getContents().add(simpleRecursiveModelGroupDefinition);

  // This is the  result that is produced.
  //
  //     <xsd:group name="simpleRecursiveModelGroupDefinition">
  //         <xsd:annotation/>
  //         <xsd:sequence>
  //             <xsd:element maxOccurs="unbounded" minOccurs="0" ref="PTS:simpleRecursiveElementDeclaration"/>
  //             <xsd:any namespace="##other" processContents="lax"/>
  //         </xsd:sequence>
  //     </xsd:group>
}


initializeSimpleRecursiveComplexTypeDefinition

public void initializeSimpleRecursiveComplexTypeDefinition()

{
  // Create a complex type definition and set its name to SimpleRecursiveComplexTypeDefinition.
  // Set the complex type to be abstract, block all substitution, make it final in all regards, 
  // set it to be a restriction, and set it's content type to be mixed.
  //
  XSDComplexTypeDefinition simpleRecursiveComplexTypeDefinition = xsdFactory.createXSDComplexTypeDefinition();
  simpleRecursiveComplexTypeDefinition.setName("SimpleRecursiveComplexTypeDefinition");
  simpleRecursiveComplexTypeDefinition.setAbstract(false);
  simpleRecursiveComplexTypeDefinition.getBlock().add(XSDProhibitedSubstitutions.ALL_LITERAL);
  simpleRecursiveComplexTypeDefinition.getLexicalFinal().add(XSDComplexFinal.ALL_LITERAL);
  simpleRecursiveComplexTypeDefinition.setDerivationMethod(XSDDerivationMethod.RESTRICTION_LITERAL);
  simpleRecursiveComplexTypeDefinition.setMixed(true);

  // Create an annotation placeholder and add it to the complex type
  //
  XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
  simpleRecursiveComplexTypeDefinition.setAnnotation(annotation);

  // Create a model group definition reference to simpleRecursiveModelGroupDefinition.
  // Also create particle to hold it and set the particle to be the content of the complex type.
  //
  XSDModelGroupDefinition simpleRecursiveModelGroupDefinitionReference = xsdFactory.createXSDModelGroupDefinition();
  simpleRecursiveModelGroupDefinitionReference.setResolvedModelGroupDefinition
    (prototypeSchema.resolveModelGroupDefinition("simpleRecursiveModelGroupDefinition"));
  XSDParticle modelGroupParticle = xsdFactory.createXSDParticle();
  modelGroupParticle.setContent(simpleRecursiveModelGroupDefinitionReference);
  simpleRecursiveComplexTypeDefinition.setContent(modelGroupParticle);

  // Create an attribute reference to simpleAttributeDeclaration.
  // Also create an attribute use to hold it, set the use to be optional, 
  // and add it to the complex type's attribute contents.
  //
  XSDAttributeDeclaration simpleAttributeDeclarationReference = xsdFactory.createXSDAttributeDeclaration();
  simpleAttributeDeclarationReference.setResolvedAttributeDeclaration
    (prototypeSchema.resolveAttributeDeclaration("simpleAttributeDeclaration"));
  XSDAttributeUse simpleAttributeUse = xsdFactory.createXSDAttributeUse();
  simpleAttributeUse.setContent(simpleAttributeDeclarationReference);
  simpleAttributeUse.setUse(XSDAttributeUseCategory.OPTIONAL_LITERAL);
  simpleRecursiveComplexTypeDefinition.getAttributeContents().add(simpleAttributeUse);

  // Create an attribute group reference to simpleAttributeGroupDefinition.
  // And add it to the complex type's attribute contents.
  //
  XSDAttributeGroupDefinition simpleAttributeGroupDefinitionReference = xsdFactory.createXSDAttributeGroupDefinition();
  simpleAttributeGroupDefinitionReference.setResolvedAttributeGroupDefinition
    (prototypeSchema.resolveAttributeGroupDefinition("simpleAttributeGroupDefinition"));
  simpleRecursiveComplexTypeDefinition.getAttributeContents().add(simpleAttributeGroupDefinitionReference);

  // Create an attribute wildcard and set its constraint to other.
  // Also add it as the complex type's attribute wildcard.
  //
  XSDWildcard attributeWildcard = xsdFactory.createXSDWildcard();
  attributeWildcard.getLexicalNamespaceConstraint().add("##other");
  simpleRecursiveComplexTypeDefinition.setAttributeWildcardContent(attributeWildcard);

  prototypeSchema.getContents().add(simpleRecursiveComplexTypeDefinition);

  // Create an appinfo DOM element with the given sourceURI attribute.
  // Also add the element as the annotation's child.
  // NOTE
  // Working with the contents of an annotation requires dropping down into the DOM model.
  // This imposes the additional requirement that the annotation must be attached to a schema.
  // It also has the effect of calling XSDConcreteComponent.updateElement(),
  // if the schema does not have an element or document yet.
  //
  Element appinfo = annotation.createApplicationInformation("http://www.example.com/appinfo");
  annotation.getElement().appendChild(appinfo);

  // Create a documentation DOM element with the given sourceURI attribute.
  // Also add the element as the annotation's child.
  //
  Element documentation = annotation.createUserInformation("http://www.example.com/documentation");
  annotation.getElement().appendChild(documentation);
  documentation.appendChild
    (documentation.getOwnerDocument().createTextNode
      ("A simple recursive complex type definition."));

  // This is the  result that is produced.
  //
  //     <xsd:complexType abstract="false" block="#all" final="#all"
  //         mixed="true" name="SimpleRecursiveComplexTypeDefinition">
  //         <xsd:annotation>
  //             <xsd:appinfo source="http://www.example.com/appinfo"/>
  //             <xsd:documentation
  //                 source="http://www.example.com/documentation">A simple
  //                 recursive complex type definition.</xsd:documentation>
  //         </xsd:annotation>
  //         <xsd:group ref="PTS:simpleRecursiveModelGroupDefinition"/>
  //         <xsd:attribute ref="PTS:simpleAttributeDeclaration" use="optional"/>
  //         <xsd:attributeGroup ref="PTS:simpleAttributeGroupDefinition"/>
  //         <xsd:anyAttribute namespace="##other"/>
  //     </xsd:complexType>
}


initializeSimpleRecursiveElementDeclaration

public void initializeSimpleRecursiveElementDeclaration()

{
  // Create an element declaration and name it simpleRecursiveElementDeclaration.
  //
  XSDElementDeclaration simpleRecursiveElementDeclaration = xsdFactory.createXSDElementDeclaration();
  simpleRecursiveElementDeclaration.setName("simpleRecursiveElementDeclaration");

  // Create an annotation placeholder and set it to the element.
  //
  XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
  simpleRecursiveElementDeclaration.setAnnotation(annotation);

  // Set the element type definition to complex SimpleRecursiveComplexTypeDefinition from this schema.
  //
  simpleRecursiveElementDeclaration.setTypeDefinition
    (prototypeSchema.resolveComplexTypeDefinition("SimpleRecursiveComplexTypeDefinition"));

  // Create a unique identity constraint called unique.
  //
  XSDIdentityConstraintDefinition unique = xsdFactory.createXSDIdentityConstraintDefinition();
  unique.setIdentityConstraintCategory(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
  unique.setName("unique");
  XSDXPathDefinition uniqueSelector = xsdFactory.createXSDXPathDefinition();
  uniqueSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
  uniqueSelector.setValue("simpleRecursiveElementDeclaration");
  unique.setSelector(uniqueSelector);
  XSDXPathDefinition uniqueField = xsdFactory.createXSDXPathDefinition();
  uniqueField.setVariety(XSDXPathVariety.FIELD_LITERAL);
  uniqueField.setValue("simpleAttributeDeclarationGroupMember");
  unique.getFields().add(uniqueField);
  simpleRecursiveElementDeclaration.getIdentityConstraintDefinitions().add(unique);

  // Create a key identity constraint called key.
  //
  XSDIdentityConstraintDefinition key = xsdFactory.createXSDIdentityConstraintDefinition();
  key.setIdentityConstraintCategory(XSDIdentityConstraintCategory.KEY_LITERAL);
  key.setName("key");
  XSDXPathDefinition keySelector = xsdFactory.createXSDXPathDefinition();
  keySelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
  keySelector.setValue("simpleRecursiveElementDeclaration");
  key.setSelector(keySelector);
  XSDXPathDefinition keyField = xsdFactory.createXSDXPathDefinition();
  keyField.setVariety(XSDXPathVariety.FIELD_LITERAL);
  keyField.setValue("simpleAttributeDeclarationGroupMember");
  key.getFields().add(keyField);
  simpleRecursiveElementDeclaration.getIdentityConstraintDefinitions().add(key);

  // Create a keyref identity constraint called keyref that references key.
  //
  XSDIdentityConstraintDefinition keyref = xsdFactory.createXSDIdentityConstraintDefinition();
  keyref.setIdentityConstraintCategory(XSDIdentityConstraintCategory.KEYREF_LITERAL);
  keyref.setName("keyref");
  XSDXPathDefinition keyrefSelector = xsdFactory.createXSDXPathDefinition();
  keyrefSelector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
  keyrefSelector.setValue("simpleRecursiveElementDeclaration");
  keyref.setSelector(keyrefSelector);
  XSDXPathDefinition keyrefField = xsdFactory.createXSDXPathDefinition();
  keyrefField.setVariety(XSDXPathVariety.FIELD_LITERAL);
  keyrefField.setValue("simpleAttributeDeclaration");
  keyref.setReferencedKey(key);
  keyref.getFields().add(keyrefField);
  simpleRecursiveElementDeclaration.getIdentityConstraintDefinitions().add(keyref);

  // Add the element to the schema.
  // 
  prototypeSchema.getContents().add(simpleRecursiveElementDeclaration);

  // This is the  result that is produced.
  //
  //     <xsd:element name="simpleRecursiveElementDeclaration" type="PTS:SimpleRecursiveComplexTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:unique name="unique">
  //             <xsd:selector xpath="simpleRecursiveElementDeclaration"/>
  //             <xsd:field xpath="simpleAttributeDeclarationGroupMember"/>
  //         </xsd:unique>
  //         <xsd:key name="key">
  //             <xsd:selector xpath="simpleRecursiveElementDeclaration"/>
  //             <xsd:field xpath="simpleAttributeDeclarationGroupMember"/>
  //         </xsd:key>
  //         <xsd:keyref name="keyref" refer="PTS:key">
  //             <xsd:selector xpath="simpleRecursiveElementDeclaration"/>
  //             <xsd:field xpath="simpleAttributeDeclaration"/>
  //         </xsd:keyref>
  //     </xsd:element>
}


initializeSimpleAttributeDeclaration

public void initializeSimpleAttributeDeclaration()

{
  // Create an attribute declaration and set it's name to simpleAttributeDeclaration.
  // Set it's type definition to be someOtherTypeDefinition in someOtherSchemaURI,
  // and set the default constraint to "defaultValue".
  //
  XSDAttributeDeclaration simpleAttributeDeclaration = xsdFactory.createXSDAttributeDeclaration();
  simpleAttributeDeclaration.setName("simpleAttributeDeclaration");
  simpleAttributeDeclaration.setTypeDefinition
    (prototypeSchema.resolveSimpleTypeDefinition(someOtherSchemaURI, "someOtherTypeDefinition"));
  simpleAttributeDeclaration.setConstraint(XSDConstraint.DEFAULT_LITERAL);
  simpleAttributeDeclaration.setLexicalValue("defaultValue");

  // Add the attribute to the schema.
  //
  prototypeSchema.getContents().add(simpleAttributeDeclaration);

  // This is the  result that is produced.
  //
  //     <xsd:attribute default="defaultValue"
  //         name="simpleAttributeDeclaration" type="EXT:someOtherTypeDefinition"/>
}


initializeSimpleElementDeclarationWithAnonymousType

public void initializeSimpleElementDeclarationWithAnonymousType()

{
  // Create an element declaration and set it's name to simpleElementWithAnonymousType.
  //
  XSDElementDeclaration simpleElementDeclarationWithAnonymousType = xsdFactory.createXSDElementDeclaration();
  simpleElementDeclarationWithAnonymousType.setName("simpleElementWithAnonymousType");

  // Create an annotation placeholder and add it to the element.
  //
  XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
  simpleElementDeclarationWithAnonymousType.setAnnotation(annotation);

  // Create an anonymous simple type definition and set it's base type to the built-in string.
  //
  XSDSimpleTypeDefinition anonymousSimpleTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  anonymousSimpleTypeDefinition.setBaseTypeDefinition
    (prototypeSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));

  // Create pattern facet.
  // Set it to match two digits followed by any two characters.
  // And add it to the anonymous type.
  //
  XSDPatternFacet xsdPatternFacet = xsdFactory.createXSDPatternFacet();
  xsdPatternFacet.setLexicalValue("\\d\\d..");
  anonymousSimpleTypeDefinition.getFacetContents().add(xsdPatternFacet);

  // Set the element's annotation and anonymous type, 
  // and set the fixed constraint to "12ab".
  //
  simpleElementDeclarationWithAnonymousType.setAnnotation(annotation);
  simpleElementDeclarationWithAnonymousType.setAnonymousTypeDefinition(anonymousSimpleTypeDefinition);
  simpleElementDeclarationWithAnonymousType.setConstraint(XSDConstraint.FIXED_LITERAL);
  simpleElementDeclarationWithAnonymousType.setLexicalValue("12ab");

  // Add the element to the schema.
  //
  prototypeSchema.getContents().add(simpleElementDeclarationWithAnonymousType);

  // This is the  result that is produced.
  //
  //     <xsd:element fixed="12ab" name="simpleElementWithAnonymousType">
  //         <xsd:annotation/>
  //         <xsd:simpleType>
  //             <xsd:restriction base="xsd:string">
  //                 <xsd:pattern value="\d\d.."/>
  //             </xsd:restriction>
  //         </xsd:simpleType>
  //     </xsd:element>
}


initializeSimpleTypeDefinition

public void initializeSimpleTypeDefinition()

{
  // Create a simple type definition and set it's name to SimpleTypeDefinition.
  // Also set it's base type to be the built-in positiveInteger.
  //
  XSDSimpleTypeDefinition simpleTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  simpleTypeDefinition.setName("SimpleTypeDefinition");
  simpleTypeDefinition.setBaseTypeDefinition
    (prototypeSchema.getSchemaForSchema().resolveSimpleTypeDefinition("positiveInteger"));

  // Create an annotation placeholder and add it to the type.
  //
  XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
  simpleTypeDefinition.setAnnotation(annotation);

  // Create max inclusive facet, set it to 100, and add it to the simple type's facet contents.
  //
  XSDMaxInclusiveFacet xsdMaxInclusiveFacet = xsdFactory.createXSDMaxInclusiveFacet();
  xsdMaxInclusiveFacet.setLexicalValue("100");
  simpleTypeDefinition.getFacetContents().add(xsdMaxInclusiveFacet);

  // Add the simple type to the schema
  //
  prototypeSchema.getContents().add(simpleTypeDefinition);

  // This is the  result that is produced.
  //
  //     <xsd:simpleType name="SimpleTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:restriction base="xsd:positiveInteger">
  //             <xsd:maxInclusive value="100"/>
  //         </xsd:restriction>
  //     </xsd:simpleType>
}


initializeSimpleListTypeDefinition

public void initializeSimpleListTypeDefinition()

{
  // Create a simple list type definition and set it's name to SimpleListTypeDefinition.
  // Also set its item type to the built-in integer.
  //
  XSDSimpleTypeDefinition simpleListTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  simpleListTypeDefinition.setName("SimpleListTypeDefinition");
  simpleListTypeDefinition.setItemTypeDefinition
    (prototypeSchema.getSchemaForSchema().resolveSimpleTypeDefinition("integer"));

  // Create an annotation placeholder and add it to the type.
  //
  XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
  simpleListTypeDefinition.setAnnotation(annotation);

  // Add the simple list type to the schema
  //
  prototypeSchema.getContents().add(simpleListTypeDefinition);

  // This is the  result that is produced.
  //
  //     <xsd:simpleType name="SimpleListTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:list itemType="xsd:integer"/>
  //     </xsd:simpleType>
}


initializeSimpleUnionTypeDefinition

public void initializeSimpleUnionTypeDefinition()

{
  // Create a simple list type definition and set it's name to SimpleUnionTypeDefinition.
  // Also set its item type to the built-in integer.
  //
  XSDSimpleTypeDefinition simpleUnionTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  simpleUnionTypeDefinition.setName("SimpleUnionTypeDefinition");
  simpleUnionTypeDefinition.getMemberTypeDefinitions().add
    (prototypeSchema.getSchemaForSchema().resolveSimpleTypeDefinition("integer"));

  // Create an annotation placeholder and add it to the type.
  //
  XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
  simpleUnionTypeDefinition.setAnnotation(annotation);

  // Add the simple list type to the schema
  //
  prototypeSchema.getContents().add(simpleUnionTypeDefinition);

  // This is the  result that is produced.
  //
  //     <xsd:simpleType name="SimpleUnionTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:union memberTypes="xsd:integer"/>
  //     </xsd:simpleType>
}


initializeFancyListTypeDefinition

public void initializeFancyListTypeDefinition()

{
  // Create a fancy list type definition and set it's name to FancyListTypeDefinition.
  //
  XSDSimpleTypeDefinition fancyListTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  fancyListTypeDefinition.setName("FancyListTypeDefinition");

  // Create an anonymous list type definition for fancyListTypeDefinition to restrict.
  //
  XSDSimpleTypeDefinition listTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();

  // Create an anonymous union type definition for the item type of the listTypeDefiniton.
  //
  XSDSimpleTypeDefinition unionTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();

  // Create an anonymous "member" type definition for the unionTypeDefinition,
  // and set it to restrict the built-in string.
  //
  XSDSimpleTypeDefinition firstMemberTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  firstMemberTypeDefinition.setBaseTypeDefinition
    (prototypeSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));

  // Create an enumeration facet to specify the value "unknown" and add it to the member.
  //
  XSDEnumerationFacet xsdEnumerationFacet = xsdFactory.createXSDEnumerationFacet();
  xsdEnumerationFacet.setLexicalValue("unknown");
  firstMemberTypeDefinition.getFacetContents().add(xsdEnumerationFacet);

  // Add the anonymous member to the contents and the member type definitions of unionTypeDefinition.
  //
  unionTypeDefinition.getContents().add(firstMemberTypeDefinition);
  unionTypeDefinition.getMemberTypeDefinitions().add(firstMemberTypeDefinition);

  // Create another anonymous "member" type definition for the unionTypeDefinition,
  // and set it to restrict the built-in decimal.
  //
  XSDSimpleTypeDefinition secondMemberTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  secondMemberTypeDefinition.setBaseTypeDefinition
    (prototypeSchema.getSchemaForSchema().resolveSimpleTypeDefinition("decimal"));

  // Create a fraction digits facet to specify the value "2" and add it to the member.
  //
  XSDFractionDigitsFacet xsdFractionDigitsFacet = xsdFactory.createXSDFractionDigitsFacet();
  xsdFractionDigitsFacet.setLexicalValue("2");
  secondMemberTypeDefinition.getFacetContents().add(xsdFractionDigitsFacet);

  // Add the anonymous member to the contents and the member type definitions of unionTypeDefinition.
  //
  unionTypeDefinition.getContents().add(secondMemberTypeDefinition);
  unionTypeDefinition.getMemberTypeDefinitions().add(secondMemberTypeDefinition);

  // Add the anonymous item to the contents and set the item type definition.
  //
  listTypeDefinition.getContents().add(unionTypeDefinition);
  listTypeDefinition.setItemTypeDefinition(unionTypeDefinition);

  // Add the anonymous restriction to the contents and set the base type definition.
  //
  fancyListTypeDefinition.getContents().add(listTypeDefinition);
  fancyListTypeDefinition.setBaseTypeDefinition(listTypeDefinition);

  // Create a max length facet to specify the value "4" and add it to the fancy list.
  //
  XSDMaxLengthFacet xsdMaxLengthFacet = xsdFactory.createXSDMaxLengthFacet();
  xsdMaxLengthFacet.setLexicalValue("4");
  fancyListTypeDefinition.getFacetContents().add(xsdMaxLengthFacet);

  // Add the fancy list type to the schema.
  //
  prototypeSchema.getContents().add(fancyListTypeDefinition);

  // This is the  result that is produced.
  //
  //     <xsd:simpleType name="FancyListTypeDefinition">
  //         <xsd:restriction>
  //             <xsd:simpleType>
  //                 <xsd:list>
  //                     <xsd:simpleType>
  //                         <xsd:union>
  //                             <xsd:simpleType>
  //                                 <xsd:restriction base="xsd:string">
  //                                     <xsd:enumeration value="unknown"/>
  //                                 </xsd:restriction>
  //                             </xsd:simpleType>
  //                             <xsd:simpleType>
  //                                 <xsd:restriction base="xsd:decimal">
  //                                     <xsd:fractionDigits value="2"/>
  //                                 </xsd:restriction>
  //                             </xsd:simpleType>
  //                         </xsd:union>
  //                     </xsd:simpleType>
  //                 </xsd:list>
  //             </xsd:simpleType>
  //             <xsd:maxLength value="4"/>
  //         </xsd:restriction>
  //     </xsd:simpleType>
}


initializeSimpleContentComplexTypeDefinition

public void initializeSimpleContentComplexTypeDefinition()

{
  // Create a complex type definition and set its name to SimpleContentComplexTypeDefinition.
  // Set it to be an extension
  //  
  XSDComplexTypeDefinition simpleContentComplexTypeDefinition = xsdFactory.createXSDComplexTypeDefinition();
  simpleContentComplexTypeDefinition.setName("SimpleContentComplexTypeDefinition");
  simpleContentComplexTypeDefinition.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);

  // Create an annotation placeholder and add it to the complex type.
  //
  XSDAnnotation annotation = xsdFactory.createXSDAnnotation();
  simpleContentComplexTypeDefinition.setAnnotation(annotation);

  // Create an anonymous simple type definition, set its base type to be the built-in string,
  // and set it to be the content type of the complex type.
  //
  XSDSimpleTypeDefinition anonymousSimpleTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  simpleContentComplexTypeDefinition.setBaseTypeDefinition
    (prototypeSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
  simpleContentComplexTypeDefinition.setContent(anonymousSimpleTypeDefinition);

  // Create an attribute reference to simpleAttributeDeclaration in this schema.
  //
  XSDAttributeDeclaration simpleAttributeDeclarationReference = xsdFactory.createXSDAttributeDeclaration();
  simpleAttributeDeclarationReference.setResolvedAttributeDeclaration
    (prototypeSchema.resolveAttributeDeclaration("simpleAttributeDeclaration"));

  // Create an attribute use to hold the reference, set its use to be optional,
  // and add it to the complex type's attribute contents
  //
  XSDAttributeUse simpleAttributeUse = xsdFactory.createXSDAttributeUse();
  simpleAttributeUse.setContent(simpleAttributeDeclarationReference);
  simpleAttributeUse.setUse(XSDAttributeUseCategory.OPTIONAL_LITERAL);
  simpleContentComplexTypeDefinition.getAttributeContents().add(simpleAttributeUse);

  // Add the complex type to the schema.
  //
  prototypeSchema.getContents().add(simpleContentComplexTypeDefinition);

  // This is the  result that is produced.
  //
  //     <xsd:complexType name="SimpleContentComplexTypeDefinition">
  //         <xsd:annotation/>
  //         <xsd:simpleContent>
  //             <xsd:extension base="xsd:string">
  //                 <xsd:attribute ref="PTS:simpleAttributeDeclaration" use="optional"/>
  //             </xsd:extension>
  //         </xsd:simpleContent>
  //     </xsd:complexType>
}


initializePurchaseOrderSchema

public XSDSchema initializePurchaseOrderSchema()

{
  // Build the Purchase Order Schema from Primer 0.
  // The general approach, for efficiency, is to attach objects as late as possible.
  // The XML serialization is at the end.

  // Create the schema.
  //
  XSDSchema xsdSchema = XSDFactory.eINSTANCE.createXSDSchema();

  // If you want schema tags and references to schema types to be qualified, 
  // which is recommend, this is the recommended qualifier.
  //
  xsdSchema.setSchemaForSchemaQNamePrefix("xsd");

  // Set the target namespace.
  //
  xsdSchema.setTargetNamespace("http://nist.gov/po.xsd");

  // Choose the prefix used for this schema's namespace and the schema for schema's namespace.
  //
  Map qNamePrefixToNamespaceMap = xsdSchema.getQNamePrefixToNamespaceMap();
  qNamePrefixToNamespaceMap.put("po", xsdSchema.getTargetNamespace());
  qNamePrefixToNamespaceMap.put(xsdSchema.getSchemaForSchemaQNamePrefix(), XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);

  // Create an annotation and add it to the schema.
  //
  XSDAnnotation xsdAnnotation = xsdFactory.createXSDAnnotation();
  xsdSchema.getContents().add(xsdAnnotation);

  // Create a documentation DOM element with no sourceURI attribute.
  // NOTE
  // Working with the contents of an annotation requires dropping down into the DOM model.
  // This imposes the additional requirement that the annotation must be attached to a schema.
  // It also has the effect of calling XSDConcreteComponent.updateElement(),
  // if the schema does not have an element or document yet.
  //
  Element documentation = xsdAnnotation.createUserInformation(null);

  // Use the DOM API to set the lang attribute and content of the documentation.
  //
  documentation.setAttributeNS(XSDConstants.XML_NAMESPACE_URI_1998, "xml:lang", "en");
  String text = "Purchase order schema for Example.com.\nCopyright 2000 Example.com. All rights reserved.";
  documentation.appendChild(documentation.getOwnerDocument().createTextNode(text));

  // Use the DOM API to add the documentation to the element of the annotation.
  //
  xsdAnnotation.getElement().appendChild(documentation);

  // Create an element, name it purchaseOrder, 
  // and set it's type to the purchaseOrderType of this schema.
  // Add the purchaseOrder element to the schema.
  //
  XSDElementDeclaration purchaseOrder = xsdFactory.createXSDElementDeclaration();
  purchaseOrder.setName("purchaseOrder");
  purchaseOrder.setTypeDefinition(xsdSchema.resolveComplexTypeDefinition("PurchaseOrderType"));
  xsdSchema.getContents().add(purchaseOrder);

  // Create an element named command of the build-in type string.
  //
  XSDElementDeclaration comment = xsdFactory.createXSDElementDeclaration();
  comment.setName("comment");
  comment.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
  xsdSchema.getContents().add(comment);

  // Create a complex type and name it PurchaseOrderType.
  //
  XSDComplexTypeDefinition purchaseOrderType = xsdFactory.createXSDComplexTypeDefinition();
  purchaseOrderType.setName("PurchaseOrderType");

  // Create a sequence model group, and a particle to contain it.
  //
  XSDModelGroup purchaseOrderTypeSequence = xsdFactory.createXSDModelGroup();
  purchaseOrderTypeSequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
  XSDParticle purchaseOrderTypeParticle = xsdFactory.createXSDParticle();
  purchaseOrderTypeParticle.setContent(purchaseOrderTypeSequence);

  // Create an element named shipTo of complex type USAddress, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration shipTo = xsdFactory.createXSDElementDeclaration();
  shipTo.setName("shipTo");
  shipTo.setTypeDefinition(xsdSchema.resolveComplexTypeDefinition("USAddress"));
  XSDParticle shipToParticle = xsdFactory.createXSDParticle();
  shipToParticle.setContent(shipTo);
  purchaseOrderTypeSequence.getContents().add(shipToParticle);

  // Create an element named billTo of complex type USAddress, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration billTo = xsdFactory.createXSDElementDeclaration();
  billTo.setName("billTo");
  billTo.setTypeDefinition(xsdSchema.resolveComplexTypeDefinition("USAddress"));
  XSDParticle billToParticle = xsdFactory.createXSDParticle();
  billToParticle.setContent(billTo);
  purchaseOrderTypeSequence.getContents().add(billToParticle);

  // Create an element reference to comment, a particle to contain it,
  // set the particle to be optional, and add the particle to the sequence.
  //
  XSDElementDeclaration commentRef = xsdFactory.createXSDElementDeclaration();
  commentRef.setResolvedElementDeclaration(xsdSchema.resolveElementDeclaration("comment"));
  XSDParticle commentRefParticle = xsdFactory.createXSDParticle();
  commentRefParticle.setMinOccurs(0);
  commentRefParticle.setContent(commentRef);
  purchaseOrderTypeSequence.getContents().add(commentRefParticle);

  // Create an element named items of complex type Items, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration items = xsdFactory.createXSDElementDeclaration();
  items.setName("items");
  items.setTypeDefinition(xsdSchema.resolveComplexTypeDefinition("Items"));
  XSDParticle itemsParticle = xsdFactory.createXSDParticle();
  itemsParticle.setContent(items);
  purchaseOrderTypeSequence.getContents().add(billToParticle);

  // Set the completed content particle to be the content of the PurchaseOrderType.
  //
  purchaseOrderType.setContent(purchaseOrderTypeParticle);

  // Create an attribute, name it orderDate, and set it's type to the built-in Date type.
  // Also create an attribute use to contain the attribute and add it to PurchaseOrderType's attribute contents.
  //
  XSDAttributeDeclaration orderDate = xsdFactory.createXSDAttributeDeclaration();
  orderDate.setName("orderDate");
  orderDate.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("date"));
  XSDAttributeUse orderDateAttributeUse = xsdFactory.createXSDAttributeUse();
  orderDateAttributeUse.setContent(orderDate);
  purchaseOrderType.getAttributeContents().add(orderDateAttributeUse);

  // Add the completed complex PurchaseOrderType to the schema.
  //
  xsdSchema.getContents().add(purchaseOrderType);
   
  // Create a complex type named USAddress.
  //
  XSDComplexTypeDefinition usAddress = xsdFactory.createXSDComplexTypeDefinition();
  usAddress.setName("USAddress");

  // Create a sequence model group, and a particle to contain it.
  //
  XSDModelGroup usAddressSequence = xsdFactory.createXSDModelGroup();
  usAddressSequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
  XSDParticle usAddressParticle = xsdFactory.createXSDParticle();
  usAddressParticle.setContent(usAddressSequence);

  // Create an element named name of built-in type string, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration name = xsdFactory.createXSDElementDeclaration();
  name.setName("name");
  name.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
  XSDParticle nameParticle = xsdFactory.createXSDParticle();
  nameParticle.setContent(name);
  usAddressSequence.getContents().add(nameParticle);

  // Create an element named street of built-in type string, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration street = xsdFactory.createXSDElementDeclaration();
  street.setName("street");
  street.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
  XSDParticle streeParticle = xsdFactory.createXSDParticle();
  streeParticle.setContent(street);
  usAddressSequence.getContents().add(streeParticle);

  // Create an element named city of built-in type string, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration city = xsdFactory.createXSDElementDeclaration();
  city.setName("city");
  city.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
  XSDParticle cityParticle = xsdFactory.createXSDParticle();
  cityParticle.setContent(city);
  usAddressSequence.getContents().add(cityParticle);

  // Create an element named state of built-in type string, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration state = xsdFactory.createXSDElementDeclaration();
  state.setName("state");
  state.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
  XSDParticle stateParticle = xsdFactory.createXSDParticle();
  stateParticle.setContent(state);
  usAddressSequence.getContents().add(stateParticle);

  // Create an element named zip of built-in type string, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration zip = xsdFactory.createXSDElementDeclaration();
  zip.setName("zip");
  zip.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
  XSDParticle zipParticle = xsdFactory.createXSDParticle();
  zipParticle.setContent(zip);
  usAddressSequence.getContents().add(zipParticle);

  // Set the completed content particle to be the content of USAddress.
  //
  usAddress.setContent(usAddressParticle);

  // Create an attribute, name it country, set it's type to the built-in NMTOKEN type,
  // set its constraints to be fixed, set the lexical value of the constraint to be "US".
  // Also create an attribute use to contain the attribute and add it to USAddress' attribute contents.
  //
  XSDAttributeDeclaration country = xsdFactory.createXSDAttributeDeclaration();
  country.setName("country");
  country.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("NMTOKEN"));
  XSDAttributeUse countryAttributeUse = xsdFactory.createXSDAttributeUse();
  countryAttributeUse.setContent(country);
  countryAttributeUse.setConstraint(XSDConstraint.FIXED_LITERAL);
  countryAttributeUse.setLexicalValue("US");
  usAddress.getAttributeContents().add(countryAttributeUse);

  // Add the completed complex USAddress to the schema.
  //
  xsdSchema.getContents().add(usAddress);

  // Create a complex type named Items.
  //
  XSDComplexTypeDefinition itemsType = xsdFactory.createXSDComplexTypeDefinition();
  itemsType.setName("Items");

  // Create a sequence model group, and a particle to contain it.
  //
  XSDModelGroup itemsTypeSequence = xsdFactory.createXSDModelGroup();
  itemsTypeSequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
  XSDParticle itemsTypeParticle = xsdFactory.createXSDParticle();
  itemsTypeParticle.setContent(itemsTypeSequence);

  // Create an element and name it item.
  //
  XSDElementDeclaration item = xsdFactory.createXSDElementDeclaration();
  item.setName("item");

  // Create an anonymous complex type.
  //
  XSDComplexTypeDefinition anonymousComplexTypeDefinition = xsdFactory.createXSDComplexTypeDefinition();

  // Create a sequence model group, and a particle to contain it.
  //
  XSDModelGroup anonymousComplexTypeDefinitionSequence = xsdFactory.createXSDModelGroup();
  anonymousComplexTypeDefinitionSequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
  XSDParticle anonymousTypeDefinitionParticle = xsdFactory.createXSDParticle();
  anonymousTypeDefinitionParticle.setContent(anonymousComplexTypeDefinitionSequence);

  // Create an element productName of built-in type string, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration productName = xsdFactory.createXSDElementDeclaration();
  productName.setName("productName");
  productName.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
  XSDParticle productNameParticle = xsdFactory.createXSDParticle();
  productNameParticle.setContent(productName);
  anonymousComplexTypeDefinitionSequence.getContents().add(productNameParticle);

  // Create a particle to contain the item declaration and add it to the sequence.
  //
  XSDParticle itemParticle = xsdFactory.createXSDParticle();
  itemParticle.setContent(item);
  itemsTypeSequence.getContents().add(itemParticle);

  // Create an attribute, name it partNum, set its type to the SKU type,
  // Also create an attribute use to contain the attribute, set it to be required, 
  // and add it to Items' attribute contents.
  //
  XSDAttributeDeclaration partNum = xsdFactory.createXSDAttributeDeclaration();
  partNum.setName("partNum");
  partNum.setTypeDefinition(xsdSchema.resolveSimpleTypeDefinition("SKU"));
  XSDAttributeUse partNumAttributeUse = xsdFactory.createXSDAttributeUse();
  partNumAttributeUse.setContent(partNum);
  partNumAttributeUse.setUse(XSDAttributeUseCategory.REQUIRED_LITERAL);
  itemsType.getAttributeContents().add(partNumAttributeUse);

  // Set the completed content particle to be the content of the anonymous complex type.
  //
  anonymousComplexTypeDefinition.setContent(anonymousTypeDefinitionParticle);

  // Create an element  and name it quantity.
  //
  XSDElementDeclaration quantity = xsdFactory.createXSDElementDeclaration();
  quantity.setName("quantity");

  // Create an anonymous simple type and set it to restrict the built-in positiveInteger type.
  //
  XSDSimpleTypeDefinition anonymousSimpleTypeDefinition = xsdFactory.createXSDSimpleTypeDefinition();
  anonymousSimpleTypeDefinition.setBaseTypeDefinition
    (xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("positiveInteger"));

  // Create max exclusive facet, set its lexical value to be 100, and add it to the facet contents of the anonymous simple type.
  //
  XSDMaxExclusiveFacet quantityMaxExclusiveFacet = xsdFactory.createXSDMaxExclusiveFacet();
  quantityMaxExclusiveFacet.setLexicalValue("100");
  anonymousSimpleTypeDefinition.getFacetContents().add(quantityMaxExclusiveFacet);

  // Set the completed anonymous type of the quantity element.
  //
  quantity.setAnonymousTypeDefinition(anonymousSimpleTypeDefinition);

  // Create a particle to hold the quantity element and add it to the sequence.
  //
  XSDParticle quantityParticle = xsdFactory.createXSDParticle();
  quantityParticle.setContent(quantity);
  anonymousComplexTypeDefinitionSequence.getContents().add(quantityParticle);

  // Create an element USPrice of built-in type decimal, a particle to contain it,
  // and add the particle to the sequence.
  //
  XSDElementDeclaration usPrice = xsdFactory.createXSDElementDeclaration();
  usPrice.setName("USPrice");
  usPrice.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("decimal"));
  XSDParticle usPriceParticle = xsdFactory.createXSDParticle();
  usPriceParticle.setContent(usPrice);
  anonymousComplexTypeDefinitionSequence.getContents().add(usPriceParticle);

  // Create an element reference to comment, a particle to contain it,
  // set the particle to be optional, and add the particle to the sequence.
  //
  XSDElementDeclaration anotherCommentRef = xsdFactory.createXSDElementDeclaration();
  anotherCommentRef.setResolvedElementDeclaration(xsdSchema.resolveElementDeclaration("comment"));
  XSDParticle anotherCommentRefParticle = xsdFactory.createXSDParticle();
  anotherCommentRefParticle.setMinOccurs(0);
  anotherCommentRefParticle.setContent(anotherCommentRef);
  anonymousComplexTypeDefinitionSequence.getContents().add(anotherCommentRefParticle);

  // Create an element USPrice of built-in type Date, a particle to contain it,
  // set the particle to be optional, and add it to the sequence.
  //
  XSDElementDeclaration shipDate = xsdFactory.createXSDElementDeclaration();
  shipDate.setName("shipDate");
  shipDate.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("decimal"));
  XSDParticle shipDateParticle = xsdFactory.createXSDParticle();
  shipDateParticle.setContent(shipDate);
  shipDateParticle.setMinOccurs(0);
  anonymousComplexTypeDefinitionSequence.getContents().add(shipDateParticle);

  // Set the completed content particle to be the content of Items.
  //
  itemsType.setContent(itemsTypeParticle);

  // Set the completed anonymous type of the item element.
  //
  item.setAnonymousTypeDefinition(anonymousComplexTypeDefinition);

  // Add the completed complex Items to the schema.
  //
  xsdSchema.getContents().add(itemsType);

  // Create a simple type, name it Sku, and set it to restrict the built-in type string.
  //
  XSDSimpleTypeDefinition sku = xsdFactory.createXSDSimpleTypeDefinition();
  sku.setName("Sku");
  sku.setBaseTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));

  // Create max exclusive facet, set its lexical value to be 100, and add it to the facet contents of Sku.
  //
  XSDPatternFacet skuPatternFacet = xsdFactory.createXSDPatternFacet();
  skuPatternFacet.setLexicalValue("/d{3}-[A-Z]{2}");
  sku.getFacetContents().add(skuPatternFacet);

  // Add the completed simple Sku to the schema.
  //
  xsdSchema.getContents().add(sku);

  // This is the  result that is produced.
  //
  // <?xml version="1.0" encoding="UTF-8"?>
  // <xsd:schema targetNamespace="http://nist.gov/po.xsd"
  //     xmlns:po="http://nist.gov/po.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  //     <xsd:annotation>
  //         <xsd:documentation xml:lang="en">Purchase order schema for
  //             Example.com. Copyright 2000 Example.com. All rights reserved.</xsd:documentation>
  //     </xsd:annotation>
  //     <xsd:element name="purchaseOrder" type="po:PurchaseOrderType"/>
  //     <xsd:element name="comment" type="xsd:string"/>
  //     <xsd:complexType name="PurchaseOrderType">
  //         <xsd:sequence>
  //             <xsd:element name="shipTo" type="po:USAddress"/>
  //             <xsd:element name="billTo" type="po:USAddress"/>
  //             <xsd:element minOccurs="0" ref="po:comment"/>
  //         </xsd:sequence>
  //         <xsd:attribute name="orderDate" type="xsd:Date"/>
  //     </xsd:complexType>
  //     <xsd:complexType name="USAddress">
  //         <xsd:sequence>
  //             <xsd:element name="name" type="xsd:string"/>
  //             <xsd:element name="street" type="xsd:string"/>
  //             <xsd:element name="city" type="xsd:string"/>
  //             <xsd:element name="state" type="xsd:string"/>
  //             <xsd:element name="zip" type="xsd:string"/>
  //         </xsd:sequence>
  //         <xsd:attribute fixed="US" name="country" type="xsd:NMTOKEN"/>
  //     </xsd:complexType>
  //     <xsd:complexType name="Items">
  //         <xsd:sequence>
  //             <xsd:element name="item">
  //                 <xsd:complexType>
  //                     <xsd:sequence>
  //                         <xsd:element name="productName" type="xsd:string"/>
  //                         <xsd:element name="quantity">
  //                             <xsd:simpleType>
  //                                 <xsd:restriction base="xsd:positiveInteger">
  //                                     <xsd:maxExclusive value="100"/>
  //                                 </xsd:restriction>
  //                             </xsd:simpleType>
  //                         </xsd:element>
  //                         <xsd:element name="USPrice" type="xsd:decimal"/>
  //                         <xsd:element minOccurs="0" ref="po:comment"/>
  //                         <xsd:element minOccurs="0" name="shipDate" type="xsd:decimal"/>
  //                     </xsd:sequence>
  //                 </xsd:complexType>
  //             </xsd:element>
  //         </xsd:sequence>
  //         <xsd:attribute name="partNum" type="po:SKU" use="required"/>
  //     </xsd:complexType>
  //     <xsd:simpleType name="Sku">
  //         <xsd:restriction base="xsd:string">
  //             <xsd:pattern value="/d{3}-[A-Z]{2}"/>
  //         </xsd:restriction>
  //     </xsd:simpleType>
  // </xsd:schema>

  return xsdSchema;
}


savePurchaseOrderSchema

public void savePurchaseOrderSchema(java.lang.String schemaURI)

{
  // Saves the Purchase Order Schema to the given URI.
  //
  try
  {
    // Create a resource set and a resource with an extent.
    // Add the schema to the extent, add the resource to the set,
    // and save the resource.
    //
    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resource = new XSDResourceImpl(URI.createURI(schemaURI));
    resource.getContents().add(purchaseOrderSchema);
    resourceSet.getResources().add(resource);
    resource.save(Collections.EMPTY_MAP);
  }
  catch (Exception exception)
  {
    System.out.println(exception.getLocalizedMessage());
    exception.printStackTrace();
  }
}


printSchema

public void printSchema(java.lang.String xsdSchemaURI)

{
  // Prints the schema loaded from the given URI.
  //

  // The code assumes that the following registration has taken place.
  // Currently, unless special handling is implemented in the resource set, 
  // only files ending with a .xsd extension will be loaded as schemas.
  //
  // Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", new XSDResourceFactoryImpl());

  try
  {
    // Create a resource set, 
    // turn on the ability of the XSDParser to associate line and column information with the DOM,
    // and then load the main schema file.
    //
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_LOCATION, Boolean.TRUE);
    XSDResourceImpl xsdSchemaResource = (XSDResourceImpl)resourceSet.getResource(URI.createURI(xsdSchemaURI), true);

    // Iterate over all the resources, i.e., the main resource 
    // and those that have been included, imported, or redefined.
    //
    for (Iterator resources = resourceSet.getResources().iterator(); resources.hasNext(); )
    {
      // Check for schema resources.
      //
      Resource resource = (Resource)resources.next();
      if (resource instanceof XSDResourceImpl)
      {
        XSDResourceImpl xsdResource = (XSDResourceImpl)resource;

        // Get the schema's element and serialized it to System.out.
        //
        XSDSchema xsdSchema = xsdResource.getSchema();
        System.out.println("\n<!-- *** " + xsdResource.getURI() + " *** -->\n");
        printComponent(System.out, xsdSchema);

        // If there are no errors from just trying to parse a DOM, 
        // validate the schema.
        //
        if (xsdSchema.getAllDiagnostics().isEmpty())
        {
          xsdSchema.validate();
        }

        // Process each of the diagnostics.
        //
        for (Iterator diagnostics = xsdSchema.getAllDiagnostics().iterator(); diagnostics.hasNext(); )
        {
          XSDDiagnostic xsdDiagnostic = (XSDDiagnostic)diagnostics.next();
          String localizedSeverity = 
            XSDPlugin.INSTANCE.getString("_UI_XSDDiagnosticSeverity_" + xsdDiagnostic.getSeverity());
          System.out.println
            (XSDPlugin.INSTANCE.getString
              ("_UI_DiagnosticFileLineColumn_message", 
               new Object [] 
               { 
                 localizedSeverity, 
                 xsdDiagnostic.getLocationURI(), 
                 new Integer(xsdDiagnostic.getLine()), 
                 new Integer(xsdDiagnostic.getColumn()) 
               }));

          System.out.println(xsdDiagnostic.getMessage());
        }
      }
    }
  }
  catch (Exception exception)
  {
    System.out.println(exception.getLocalizedMessage());
    exception.printStackTrace();
  }
}


printComponent

public void printComponent(java.io.OutputStream outputStream,
                           XSDConcreteComponent xsdConcreteComponent)

{
  // Print a component's element using Xerces.
  //

  // Get the component's element and create one if there isn't one already.
  //
  Element element = xsdConcreteComponent.getElement();
  if (element == null)
  {
    xsdConcreteComponent.updateElement();
    element = xsdConcreteComponent.getElement();
  }

  if (element != null)
  {
    try
    {
      // OutputFormat outputFormat = new OutputFormat(element.getOwnerDocument());
      // outputFormat.setLineWidth(80);
      // outputFormat.setIndenting(true);
      // outputFormat.setIndent(4);
      // outputFormat.setPreserveSpace(false);
      // XMLSerializer serializer = new XMLSerializer(outputStream, outputFormat);
      // serializer.serialize(element);

      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer = transformerFactory.newTransformer();

      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty(OutputKeys.METHOD, "xml");

      transformer.transform(new DOMSource(element.getOwnerDocument()), new StreamResult(outputStream));
    }
    catch (TransformerException exception)
    {
      System.out.println(exception.getLocalizedMessage());
      exception.printStackTrace();
    }
  }
}


createSchema

public XSDSchema createSchema(org.w3c.dom.Element element)

{
  // Create a schema from an element.
  //
  // Test if the element is really a schema element.
  //
  if (element.getLocalName().equals("schema") && 
        XSDConstants.isSchemaForSchemaNamespace(element.getNamespaceURI()))
  {
    // Create the schema.
    //
    XSDSchema xsdSchema = XSDFactory.eINSTANCE.createXSDSchema();

    // Set the element to the schema.
    // This it will build the corresponding component structure.
    //
    xsdSchema.setElement(element);

    return xsdSchema;
  }
  else
  {
    return null;
  }
}


traceLoading

public void traceLoading(java.lang.String xsdSchemaURI)

{
  // Traces a resource set's loading behavior when loading the given URI.
  //
  // The code assumes that the following registration has taken place.
  // Currently, unless special handling is implemented in the resource set, 
  // only files ending with a .xsd extension will be loaded as schemas.
  //
  // Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", new XSDResourceFactoryImpl());

  try
  {
    // Create a resource set.
    //
    ResourceSet resourceSet = 
      new ResourceSetImpl()
      {
        public Resource getResource(URI uri, boolean loadOnDemand) 
        {
          Resource result = super.getResource(uri, true);
          System.out.println("<!-- loaded: " + uri + " --> " + result);
          return result;
        }
      };

    // Create a derived URIConverter to track normalization.
    //
    resourceSet.getURIConverter().getURIMap().put
      (URI.createURI("http://www.example.com/logical.xsd"), 
       URI.createURI("file://c:/physical.xsd"));

    // Load the schema from the URI.
    //
    XSDResourceImpl xsdSchemaResource = (XSDResourceImpl)resourceSet.getResource(URI.createURI(xsdSchemaURI), true);

    // Iterate over all the resources, i.e., the main resource 
    // and those that have been included, imported, or redefined.
    //
    for (Iterator resources = resourceSet.getResources().iterator(); resources.hasNext(); )
    {
      // Check for schema resources.
      //
      Resource resource = (Resource)resources.next();
      if (resource instanceof XSDResourceImpl)
      {
        XSDResourceImpl xsdResource = (XSDResourceImpl)resource;

        // Iterate over the schema's content's looking for directives.
        //
        XSDSchema xsdSchema = xsdResource.getSchema();
        for (Iterator contents = xsdSchema.getContents().iterator(); contents.hasNext(); )
        {
          XSDSchemaContent xsdSchemaContent = (XSDSchemaContent)contents.next();
          if (xsdSchemaContent instanceof XSDSchemaDirective)
          {
            // Check if the directive resolved to a schema.
            //
            XSDSchemaDirective xsdSchemaDirective = (XSDSchemaDirective)xsdSchemaContent;
            if (xsdSchemaDirective.getResolvedSchema() == null)
            {
              System.out.println("Unresolved schema in " + xsdResource.getURI());
              printComponent(System.out, xsdSchemaDirective);
            }
          }
        }
      }
    }
  }
  catch (Exception exception)
  {
    System.out.println(exception.getLocalizedMessage());
    exception.printStackTrace();
  }
}


cloneComponent

public XSDConcreteComponent cloneComponent(XSDConcreteComponent xsdConcreteComponent,
                                           boolean preserveDOM)

{
  // A component can be cloned directly or by cloning the underlying DOM.
  // By cloning the DOM you ensure that <annotation>s, non-schema namespace attributes, and formatting are preserved.
  //
  if (preserveDOM)
  {
    // If there is an element to clone.
    //
    Element element = xsdConcreteComponent.XSDConcreteComponent.getElement()getElement();
    if (element != null)
    {
      // Clone the DOM using the DOM API, and create the same type of component to hold it.
      //
      Element clonedElement = (Element)element.cloneNode(true);
      XSDConcreteComponent result = (XSDConcreteComponent)XSDFactory.eINSTANCE.create(xsdConcreteComponent.eClass());
      result.setElement(clonedElement);
      return result;
    }
  }

  // Clone just the model itself so that a new DOM will need to be created to serialize it.
  //
  XSDConcreteComponent result = xsdConcreteComponent.cloneConcreteComponent(true, false);
  return result;
}


crossReferenceTest

public void crossReferenceTest(java.io.PrintStream out)

{
  // Test cross references for the meta schemas.
  //
  XSDSchema xsdSchemaForSchema = XSDUtil.getSchemaForSchema(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
  ResourceSet resourceSet = xsdSchemaForSchema.eResource().getResourceSet();

  out.println("Show uses of the string datatype within the meta schemas themselves.");
  Collection usages = XSDUtil.UsageCrossReferencer.find(xsdSchemaForSchema.resolveSimpleTypeDefinition("string"), resourceSet);
  XSDUtil.UsageCrossReferencer.print(out, usages);

  out.println("Show uses of the ur-type URI");
  Map xsdURICrossReferences = XSDUtil.URICrossReferencer.find(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001 + "#anyType",  resourceSet);
  XSDUtil.URICrossReferencer.print(out, xsdURICrossReferences);

  out.println("Show all named components and their uses in the schema of the anySimpleType.");
  Map xsdNamedComponentUsage = 
    XSDUtil.XSDNamedComponentCrossReferencer.find(xsdSchemaForSchema.resolveSimpleTypeDefinition("anySimpleType").getSchema());
  XSDUtil.XSDNamedComponentCrossReferencer.print(out, xsdNamedComponentUsage);

  out.println("Test that the URI of evey object in the schema for schemas can be resolved.");
  for (Iterator contents = xsdSchemaForSchema.eAllContents(); contents.hasNext(); )
  {
    XSDConcreteComponent xsdConcreteComponent = (XSDConcreteComponent)contents.next();
    URI uri = EcoreUtil.getURI(xsdConcreteComponent);
    if (resourceSet.getEObject(uri, false) != xsdConcreteComponent)
    {
      out.println("BAD URI: " + uri);
    }
  }
}


Copyright 2001-2006 IBM Corporation and others.
All Rights Reserved.