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,PersistenceProductDerivation.ConfigurationParser,XMLSchemaParser,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_extendedNamespaceprotected int_openjpaNamespace
-
Constructor Summary
Constructors Constructor Description XMLMetaDataParser()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddComments(Object obj)Add current comments to the given entity.protected voidaddResult(Object result)Add a result to be returned from the current parse.voidcharacters(char[] ch, int start, int length)voidclear()Clears the cache of parsed resource names.protected voidclearDeferredMetaData()voidcomment(char[] ch, int start, int length)protected ClassLoadercurrentClassLoader()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 intcurrentDepth()Return the parse depth.protected StringcurrentLocation()Return the current location within the source file.protected StringcurrentText()Return the text value within the current node.voidendCDATA()voidendDTD()protected abstract voidendElement(String name)Override this method marking the end of some element.voidendElement(String uri, String name, String qName)voidendEntity(String name)voiderror(SAXParseException se)voidfatalError(SAXParseException se)protected voidfinish()Override this method to finish up after a parse; this is only called if no errors are encountered during parsing.ClassLoadergetClassLoader()Classloader to use for class name resolution.protected ReadergetDocType()Override this method to return anyDOCTYPEdeclaration that should be dynamically included in xml documents that will be validated.protected SAXExceptiongetException(String msg)Returns a SAXException with the source file name and the given error message.protected SAXExceptiongetException(Localizer.Message msg)Returns a SAXException with the source file name and the given error message.protected SAXExceptiongetException(Localizer.Message msg, Throwable cause)Returns a SAXException with the source file name and the given error message.LexicalHandlergetLexicalHandler()The lexical handler that should be registered with the SAX parser used by this class.LocationgetLocation()The XML document location.LoggetLog()The log to write to.booleangetOverrideContextClassloader()booleangetParseComments()Whether to parse element comments.booleangetParseText()Whether to parse element text.ListgetResults()Return the results from the last parse.protected ObjectgetSchemaSource()Implement to return the XML schema source for the document.protected FilegetSourceFile()Return the file of the source being parsed.booleangetSourceIsSystemId()Whether to use the source name as the XML system id.protected StringgetSourceName()Return the name of the source file being parsed.StringgetSuffix()Expected suffix for metadata resources, or null if unknown.protected voidignoreContent(boolean ignoreEnd)Ignore all content below the current element.booleanisCaching()Whether parsed resource names are cached to avoid duplicate parsing.booleanisParsing()booleanisValidating()Whether this is a validating parser.voidparse(File file)Parse the given file, which may be a directory, in which case it will be scanned recursively for metadata files.voidparse(Reader xml, String sourceName)Parse the metadata in the given reader.voidparse(Class cls, boolean topDown)Parse all possible metadata locations for the given class, going top-down or bottom-up.voidparse(String rsrc)Parse the given resource.voidparse(URL url)Parse the given resource.voidparse(MetaDataIterator itr)Parse the metadata supplied by the given iterator.protected booleanparsed(String src)Return true if the given source is parsed.protected voidparseNewResource(Reader xml, String sourceName)Parse a previously-unseen source.protected voidreset()Override this method to clear any state and ready the parser for a new document.voidsetCaching(boolean caching)Whether parsed resource names are cached to avoid duplicate parsing.voidsetClassLoader(ClassLoader loader)Classloader to use for class name resolution.voidsetDocumentLocator(Locator locator)voidsetLexicalHandler(LexicalHandler lh)The lexical handler that should be registered with the SAX parser used by this class.voidsetLocation(Location location)The XML document location.voidsetLog(Log log)The log to write to.voidsetOverrideContextClassloader(boolean overrideCCL)voidsetParseComments(boolean comments)Whether to parse element comments.voidsetParseText(boolean text)Whether to parse element text.voidsetParsing(boolean parsing)voidsetSourceIsSystemId(boolean systemId)Whether to use the source name as the XML system id.voidsetSuffix(String suffix)Expected suffix for metadata resources, or null if unknown.voidsetValidating(boolean validating)Whether this is a validating parser.voidstartCDATA()voidstartDTD(String name, String publicId, String systemId)voidstartElement(String uri, String name, String qName, Attributes attrs)protected abstract booleanstartElement(String name, Attributes attrs)Override this method marking the start of some element.voidstartEntity(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.extpackage 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.extpackage 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:
setClassLoaderin interfaceMetaDataParser
-
getResults
public List getResults()
Description copied from interface:MetaDataParserReturn the results from the last parse.- Specified by:
getResultsin interfaceMetaDataParser
-
parse
public void parse(String rsrc) throws IOException
Description copied from interface:MetaDataParserParse the given resource.- Specified by:
parsein interfaceMetaDataParser- Throws:
IOException
-
parse
public void parse(URL url) throws IOException
Description copied from interface:MetaDataParserParse the given resource.- Specified by:
parsein interfaceMetaDataParser- Throws:
IOException
-
parse
public void parse(File file) throws IOException
Description copied from interface:MetaDataParserParse the given file, which may be a directory, in which case it will be scanned recursively for metadata files.- Specified by:
parsein interfaceMetaDataParser- Throws:
IOException
-
parse
public void parse(Class cls, boolean topDown) throws IOException
Description copied from interface:MetaDataParserParse 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:
parsein interfaceMetaDataParser- Throws:
IOException
-
parse
public void parse(Reader xml, String sourceName) throws IOException
Description copied from interface:MetaDataParserParse the metadata in the given reader.- Specified by:
parsein 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:MetaDataParserParse the metadata supplied by the given iterator.- Specified by:
parsein 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:MetaDataParserClears the cache of parsed resource names.- Specified by:
clearin interfaceMetaDataParser
-
error
public void error(SAXParseException se) throws SAXException
- Specified by:
errorin interfaceErrorHandler- Overrides:
errorin classDefaultHandler- Throws:
SAXException
-
fatalError
public void fatalError(SAXParseException se) throws SAXException
- Specified by:
fatalErrorin interfaceErrorHandler- Overrides:
fatalErrorin classDefaultHandler- Throws:
SAXException
-
setDocumentLocator
public void setDocumentLocator(Locator locator)
- Specified by:
setDocumentLocatorin interfaceContentHandler- Overrides:
setDocumentLocatorin classDefaultHandler
-
startElement
public void startElement(String uri, String name, String qName, Attributes attrs) throws SAXException
- Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultHandler- Throws:
SAXException
-
endElement
public void endElement(String uri, String name, String qName) throws SAXException
- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultHandler- Throws:
SAXException
-
characters
public void characters(char[] ch, int start, int length)- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultHandler
-
comment
public void comment(char[] ch, int start, int length) throws SAXException- Specified by:
commentin interfaceLexicalHandler- Throws:
SAXException
-
startCDATA
public void startCDATA() throws SAXException- Specified by:
startCDATAin interfaceLexicalHandler- Throws:
SAXException
-
endCDATA
public void endCDATA() throws SAXException- Specified by:
endCDATAin interfaceLexicalHandler- Throws:
SAXException
-
startDTD
public void startDTD(String name, String publicId, String systemId) throws SAXException
- Specified by:
startDTDin interfaceLexicalHandler- Throws:
SAXException
-
endDTD
public void endDTD() throws SAXException- Specified by:
endDTDin interfaceLexicalHandler- Throws:
SAXException
-
startEntity
public void startEntity(String name) throws SAXException
- Specified by:
startEntityin interfaceLexicalHandler- Throws:
SAXException
-
endEntity
public void endEntity(String name) throws SAXException
- Specified by:
endEntityin 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:Stringpointing to schema URI.InputStreamcontaining schema contents.InputSourcecontaining schema contents.Filecontaining schema contents.- Array of any of the above elements.
- Throws:
IOException
-
getDocType
protected Reader getDocType() throws IOException
Override this method to return anyDOCTYPEdeclaration 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()
-
-