com.primix.tapestry.util.xml
Class AbstractDocumentParser

java.lang.Object
  |
  +--com.primix.tapestry.util.xml.AbstractDocumentParser
All Implemented Interfaces:
EntityResolver, ErrorHandler
Direct Known Subclasses:
ScriptParser, SlashdotParser, SpecificationParser

public abstract class AbstractDocumentParser
extends java.lang.Object
implements ErrorHandler, EntityResolver

A wrapper around DocumentBuilder (itself a wrapper around some XML parser), this class provides error handling and entity resolving.

Since:
0.2.10
Version:
$Id: AbstractDocumentParser.java,v 1.6 2001/05/30 16:13:20 hship Exp $
Author:
Howard Ship

Constructor Summary
AbstractDocumentParser()
           
 
Method Summary
protected  DocumentBuilder constructBuilder()
          Constructs a new DocumentBuilder to be used for parsing.
 void error(SAXParseException exception)
          Throws the exception, which is caught and wrapped in a DocumentParseException by parse(InputSource,String,String).
 void fatalError(SAXParseException exception)
          Throws the exception, which is caught and wrapped in a DocumentParseException by parse(InputSource,String,String).
protected  java.lang.String getAttribute(Node node, java.lang.String attributeName)
          Returns the value of the named attribute of the node.
protected  java.lang.String getId(Node node)
          Returns the value of an Element node (via getValue(Node)), but then validates that the result is a good identifier (starts with a letter, contains letters, numbers, dashes, underscore).
protected  java.lang.String getNodePath(Node node)
          Returns a 'path' to the given node, which is a list of enclosing element names seperated by periods.
protected  boolean getRequireValidatingParser()
          Used by constructBuilder() to determine if the a validating DocumentBuilder is required.
 java.lang.String getResourcePath()
           
protected  java.lang.String getValue(Node node)
          Returns the value of an Element node.
protected  boolean isElement(Node node, java.lang.String elementName)
          Returns true if the node is an element with the specified name.
protected  Document parse(InputSource source, java.lang.String resourcePath, java.lang.String rootElementName)
          Invoked by subclasses to parse a document.
protected  void register(java.lang.String publicId, java.lang.String entityPath)
          Invoked by subclasses (usually inside thier constructor) to register a public id and corresponding input source.
 InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
          Checks for a previously registered public ID and returns the corresponding input source.
 void setResourcePath(java.lang.String value)
           
 void warning(SAXParseException exception)
          Throws the exception, which is caught and wrapped in a DocumentParseException by parse(InputSource,String,String).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractDocumentParser

public AbstractDocumentParser()
Method Detail

register

protected void register(java.lang.String publicId,
                        java.lang.String entityPath)
Invoked by subclasses (usually inside thier constructor) to register a public id and corresponding input source. Generally, the source is a wrapper around an input stream to a package resource.
Parameters:
publicId - the public identifier to be registerred, generally the publicId of a DTD related to the document being parsed
entityPath - the resource path of the entity, typically a DTD file. Relative files names are expected to be stored in the same package as the class file, otherwise a leading slash is an absolute pathname within the classpath.

getResourcePath

public java.lang.String getResourcePath()

setResourcePath

public void setResourcePath(java.lang.String value)

parse

protected Document parse(InputSource source,
                         java.lang.String resourcePath,
                         java.lang.String rootElementName)
                  throws DocumentParseException
Invoked by subclasses to parse a document. Obtains (or re-uses) a DocumentBuilder and parses the document from the InputSource.
Parameters:
source - source from which to read the document
resourcePath - a description of the source, used in errors
rootElementName - the expected root element of the Document
Throws:
DocumentParseException - wrapped around SAXParseException or IOException, or if the root element is wrong.

warning

public void warning(SAXParseException exception)
             throws SAXException
Throws the exception, which is caught and wrapped in a DocumentParseException by parse(InputSource,String,String).
Specified by:
warning in interface ErrorHandler

error

public void error(SAXParseException exception)
           throws SAXException
Throws the exception, which is caught and wrapped in a DocumentParseException by parse(InputSource,String,String).
Specified by:
error in interface ErrorHandler

fatalError

public void fatalError(SAXParseException exception)
                throws SAXException
Throws the exception, which is caught and wrapped in a DocumentParseException by parse(InputSource,String,String).
Specified by:
fatalError in interface ErrorHandler

resolveEntity

public InputSource resolveEntity(java.lang.String publicId,
                                 java.lang.String systemId)
                          throws SAXException,
                                 java.io.IOException
Checks for a previously registered public ID and returns the corresponding input source.
Specified by:
resolveEntity in interface EntityResolver

isElement

protected boolean isElement(Node node,
                            java.lang.String elementName)
                     throws DocumentParseException
Returns true if the node is an element with the specified name.

getValue

protected java.lang.String getValue(Node node)
Returns the value of an Element node. That is, all the Text nodes appended together. Invokes trim() to remove leading and trailing spaces.

getId

protected java.lang.String getId(Node node)
                          throws DocumentParseException
Returns the value of an Element node (via getValue(Node)), but then validates that the result is a good identifier (starts with a letter, contains letters, numbers, dashes, underscore).

getNodePath

protected java.lang.String getNodePath(Node node)
Returns a 'path' to the given node, which is a list of enclosing element names seperated by periods. The root element name is first, and the node's element is last. This is used when reporting some parse errors.

constructBuilder

protected DocumentBuilder constructBuilder()
                                    throws ParserConfigurationException
Constructs a new DocumentBuilder to be used for parsing. The builder is used and reused, at least until there is an error parsing a document (at which point, it is discarded).

This implementation obtains a builder with the following characteristics:

These characteristics are appropriate to parsing things such as Tapestry specifications; subclasses with unusual demands may need to override this method.

The builder uses this AbstractDocumentParser as the entity resolver and error handler.


getRequireValidatingParser

protected boolean getRequireValidatingParser()
Used by constructBuilder() to determine if the a validating DocumentBuilder is required. This implementation returns true, subclasses that don't require a validating builder (such as documents without a known DTD), may override to return false.
Since:
1.0.1

getAttribute

protected java.lang.String getAttribute(Node node,
                                        java.lang.String attributeName)
Returns the value of the named attribute of the node. Returns null if the node doesn't contain an attribute with the given name.
Since:
1.0.1