org.apache.xmlbeans
Interface SchemaTypeSystem

All Superinterfaces:
SchemaTypeLoader

public interface SchemaTypeSystem
extends SchemaTypeLoader

A finite set of XML Schema component definitions.

Every SchemaComponent such as a SchemaType, SchemaGlobalElement, SchemaGlobalAttribute, SchemaModelGroup, SchemaAttributeGroup, or SchemaIdentityConstraint, is defined in exactly one SchemaTypeSystem. (See SchemaComponent.getTypeSystem().) A single SchemaTypeSystem can include definitions from any number of namespaces; one SchemaTypeSystem consists simply of a set of component definitions that were compiled together.

Since every component is defined in a single SchemaTypeSystem, no SchemaTypeSystem other than XmlBeans.getBuiltinTypeSystem() includes any of the the built-in types. That means you cannot ordinarily load instances using a single SchemaTypeSystem by itself. Instead, you will want to combine a path of SchemaTypeSystems together using XmlBeans.typeLoaderUnion(org.apache.xmlbeans.SchemaTypeLoader[]) to form a SchemaTypeLoader that can be used for loading instances.

For example, the following code compiles the schema in myXSDFile in the presence of only the minimal builtin type system. The resulting SchemaTypeSystem sts contains only the definitions from myXSD file. In order to load and validate an instance within the context of those types, we must next construct a SchemaTypeLoader stl that contains both the builtin type system and the types defined within the myXSD file.

 SchemaTypeSystem sts = XmlBeans.compileXsd(new XmlObject[]
    { XmlObject.Factory.parse(myXSDFile) },
    XmlBeans.getBuiltinTypeSystem(),
    null);
 SchemaTypeLoader stl = XmlBeans.typeLoaderUnion(new SchemaTypeLoader[]
    { sts, XmlBeans.getBuiltinTypeSystem() });
 XmlObject mydoc = stl.parse(instanceFile, null, null);
 System.out.println("Document valid: " + mydoc.validate());
 

As you can see, for working with instances, you typically want to work with a SchemaTypeLoader constructed from a path rather than a solitary SchemaTypeSystem. See XmlBeans.loadXsd(org.apache.xmlbeans.XmlObject[]) for a convenient alternative to XmlBeans.compileXsd(org.apache.xmlbeans.XmlObject[], org.apache.xmlbeans.SchemaTypeLoader, org.apache.xmlbeans.XmlOptions).

A SchemaTypeSystem is useful when you need to enumerate the exact set of component definitions derived from a set of XSD files, for example, when you are analyzing the contents of the XSD files themselves. Here is how to use a SchemaTypeSystem to inspect a set of schema definitions:

  1. First, use XmlBeans.compileXsd(org.apache.xmlbeans.XmlObject[], org.apache.xmlbeans.SchemaTypeLoader, org.apache.xmlbeans.XmlOptions) to compile any number of schema files. If the schema files are valid, result will be a SchemaTypeSystem that contains all the component definitions from those files. It will contain no other component definitions.
  2. Alternatively, call SchemaComponent.getTypeSystem() on a precompiled schema component to discover the SchemaTypeSystem within which that component was originally compiled.
  3. Once you have a SchemaTypeSystem, call:
  4. In addition, there are special types generated for XML Beans thare are not formally part of the Schema specification:

A document type is a type that contains a single global element; there is one document type for each global element definition in a SchemaTypeSystem. In an instance document, only the root XmlObject can have a document type as its type.

Similarly, an attribute type is a type that contains a single global attribute, and there is one attribute type for each global attribute definition in a SchemaTypeSystem. It is possible to have a root XmlObject representing a fragment whose type is an attribute type, but attribute types are present mainly for symmetry and to simplify code such as the type-tree-walking code below.

The global component methods above only provide a view of the top-level components of a SchemaTypeSystem and do not include any nested definitions. To view all the nested definitions, you will want to traverse the entire tree of SchemaType defintions within a SchemaTypeSystem by examining the SchemaType.getAnonymousTypes() within each SchemaType recursively.

The following code is a standard treewalk that visits every SchemaType in the SchemaTypeSystem once, including nested definitions.

 List allSeenTypes = new ArrayList();
 allSeenTypes.addAll(Arrays.asList(typeSystem.documentTypes()));
 allSeenTypes.addAll(Arrays.asList(typeSystem.attributeTypes()));
 allSeenTypes.addAll(Arrays.asList(typeSystem.globalTypes()));
 for (int i = 0; i < allSeenTypes.size(); i++)
 {
     SchemaType sType = (SchemaType)allSeenTypes.get(i);
     System.out.prinlnt("Visiting " + sType.toString());
     allSeenTypes.addAll(Arrays.asList(sType.getAnonymousTypes()));
 }
 

See Also:
SchemaType, SchemaTypeLoader, XmlBeans.compileXsd(org.apache.xmlbeans.XmlObject[], org.apache.xmlbeans.SchemaTypeLoader, org.apache.xmlbeans.XmlOptions), XmlBeans.typeLoaderUnion(org.apache.xmlbeans.SchemaTypeLoader[]), XmlBeans.getBuiltinTypeSystem()

Method Summary
 SchemaAnnotation[] annotations()
          Returns the top-level annotations
 SchemaAttributeGroup[] attributeGroups()
          Returns the attribute groups defined in this loader.
 SchemaType[] attributeTypes()
          Returns the attribute types defined in this loader.
 SchemaType[] documentTypes()
          Returns the document types defined in this loader.
 ClassLoader getClassLoader()
          Returns the classloader used by this loader for resolving types.
 String getName()
          Returns the name of this loader.
 SchemaGlobalAttribute[] globalAttributes()
          Returns the global attributes defined in this loader.
 SchemaGlobalElement[] globalElements()
          Returns the global elements defined in this loader.
 SchemaType[] globalTypes()
          Returns the global types defined in this loader.
 SchemaModelGroup[] modelGroups()
          Returns the model groups defined in this loader.
 void resolve()
          Initializes a type system (resolves all handles within the type system).
 SchemaComponent resolveHandle(String handle)
          Locates a type, element, or attribute using the handle.
 void save(Filer filer)
          Saves this type system using a Filer
 void saveToDirectory(File classDir)
          Saves this type system to a directory.
 SchemaType typeForHandle(String handle)
          Locates a type, element, or attribute using the handle.
 
Methods inherited from interface org.apache.xmlbeans.SchemaTypeLoader
compilePath, compileQuery, findAttribute, findAttributeGroup, findAttributeGroupRef, findAttributeRef, findAttributeType, findAttributeTypeRef, findDocumentType, findDocumentTypeRef, findElement, findElementRef, findIdentityConstraintRef, findModelGroup, findModelGroupRef, findType, findTypeRef, getSourceAsStream, isNamespaceDefined, newDomImplementation, newInstance, newValidatingXMLInputStream, newXmlSaxHandler, parse, parse, parse, parse, parse, parse, parse, typeForClassname, typeForSignature
 

Method Detail

getName

String getName()
Returns the name of this loader.


globalTypes

SchemaType[] globalTypes()
Returns the global types defined in this loader.


documentTypes

SchemaType[] documentTypes()
Returns the document types defined in this loader.


attributeTypes

SchemaType[] attributeTypes()
Returns the attribute types defined in this loader.


globalElements

SchemaGlobalElement[] globalElements()
Returns the global elements defined in this loader.


globalAttributes

SchemaGlobalAttribute[] globalAttributes()
Returns the global attributes defined in this loader.


modelGroups

SchemaModelGroup[] modelGroups()
Returns the model groups defined in this loader.


attributeGroups

SchemaAttributeGroup[] attributeGroups()
Returns the attribute groups defined in this loader.


annotations

SchemaAnnotation[] annotations()
Returns the top-level annotations


resolve

void resolve()
Initializes a type system (resolves all handles within the type system).


resolveHandle

SchemaComponent resolveHandle(String handle)
Locates a type, element, or attribute using the handle.


typeForHandle

SchemaType typeForHandle(String handle)
Locates a type, element, or attribute using the handle.


getClassLoader

ClassLoader getClassLoader()
Returns the classloader used by this loader for resolving types.


saveToDirectory

void saveToDirectory(File classDir)
Saves this type system to a directory.


save

void save(Filer filer)
Saves this type system using a Filer