com.bowstreet.xml.schema
Class Schema

java.lang.Object
  extended by com.bowstreet.xml.schema.Schema

public class Schema
extends java.lang.Object

The Schema class encapsulates a W3C schema or DTD,providing convenienebet access to the elements defined within it.A Schema object can be instantiated either with a URL (file:// or http://) or by passing in the String holding the actual schema content. Then the elements can be accessed. So a typical call sequence might be:

Schema schema = new Schema(httpUrlToSchema);

// get the source
String s = schema.getSchemaSource();
System.out.println(s);

// dump the root elements..
java.util.List elements = schema.getRootElements(true);
java.util.Iterator iter = elements.iterator();
while (iter.hasNext())
System.out.println(iter.next());

// get all elements..
elements = schema.getElements(true);

// get non leaf elements..
elements = schema.getNonLeafElements(true);

The Schemas are registered in the WebApp at regen time using the WebApp api (addSchema()). Here's a sample valid schema. It incorporates a targetNamespace to allow it to be referenced in WSDL generated via the WebServiceEnable builder.

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://bowstreet.com/02/2002/wsenabled"
targetNamespace="http://bowstreet.com/02/2002/wsenabled">

<element name="books" type="tns:books" />

<complexType name="books">
<sequence>
<element name="book" type="tns:book"/>
</sequence>
</complexType>

<complexType name="book">
<sequence>
<element name="title" type="string"/>
<element name="authors" type="tns:authors"/>
<element name="price" type="string"/>
<element name="salePrice" type="string"/>
<attribute name="copyright" type="string"/>
<attribute name="edition" type="string"/>
</sequence>
</complexType>

<complexType name="authors">
<sequence>
<element name="author" type="string"/>
</sequence>
</complexType>

</schema>

See Also:
WebApp

Nested Class Summary
 class Schema.Eh
           
 
Field Summary
static java.lang.String BOWSTREET_XML_SCHEMA_HONOUR_ALL_SCHEMA_LOCATIONS
           
static java.lang.String BOWSTREET_XML_SCHEMA_LOG_PARSING_ERRORS
           
static java.lang.String BOWSTREET_XML_SCHEMA_RECURSIONS
           
static int ELEMENT_PATH_DATA_TYPE_ELEMENT
           
static int ELEMENT_PATH_DATA_TYPE_TYPE
           
 
Constructor Summary
Schema(java.lang.String schemaText)
          Constructor taking a schema as a String input.
Schema(java.lang.String schemaText, java.lang.String importsLocationURL)
          Constructor taking a schema and its location as String inputs.
Schema(java.lang.String schemaText, java.lang.String importsLocationURL, java.util.List otherSchemas)
          Constructor taking a schema and its location as String inputs.
Schema(java.net.URL url)
          Constructor taking a URL as input.
 
Method Summary
 java.lang.String expandSchemaImportLocations(java.lang.String schemaLocation)
           
 java.lang.String expandSchemaImportLocations(java.lang.String schemaSource, java.lang.String location, boolean addToImportURIs, boolean expandImports, java.lang.String absolutePath, java.lang.String schemaURL)
           
 java.lang.String findName(java.lang.String name)
          Given a name, try to find it in the elements or types of the schema and return the full path name.
 java.lang.String getAnnotation(java.lang.String itemName)
           
 java.util.List getAttributes()
          Returns a list of the attributes in the schema, as xpaths.
 ElementPathData getElementPathData(java.lang.String xPathRef)
          Returns an ElementPathData description object for the element or type xpath provided.
 ElementPathData getElementPathData(java.lang.String xPathRef, int type)
          Returns an ElementPathData description object for the element or type xpath provided.
 java.util.List getElements()
          Returns a list of element paths extracted earlier from the schema.
static java.io.File getFileFromURL(java.net.URL url)
          Utility function to get a File from a URL which has already been proven to have url.getProtocol().equals("file").
 java.util.List getNonLeafElements()
          Extracts just all elements that are not leaf elements (so they are still truly complex elements and not just Strings) returning a List of Xpaths.
 java.lang.Object getPreparser()
           
 java.util.List getRootElements()
          Returns a list of element paths extracted earlier from the schema, but only for the "root" element names, not subelements.
 java.lang.String getSchemaImportSource(java.lang.String url)
          Returns the schema source for the URL supplied.
 java.util.Iterator getSchemaImportURIs()
          Returns the keyset iterator of locations that we built up by recursively following imports in the Schema.
 java.lang.String getSchemaSource()
          Returns the source string that was used in the Schema(String) constructor.
 java.util.List getSubElements(java.lang.String reqdParentPath, boolean immediateChildrenOnly)
          For a given parent path, returns the list of subelements.
 java.lang.String getTargetNamespace()
          Returns the targetNamespace defined for this schema.
 java.util.List getTypes(boolean getSubElements, boolean getLeafElements, java.lang.String typeSuffix)
          Extracts the types and optionally subelement trees from the Schema returning a list of XPaths.
 org.apache.xerces.xs.XSModel getXSModel()
           
 boolean isAnElement(java.lang.String path)
          Returns true if the passed in XPath maps to an element in the Schema (rather than a type).
 boolean isAType(java.lang.String path)
          Returns true if the passed in XPath maps to a Type in the Schema (rather than an Element).
 boolean isQualified()
           
 boolean isRootElementWithChildren(java.lang.String path)
          Returns TRUE when the given path maps to a root elemment with children.
static void main(java.lang.String[] args)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BOWSTREET_XML_SCHEMA_HONOUR_ALL_SCHEMA_LOCATIONS

public static final java.lang.String BOWSTREET_XML_SCHEMA_HONOUR_ALL_SCHEMA_LOCATIONS
See Also:
Constant Field Values

BOWSTREET_XML_SCHEMA_LOG_PARSING_ERRORS

public static final java.lang.String BOWSTREET_XML_SCHEMA_LOG_PARSING_ERRORS
See Also:
Constant Field Values

BOWSTREET_XML_SCHEMA_RECURSIONS

public static final java.lang.String BOWSTREET_XML_SCHEMA_RECURSIONS
See Also:
Constant Field Values

ELEMENT_PATH_DATA_TYPE_ELEMENT

public static final int ELEMENT_PATH_DATA_TYPE_ELEMENT
See Also:
Constant Field Values

ELEMENT_PATH_DATA_TYPE_TYPE

public static final int ELEMENT_PATH_DATA_TYPE_TYPE
See Also:
Constant Field Values
Constructor Detail

Schema

public Schema(java.lang.String schemaText)
       throws java.lang.Exception
Constructor taking a schema as a String input. The Schema class supports CR XSD schemas (http://www.w3.org/2000/10/XMLSchema) as well as 'final' rec. (http://www.w3.org/2001/XMLSchema) in addition to XDR and SOX schemas and DTDs. This constructor will not follow through on any import statements in the schema. To do that we need the location to be passed in. (see Schema( String src, String location) or Schema(URL)).

Parameters:
schemaText - the w3c schema or DTD as string. This is not a filename! It should be the actual schema.
Throws:
java.lang.Exception

Schema

public Schema(java.lang.String schemaText,
              java.lang.String importsLocationURL)
       throws java.lang.Exception
Constructor taking a schema and its location as String inputs. The location is used to follow through on any import statements in the schema, so elements/types can be extracted from those schemas too.

Parameters:
schemaText - the w3c schema or DTD as string. This is not a filename! It should be the actual schema.
importsLocationURL - if available (maybe null) the location where this schema source was pulled from so it can be used as a relative URL for imports
Throws:
java.lang.Exception

Schema

public Schema(java.lang.String schemaText,
              java.lang.String importsLocationURL,
              java.util.List otherSchemas)
       throws java.lang.Exception
Constructor taking a schema and its location as String inputs. The location is used to follow through on any import statements in the schema, so elements/types can be extracted from those schemas too. The third argument is a List of other Schemas that have already been parsed which can be used to resolve references from this schema source.

Parameters:
schemaText - the w3c schema or DTD as string. This is not a filename! It should be the actual schema.
importsLocationURL - if available (maybe null) the location where this schema source was pulled from so it can be used as a relative URL for imports
otherSchemas - a list of other schemas that can be "stacked" into this schemaspace for resolution of references
Throws:
java.lang.Exception

Schema

public Schema(java.net.URL url)
       throws java.lang.Exception
Constructor taking a URL as input. If the caller has the actual Schema (not a reference to a Schema) in a variable the Schema(String) constructor should be used, passing in the dereferenced variable's content. The URI in the schemaPath can be "file://filepath" or "http://url". The Schema class supports CR XSD schemas (http://www.w3.org/2000/10/XMLSchema) as well as 'final' rec. (http://www.w3.org/2001/XMLSchema) in addition to XDR and SOX schemas and DTDs.

Parameters:
url - URL to schema (file:// or http:// )
Throws:
java.lang.Exception
See Also:
SchemaPath
Method Detail

expandSchemaImportLocations

public java.lang.String expandSchemaImportLocations(java.lang.String schemaLocation)

expandSchemaImportLocations

public java.lang.String expandSchemaImportLocations(java.lang.String schemaSource,
                                                    java.lang.String location,
                                                    boolean addToImportURIs,
                                                    boolean expandImports,
                                                    java.lang.String absolutePath,
                                                    java.lang.String schemaURL)
                                             throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

findName

public java.lang.String findName(java.lang.String name)
Given a name, try to find it in the elements or types of the schema and return the full path name. Return null if not found.

Parameters:
name - name of the type or element to be located
Returns:
full pathname of the item found, or null if not found.

getAnnotation

public java.lang.String getAnnotation(java.lang.String itemName)

getAttributes

public java.util.List getAttributes()
                             throws java.lang.Exception
Returns a list of the attributes in the schema, as xpaths. Attributes for both elements and types are returned.

Returns:
List of XPath Strings giving the attributes
Throws:
java.lang.Exception

getElementPathData

public ElementPathData getElementPathData(java.lang.String xPathRef)
Returns an ElementPathData description object for the element or type xpath provided. By preference will return an element with a matching xpath, and if not found will return a matching type. If a type or element is specifically desired then use "getElementPathData(xPathRef, type)".

Parameters:
xPathRef - the xpath for the element or type
Returns:
the ElementPathData object or null if not found

getElementPathData

public ElementPathData getElementPathData(java.lang.String xPathRef,
                                          int type)
Returns an ElementPathData description object for the element or type xpath provided. Use type=ELEMENT_PATH_DATA_TYPE_ELEMENT to get an element, or type=ELEMENT_PATH_DATA_TYPE_TYPE to get a type. Alternatively to look for either one, returning element in preference, use "getElementPathData(xPathRef)".

Parameters:
xPathRef - the xpath for the element or type
type - the type of match desired: ELEMENT_PATH_DATA_TYPE_ELEMENT for an element or ELEMENT_PATH_DATA_TYPE_TYPE for a type.
Returns:
the ElementPathData object or null if not found

getElements

public java.util.List getElements()
                           throws java.lang.Exception
Returns a list of element paths extracted earlier from the schema. Does not return types. More information for each element can be fetched with getElementPathData() passing in the element path.

Returns:
List of XPath Strings giving the Elements
Throws:
java.lang.Exception

getFileFromURL

public static java.io.File getFileFromURL(java.net.URL url)
Utility function to get a File from a URL which has already been proven to have url.getProtocol().equals("file"). This will fail otherwise.

Parameters:
url - which is known to return true from url.getProtocol().equals("file")
Returns:
File object pointing to the file referenced.

getNonLeafElements

public java.util.List getNonLeafElements()
                                  throws java.lang.Exception
Extracts just all elements that are not leaf elements (so they are still truly complex elements and not just Strings) returning a List of Xpaths. Does not include Types.

Returns:
List of XPath Strings giving the Elements
Throws:
java.lang.Exception

getPreparser

public java.lang.Object getPreparser()

getRootElements

public java.util.List getRootElements()
                               throws java.lang.Exception
Returns a list of element paths extracted earlier from the schema, but only for the "root" element names, not subelements. Does not return types. More information for each element can be fetched with getElementPathData() passing in the element path.

Returns:
List of XPath Strings giving the Elements
Throws:
java.lang.Exception

getSchemaImportSource

public java.lang.String getSchemaImportSource(java.lang.String url)
Returns the schema source for the URL supplied. This URL should be one of the values from the iterator returned in "getSchemaImportURIs". source string that was used in the Schema(source, location) constructor.

Returns:
the schema source string.

getSchemaImportURIs

public java.util.Iterator getSchemaImportURIs()
Returns the keyset iterator of locations that we built up by recursively following imports in the Schema. Calling getSchemaSource(location) with each location String returned from the Iterator will return the schema source for that inported schema. This, amongst other things, allows the ServiceCall builder to retrieve all (recursively) imported schemas and add their definitions to the WebApp.

Returns:
the schema source string.

getSchemaSource

public java.lang.String getSchemaSource()
Returns the source string that was used in the Schema(String) constructor.

Returns:
the schema source string.

getSubElements

public java.util.List getSubElements(java.lang.String reqdParentPath,
                                     boolean immediateChildrenOnly)
For a given parent path, returns the list of subelements. If the supplied path is an empty string then all elements will be returned. If "immediateChildrenOnly" is specified, then only the elements directly underneath the element matching the parent path will be returned.

Parameters:
reqdParentPath -
immediateChildrenOnly -
Returns:
List

getTargetNamespace

public java.lang.String getTargetNamespace()
Returns the targetNamespace defined for this schema.

Returns:
targetNamespace

getTypes

public java.util.List getTypes(boolean getSubElements,
                               boolean getLeafElements,
                               java.lang.String typeSuffix)
                        throws java.lang.Exception
Extracts the types and optionally subelement trees from the Schema returning a list of XPaths.

Parameters:
getSubElements - indicator of whether subelement tree should be retrieved
getLeafElements - indicator of whether leafs elements should be retrieved if subelements are being retrieved
typeSuffix - can be null, an optional suffix added to the type name to make it clear in UIs that this is a type, not an element
Returns:
List of XPath Strings giving the Types
Throws:
java.lang.Exception

getXSModel

public org.apache.xerces.xs.XSModel getXSModel()

isAnElement

public boolean isAnElement(java.lang.String path)
Returns true if the passed in XPath maps to an element in the Schema (rather than a type).

Parameters:
path - XPath to target, eg books/book/book
Returns:
true if XPath maps to an element

isAType

public boolean isAType(java.lang.String path)
Returns true if the passed in XPath maps to a Type in the Schema (rather than an Element). If the xpath maps to an element and a type, then false will be returned.

Parameters:
path - XPath to target, eg books/book/book
Returns:
true if XPath maps to a type

isQualified

public boolean isQualified()

isRootElementWithChildren

public boolean isRootElementWithChildren(java.lang.String path)
Returns TRUE when the given path maps to a root elemment with children.

Parameters:
path - XPath to element that will be tested, eg books/book.
Returns:
TRUE when the given path maps to a root elemment with children.

main

public static void main(java.lang.String[] args)


Copyright © 2009 IBM. All Rights Reserved.