Class XPathParser

java.lang.Object
org.loboevolution.apache.xpath.compiler.XPathParser

public class XPathParser extends Object
Tokenizes and parses XPath expressions. This should really be named XPathParserImpl, and may be renamed in the future.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    protected static final int
    Results from checking FilterExpr syntax
    protected static final int
     
    protected static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    XPathParser(ErrorListener errorListener)
    The parser constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    AbbreviatedNodeTestStep(boolean isLeadingSlashPermitted)
    AbbreviatedNodeTestStep ::= '@'?
    protected int
    AdditiveExpr(int addPos)
    This has to handle construction of the operations so that they are evaluated in pre-fix order.
    protected void
    AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
    protected void
    Argument ::= Expr
    protected int
    Basis ::= AxisName '::' NodeTest | AbbreviatedBasis
    protected void
    Basis ::= AxisName '::' NodeTest | AbbreviatedBasis
    protected void
    StringExpr ::= Expr
    protected String
    Dump the remaining token queue.
    protected int
    EqualityExpr(int addPos)
    EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
    protected void
    Expr ::= OrExpr
    protected int
    FilterExpr ::= PrimaryExpr | FilterExpr Predicate
    protected boolean
    FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)?
    Return the current error listener.
    protected void
    IdKeyPattern ::= 'id' '(' Literal ')' | 'key' '(' Literal ',' Literal ')' (Also handle doc())
    void
    initMatchPattern(Compiler compiler, String expression, PrefixResolver namespaceContext)
    Given an string, init an XPath object for pattern matches, in order that a parse doesn't have to be done each time the expression is evaluated.
    void
    initXPath(Compiler compiler, String expression, PrefixResolver namespaceContext)
    Given an string, init an XPath object for selections, in order that a parse doesn't have to be done each time the expression is evaluated.
    protected void
    The value of the Literal is the sequence of characters inside the " or ' characters.
    protected void
    LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
    protected void
    LocationPathPattern ::= '/' RelativePathPattern?
    protected int
    MultiplicativeExpr(int addPos)
    This has to handle construction of the operations so that they are evaluated in pre-fix order.
    protected void
    NCName ::= (Letter | '_') (NCNameChar) NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender
    protected void
    NodeTest(int axesType)
    NodeTest ::= WildcardName | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'
    protected void
    Number ::= [0-9]+('
    protected void
    NumberExpr ::= Expr
    protected void
    OrExpr ::= AndExpr | OrExpr 'or' AndExpr
    protected void
    PathExpr ::= LocationPath | FilterExpr | FilterExpr '/' RelativeLocationPath | FilterExpr '//' RelativeLocationPath
    protected void
    Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
    protected void
    Predicate ::= '[' PredicateExpr ']'
    protected void
    PredicateExpr ::= Expr
    protected boolean
    PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
    protected void
    QName ::= (Prefix ':')?
    protected int
    RelationalExpr(int addPos)
    RelationalExpr ::= AdditiveExpr | RelationalExpr '<' AdditiveExpr | RelationalExpr '>' AdditiveExpr | RelationalExpr '<=' AdditiveExpr | RelationalExpr '<=' AdditiveExpr
    protected boolean
    RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath
    protected void
    RelativePathPattern ::= StepPattern | RelativePathPattern '/' StepPattern | RelativePathPattern '//' StepPattern
    void
    Allow an application to register an error event handler, where syntax errors will be sent.
    protected boolean
    Step ::= Basis Predicate | AbbreviatedStep
    protected boolean
    StepPattern(boolean isLeadingSlashPermitted)
    StepPattern ::= AbbreviatedNodeTestStep
    protected void
    StringExpr ::= Expr
    protected void
    UnaryExpr ::= UnionExpr | '-' UnaryExpr
    protected void
    The context of the right hand side expressions is the context of the left hand side expression.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CONTINUE_AFTER_FATAL_ERROR

      public static final String CONTINUE_AFTER_FATAL_ERROR
      See Also:
    • FILTER_MATCH_FAILED

      protected static final int FILTER_MATCH_FAILED
      Results from checking FilterExpr syntax
      See Also:
    • FILTER_MATCH_PRIMARY

      protected static final int FILTER_MATCH_PRIMARY
      See Also:
    • FILTER_MATCH_PREDICATES

      protected static final int FILTER_MATCH_PREDICATES
      See Also:
  • Constructor Details

    • XPathParser

      public XPathParser(ErrorListener errorListener)
      The parser constructor.
  • Method Details

    • initXPath

      public void initXPath(Compiler compiler, String expression, PrefixResolver namespaceContext) throws TransformerException
      Given an string, init an XPath object for selections, in order that a parse doesn't have to be done each time the expression is evaluated.
      Parameters:
      compiler - The compiler object.
      expression - A string conforming to the XPath grammar.
      namespaceContext - An object that is able to resolve prefixes in the XPath to namespaces.
      Throws:
      TransformerException - in case of error
    • initMatchPattern

      public void initMatchPattern(Compiler compiler, String expression, PrefixResolver namespaceContext) throws TransformerException
      Given an string, init an XPath object for pattern matches, in order that a parse doesn't have to be done each time the expression is evaluated.
      Parameters:
      compiler - The XPath object to be initialized.
      expression - A String representing the XPath.
      namespaceContext - An object that is able to resolve prefixes in the XPath to namespaces.
      Throws:
      TransformerException - in case of error
    • setErrorHandler

      public void setErrorHandler(ErrorListener handler)
      Allow an application to register an error event handler, where syntax errors will be sent. If the error listener is not set, syntax errors will be sent to System.err.
      Parameters:
      handler - Reference to error listener where syntax errors will be sent.
    • getErrorListener

      public ErrorListener getErrorListener()
      Return the current error listener.
      Returns:
      The error listener, which should not normally be null, but may be.
    • dumpRemainingTokenQueue

      protected String dumpRemainingTokenQueue()
      Dump the remaining token queue. Thanks to Craig for this.
      Returns:
      A dump of the remaining token queue, which may be appended to an error message.
    • Expr

      protected void Expr() throws TransformerException
      Expr ::= OrExpr
      Throws:
      TransformerException - in case of error
    • OrExpr

      protected void OrExpr() throws TransformerException
      OrExpr ::= AndExpr | OrExpr 'or' AndExpr
      Throws:
      TransformerException - in case of error
    • AndExpr

      protected void AndExpr() throws TransformerException
      AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
      Throws:
      TransformerException - in case of error
    • EqualityExpr

      protected int EqualityExpr(int addPos) throws TransformerException
      EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
      Parameters:
      addPos - Position where expression is to be added, or -1 for append.
      Returns:
      the position at the end of the equality expression.
      Throws:
      TransformerException - in case of error
    • RelationalExpr

      protected int RelationalExpr(int addPos) throws TransformerException
      RelationalExpr ::= AdditiveExpr | RelationalExpr '<' AdditiveExpr | RelationalExpr '>' AdditiveExpr | RelationalExpr '<=' AdditiveExpr | RelationalExpr '<=' AdditiveExpr
      Parameters:
      addPos - Position where expression is to be added, or -1 for append.
      Returns:
      the position at the end of the relational expression.
      Throws:
      TransformerException - in case of error
    • AdditiveExpr

      protected int AdditiveExpr(int addPos) throws TransformerException
      This has to handle construction of the operations so that they are evaluated in pre-fix order. So, for 9+7-6, instead of |+|9|-|7|6|, this needs to be evaluated as |-|+|9|7|6|.

      AdditiveExpr ::= MultiplicativeExpr | AdditiveExpr '+' MultiplicativeExpr | AdditiveExpr '-' MultiplicativeExpr

      Parameters:
      addPos - Position where expression is to be added, or -1 for append.
      Returns:
      the position at the end of the equality expression.
      Throws:
      TransformerException - in case of error
    • MultiplicativeExpr

      protected int MultiplicativeExpr(int addPos) throws TransformerException
      This has to handle construction of the operations so that they are evaluated in pre-fix order. So, for 9+7-6, instead of |+|9|-|7|6|, this needs to be evaluated as |-|+|9|7|6|.

      MultiplicativeExpr ::= UnaryExpr | MultiplicativeExpr MultiplyOperator UnaryExpr | MultiplicativeExpr 'div' UnaryExpr | MultiplicativeExpr 'mod' UnaryExpr | MultiplicativeExpr 'quo' UnaryExpr

      Parameters:
      addPos - Position where expression is to be added, or -1 for append.
      Returns:
      the position at the end of the equality expression.
      Throws:
      TransformerException - in case of error
    • UnaryExpr

      protected void UnaryExpr() throws TransformerException
      UnaryExpr ::= UnionExpr | '-' UnaryExpr
      Throws:
      TransformerException - in case of error
    • StringExpr

      protected void StringExpr() throws TransformerException
      StringExpr ::= Expr
      Throws:
      TransformerException - in case of error
    • BooleanExpr

      protected void BooleanExpr() throws TransformerException
      StringExpr ::= Expr
      Throws:
      TransformerException - in case of error
    • NumberExpr

      protected void NumberExpr() throws TransformerException
      NumberExpr ::= Expr
      Throws:
      TransformerException - in case of error
    • UnionExpr

      protected void UnionExpr() throws TransformerException
      The context of the right hand side expressions is the context of the left hand side expression. The results of the right hand side expressions are node sets. The result of the left hand side UnionExpr is the union of the results of the right hand side expressions.

      UnionExpr ::= PathExpr | UnionExpr '|' PathExpr

      Throws:
      TransformerException - in case of error
    • PathExpr

      protected void PathExpr() throws TransformerException
      PathExpr ::= LocationPath | FilterExpr | FilterExpr '/' RelativeLocationPath | FilterExpr '//' RelativeLocationPath
      Throws:
      TransformerException - in case of error
    • FilterExpr

      protected int FilterExpr() throws TransformerException
      FilterExpr ::= PrimaryExpr | FilterExpr Predicate
      Returns:
      FILTER_MATCH_PREDICATES, if this method successfully matched a FilterExpr with one or more Predicates; FILTER_MATCH_PRIMARY, if this method successfully matched a FilterExpr that was just a PrimaryExpr; or FILTER_MATCH_FAILED, if this method did not match a FilterExpr
      Throws:
      TransformerException - in case of error
    • PrimaryExpr

      protected boolean PrimaryExpr() throws TransformerException
      PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
      Returns:
      true if this method successfully matched a PrimaryExpr
      Throws:
      TransformerException - in case of error
    • Argument

      protected void Argument() throws TransformerException
      Argument ::= Expr
      Throws:
      TransformerException - in case of error
    • FunctionCall

      protected boolean FunctionCall() throws TransformerException
      FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'
      Returns:
      true if, and only if, a FunctionCall was matched
      Throws:
      TransformerException - in case of error
    • LocationPath

      protected void LocationPath() throws TransformerException
      LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
      Throws:
      TransformerException - in case of error
    • RelativeLocationPath

      protected boolean RelativeLocationPath() throws TransformerException
      RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath
      Returns:
      true if, and only if, a RelativeLocationPath was matched
      Throws:
      TransformerException - in case of error
    • Step

      protected boolean Step() throws TransformerException
      Step ::= Basis Predicate | AbbreviatedStep
      Returns:
      false if step was empty (or only a '/'); true, otherwise
      Throws:
      TransformerException - in case of error
    • Basis

      protected void Basis() throws TransformerException
      Basis ::= AxisName '::' NodeTest | AbbreviatedBasis
      Throws:
      TransformerException - in case of error
    • AxisName

      protected int AxisName() throws TransformerException
      Basis ::= AxisName '::' NodeTest | AbbreviatedBasis
      Returns:
      FROM_XXX axes type, found in Keywords.
      Throws:
      TransformerException - in case of error
    • NodeTest

      protected void NodeTest(int axesType) throws TransformerException
      NodeTest ::= WildcardName | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'
      Parameters:
      axesType - FROM_XXX axes type, found in Keywords.
      Throws:
      TransformerException - in case of error
    • Predicate

      protected void Predicate() throws TransformerException
      Predicate ::= '[' PredicateExpr ']'
      Throws:
      TransformerException - in case of error
    • PredicateExpr

      protected void PredicateExpr() throws TransformerException
      PredicateExpr ::= Expr
      Throws:
      TransformerException - in case of error
    • QName

      protected void QName() throws TransformerException
      QName ::= (Prefix ':')? LocalPart Prefix ::= NCName LocalPart ::= NCName
      Throws:
      TransformerException - in case of error
    • NCName

      protected void NCName()
      NCName ::= (Letter | '_') (NCNameChar) NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender
    • Literal

      protected void Literal() throws TransformerException
      The value of the Literal is the sequence of characters inside the " or ' characters.

      Literal ::= '"' [^"]* '"' | "'" [^']* "'"

      Throws:
      TransformerException - in case of error
    • Number

      protected void Number() throws TransformerException
      Number ::= [0-9]+('.'[0-9]+)? | '.'[0-9]+
      Throws:
      TransformerException - in case of error
    • Pattern

      protected void Pattern() throws TransformerException
      Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
      Throws:
      TransformerException - in case of error
    • LocationPathPattern

      protected void LocationPathPattern() throws TransformerException
      LocationPathPattern ::= '/' RelativePathPattern? | IdKeyPattern (('/' | '//') RelativePathPattern)? | '//'? RelativePathPattern
      Throws:
      TransformerException - in case of error
    • IdKeyPattern

      protected void IdKeyPattern() throws TransformerException
      IdKeyPattern ::= 'id' '(' Literal ')' | 'key' '(' Literal ',' Literal ')' (Also handle doc())
      Throws:
      TransformerException - in case of error
    • RelativePathPattern

      protected void RelativePathPattern() throws TransformerException
      RelativePathPattern ::= StepPattern | RelativePathPattern '/' StepPattern | RelativePathPattern '//' StepPattern
      Throws:
      TransformerException - in case of error
    • StepPattern

      protected boolean StepPattern(boolean isLeadingSlashPermitted) throws TransformerException
      StepPattern ::= AbbreviatedNodeTestStep
      Parameters:
      isLeadingSlashPermitted - a boolean indicating whether a slash can appear at the start of this step
      Returns:
      boolean indicating whether a slash following the step was consumed
      Throws:
      TransformerException - in case of error
    • AbbreviatedNodeTestStep

      protected boolean AbbreviatedNodeTestStep(boolean isLeadingSlashPermitted) throws TransformerException
      AbbreviatedNodeTestStep ::= '@'? NodeTest Predicate
      Parameters:
      isLeadingSlashPermitted - a boolean indicating whether a slash can appear at the start of this step
      Returns:
      boolean indicating whether a slash following the step was consumed
      Throws:
      TransformerException - in case of error