Custom SAX parser used by the system to quickly parse metadata files.
Subclasses should handle the processing of the content.
| Method from org.apache.openjpa.lib.meta.XMLMetaDataParser Detail: |
protected void addComments(Object obj) {
String[] comments = currentComments();
if (comments.length > 0 && obj instanceof Commentable)
((Commentable) obj).setComments(comments);
}
Add current comments to the given entity. By default, assumes entity
is Commentable . |
protected void addResult(Object result) {
if (_log != null && _log.isTraceEnabled())
_log.trace(_loc.get("add-result", result));
_curResults.add(result);
}
Add a result to be returned from the current parse. |
public void characters(char[] ch,
int start,
int length) {
if (_parseText && _depth < = _ignore) {
if (_text == null)
_text = new StringBuffer();
_text.append(ch, start, length);
}
}
|
public void clear() {
if (_log != null && _log.isTraceEnabled())
_log.trace(_loc.get("clear-parser", this));
if (_parsed != null)
_parsed.clear();
}
|
public void comment(char[] ch,
int start,
int length) throws SAXException {
if (_parseComments && _depth < = _ignore) {
if (_comments == null)
_comments = new ArrayList(3);
_comments.add(String.valueOf(ch, start, length));
}
if (_lh != null)
_lh.comment(ch, start, length);
}
|
protected ClassLoader currentClassLoader() {
if (_loader != null)
return _loader;
if (_curLoader == null)
_curLoader = (ClassLoader) AccessController.doPrivileged(
J2DoPrivHelper.getContextClassLoaderAction());
return _curLoader;
}
Return the class loader to use when resolving resources and loading
classes. |
protected String[] currentComments() {
if (_comments == null || _comments.isEmpty())
return Commentable.EMPTY_COMMENTS;
return (String[]) _comments.toArray(new String[_comments.size()]);
}
Array of comments for the current node, or empty array if none. |
protected int currentDepth() {
return _depth;
}
Return the parse depth. Within the root element, the depth is 0,
within the first nested element, it is 1, and so forth. |
protected String currentLocation() {
return " [" + _loc.get("loc-prefix") + _location.getLocation() + "]";
}
Return the current location within the source file. |
protected String currentText() {
if (_text == null)
return "";
return _text.toString().trim();
}
Return the text value within the current node. |
public void endCDATA() throws SAXException {
if (_lh != null)
_lh.endCDATA();
}
|
public void endDTD() throws SAXException {
if (_lh != null)
_lh.endDTD();
}
|
abstract protected void endElement(String name) throws SAXException
Override this method marking the end of some element. |
public void endElement(String uri,
String name,
String qName) throws SAXException {
if (_depth < _ignore)
endElement(qName);
_text = null;
if (_comments != null)
_comments.clear();
if (_depth == _ignore)
_ignore = Integer.MAX_VALUE;
_depth--;
}
|
public void endEntity(String name) throws SAXException {
if (_lh != null)
_lh.endEntity(name);
}
|
public void error(SAXParseException se) throws SAXException {
throw getException(se.toString());
}
|
public void fatalError(SAXParseException se) throws SAXException {
throw getException(se.toString());
}
|
protected void finish() {
if (_log != null && _log.isTraceEnabled())
_log.trace(_loc.get("end-parse", getSourceName()));
_results = new ArrayList(_curResults);
}
Override this method to finish up after a parse; this is only
called if no errors are encountered during parsing. Subclasses should
call super.finish() to resolve superclass state. |
public ClassLoader getClassLoader() {
return _loader;
}
Classloader to use for class name resolution. |
protected Reader getDocType() throws IOException {
return null;
}
Override this method to return any DOCTYPE declaration
that should be dynamically included in xml documents that will be
validated. Returns null by default. |
protected SAXException getException(String msg) {
return new SAXException(getSourceName() + currentLocation() +
": " + msg);
}
Returns a SAXException with the source file name and the given error
message. |
protected SAXException getException(Message msg) {
return new SAXException(getSourceName() + currentLocation() +
": " + msg.getMessage());
}
Returns a SAXException with the source file name and the given error
message. |
protected SAXException getException(Message msg,
Throwable cause) {
if (cause != null && _log != null && _log.isTraceEnabled())
_log.trace(_loc.get("sax-exception",
getSourceName(), _location.getLocation()), cause);
SAXException e = new SAXException(getSourceName() + currentLocation() +
": " + msg + " [" + cause + "]");
e.initCause(cause);
return e;
}
Returns a SAXException with the source file name and the given error
message. |
public LexicalHandler getLexicalHandler() {
return _lh;
}
The lexical handler that should be registered with the SAX parser used
by this class. Since the org.xml.sax.ext package is not
a required part of SAX2, this handler might not be used by the parser. |
public Location getLocation() {
return _location;
}
The XML document location. |
public Log getLog() {
return _log;
}
|
public boolean getParseComments() {
return _parseComments;
}
Whether to parse element comments. |
public boolean getParseText() {
return _parseText;
}
Whether to parse element text. |
public List getResults() {
if (_results == null)
return Collections.EMPTY_LIST;
return _results;
}
|
protected Object getSchemaSource() throws IOException {
return null;
}
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.
|
protected File getSourceFile() {
return _sourceFile;
}
Return the file of the source being parsed. |
public boolean getSourceIsSystemId() {
return _systemId;
}
Whether to use the source name as the XML system id. |
protected String getSourceName() {
return _sourceName;
}
Return the name of the source file being parsed. |
public String getSuffix() {
return _suffix;
}
Expected suffix for metadata resources, or null if unknown. |
protected void ignoreContent(boolean ignoreEnd) {
_ignore = _depth;
if (!ignoreEnd)
_ignore++;
}
Ignore all content below the current element. |
public boolean isCaching() {
return _caching;
}
Whether parsed resource names are cached to avoid duplicate parsing. |
public boolean isValidating() {
return _validating;
}
Whether this is a validating parser. |
public void parse(String rsrc) throws IOException {
if (rsrc != null)
parse(new ResourceMetaDataIterator(rsrc, _loader));
}
|
public void parse(URL url) throws IOException {
if (url != null)
parse(new URLMetaDataIterator(url));
}
|
public void parse(File file) throws IOException {
if (file == null)
return;
if (!((Boolean) AccessController.doPrivileged(J2DoPrivHelper
.isDirectoryAction(file))).booleanValue())
parse(new FileMetaDataIterator(file));
else {
String suff = (_suffix == null) ? "" : _suffix;
parse(new FileMetaDataIterator(file,
new SuffixMetaDataFilter(suff)));
}
}
|
public void parse(MetaDataIterator itr) throws IOException {
parse(itr, false);
}
|
public void parse(Class cls,
boolean topDown) throws IOException {
String suff = (_suffix == null) ? "" : _suffix;
parse(new ClassMetaDataIterator(cls, suff, topDown), !topDown);
}
|
public void parse(Reader xml,
String sourceName) throws IOException {
if (xml != null && (sourceName == null || !parsed(sourceName)))
parseNewResource(xml, sourceName);
}
|
protected void parseNewResource(Reader xml,
String sourceName) throws IOException {
if (_log != null && _log.isTraceEnabled())
_log.trace(_loc.get("start-parse", sourceName));
// even if we want to validate, specify that it won't happen
// if we have neither a DocType not a Schema
Object schemaSource = getSchemaSource();
if (schemaSource != null && _schemaBug) {
if (_log != null && _log.isTraceEnabled())
_log.trace(_loc.get("parser-schema-bug"));
schemaSource = null;
}
boolean validating = _validating && (getDocType() != null
|| schemaSource != null);
// parse the metadata with a SAX parser
try {
_sourceName = sourceName;
SAXParser parser = XMLFactory.getSAXParser(validating, true);
Object schema = null;
if (validating) {
schema = schemaSource;
if (schema == null && getDocType() != null)
xml = new DocTypeReader(xml, getDocType());
}
if (_parseComments || _lh != null)
parser.setProperty
("http://xml.org/sax/properties/lexical-handler", this);
if (schema != null) {
parser.setProperty
("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
parser.setProperty
("http://java.sun.com/xml/jaxp/properties/schemaSource",
schema);
}
InputSource is = new InputSource(xml);
if (_systemId && sourceName != null)
is.setSystemId(sourceName);
parser.parse(is, this);
finish();
} catch (SAXException se) {
IOException ioe = new IOException(se.toString());
JavaVersions.initCause(ioe, se);
throw ioe;
} finally {
reset();
}
}
Parse a previously-unseen source. All parsing methods delegate
to this one. |
protected boolean parsed(String src) {
if (!_caching)
return false;
if (_parsed == null)
_parsed = new HashMap();
ClassLoader loader = currentClassLoader();
Set set = (Set) _parsed.get(loader);
if (set == null) {
set = new HashSet();
_parsed.put(loader, set);
}
boolean added = set.add(src);
if (!added && _log != null && _log.isTraceEnabled())
_log.trace(_loc.get("already-parsed", src));
return !added;
}
Return true if the given source is parsed. Otherwise, record that
it will be parsed. |
protected void reset() {
_curResults.clear();
_curLoader = null;
_sourceName = null;
_sourceFile = null;
_depth = -1;
_ignore = Integer.MAX_VALUE;
if (_comments != null)
_comments.clear();
}
Override this method to clear any state and ready the parser for
a new document. Subclasses should call
super.reset() to clear superclass state. |
public void setCaching(boolean caching) {
_caching = caching;
if (!caching)
clear();
}
Whether parsed resource names are cached to avoid duplicate parsing. |
public void setClassLoader(ClassLoader loader) {
_loader = loader;
}
Classloader to use for class name resolution. |
public void setDocumentLocator(Locator locator) {
_location.setLocator(locator);
}
|
public void setLexicalHandler(LexicalHandler lh) {
_lh = lh;
}
The lexical handler that should be registered with the SAX parser used
by this class. Since the org.xml.sax.ext package is not
a required part of SAX2, this handler might not be used by the parser. |
public void setLocation(Location location) {
_location = location;
}
The XML document location. |
public void setLog(Log log) {
_log = log;
}
|
public void setParseComments(boolean comments) {
_parseComments = comments;
}
Whether to parse element comments. |
public void setParseText(boolean text) {
_parseText = text;
}
Whether to parse element text. |
public void setSourceIsSystemId(boolean systemId) {
_systemId = systemId;
}
Whether to use the source name as the XML system id. |
public void setSuffix(String suffix) {
_suffix = suffix;
}
Expected suffix for metadata resources, or null if unknown. |
public void setValidating(boolean validating) {
_validating = validating;
}
Whether this is a validating parser. |
public void startCDATA() throws SAXException {
if (_lh != null)
_lh.startCDATA();
}
|
public void startDTD(String name,
String publicId,
String systemId) throws SAXException {
if (_lh != null)
_lh.startDTD(name, publicId, systemId);
}
|
abstract protected 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. |
public void startElement(String uri,
String name,
String qName,
Attributes attrs) throws SAXException {
_depth++;
if (_depth < = _ignore)
if (!startElement(qName, attrs))
ignoreContent(true);
}
|
public void startEntity(String name) throws SAXException {
if (_lh != null)
_lh.startEntity(name);
}
|