Class Schemas
- Object
-
- Schemas
-
public class Schemas extends Object
Utility class for performing various operations.- Author:
- Justin Deoliveira, The Open Planning Project
-
-
Constructor Summary
Constructors Constructor Description Schemas()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
dispose(XSDSchema schema)
Remove all references to a schema It is important to call this method for every dynamic schema created that is not needed anymore, because references in the static schema's will otherwise keep it alive foreverstatic List<XSDSchemaLocationResolver>
findSchemaLocationResolvers(Configuration configuration)
Finds allXSDSchemaLocationResolver
's used by the configuration.static SchemaIndex
findSchemas(Configuration configuration)
Finds all the XSDSchemas used by theconfiguration
by looking at the configuration's schema locator and its dependencies.static List<XSDParticle>
getAnyElementParticles(XSDTypeDefinition type)
Returns a list of all xs:any element particles that correspond to element declarations of the specified type.static XSDAttributeDeclaration
getAttributeDeclaration(XSDElementDeclaration element, QName qName)
Returns an attribute declaration that is contained in the type of another element declaration.static List<XSDAttributeDeclaration>
getAttributeDeclarations(XSDElementDeclaration element)
Returns a list of all attribute declarations declared in the type (or any base type) of the specified element.static List<XSDAttributeDeclaration>
getAttributeDeclarations(XSDTypeDefinition type)
Returns a list of all attribute declarations declared in the type (or any base type) of the specified element.static List<XSDAttributeDeclaration>
getAttributeDeclarations(XSDTypeDefinition type, boolean includeParents)
Returns a list of all attribute declarations declared in the type (and optionally any base type) of the specified element.static XSDTypeDefinition
getBaseTypeDefinition(XSDTypeDefinition type, QName parentTypeName)
Returns the base type defintion oftype
namedparentTypeName
.
static XSDElementDeclaration
getChildElementDeclaration(XSDElementDeclaration parent, QName qName)
Returns an element declaration that is contained in the type of another element declaration.static List
getChildElementDeclarations(XSDTypeDefinition type)
Returns a list of all child element declarations of the specified type, no order is guaranteed.static List<XSDElementDeclaration>
getChildElementDeclarations(XSDTypeDefinition type, boolean includeParents)
Returns a list of all child element declarations of the specified type, no order is guaranteed.static XSDParticle
getChildElementParticle(XSDTypeDefinition type, String name, boolean includeParents)
Returns the particle for an element declaration that is part of a type.static List<XSDParticle>
getChildElementParticles(XSDTypeDefinition type, boolean includeParents)
Returns a list of all child element particles that corresponde to element declarations of the specified type, no order is guaranteed.static List<ComponentAdapter>
getComponentAdaptersOfType(PicoContainer container, Class clazz)
Obtains all component adapters of a particular class from a container by navigating up the container hierarchy.static <T> List<T>
getComponentInstancesOfType(PicoContainer container, Class<T> clazz)
Obtains all instances of a particular class from a container by navigating up the container hierachy.static List<XSDElementDeclaration>
getDerivedElementDeclarations(XSDElementDeclaration element)
Returns a list of all top level elements that are of a type derived from the type of the specified element.static XSDElementDeclaration
getElementDeclaration(XSDSchema schema, QName name)
Searchesschema
for an element which matchesname
.static List
getImports(XSDSchema schema)
Returns a flat list of imports from the specified schema.static List
getIncludes(XSDSchema schema)
Returns a flat list of includes from the specified schema.static int
getMaxOccurs(XSDComplexTypeDefinition type, XSDElementDeclaration element)
Returns the minimum number of occurences of an element within a complex type.static int
getMinOccurs(XSDComplexTypeDefinition type, XSDElementDeclaration element)
Returns the minimum number of occurences of an element within a complex type.static QName
getParticleName(XSDParticle particle)
Returns the name of the element represented by the particle as a QName.static String
getTargetPrefix(XSDSchema schema)
Returns the namespace prefix mapped to the targetNamespace of the schema.static XSDImport
importSchema(XSDSchema schema, XSDSchema importee)
Imports one schema into another.static boolean
isBaseType(XSDElementDeclaration e1, XSDElementDeclaration e2)
Determines if the type of an element is a sub-type of another element.static boolean
nameMatches(XSDNamedComponent component, QName qName)
Method for comparing the name of a schema component to a qualified name.static XSDSchema
parse(String location)
Parses a schema at the specified location.static XSDSchema
parse(String location, List<XSDSchemaLocator> locators, List<XSDSchemaLocationResolver> resolvers)
Parses a schema at the specified location.static XSDSchema
parse(String location, List<XSDSchemaLocator> locators, List<XSDSchemaLocationResolver> resolvers, List<URIHandler> uriHandlers)
Parses a schema at the specified location.static XSDSchema
parse(String location, ResourceSet resourceSet)
static void
unregisterComponent(PicoContainer container, Object key)
Unregisters a component in the container and all parent containers.static List
validateImportsIncludes(String location)
static List
validateImportsIncludes(String location, List<XSDSchemaLocator> locators, List<XSDSchemaLocationResolver> resolvers)
static List
validateImportsIncludes(String location, XSDSchemaLocator[] locators, XSDSchemaLocationResolver[] resolvers)
-
-
-
Method Detail
-
findSchemas
public static final SchemaIndex findSchemas(Configuration configuration)
Finds all the XSDSchemas used by theconfiguration
by looking at the configuration's schema locator and its dependencies.- Parameters:
configuration
- theConfiguration
for which to find all its related schemas- Returns:
- a
SchemaIndex
holding the schemas related toconfiguration
-
findSchemaLocationResolvers
public static List<XSDSchemaLocationResolver> findSchemaLocationResolvers(Configuration configuration)
Finds allXSDSchemaLocationResolver
's used by the configuration.- Parameters:
configuration
- The parser configuration.- Returns:
- A list of location resolvers, empty if none found.
-
parse
public static final XSDSchema parse(String location) throws IOException
Parses a schema at the specified location.- Parameters:
location
- A uri pointing to the location of the schema.- Returns:
- The parsed schema, or null if the schema could not be parsed.
- Throws:
IOException
- In the event of a schema parsing error.
-
parse
public static final XSDSchema parse(String location, List<XSDSchemaLocator> locators, List<XSDSchemaLocationResolver> resolvers) throws IOException
Parses a schema at the specified location.- Parameters:
location
- A uri pointing to the location of the schema.locators
- A list of schema locator objects to be used when parsing imports/includes of the main schema.resolvers
- A list of schema location resolvers used to override schema locations encountered in an instance document or an imported schema.- Returns:
- The parsed schema, or null if the schema could not be parsed.
- Throws:
IOException
- In the event of a schema parsing error.
-
parse
public static final XSDSchema parse(String location, List<XSDSchemaLocator> locators, List<XSDSchemaLocationResolver> resolvers, List<URIHandler> uriHandlers) throws IOException
Parses a schema at the specified location.- Parameters:
location
- A uri pointing to the location of the schema.locators
- A list of schema locator objects to be used when parsing imports/includes of the main schema.resolvers
- A list of schema location resolvers used to override schema locations encountered in an instance document or an imported schema.uriHandlers
- A list of uri handlers to inject into the parsing chain, to handle externally referenced resources, like external schemas, etc...- Returns:
- The parsed schema, or null if the schema could not be parsed.
- Throws:
IOException
- In the event of a schema parsing error.
-
parse
public static final XSDSchema parse(String location, ResourceSet resourceSet) throws IOException
- Throws:
IOException
-
importSchema
public static final XSDImport importSchema(XSDSchema schema, XSDSchema importee) throws IOException
Imports one schema into another.- Parameters:
schema
- The schema being imported into.importee
- The schema being imported.- Returns:
- The import object.
- Throws:
IOException
-
dispose
public static final void dispose(XSDSchema schema)
Remove all references to a schema It is important to call this method for every dynamic schema created that is not needed anymore, because references in the static schema's will otherwise keep it alive forever- Parameters:
schema
- to be flushed
-
validateImportsIncludes
public static final List validateImportsIncludes(String location) throws IOException
- Throws:
IOException
-
validateImportsIncludes
public static final List validateImportsIncludes(String location, XSDSchemaLocator[] locators, XSDSchemaLocationResolver[] resolvers) throws IOException
- Throws:
IOException
-
validateImportsIncludes
public static final List validateImportsIncludes(String location, List<XSDSchemaLocator> locators, List<XSDSchemaLocationResolver> resolvers) throws IOException
- Throws:
IOException
-
getChildElementDeclarations
public static final List getChildElementDeclarations(XSDTypeDefinition type)
Returns a list of all child element declarations of the specified type, no order is guaranteed.- Parameters:
type
- The type.- Returns:
- A list of @link XSDElementDeclaration objects, one for each child element.
-
getChildElementParticle
public static final XSDParticle getChildElementParticle(XSDTypeDefinition type, String name, boolean includeParents)
Returns the particle for an element declaration that is part of a type.- Parameters:
type
- The type definition.name
- The naem of the child element declaration.includeParents
- Flag to control wether parent types are included.- Returns:
- The particle representing the element declaration, or
null
if it could not be found.
-
getChildElementParticles
public static final List<XSDParticle> getChildElementParticles(XSDTypeDefinition type, boolean includeParents)
Returns a list of all child element particles that corresponde to element declarations of the specified type, no order is guaranteed.The
includeParents
flag controls if this method should returns those elements defined on parent types.- Parameters:
type
- THe type.includeParents
- flag indicating if parent types should be processed- Returns:
- A list of
XSDParticle
.
-
getAnyElementParticles
public static final List<XSDParticle> getAnyElementParticles(XSDTypeDefinition type)
Returns a list of all xs:any element particles that correspond to element declarations of the specified type.- Parameters:
type
- The type.- Returns:
- A list of
XSDParticle
.
-
getChildElementDeclarations
public static final List<XSDElementDeclaration> getChildElementDeclarations(XSDTypeDefinition type, boolean includeParents)
Returns a list of all child element declarations of the specified type, no order is guaranteed.The
includeParents
flag controls if this method should returns those elements defined on parent types.- Parameters:
type
- The typeincludeParents
- flag indicating if parent types should be processed- Returns:
- A list of @link XSDElementDeclaration objects, one for each child element.
-
getBaseTypeDefinition
public static final XSDTypeDefinition getBaseTypeDefinition(XSDTypeDefinition type, QName parentTypeName)
Returns the base type defintion oftype
namedparentTypeName
.
This method will handle the case in which the
parentTypeName == type.getTypeName()
. If no such parent type is found this method will returnnull
.- Parameters:
type
- The type.parentTypeName
- The name of the base type to return.- Returns:
- The base type, or null if it could not be found.
-
isBaseType
public static final boolean isBaseType(XSDElementDeclaration e1, XSDElementDeclaration e2)
Determines if the type of an element is a sub-type of another element.- Parameters:
e1
- The element.e2
- The element to be tested as a base type.- Since:
- 2.5
-
getMinOccurs
public static final int getMinOccurs(XSDComplexTypeDefinition type, XSDElementDeclaration element)
Returns the minimum number of occurences of an element within a complex type.- Parameters:
type
- The type definition containg the declarationelement
element
- The declaration of the element.- Returns:
- The minimum number of occurences.
- Throws:
IllegalArgumentException
- If the element declaration cannot be locaated withing the type definition.
-
getMaxOccurs
public static final int getMaxOccurs(XSDComplexTypeDefinition type, XSDElementDeclaration element)
Returns the minimum number of occurences of an element within a complex type.- Parameters:
type
- The type definition containg the declarationelement
element
- The declaration of the element.- Returns:
- The minimum number of occurences.
- Throws:
IllegalArgumentException
- If the element declaration cannot be locaated withing the type definition.
-
getChildElementDeclaration
public static final XSDElementDeclaration getChildElementDeclaration(XSDElementDeclaration parent, QName qName)
Returns an element declaration that is contained in the type of another element declaration. The following strategy is used to locate the child element declaration.- The immediate children of the specified element are examined, if a match is found, it is returned. If 1. does not match, global elements that derive from the immediate children are examined.
- Parameters:
parent
- the containing element declarationqName
- the qualified name of the contained element- Returns:
- The contained element declaration, or false if containment is not satisfied.
-
getDerivedElementDeclarations
public static final List<XSDElementDeclaration> getDerivedElementDeclarations(XSDElementDeclaration element)
Returns a list of all top level elements that are of a type derived from the type of the specified element.- Parameters:
element
- The element.- Returns:
- All elements which are of a type derived from the type of the specified element.
-
getAttributeDeclarations
public static final List<XSDAttributeDeclaration> getAttributeDeclarations(XSDElementDeclaration element)
Returns a list of all attribute declarations declared in the type (or any base type) of the specified element.This method is just a shortcut for
getAttributeDeclarations(element.getType()
- Parameters:
element
- The element.- Returns:
- A list of @link XSDAttributeDeclaration objects, one for each attribute of the element.
-
getAttributeDeclarations
public static final List<XSDAttributeDeclaration> getAttributeDeclarations(XSDTypeDefinition type)
Returns a list of all attribute declarations declared in the type (or any base type) of the specified element.- Parameters:
type
- The type definition- Returns:
- A list of @link XSDAttributeDeclaration objects, one for each attribute of the element.
-
getAttributeDeclarations
public static final List<XSDAttributeDeclaration> getAttributeDeclarations(XSDTypeDefinition type, boolean includeParents)
Returns a list of all attribute declarations declared in the type (and optionally any base type) of the specified element.- Parameters:
type
- The element typeincludeParents
- Wether to include parent types.- Returns:
- A list of @link XSDAttributeDeclaration objects, one for each attribute of the element.
-
getAttributeDeclaration
public static final XSDAttributeDeclaration getAttributeDeclaration(XSDElementDeclaration element, QName qName)
Returns an attribute declaration that is contained in the type of another element declaration.- Parameters:
element
- The containing element declaration.qName
- The qualified name of the contained attribute- Returns:
- The contained attribute declaration, or false if containment is not satisfied.
-
getImports
public static final List getImports(XSDSchema schema)
Returns a flat list of imports from the specified schema.The method recurses into imported schemas. The list returned is filtered so that duplicate includes are removed. Two includes are considered equal if they have the same target namespace.
- Parameters:
schema
- The top-level schema.- Returns:
- A list containing objects of type
XSDImport
.
-
getIncludes
public static final List getIncludes(XSDSchema schema)
Returns a flat list of includes from the specified schema.The method recurses into included schemas. The list returned is filtered so that duplicate includes are removed. Two includes are considered equal if they have the same uri location
- Parameters:
schema
- The top-level schema.- Returns:
- A list containing objects of type
XSDInclude
.
-
getElementDeclaration
public static XSDElementDeclaration getElementDeclaration(XSDSchema schema, QName name)
Searchesschema
for an element which matchesname
.- Parameters:
schema
- The schemaname
- The element to search for- Returns:
- The element declaration, or null if it could not be found.
-
nameMatches
public static final boolean nameMatches(XSDNamedComponent component, QName qName)
Method for comparing the name of a schema component to a qualified name. The component name and the qualified name match if both the namespaces match, and the local parts match. Prefixes are ignored. Two strings will match if one of the following conditions hold.- Both strings are null.
- Both strings are the empty string.
- One string is null, and the other is the empty string.
- Both strings are non-null and non-empty and equals() return true.
- Parameters:
component
- The component in question.qName
- The qualifined name.
-
getTargetPrefix
public static String getTargetPrefix(XSDSchema schema)
Returns the namespace prefix mapped to the targetNamespace of the schema.- Parameters:
schema
- The schema in question.- Returns:
- The namesapce prefix, or
null
if not found.
-
getComponentInstancesOfType
public static <T> List<T> getComponentInstancesOfType(PicoContainer container, Class<T> clazz)
Obtains all instances of a particular class from a container by navigating up the container hierachy.- Parameters:
container
- The container.clazz
- The class.- Returns:
- A list of all instances of
clazz
, or the empty list if none found.
-
getComponentAdaptersOfType
public static List<ComponentAdapter> getComponentAdaptersOfType(PicoContainer container, Class clazz)
Obtains all component adapters of a particular class from a container by navigating up the container hierarchy.- Parameters:
container
- The container.clazz
- The class.- Returns:
- A list of all adapters for components of class
clazz
, or the empty list if none found.
-
unregisterComponent
public static void unregisterComponent(PicoContainer container, Object key)
Unregisters a component in the container and all parent containers.- Parameters:
container
- The container.key
- The key of the component.
-
getParticleName
public static QName getParticleName(XSDParticle particle)
Returns the name of the element represented by the particle as a QName.
-
-