org.apache.tapestry.util.xml
Class RuleDirectedParser

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by org.apache.tapestry.util.xml.RuleDirectedParser
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class RuleDirectedParser
extends org.xml.sax.helpers.DefaultHandler

A simplified version of Digester. This version is without as many bells and whistles but has some key features needed when parsing a document (rather than a configuration file):

  • Tracking of exact location within the document.
  • Like Digester, there's an object stack and a rule stack. The rules are much simpler (more coding), in that there's a one-to-one relationship between an element and a rule.

    Based on SAX2.

    Since:
    3.0
    Version:
    $Id: RuleDirectedParser.java 243791 2004-02-19 17:38:13Z hlship $
    Author:
    Howard Lewis Ship

    Constructor Summary
    RuleDirectedParser()
               
     
    Method Summary
     void addRule(String localElementName, IRule rule)
               
     void characters(char[] ch, int start, int length)
              Accumulates the content in a buffer; the concatinated content is provided to the top rule just before any start or end tag.
    protected  void configureParserFactory(javax.xml.parsers.SAXParserFactory factory)
              Configures a SAXParserFactory before SAXParserFactory.newSAXParser() is invoked.
    protected  javax.xml.parsers.SAXParser constructParser()
              Uses SAXParserFactory to create a instance of a validation SAX2 parser.
     void endElement(String uri, String localName, String qName)
              Pops the top rule off the stack and invokes IRule.endElement(RuleDirectedParser).
     void error(org.xml.sax.SAXParseException ex)
              Throws the exception.
     void fatalError(org.xml.sax.SAXParseException ex)
              Throws the exception.
     IResourceLocation getDocumentLocation()
               
     String getLocalName()
              Returns the localName for the current element.
     ILocation getLocation()
              Returns an ILocation representing the current position within the document (depending on the parser, this may be accurate to column number level).
     String getQName()
              Returns the qualified name for the current element.
     String getUri()
              Returns the URI for the current element.
     void ignorableWhitespace(char[] ch, int start, int length)
              Ignorable content is ignored.
     Object parse(IResourceLocation documentLocation)
               
    protected  Object parse(URL url)
               
     Object peek()
              Returns the top object on the object stack.
     Object peek(int depth)
              Returns an object within the object stack, at depth.
    protected  IRule peekRule()
              Returns the top rule on the stack.
     Object pop()
              Removes and returns the top object on the object stack.
    protected  IRule popRule()
               
     void push(Object object)
              Pushes an object onto the object stack.
    protected  void pushRule(IRule rule)
              Pushes a new rule onto the rule stack.
     void registerEntity(String publicId, String entityPath)
              Registers a public id and corresponding input source.
     org.xml.sax.InputSource resolveEntity(String publicId, String systemId)
               
    protected  IRule selectRule(String localName, org.xml.sax.Attributes attributes)
               
     void setDocumentLocator(org.xml.sax.Locator locator)
              Uses the Locator to track the position in the document as a ILocation.
     void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes)
              Invokes selectRule(String, Attributes) to choose a new rule, which is pushed onto the rule stack, then invokes IRule.startElement(RuleDirectedParser, Attributes).
     void validate(String value, String pattern, String errorKey)
              Validates that the input value matches against the specified Perl5 pattern.
     void warning(org.xml.sax.SAXParseException ex)
              Throws the exception.
     
    Methods inherited from class org.xml.sax.helpers.DefaultHandler
    endDocument, endPrefixMapping, notationDecl, processingInstruction, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Constructor Detail

    RuleDirectedParser

    public RuleDirectedParser()
    Method Detail

    parse

    public Object parse(IResourceLocation documentLocation)

    parse

    protected Object parse(URL url)

    getLocation

    public ILocation getLocation()
    Returns an ILocation representing the current position within the document (depending on the parser, this may be accurate to column number level).


    push

    public void push(Object object)
    Pushes an object onto the object stack. The first object pushed is the "document object", the root object returned by the parse.


    peek

    public Object peek()
    Returns the top object on the object stack.


    peek

    public Object peek(int depth)
    Returns an object within the object stack, at depth. Depth 0 is the top object, depth 1 is the next-to-top object, etc.


    pop

    public Object pop()
    Removes and returns the top object on the object stack.


    pushRule

    protected void pushRule(IRule rule)
    Pushes a new rule onto the rule stack.


    peekRule

    protected IRule peekRule()
    Returns the top rule on the stack.


    popRule

    protected IRule popRule()

    addRule

    public void addRule(String localElementName,
                        IRule rule)

    registerEntity

    public void registerEntity(String publicId,
                               String entityPath)
    Registers 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.

    selectRule

    protected IRule selectRule(String localName,
                               org.xml.sax.Attributes attributes)

    setDocumentLocator

    public void setDocumentLocator(org.xml.sax.Locator locator)
    Uses the Locator to track the position in the document as a ILocation. This is invoked once (before the initial element is parsed) and the Locator is retained and queried as to the current file location.

    Specified by:
    setDocumentLocator in interface org.xml.sax.ContentHandler
    Overrides:
    setDocumentLocator in class org.xml.sax.helpers.DefaultHandler
    See Also:
    getLocation()

    characters

    public void characters(char[] ch,
                           int start,
                           int length)
                    throws org.xml.sax.SAXException
    Accumulates the content in a buffer; the concatinated content is provided to the top rule just before any start or end tag.

    Specified by:
    characters in interface org.xml.sax.ContentHandler
    Overrides:
    characters in class org.xml.sax.helpers.DefaultHandler
    Throws:
    org.xml.sax.SAXException

    endElement

    public void endElement(String uri,
                           String localName,
                           String qName)
                    throws org.xml.sax.SAXException
    Pops the top rule off the stack and invokes IRule.endElement(RuleDirectedParser).

    Specified by:
    endElement in interface org.xml.sax.ContentHandler
    Overrides:
    endElement in class org.xml.sax.helpers.DefaultHandler
    Throws:
    org.xml.sax.SAXException

    ignorableWhitespace

    public void ignorableWhitespace(char[] ch,
                                    int start,
                                    int length)
                             throws org.xml.sax.SAXException
    Ignorable content is ignored.

    Specified by:
    ignorableWhitespace in interface org.xml.sax.ContentHandler
    Overrides:
    ignorableWhitespace in class org.xml.sax.helpers.DefaultHandler
    Throws:
    org.xml.sax.SAXException

    startElement

    public void startElement(String uri,
                             String localName,
                             String qName,
                             org.xml.sax.Attributes attributes)
                      throws org.xml.sax.SAXException
    Invokes selectRule(String, Attributes) to choose a new rule, which is pushed onto the rule stack, then invokes IRule.startElement(RuleDirectedParser, Attributes).

    Specified by:
    startElement in interface org.xml.sax.ContentHandler
    Overrides:
    startElement in class org.xml.sax.helpers.DefaultHandler
    Throws:
    org.xml.sax.SAXException

    constructParser

    protected javax.xml.parsers.SAXParser constructParser()
    Uses SAXParserFactory to create a instance of a validation SAX2 parser.


    configureParserFactory

    protected void configureParserFactory(javax.xml.parsers.SAXParserFactory factory)
    Configures a SAXParserFactory before SAXParserFactory.newSAXParser() is invoked. The default implementation sets validating to true and namespaceAware to false,


    error

    public void error(org.xml.sax.SAXParseException ex)
               throws org.xml.sax.SAXException
    Throws the exception.

    Specified by:
    error in interface org.xml.sax.ErrorHandler
    Overrides:
    error in class org.xml.sax.helpers.DefaultHandler
    Throws:
    org.xml.sax.SAXException

    fatalError

    public void fatalError(org.xml.sax.SAXParseException ex)
                    throws org.xml.sax.SAXException
    Throws the exception.

    Specified by:
    fatalError in interface org.xml.sax.ErrorHandler
    Overrides:
    fatalError in class org.xml.sax.helpers.DefaultHandler
    Throws:
    org.xml.sax.SAXException

    warning

    public void warning(org.xml.sax.SAXParseException ex)
                 throws org.xml.sax.SAXException
    Throws the exception.

    Specified by:
    warning in interface org.xml.sax.ErrorHandler
    Overrides:
    warning in class org.xml.sax.helpers.DefaultHandler
    Throws:
    org.xml.sax.SAXException

    resolveEntity

    public org.xml.sax.InputSource resolveEntity(String publicId,
                                                 String systemId)
                                          throws org.xml.sax.SAXException
    Specified by:
    resolveEntity in interface org.xml.sax.EntityResolver
    Overrides:
    resolveEntity in class org.xml.sax.helpers.DefaultHandler
    Throws:
    org.xml.sax.SAXException

    validate

    public void validate(String value,
                         String pattern,
                         String errorKey)
                  throws DocumentParseException
    Validates that the input value matches against the specified Perl5 pattern. If valid, the method simply returns. If not a match, then an error message is generated (using the errorKey and the input value) and a InvalidStringException is thrown.

    Throws:
    DocumentParseException

    getDocumentLocation

    public IResourceLocation getDocumentLocation()

    getLocalName

    public String getLocalName()
    Returns the localName for the current element.

    See Also:
    ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)

    getQName

    public String getQName()
    Returns the qualified name for the current element.

    See Also:
    ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)

    getUri

    public String getUri()
    Returns the URI for the current element.

    See Also:
    ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)