Package org.apache.openjpa.lib.meta
Class XMLMetaDataParser
- java.lang.Object
-
- org.xml.sax.helpers.DefaultHandler
-
- org.apache.openjpa.lib.meta.XMLMetaDataParser
-
- All Implemented Interfaces:
MetaDataParser
,ContentHandler
,DTDHandler
,EntityResolver
,ErrorHandler
,LexicalHandler
- Direct Known Subclasses:
CFMetaDataParser
,XMLVersionParser
public abstract class XMLMetaDataParser extends DefaultHandler implements LexicalHandler, MetaDataParser
Custom SAX parser used by the system to quickly parse metadata files. Subclasses should handle the processing of the content.- Author:
- Abe White
-
-
Field Summary
Fields Modifier and Type Field Description protected int
_extendedNamespace
protected int
_openjpaNamespace
-
Constructor Summary
Constructors Constructor Description XMLMetaDataParser()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
addComments(Object obj)
Add current comments to the given entity.protected void
addResult(Object result)
Add a result to be returned from the current parse.void
characters(char[] ch, int start, int length)
void
clear()
Clears the cache of parsed resource names.protected void
clearDeferredMetaData()
void
comment(char[] ch, int start, int length)
protected ClassLoader
currentClassLoader()
Return the class loader to use when resolving resources and loading classes.protected String[]
currentComments()
Array of comments for the current node, or empty array if none.protected int
currentDepth()
Return the parse depth.protected String
currentLocation()
Return the current location within the source file.protected String
currentText()
Return the text value within the current node.void
endCDATA()
void
endDTD()
protected abstract void
endElement(String name)
Override this method marking the end of some element.void
endElement(String uri, String name, String qName)
void
endEntity(String name)
void
error(SAXParseException se)
void
fatalError(SAXParseException se)
protected void
finish()
Override this method to finish up after a parse; this is only called if no errors are encountered during parsing.ClassLoader
getClassLoader()
Classloader to use for class name resolution.protected Reader
getDocType()
Override this method to return anyDOCTYPE
declaration that should be dynamically included in xml documents that will be validated.protected SAXException
getException(String msg)
Returns a SAXException with the source file name and the given error message.protected SAXException
getException(Localizer.Message msg)
Returns a SAXException with the source file name and the given error message.protected SAXException
getException(Localizer.Message msg, Throwable cause)
Returns a SAXException with the source file name and the given error message.LexicalHandler
getLexicalHandler()
The lexical handler that should be registered with the SAX parser used by this class.Location
getLocation()
The XML document location.Log
getLog()
The log to write to.boolean
getOverrideContextClassloader()
boolean
getParseComments()
Whether to parse element comments.boolean
getParseText()
Whether to parse element text.List
getResults()
Return the results from the last parse.protected Object
getSchemaSource()
Implement to return the XML schema source for the document.protected File
getSourceFile()
Return the file of the source being parsed.boolean
getSourceIsSystemId()
Whether to use the source name as the XML system id.protected String
getSourceName()
Return the name of the source file being parsed.String
getSuffix()
Expected suffix for metadata resources, or null if unknown.protected void
ignoreContent(boolean ignoreEnd)
Ignore all content below the current element.boolean
isCaching()
Whether parsed resource names are cached to avoid duplicate parsing.boolean
isParsing()
boolean
isValidating()
Whether this is a validating parser.void
parse(File file)
Parse the given file, which may be a directory, in which case it will be scanned recursively for metadata files.void
parse(Reader xml, String sourceName)
Parse the metadata in the given reader.void
parse(Class cls, boolean topDown)
Parse all possible metadata locations for the given class, going top-down or bottom-up.void
parse(String rsrc)
Parse the given resource.void
parse(URL url)
Parse the given resource.void
parse(MetaDataIterator itr)
Parse the metadata supplied by the given iterator.protected boolean
parsed(String src)
Return true if the given source is parsed.protected void
parseNewResource(Reader xml, String sourceName)
Parse a previously-unseen source.protected void
reset()
Override this method to clear any state and ready the parser for a new document.void
setCaching(boolean caching)
Whether parsed resource names are cached to avoid duplicate parsing.void
setClassLoader(ClassLoader loader)
Classloader to use for class name resolution.void
setDocumentLocator(Locator locator)
void
setLexicalHandler(LexicalHandler lh)
The lexical handler that should be registered with the SAX parser used by this class.void
setLocation(Location location)
The XML document location.void
setLog(Log log)
The log to write to.void
setOverrideContextClassloader(boolean overrideCCL)
void
setParseComments(boolean comments)
Whether to parse element comments.void
setParseText(boolean text)
Whether to parse element text.void
setParsing(boolean parsing)
void
setSourceIsSystemId(boolean systemId)
Whether to use the source name as the XML system id.void
setSuffix(String suffix)
Expected suffix for metadata resources, or null if unknown.void
setValidating(boolean validating)
Whether this is a validating parser.void
startCDATA()
void
startDTD(String name, String publicId, String systemId)
void
startElement(String uri, String name, String qName, Attributes attrs)
protected abstract boolean
startElement(String name, Attributes attrs)
Override this method marking the start of some element.void
startEntity(String name)
-
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
-
-
-
-
Method Detail
-
getOverrideContextClassloader
public boolean getOverrideContextClassloader()
-
setOverrideContextClassloader
public void setOverrideContextClassloader(boolean overrideCCL)
-
isParsing
public boolean isParsing()
-
setParsing
public void setParsing(boolean parsing)
-
getParseText
public boolean getParseText()
Whether to parse element text.
-
setParseText
public void setParseText(boolean text)
Whether to parse element text.
-
getParseComments
public boolean getParseComments()
Whether to parse element comments.
-
setParseComments
public void setParseComments(boolean comments)
Whether to parse element comments.
-
getLocation
public Location getLocation()
The XML document location.
-
getLexicalHandler
public LexicalHandler getLexicalHandler()
The lexical handler that should be registered with the SAX parser used by this class. Since theorg.xml.sax.ext
package is not a required part of SAX2, this handler might not be used by the parser.
-
setLexicalHandler
public void setLexicalHandler(LexicalHandler lh)
The lexical handler that should be registered with the SAX parser used by this class. Since theorg.xml.sax.ext
package is not a required part of SAX2, this handler might not be used by the parser.
-
setLocation
public void setLocation(Location location)
The XML document location.
-
getSourceIsSystemId
public boolean getSourceIsSystemId()
Whether to use the source name as the XML system id.
-
setSourceIsSystemId
public void setSourceIsSystemId(boolean systemId)
Whether to use the source name as the XML system id.
-
isValidating
public boolean isValidating()
Whether this is a validating parser.
-
setValidating
public void setValidating(boolean validating)
Whether this is a validating parser.
-
getSuffix
public String getSuffix()
Expected suffix for metadata resources, or null if unknown.
-
setSuffix
public void setSuffix(String suffix)
Expected suffix for metadata resources, or null if unknown.
-
isCaching
public boolean isCaching()
Whether parsed resource names are cached to avoid duplicate parsing.
-
setCaching
public void setCaching(boolean caching)
Whether parsed resource names are cached to avoid duplicate parsing.
-
getLog
public Log getLog()
The log to write to.
-
setLog
public void setLog(Log log)
The log to write to.
-
getClassLoader
public ClassLoader getClassLoader()
Classloader to use for class name resolution.
-
setClassLoader
public void setClassLoader(ClassLoader loader)
Classloader to use for class name resolution.- Specified by:
setClassLoader
in interfaceMetaDataParser
-
getResults
public List getResults()
Description copied from interface:MetaDataParser
Return the results from the last parse.- Specified by:
getResults
in interfaceMetaDataParser
-
parse
public void parse(String rsrc) throws IOException
Description copied from interface:MetaDataParser
Parse the given resource.- Specified by:
parse
in interfaceMetaDataParser
- Throws:
IOException
-
parse
public void parse(URL url) throws IOException
Description copied from interface:MetaDataParser
Parse the given resource.- Specified by:
parse
in interfaceMetaDataParser
- Throws:
IOException
-
parse
public void parse(File file) throws IOException
Description copied from interface:MetaDataParser
Parse the given file, which may be a directory, in which case it will be scanned recursively for metadata files.- Specified by:
parse
in interfaceMetaDataParser
- Throws:
IOException
-
parse
public void parse(Class cls, boolean topDown) throws IOException
Description copied from interface:MetaDataParser
Parse all possible metadata locations for the given class, going top-down or bottom-up. If the class is null, only top-level locations will be parsed.- Specified by:
parse
in interfaceMetaDataParser
- Throws:
IOException
-
parse
public void parse(Reader xml, String sourceName) throws IOException
Description copied from interface:MetaDataParser
Parse the metadata in the given reader.- Specified by:
parse
in interfaceMetaDataParser
- Parameters:
xml
- reader containing the metadata to parsesourceName
- the name of the source being parsed, for use in error messages- Throws:
IOException
-
parse
public void parse(MetaDataIterator itr) throws IOException
Description copied from interface:MetaDataParser
Parse the metadata supplied by the given iterator.- Specified by:
parse
in interfaceMetaDataParser
- Throws:
IOException
-
parseNewResource
protected void parseNewResource(Reader xml, String sourceName) throws IOException
Parse a previously-unseen source. All parsing methods delegate to this one.- Throws:
IOException
-
parsed
protected boolean parsed(String src)
Return true if the given source is parsed. Otherwise, record that it will be parsed.
-
clear
public void clear()
Description copied from interface:MetaDataParser
Clears the cache of parsed resource names.- Specified by:
clear
in interfaceMetaDataParser
-
error
public void error(SAXParseException se) throws SAXException
- Specified by:
error
in interfaceErrorHandler
- Overrides:
error
in classDefaultHandler
- Throws:
SAXException
-
fatalError
public void fatalError(SAXParseException se) throws SAXException
- Specified by:
fatalError
in interfaceErrorHandler
- Overrides:
fatalError
in classDefaultHandler
- Throws:
SAXException
-
setDocumentLocator
public void setDocumentLocator(Locator locator)
- Specified by:
setDocumentLocator
in interfaceContentHandler
- Overrides:
setDocumentLocator
in classDefaultHandler
-
startElement
public void startElement(String uri, String name, String qName, Attributes attrs) throws SAXException
- Specified by:
startElement
in interfaceContentHandler
- Overrides:
startElement
in classDefaultHandler
- Throws:
SAXException
-
endElement
public void endElement(String uri, String name, String qName) throws SAXException
- Specified by:
endElement
in interfaceContentHandler
- Overrides:
endElement
in classDefaultHandler
- Throws:
SAXException
-
characters
public void characters(char[] ch, int start, int length)
- Specified by:
characters
in interfaceContentHandler
- Overrides:
characters
in classDefaultHandler
-
comment
public void comment(char[] ch, int start, int length) throws SAXException
- Specified by:
comment
in interfaceLexicalHandler
- Throws:
SAXException
-
startCDATA
public void startCDATA() throws SAXException
- Specified by:
startCDATA
in interfaceLexicalHandler
- Throws:
SAXException
-
endCDATA
public void endCDATA() throws SAXException
- Specified by:
endCDATA
in interfaceLexicalHandler
- Throws:
SAXException
-
startDTD
public void startDTD(String name, String publicId, String systemId) throws SAXException
- Specified by:
startDTD
in interfaceLexicalHandler
- Throws:
SAXException
-
endDTD
public void endDTD() throws SAXException
- Specified by:
endDTD
in interfaceLexicalHandler
- Throws:
SAXException
-
startEntity
public void startEntity(String name) throws SAXException
- Specified by:
startEntity
in interfaceLexicalHandler
- Throws:
SAXException
-
endEntity
public void endEntity(String name) throws SAXException
- Specified by:
endEntity
in interfaceLexicalHandler
- Throws:
SAXException
-
startElement
protected abstract boolean startElement(String name, Attributes attrs) throws SAXException
Override this method marking the start of some element. If this method returns false, the content of the element and the end element event will be ignored.- Throws:
SAXException
-
endElement
protected abstract void endElement(String name) throws SAXException
Override this method marking the end of some element.- Throws:
SAXException
-
addResult
protected void addResult(Object result)
Add a result to be returned from the current parse.
-
finish
protected void finish()
Override this method to finish up after a parse; this is only called if no errors are encountered during parsing. Subclasses should callsuper.finish()
to resolve superclass state.
-
reset
protected void reset()
Override this method to clear any state and ready the parser for a new document. Subclasses should callsuper.reset()
to clear superclass state.
-
getSchemaSource
protected Object getSchemaSource() throws IOException
Implement to return the XML schema source for the document. Returns null by default. May return:String
pointing to schema URI.InputStream
containing schema contents.InputSource
containing schema contents.File
containing schema contents.- Array of any of the above elements.
- Throws:
IOException
-
getDocType
protected Reader getDocType() throws IOException
Override this method to return anyDOCTYPE
declaration that should be dynamically included in xml documents that will be validated. Returns null by default.- Throws:
IOException
-
getSourceName
protected String getSourceName()
Return the name of the source file being parsed.
-
getSourceFile
protected File getSourceFile()
Return the file of the source being parsed.
-
addComments
protected void addComments(Object obj)
Add current comments to the given entity. By default, assumes entity isCommentable
.
-
currentComments
protected String[] currentComments()
Array of comments for the current node, or empty array if none.
-
currentText
protected String currentText()
Return the text value within the current node.
-
currentLocation
protected String currentLocation()
Return the current location within the source file.
-
currentDepth
protected int currentDepth()
Return the parse depth. Within the root element, the depth is 0, within the first nested element, it is 1, and so forth.
-
currentClassLoader
protected ClassLoader currentClassLoader()
Return the class loader to use when resolving resources and loading classes.
-
ignoreContent
protected void ignoreContent(boolean ignoreEnd)
Ignore all content below the current element.- Parameters:
ignoreEnd
- whether to ignore the end element event
-
getException
protected SAXException getException(String msg)
Returns a SAXException with the source file name and the given error message.
-
getException
protected SAXException getException(Localizer.Message msg)
Returns a SAXException with the source file name and the given error message.
-
getException
protected SAXException getException(Localizer.Message msg, Throwable cause)
Returns a SAXException with the source file name and the given error message.
-
clearDeferredMetaData
protected void clearDeferredMetaData()
-
-