Package com.sun.msv.reader
Class GrammarReader
- java.lang.Object
-
- org.xml.sax.helpers.XMLFilterImpl
-
- com.sun.msv.reader.GrammarReader
-
- All Implemented Interfaces:
IDContextProvider2
,org.relaxng.datatype.ValidationContext
,org.xml.sax.ContentHandler
,org.xml.sax.DTDHandler
,org.xml.sax.EntityResolver
,org.xml.sax.ErrorHandler
,org.xml.sax.XMLFilter
,org.xml.sax.XMLReader
- Direct Known Subclasses:
RELAXReader
,TREXBaseReader
,XMLSchemaReader
public abstract class GrammarReader extends org.xml.sax.helpers.XMLFilterImpl implements IDContextProvider2
base implementation of grammar readers that read grammar from SAX2 stream. GrammarReader class can be used as a ContentHandler that parses a grammar. So the typical usage is
Or you may want to use several pre-defined static "parse" methods for ease of use.GrammarReader reader = new RELAXGrammarReader(...); XMLReader parser = .... // create a new XMLReader here parser.setContentHandler(reader); parser.parse(whateverYouLike); return reader.grammar; // obtain parsed grammar.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
GrammarReader.BackPatch
class
GrammarReader.BackwardReferenceMap
memorizes what declarations are referenced from where.class
GrammarReader.ChainPrefixResolver
private class
GrammarReader.InclusionContext
information that must be sheltered before switching InputSource (typically by inclusion).static interface
GrammarReader.PrefixResolver
namespace prefix to URI conversion map.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Vector
backPatchJobs
GrammarReader.BackwardReferenceMap
backwardReference
keeps track of all backward references to every ReferenceExp.static GrammarReader.PrefixResolver
basePrefixResolver
The namespace prefix resolver that only resolves "xml" prefix.Controller
controller
this object receives errors and warningsprivate java.util.Map
declaredLocations
this map remembers where ReferenceExps are defined, and where user defined types are defined.private java.util.Vector
delayedBackPatchJobs
static java.lang.String
ERR_BAD_ATTRIBUTE_VALUE
static java.lang.String
ERR_BAD_TYPE
static java.lang.String
ERR_CHARACTERS
static java.lang.String
ERR_CONFLICTING_ATTRIBUTES
static java.lang.String
ERR_DATATYPE_ALREADY_DEFINED
static java.lang.String
ERR_DISALLOWED_ATTRIBUTE
static java.lang.String
ERR_FRAGMENT_IDENTIFIER
static java.lang.String
ERR_ILLEGAL_FINAL_VALUE
static java.lang.String
ERR_MALPLACED_ELEMENT
static java.lang.String
ERR_MISSING_ATTRIBUTE
static java.lang.String
ERR_MISSING_ATTRIBUTE_2
static java.lang.String
ERR_MISSING_CHILD_EXPRESSION
static java.lang.String
ERR_MISSING_CHILD_TYPE
static java.lang.String
ERR_MISSING_TOPLEVEL
static java.lang.String
ERR_MORE_THAN_ONE_CHILD_EXPRESSION
static java.lang.String
ERR_MORE_THAN_ONE_CHILD_TYPE
static java.lang.String
ERR_RECURSIVE_DATATYPE
static java.lang.String
ERR_RECURSIVE_INCLUDE
static java.lang.String
ERR_RUNAWAY_EXPRESSION
static java.lang.String
ERR_UNDEFINED_DATATYPE
private org.xml.sax.Locator
locator
document Locator that is given by XML readerjavax.xml.parsers.SAXParserFactory
parserFactory
Reader may create another SAXParser from this factoryprivate GrammarReader.InclusionContext
pendingIncludes
current inclusion contextExpressionPool
pool
this object must be used to create a new expressionGrammarReader.PrefixResolver
prefixResolver
static java.lang.String
WRN_DEPRECATED_TYPENAME
static java.lang.String
WRN_MAYBE_WRONG_NAMESPACE
-
Constructor Summary
Constructors Modifier Constructor Description protected
GrammarReader(GrammarReaderController _controller, javax.xml.parsers.SAXParserFactory parserFactory, ExpressionPool pool, State initialState)
constructor that should be called from parse method.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
_parse(java.lang.Object source, org.xml.sax.Locator errorSource)
parses a grammar from the specified sourcevoid
addBackPatchJob(XSDatatypeExp job)
void
addBackPatchJob(GrammarReader.BackPatch job)
java.lang.String
combineURI(java.lang.String baseURI, java.lang.String relativeURI)
converts the relative URL to the absolute one by using the specified base URL.java.lang.String
combineURL(java.lang.String baseURI, java.lang.String relativeURI)
Deprecated.use the combineURI method.abstract State
createExpressionChildState(State parent, StartTagInfo tag)
this method must be implemented by the derived class to create language-default expresion state.protected static javax.xml.parsers.SAXParserFactory
createParserFactory()
Creates a default SAXParserFactory.void
detectUndefinedOnes(ReferenceContainer container, java.lang.String errMsg)
detects undefined ReferenceExp and reports it as an error.void
endPrefixMapping(java.lang.String prefix)
XSDatatype
getBackwardCompatibleType(java.lang.String typeName)
tries to obtain a DataType object by resolving obsolete names.java.lang.String
getBaseUri()
State
getCurrentState()
gets current State object.org.xml.sax.Locator
getDeclaredLocationOf(java.lang.Object o)
org.xml.sax.Locator
getLocator()
abstract Grammar
getResultAsGrammar()
gets the parsed AGM.static javax.xml.transform.Source
inputSourceFromLSInput(org.w3c.dom.ls.LSInput input)
This is a stopgap until we can wean more of this code from SAX-dependence.protected Expression
interceptExpression(State state, Expression exp)
intercepts an expression made by ExpressionState before it is passed to the parent state.protected abstract boolean
isGrammarElement(StartTagInfo tag)
checks if given element is that of the grammar elements.boolean
isNotation(java.lang.String notationName)
boolean
isUnparsedEntity(java.lang.String entityName)
java.util.Iterator
iterateInscopeNamespaces()
Iterates Map.Entry objects which has the prefix as key and the namespace URI as value.protected abstract java.lang.String
localizeMessage(java.lang.String propertyName, java.lang.Object[] args)
formats localized message with argumentsvoid
onID(org.relaxng.datatype.Datatype dt, StringToken token)
this method is called when a type with ID semantics is matched.void
parse(java.lang.String source)
parses a grammar from the specified sourcevoid
parse(javax.xml.transform.Source source)
Parse from an arbitrary javax.xml.transform source.void
parse(org.xml.sax.InputSource source)
parses a grammar from the specified sourceprivate void
popInclusionContext()
void
popState()
pops the previous state from the stackprivate org.xml.sax.Locator[]
prepareLocation(org.xml.sax.Locator[] param)
private void
pushInclusionContext()
void
pushState(State newState, State parentState, StartTagInfo startTag)
pushs the current state into the stack and sets new onevoid
reportError(java.lang.Exception nestedException, java.lang.String propertyName)
void
reportError(java.lang.Exception nestedException, java.lang.String propertyName, java.lang.Object arg1)
void
reportError(java.lang.String propertyName)
void
reportError(java.lang.String propertyName, java.lang.Object arg1)
void
reportError(java.lang.String propertyName, java.lang.Object[] args, java.lang.Exception nestedException, org.xml.sax.Locator[] errorLocations)
reports an error to the controllervoid
reportError(java.lang.String propertyName, java.lang.Object arg1, java.lang.Object arg2)
void
reportError(java.lang.String propertyName, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
void
reportError(org.xml.sax.Locator[] locs, java.lang.String propertyName, java.lang.Object[] args)
void
reportWarning(java.lang.String propertyName)
void
reportWarning(java.lang.String propertyName, java.lang.Object arg1)
void
reportWarning(java.lang.String propertyName, java.lang.Object[] args, org.xml.sax.Locator[] locations)
reports a warning to the controllervoid
reportWarning(java.lang.String propertyName, java.lang.Object arg1, java.lang.Object arg2)
org.xml.sax.InputSource
resolveLocation(State sourceState, java.lang.String uri)
obtains InputSource for the specified url.java.lang.String
resolveNamespacePrefix(java.lang.String prefix)
void
runBackPatchJob()
Performs all back-patchings.private void
runBackPatchJob(java.util.Vector vec)
void
setDeclaredLocationOf(java.lang.Object o)
void
setDocumentLocator(org.xml.sax.Locator loc)
void
setLocator(org.xml.sax.Locator locator)
java.lang.String[]
splitQName(java.lang.String qName)
Resolves a QName into a pair of (namespace URI,local name).void
startPrefixMapping(java.lang.String prefix, java.lang.String uri)
void
switchSource(State sourceState, java.lang.String url, State newState)
Switchs InputSource to the specified URL and parses it by the specified state.void
switchSource(javax.xml.transform.Source source, State newState)
Start reading input from a source defined by a javax.xml.transform source.void
switchSource(org.xml.sax.InputSource source, State newState)
start reading input from a source defined by a SAX InputSource.-
Methods inherited from class org.xml.sax.helpers.XMLFilterImpl
characters, endDocument, endElement, error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, getProperty, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setContentHandler, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, setProperty, skippedEntity, startDocument, startElement, unparsedEntityDecl, warning
-
-
-
-
Field Detail
-
locator
private org.xml.sax.Locator locator
document Locator that is given by XML reader
-
controller
public final Controller controller
this object receives errors and warnings
-
parserFactory
public final javax.xml.parsers.SAXParserFactory parserFactory
Reader may create another SAXParser from this factory
-
pool
public final ExpressionPool pool
this object must be used to create a new expression
-
basePrefixResolver
public static final GrammarReader.PrefixResolver basePrefixResolver
The namespace prefix resolver that only resolves "xml" prefix. This class should be used as the base resolver.
-
prefixResolver
public GrammarReader.PrefixResolver prefixResolver
-
pendingIncludes
private GrammarReader.InclusionContext pendingIncludes
current inclusion context
-
backwardReference
public final GrammarReader.BackwardReferenceMap backwardReference
keeps track of all backward references to every ReferenceExp. this map should be used to report the source of error of undefined-something.
-
declaredLocations
private final java.util.Map declaredLocations
this map remembers where ReferenceExps are defined, and where user defined types are defined. some ReferenceExp can be defined in more than one location. In those cases, the last one is always memorized. This behavior is essential to correctly implement TREX constraint that no two <define> is allowed in the same file.
-
backPatchJobs
private final java.util.Vector backPatchJobs
-
delayedBackPatchJobs
private final java.util.Vector delayedBackPatchJobs
-
ERR_MALPLACED_ELEMENT
public static final java.lang.String ERR_MALPLACED_ELEMENT
- See Also:
- Constant Field Values
-
ERR_CHARACTERS
public static final java.lang.String ERR_CHARACTERS
- See Also:
- Constant Field Values
-
ERR_DISALLOWED_ATTRIBUTE
public static final java.lang.String ERR_DISALLOWED_ATTRIBUTE
- See Also:
- Constant Field Values
-
ERR_MISSING_ATTRIBUTE
public static final java.lang.String ERR_MISSING_ATTRIBUTE
- See Also:
- Constant Field Values
-
ERR_BAD_ATTRIBUTE_VALUE
public static final java.lang.String ERR_BAD_ATTRIBUTE_VALUE
- See Also:
- Constant Field Values
-
ERR_MISSING_ATTRIBUTE_2
public static final java.lang.String ERR_MISSING_ATTRIBUTE_2
- See Also:
- Constant Field Values
-
ERR_CONFLICTING_ATTRIBUTES
public static final java.lang.String ERR_CONFLICTING_ATTRIBUTES
- See Also:
- Constant Field Values
-
ERR_RECURSIVE_INCLUDE
public static final java.lang.String ERR_RECURSIVE_INCLUDE
- See Also:
- Constant Field Values
-
ERR_FRAGMENT_IDENTIFIER
public static final java.lang.String ERR_FRAGMENT_IDENTIFIER
- See Also:
- Constant Field Values
-
ERR_UNDEFINED_DATATYPE
public static final java.lang.String ERR_UNDEFINED_DATATYPE
- See Also:
- Constant Field Values
-
ERR_DATATYPE_ALREADY_DEFINED
public static final java.lang.String ERR_DATATYPE_ALREADY_DEFINED
- See Also:
- Constant Field Values
-
ERR_MISSING_CHILD_EXPRESSION
public static final java.lang.String ERR_MISSING_CHILD_EXPRESSION
- See Also:
- Constant Field Values
-
ERR_MORE_THAN_ONE_CHILD_EXPRESSION
public static final java.lang.String ERR_MORE_THAN_ONE_CHILD_EXPRESSION
- See Also:
- Constant Field Values
-
ERR_MORE_THAN_ONE_CHILD_TYPE
public static final java.lang.String ERR_MORE_THAN_ONE_CHILD_TYPE
- See Also:
- Constant Field Values
-
ERR_MISSING_CHILD_TYPE
public static final java.lang.String ERR_MISSING_CHILD_TYPE
- See Also:
- Constant Field Values
-
ERR_ILLEGAL_FINAL_VALUE
public static final java.lang.String ERR_ILLEGAL_FINAL_VALUE
- See Also:
- Constant Field Values
-
ERR_RUNAWAY_EXPRESSION
public static final java.lang.String ERR_RUNAWAY_EXPRESSION
- See Also:
- Constant Field Values
-
ERR_MISSING_TOPLEVEL
public static final java.lang.String ERR_MISSING_TOPLEVEL
- See Also:
- Constant Field Values
-
WRN_MAYBE_WRONG_NAMESPACE
public static final java.lang.String WRN_MAYBE_WRONG_NAMESPACE
- See Also:
- Constant Field Values
-
WRN_DEPRECATED_TYPENAME
public static final java.lang.String WRN_DEPRECATED_TYPENAME
- See Also:
- Constant Field Values
-
ERR_BAD_TYPE
public static final java.lang.String ERR_BAD_TYPE
- See Also:
- Constant Field Values
-
ERR_RECURSIVE_DATATYPE
public static final java.lang.String ERR_RECURSIVE_DATATYPE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
GrammarReader
protected GrammarReader(GrammarReaderController _controller, javax.xml.parsers.SAXParserFactory parserFactory, ExpressionPool pool, State initialState)
constructor that should be called from parse method.
-
-
Method Detail
-
createParserFactory
protected static javax.xml.parsers.SAXParserFactory createParserFactory()
Creates a default SAXParserFactory.
-
getResultAsGrammar
public abstract Grammar getResultAsGrammar()
gets the parsed AGM. Should any error happens, this method should returns null. derived classes should implement type-safe getGrammar method, along with this method.
-
isGrammarElement
protected abstract boolean isGrammarElement(StartTagInfo tag)
checks if given element is that of the grammar elements.
-
startPrefixMapping
public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) throws org.xml.sax.SAXException
- Specified by:
startPrefixMapping
in interfaceorg.xml.sax.ContentHandler
- Overrides:
startPrefixMapping
in classorg.xml.sax.helpers.XMLFilterImpl
- Throws:
org.xml.sax.SAXException
-
endPrefixMapping
public void endPrefixMapping(java.lang.String prefix) throws org.xml.sax.SAXException
- Specified by:
endPrefixMapping
in interfaceorg.xml.sax.ContentHandler
- Overrides:
endPrefixMapping
in classorg.xml.sax.helpers.XMLFilterImpl
- Throws:
org.xml.sax.SAXException
-
iterateInscopeNamespaces
public java.util.Iterator iterateInscopeNamespaces()
Iterates Map.Entry objects which has the prefix as key and the namespace URI as value.
-
splitQName
public java.lang.String[] splitQName(java.lang.String qName)
Resolves a QName into a pair of (namespace URI,local name). Therefore this method returns null if it fails to process QName.
-
interceptExpression
protected Expression interceptExpression(State state, Expression exp)
intercepts an expression made by ExpressionState before it is passed to the parent state. derived class can perform further wrap-up before it is received by the parent. This mechanism is used by RELAXReader to handle occurs attribute.
-
getBackwardCompatibleType
public XSDatatype getBackwardCompatibleType(java.lang.String typeName)
tries to obtain a DataType object by resolving obsolete names. this method is useful for backward compatibility purpose.
-
pushInclusionContext
private void pushInclusionContext()
-
popInclusionContext
private void popInclusionContext()
-
resolveLocation
public final org.xml.sax.InputSource resolveLocation(State sourceState, java.lang.String uri) throws AbortException
obtains InputSource for the specified url. Also this method allows GrammarReaderController to redirect or prohibit inclusion.- Parameters:
sourceState
- The base URI of this state is used to resolve the resource.- Returns:
- always return non-null valid object
- Throws:
AbortException
-
combineURI
public final java.lang.String combineURI(java.lang.String baseURI, java.lang.String relativeURI)
converts the relative URL to the absolute one by using the specified base URL.
-
combineURL
public final java.lang.String combineURL(java.lang.String baseURI, java.lang.String relativeURI)
Deprecated.use the combineURI method.
-
switchSource
public void switchSource(State sourceState, java.lang.String url, State newState) throws AbortException
Switchs InputSource to the specified URL and parses it by the specified state. The method will return after the parsing of the new source is completed. derived classes can use this method to realize semantics of 'include'.- Parameters:
sourceState
- this state is used to resolve the URL.newState
- this state will parse top-level of new XML source. this state receives document element by its createChildState method.- Throws:
AbortException
-
switchSource
public void switchSource(org.xml.sax.InputSource source, State newState)
start reading input from a source defined by a SAX InputSource.- Parameters:
source
-newState
-
-
switchSource
public void switchSource(javax.xml.transform.Source source, State newState)
Start reading input from a source defined by a javax.xml.transform source.- Parameters:
source
-newState
-
-
parse
public final void parse(java.lang.String source)
parses a grammar from the specified source- Specified by:
parse
in interfaceorg.xml.sax.XMLReader
- Overrides:
parse
in classorg.xml.sax.helpers.XMLFilterImpl
-
parse
public final void parse(org.xml.sax.InputSource source)
parses a grammar from the specified source- Specified by:
parse
in interfaceorg.xml.sax.XMLReader
- Overrides:
parse
in classorg.xml.sax.helpers.XMLFilterImpl
-
parse
public void parse(javax.xml.transform.Source source) throws javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException
Parse from an arbitrary javax.xml.transform source. If the Source can be processed bySAXSource.sourceToInputSource(Source)
, then this API will use that. Otherwise, it will use a transformer to create a stream of SAX events. In that later case, the grammar controller will not be called for any errors on the source side or to resolve any references; the caller is responsible for making separate arrangements.- Parameters:
source
-- Throws:
javax.xml.transform.TransformerConfigurationException
javax.xml.transform.TransformerException
-
_parse
public final void _parse(java.lang.Object source, org.xml.sax.Locator errorSource)
parses a grammar from the specified source
-
setDeclaredLocationOf
public void setDeclaredLocationOf(java.lang.Object o)
-
getDeclaredLocationOf
public org.xml.sax.Locator getDeclaredLocationOf(java.lang.Object o)
-
detectUndefinedOnes
public void detectUndefinedOnes(ReferenceContainer container, java.lang.String errMsg)
detects undefined ReferenceExp and reports it as an error. this method is used in the final wrap-up process of parsing.
-
pushState
public void pushState(State newState, State parentState, StartTagInfo startTag)
pushs the current state into the stack and sets new one
-
popState
public void popState()
pops the previous state from the stack
-
getCurrentState
public final State getCurrentState()
gets current State object.
-
createExpressionChildState
public abstract State createExpressionChildState(State parent, StartTagInfo tag)
this method must be implemented by the derived class to create language-default expresion state.- Returns:
- null if the start tag is an error.
-
setDocumentLocator
public void setDocumentLocator(org.xml.sax.Locator loc)
- Specified by:
setDocumentLocator
in interfaceorg.xml.sax.ContentHandler
- Overrides:
setDocumentLocator
in classorg.xml.sax.helpers.XMLFilterImpl
-
resolveNamespacePrefix
public java.lang.String resolveNamespacePrefix(java.lang.String prefix)
- Specified by:
resolveNamespacePrefix
in interfaceorg.relaxng.datatype.ValidationContext
-
isUnparsedEntity
public boolean isUnparsedEntity(java.lang.String entityName)
- Specified by:
isUnparsedEntity
in interfaceorg.relaxng.datatype.ValidationContext
-
isNotation
public boolean isNotation(java.lang.String notationName)
- Specified by:
isNotation
in interfaceorg.relaxng.datatype.ValidationContext
-
getBaseUri
public java.lang.String getBaseUri()
- Specified by:
getBaseUri
in interfaceorg.relaxng.datatype.ValidationContext
-
onID
public final void onID(org.relaxng.datatype.Datatype dt, StringToken token)
Description copied from interface:IDContextProvider2
this method is called when a type with ID semantics is matched. It is the callee's responsibility that stores ID and checks doubly defined ID, if it is necessary.- Specified by:
onID
in interfaceIDContextProvider2
-
addBackPatchJob
public final void addBackPatchJob(GrammarReader.BackPatch job)
-
addBackPatchJob
public final void addBackPatchJob(XSDatatypeExp job)
-
runBackPatchJob
public final void runBackPatchJob()
Performs all back-patchings.
-
runBackPatchJob
private final void runBackPatchJob(java.util.Vector vec)
-
reportError
public final void reportError(java.lang.String propertyName)
-
reportError
public final void reportError(java.lang.String propertyName, java.lang.Object arg1)
-
reportError
public final void reportError(java.lang.String propertyName, java.lang.Object arg1, java.lang.Object arg2)
-
reportError
public final void reportError(java.lang.String propertyName, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
-
reportError
public final void reportError(java.lang.Exception nestedException, java.lang.String propertyName)
-
reportError
public final void reportError(java.lang.Exception nestedException, java.lang.String propertyName, java.lang.Object arg1)
-
reportError
public final void reportError(org.xml.sax.Locator[] locs, java.lang.String propertyName, java.lang.Object[] args)
-
reportWarning
public final void reportWarning(java.lang.String propertyName)
-
reportWarning
public final void reportWarning(java.lang.String propertyName, java.lang.Object arg1)
-
reportWarning
public final void reportWarning(java.lang.String propertyName, java.lang.Object arg1, java.lang.Object arg2)
-
prepareLocation
private org.xml.sax.Locator[] prepareLocation(org.xml.sax.Locator[] param)
-
reportError
public final void reportError(java.lang.String propertyName, java.lang.Object[] args, java.lang.Exception nestedException, org.xml.sax.Locator[] errorLocations)
reports an error to the controller
-
reportWarning
public final void reportWarning(java.lang.String propertyName, java.lang.Object[] args, org.xml.sax.Locator[] locations)
reports a warning to the controller
-
inputSourceFromLSInput
public static javax.xml.transform.Source inputSourceFromLSInput(org.w3c.dom.ls.LSInput input)
This is a stopgap until we can wean more of this code from SAX-dependence.- Parameters:
input
-- Returns:
-
localizeMessage
protected abstract java.lang.String localizeMessage(java.lang.String propertyName, java.lang.Object[] args)
formats localized message with arguments
-
setLocator
public void setLocator(org.xml.sax.Locator locator)
-
getLocator
public org.xml.sax.Locator getLocator()
-
-