xml.parser
Class SchemaMapping

java.lang.Object
  extended by xml.parser.SchemaMapping

public final class SchemaMapping
extends java.lang.Object

keeps rules to map a schema to a given instance document. The schema mapping can be serialized in a "schemas.xml" file, compatible with the nXML emacs mode written by James Clark. TODO: some features are still missing (apply following rule) TODO: with XSD, one has to specify one schema per namespace, it is not supported by this implementation.

Version:
$Id: SchemaMapping.java 21319 2012-03-11 10:34:48Z kerik-sf $
Author:
Eric Le Lay

Nested Class Summary
static class SchemaMapping.DefaultRule
          always matches : not really useful for us, is it ?
static class SchemaMapping.DoctypeRule
          doctype -> typeID or URL.
static class SchemaMapping.DocumentElementRule
          prefix + localName -> typeId or URL
static class SchemaMapping.IncludeMapping
           
static class SchemaMapping.Mapping
           
static class SchemaMapping.NamespaceRule
          namespace -> typeId or URL
static class SchemaMapping.Result
           
static class SchemaMapping.TransformURI
          URI pattern -> related URL.
static class SchemaMapping.TypeIdMapping
          models the typeId element in schemas.xml
static class SchemaMapping.URIPatternRule
          URI pattern -> typeID or URL.
static class SchemaMapping.URIResourceRule
          URI -> typeId or URL.
 
Field Summary
static java.lang.String SCHEMAS_FILE
          default name of the file containing schema mapping rules
 
Constructor Summary
SchemaMapping()
          empty mapping
SchemaMapping(java.net.URI baseURI)
          empty mapping
 
Method Summary
 void addRule(SchemaMapping.Mapping r)
          manually add a rule
 void addTypeId(java.lang.String tid, java.lang.String target, boolean targetIsTypeId)
          manually add a typeId mapping.
 boolean ensureIncluded(SchemaMapping mapping)
           
static SchemaMapping fromDocument(java.lang.String url, org.xml.sax.ErrorHandler errorHandler)
          deserialize an xml document describing the mapping rules (schemas.xml)
 java.net.URI getBaseURI()
           
 SchemaMapping.Mapping getMappingForDocument(java.lang.String publicId, java.lang.String systemId, java.lang.String namespace, java.lang.String prefix, java.lang.String localName)
          iterate over the mappings and return the first hit.
 java.util.List<SchemaMapping.Mapping> getRules()
           
 SchemaMapping.Result getSchemaForDocument(java.lang.String publicId, java.lang.String systemId, java.lang.String namespace, java.lang.String prefix, java.lang.String localName, boolean followTypeId)
          iterate over the mappings and return the first hit.
 java.lang.String getTypeIdForDocument(java.lang.String resourceURL)
           
 java.util.List<SchemaMapping.TypeIdMapping> getTypeIds()
           
 void insertRuleAt(int pos, SchemaMapping.Mapping m)
           
 void removeRule(SchemaMapping.Mapping m)
           
 void removeRuleAt(int pos)
           
 SchemaMapping.Result resolveTypeId(java.lang.String tid)
           
static boolean resourceExists(java.net.URI resource)
          try to use the VFS to detect if a resource exists
 void toDocument(java.lang.String output)
          serialize to an XML document
 void updateMapping(SchemaMapping.URIResourceRule newRule)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SCHEMAS_FILE

public static final java.lang.String SCHEMAS_FILE
default name of the file containing schema mapping rules

See Also:
Constant Field Values
Constructor Detail

SchemaMapping

public SchemaMapping()
empty mapping


SchemaMapping

public SchemaMapping(java.net.URI baseURI)
empty mapping

Parameters:
baseURI - url to resolve relative URLs in this document
Method Detail

getBaseURI

public java.net.URI getBaseURI()
Returns:
this schema mapping's URL or null if it's totally in memory

insertRuleAt

public void insertRuleAt(int pos,
                         SchemaMapping.Mapping m)

removeRule

public void removeRule(SchemaMapping.Mapping m)

removeRuleAt

public void removeRuleAt(int pos)

updateMapping

public void updateMapping(SchemaMapping.URIResourceRule newRule)

ensureIncluded

public boolean ensureIncluded(SchemaMapping mapping)
Returns:
was there ?

getTypeIdForDocument

public java.lang.String getTypeIdForDocument(java.lang.String resourceURL)
Returns:
typeId for resource or null

resolveTypeId

public SchemaMapping.Result resolveTypeId(java.lang.String tid)
Parameters:
tid - the type identifier to look up (try "RNG")
Returns:
URL (eventually, in case of chaining of typeIds) associated to this typeId or null if it can't be found.

addRule

public void addRule(SchemaMapping.Mapping r)
manually add a rule

Parameters:
r - rule to add

addTypeId

public void addTypeId(java.lang.String tid,
                      java.lang.String target,
                      boolean targetIsTypeId)
manually add a typeId mapping. If the typeId mapping exists already, it is overriden

Parameters:
tid - typeId
target - target url or typeId
targetIsTypeId - is the target itself a type id ?

getTypeIds

public java.util.List<SchemaMapping.TypeIdMapping> getTypeIds()

getRules

public java.util.List<SchemaMapping.Mapping> getRules()

getSchemaForDocument

public SchemaMapping.Result getSchemaForDocument(java.lang.String publicId,
                                                 java.lang.String systemId,
                                                 java.lang.String namespace,
                                                 java.lang.String prefix,
                                                 java.lang.String localName,
                                                 boolean followTypeId)
iterate over the mappings and return the first hit. all the parameters are given the same priority : it's really the ordering of rules which defines a priority order. Any of the paremeters can be null.

Parameters:
publicId - public ID of the parsed document
systemId - system ID of the parsed document
namespace - namespace of the root element of the parsed document
prefix - prefix of the root element of the parsed document
localName - localName of the root element of the parsed document
followTypeId - if the schema referenced from the typeId should be returned
Returns:
schema URL for given document or null if not found

getMappingForDocument

public SchemaMapping.Mapping getMappingForDocument(java.lang.String publicId,
                                                   java.lang.String systemId,
                                                   java.lang.String namespace,
                                                   java.lang.String prefix,
                                                   java.lang.String localName)
iterate over the mappings and return the first hit. all the parameters are given the same priority : it's really the ordering of rules which defines a priority order. Any of the paremeters can be null.

Parameters:
publicId - public ID of the parsed document
systemId - system ID of the parsed document
namespace - namespace of the root element of the parsed document
prefix - prefix of the root element of the parsed document
localName - localName of the root element of the parsed document
Returns:
matching mapping

fromDocument

public static SchemaMapping fromDocument(java.lang.String url,
                                         org.xml.sax.ErrorHandler errorHandler)
                                  throws java.io.IOException,
                                         org.xml.sax.SAXException,
                                         java.lang.IllegalArgumentException
deserialize an xml document describing the mapping rules (schemas.xml)

Parameters:
url - url of the document to read
errorHandler - error handler notified of errors encountered when loading the schema
Returns:
new SchemaMapping taken from the document at url, or null
Throws:
java.lang.IllegalArgumentException - if the url is null
java.io.IOException
org.xml.sax.SAXException

toDocument

public void toDocument(java.lang.String output)
                throws java.io.IOException
serialize to an XML document

Parameters:
output - url to the output file
Throws:
java.io.IOException - if there is an error during serialization FIXME: serialized document could be invalid. Is it worth using proper XML serialization ?

resourceExists

public static boolean resourceExists(java.net.URI resource)
try to use the VFS to detect if a resource exists