org.xlattice.corexml.expr
Class ExprParser

java.lang.Object
  extended by org.xlattice.corexml.expr.ExprParser

public class ExprParser
extends java.lang.Object

XPath 1.0 expression parser. This class is best understood by reading it with the W3C XPath 1.0 specification in hand.

The parse() method accepts an expression in String form, tokenizes it using its lexer, and returns an unevaluated expression tree.

The eval() method accepts an XPath expression tree and returns a result. The evaluation is done given the current context and context node. XPath return types do not map particularly well into Java types.

This implementation is incomplete.

Author:
Jim Dixon

Field Summary
protected  org.xlattice.Context ctx
          the context, hashed access to global variables
protected  Node node
          the node expressions are evaluated in relation to
static NodeTest NTEST_COMMENT
           
static NodeTest NTEST_ELEMENT
           
static NodeTest NTEST_NODE
           
static NodeTest NTEST_PI
           
static NodeTest NTEST_STAR
           
static NodeTest NTEST_TEXT
           
static NodeType NTYPE_COMMENT
           
static NodeType NTYPE_ELEMENT
           
static NodeType NTYPE_NODE
           
static NodeType NTYPE_PI
           
static NodeType NTYPE_TEXT
           
static Operator OP_DSLASH
           
static Operator OP_SLASH
           
static Operator OP_STAR
           
static Symbol SYM_COLON
           
static Symbol SYM_DCOLON
           
static Symbol SYM_DDOT
           
static Symbol SYM_DOT
           
static Symbol SYM_LBRACKET
           
static Symbol SYM_LPAREN
           
static Symbol SYM_RBRACKET
           
static Symbol SYM_RPAREN
           
static Symbol SYM_STAR
           
 
Constructor Summary
ExprParser(Node n, org.xlattice.Context c)
          Create an expression parser.
 
Method Summary
static boolean booleanFunc(java.lang.Object o)
           
 void cantCast(Expr e)
           
 Bool castToBool(Expr e)
           
 Literal castToLiteral(Expr e)
           
 Numeric castToNumeric(Expr e)
           
(package private)  NodeTest collectNodeTest()
          Expect the next tokens from the lexer to represent a NodeTest.
(package private)  Step collectPredicates(Step step)
          Given a step, collect any predicates following it.
(package private)  Step collectStep(AxisName aName, boolean single)
          Having encountered an axis name, collect the step.
(package private)  Step collectStep(NCName ncName, boolean single)
          Verify NCName followed by :*, then collect the step, defaulting it to one along the CHILD axis.
(package private)  Step collectStep(NodeType nodeType, boolean single)
          Having encountered a nodeType, collect the step, defaulting it to one along the CHILD axis.
(package private)  Step collectStep(QName qName, boolean single)
          Having encountered a QName, collect the step, defaulting it to one along the CHILD axis.
(package private)  Step collectStep(Symbol sym, boolean single)
          Having encountered a symbol, collect the step.
(package private)  LocationPath collectSteps(LocationPath lp, boolean single, boolean required)
          Collect a series of steps and append them to the location path, which is returned.
static java.lang.String concat(java.lang.String[] args)
          This interface deviates from the specs, because Java 1.4 doesn't support variable length argument lists.
static Numeric count(NodeSet nodes)
          XPath NodeSet function 1, count.
 Expr eval(Expr e)
           
 Expr eval(java.lang.String str)
           
(package private)  Bool evalBoolBinary(BinaryExpr e)
           
(package private)  Literal evalLiteralBinary(BinaryExpr e)
           
(package private)  NodeSet evalLocationPath(LocationPath lp)
           
(package private)  NodeSet evalNodeSetBinary(BinaryExpr e)
           
(package private)  Numeric evalNumericBinary(BinaryExpr e)
           
(package private)  void expect(Token t)
           
 Node getNode()
           
 Numeric id()
           
 Numeric last()
           
 java.lang.String localName()
           
static java.lang.String localName(NodeSet nodes)
           
 java.lang.String name()
           
static java.lang.String name(NodeSet nodes)
           
 java.lang.String namespaceUri()
           
static java.lang.String namespaceUri(NodeSet nodes)
           
static java.lang.Double number()
           
static java.lang.Double number(java.lang.Object o)
          XPath numeric function 1, number()
 Expr parse(java.lang.String str)
          Parse a String holding an XPath expression.
(package private)  LocationPath parseAbsLocationPath(Operator op)
          Parse an absolute location path, having encountered an Operator token.
(package private)  BinaryExpr parseAdditiveExpr(Operator op, Expr left)
           
(package private)  BinaryExpr parseAndExpr(Operator op, Expr left)
           
(package private)  BinaryExpr parseEqualityExpr(Operator op, Expr left)
           
(package private)  BinaryExpr parseMultiplicativeExpr(Operator op, Expr left)
           
(package private)  BinaryExpr parseOrExpr(Operator op, Expr left)
           
(package private)  BinaryExpr parseRelationalExpr(Operator op, Expr left)
           
(package private)  LocationPath parseRelLocationPath(Token t)
          Parse an absolute location path, having encountered the current token.
(package private)  LocationPath parseStep(Token t, Expr soFar)
          Parse a step beginning with Token, using the expression collected so far to build a location path.
(package private)  UnaryExpr parseUnaryExpr(Operator op)
          Parse a unary expression with a leading minus.
(package private)  BinaryExpr parseUnionExpr(Operator op, Expr left)
          Called after seeing a '|' (BAR)
 Numeric position()
           
 void setNode(Node newContextNode)
          Change the context node, the node that expressions are evaluated in relation to.
 java.lang.String string()
          Convert the context node.
static java.lang.String string(Node node)
          XPath String function 1, string(node).
static java.lang.String string(java.lang.Object e)
          Convert objects to Java Strings in accordance with the spec.
(package private)  Node takeStep(Node ctxNode, NodeSet collected, java.util.ArrayList steps)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

node

protected Node node
the node expressions are evaluated in relation to


ctx

protected final org.xlattice.Context ctx
the context, hashed access to global variables


OP_SLASH

public static final Operator OP_SLASH

OP_DSLASH

public static final Operator OP_DSLASH

OP_STAR

public static final Operator OP_STAR

SYM_COLON

public static final Symbol SYM_COLON

SYM_DCOLON

public static final Symbol SYM_DCOLON

SYM_DOT

public static final Symbol SYM_DOT

SYM_DDOT

public static final Symbol SYM_DDOT

SYM_LBRACKET

public static final Symbol SYM_LBRACKET

SYM_LPAREN

public static final Symbol SYM_LPAREN

SYM_RBRACKET

public static final Symbol SYM_RBRACKET

SYM_RPAREN

public static final Symbol SYM_RPAREN

SYM_STAR

public static final Symbol SYM_STAR

NTYPE_COMMENT

public static final NodeType NTYPE_COMMENT

NTEST_COMMENT

public static final NodeTest NTEST_COMMENT

NTYPE_ELEMENT

public static final NodeType NTYPE_ELEMENT

NTEST_ELEMENT

public static final NodeTest NTEST_ELEMENT

NTYPE_NODE

public static final NodeType NTYPE_NODE

NTEST_NODE

public static final NodeTest NTEST_NODE

NTYPE_PI

public static final NodeType NTYPE_PI

NTEST_PI

public static final NodeTest NTEST_PI

NTYPE_TEXT

public static final NodeType NTYPE_TEXT

NTEST_TEXT

public static final NodeTest NTEST_TEXT

NTEST_STAR

public static final NodeTest NTEST_STAR
Constructor Detail

ExprParser

public ExprParser(Node n,
                  org.xlattice.Context c)
Create an expression parser.

Parameters:
n - current node
c - current context
Method Detail

getNode

public Node getNode()
Returns:
the context node

setNode

public void setNode(Node newContextNode)
Change the context node, the node that expressions are evaluated in relation to.


expect

void expect(Token t)
      throws CoreXmlException
Throws:
CoreXmlException - if t not next Token from lexer

parse

public Expr parse(java.lang.String str)
           throws CoreXmlException
Parse a String holding an XPath expression. If the String is null or empty, return null. Otherwise, return an unevaluated expression tree.

Returns:
a parse tree
Throws:
CoreXmlException

parseAbsLocationPath

LocationPath parseAbsLocationPath(Operator op)
                            throws CoreXmlException
Parse an absolute location path, having encountered an Operator token.

Returns:
a LocationPath subexpression tree
Throws:
CoreXmlException

parseRelLocationPath

LocationPath parseRelLocationPath(Token t)
                            throws CoreXmlException
Parse an absolute location path, having encountered the current token.

Returns:
a LocationPath subexpression tree
Throws:
CoreXmlException

collectSteps

LocationPath collectSteps(LocationPath lp,
                          boolean single,
                          boolean required)
                    throws CoreXmlException
Collect a series of steps and append them to the location path, which is returned.

Parameters:
lp - location path input
single - collect only one step
required - whether a step MUST be found
Returns:
location path with steps appended
Throws:
CoreXmlException

collectStep

Step collectStep(AxisName aName,
                 boolean single)
           throws CoreXmlException
Having encountered an axis name, collect the step.

Throws:
CoreXmlException

collectStep

Step collectStep(Symbol sym,
                 boolean single)
           throws CoreXmlException
Having encountered a symbol, collect the step.

Throws:
CoreXmlException

collectStep

Step collectStep(NodeType nodeType,
                 boolean single)
           throws CoreXmlException
Having encountered a nodeType, collect the step, defaulting it to one along the CHILD axis.

Throws:
CoreXmlException

collectStep

Step collectStep(NCName ncName,
                 boolean single)
           throws CoreXmlException
Verify NCName followed by :*, then collect the step, defaulting it to one along the CHILD axis.

Throws:
CoreXmlException

collectStep

Step collectStep(QName qName,
                 boolean single)
           throws CoreXmlException
Having encountered a QName, collect the step, defaulting it to one along the CHILD axis.

Throws:
CoreXmlException

collectNodeTest

NodeTest collectNodeTest()
                   throws CoreXmlException
Expect the next tokens from the lexer to represent a NodeTest.

Returns:
the NodeTest constructed from the stream of Tokens
Throws:
CoreXmlException

collectPredicates

Step collectPredicates(Step step)
                 throws CoreXmlException
Given a step, collect any predicates following it.

Throws:
CoreXmlException

parseStep

LocationPath parseStep(Token t,
                       Expr soFar)
                 throws CoreXmlException
Parse a step beginning with Token, using the expression collected so far to build a location path.

Parameters:
t - curent token
soFar - expression seen so far, must be a LocationPath
Throws:
CoreXmlException

parseUnaryExpr

UnaryExpr parseUnaryExpr(Operator op)
                   throws CoreXmlException
Parse a unary expression with a leading minus.

Throws:
CoreXmlException

parseUnionExpr

BinaryExpr parseUnionExpr(Operator op,
                          Expr left)
                    throws CoreXmlException
Called after seeing a '|' (BAR)

Throws:
CoreXmlException

parseMultiplicativeExpr

BinaryExpr parseMultiplicativeExpr(Operator op,
                                   Expr left)
                             throws CoreXmlException
Throws:
CoreXmlException

parseAdditiveExpr

BinaryExpr parseAdditiveExpr(Operator op,
                             Expr left)
                       throws CoreXmlException
Throws:
CoreXmlException

parseRelationalExpr

BinaryExpr parseRelationalExpr(Operator op,
                               Expr left)
                         throws CoreXmlException
Throws:
CoreXmlException

parseEqualityExpr

BinaryExpr parseEqualityExpr(Operator op,
                             Expr left)
                       throws CoreXmlException
Throws:
CoreXmlException

parseAndExpr

BinaryExpr parseAndExpr(Operator op,
                        Expr left)
                  throws CoreXmlException
Throws:
CoreXmlException

parseOrExpr

BinaryExpr parseOrExpr(Operator op,
                       Expr left)
                 throws CoreXmlException
Throws:
CoreXmlException

eval

public Expr eval(java.lang.String str)
          throws CoreXmlException
Throws:
CoreXmlException

eval

public Expr eval(Expr e)
          throws CoreXmlException
Throws:
CoreXmlException

evalLocationPath

NodeSet evalLocationPath(LocationPath lp)
                   throws CoreXmlException
Throws:
CoreXmlException

takeStep

Node takeStep(Node ctxNode,
              NodeSet collected,
              java.util.ArrayList steps)
        throws CoreXmlException
Throws:
CoreXmlException

evalBoolBinary

Bool evalBoolBinary(BinaryExpr e)
              throws CoreXmlException
Throws:
CoreXmlException

evalLiteralBinary

Literal evalLiteralBinary(BinaryExpr e)
                    throws CoreXmlException
Throws:
CoreXmlException

evalNodeSetBinary

NodeSet evalNodeSetBinary(BinaryExpr e)
                    throws CoreXmlException
Throws:
CoreXmlException

evalNumericBinary

Numeric evalNumericBinary(BinaryExpr e)
                    throws CoreXmlException
Throws:
CoreXmlException

castToBool

public Bool castToBool(Expr e)
                throws CoreXmlException
Throws:
CoreXmlException

castToNumeric

public Numeric castToNumeric(Expr e)
                      throws CoreXmlException
Throws:
CoreXmlException

castToLiteral

public Literal castToLiteral(Expr e)
                      throws CoreXmlException
Throws:
CoreXmlException

cantCast

public void cantCast(Expr e)
              throws CoreXmlException
Throws:
CoreXmlException

count

public static Numeric count(NodeSet nodes)
XPath NodeSet function 1, count.

Returns:
the number of nodes in the NodeSet

id

public Numeric id()

last

public Numeric last()

name

public java.lang.String name()

name

public static java.lang.String name(NodeSet nodes)

localName

public java.lang.String localName()

localName

public static java.lang.String localName(NodeSet nodes)

namespaceUri

public java.lang.String namespaceUri()

namespaceUri

public static java.lang.String namespaceUri(NodeSet nodes)

position

public Numeric position()

string

public static java.lang.String string(Node node)
XPath String function 1, string(node).


string

public java.lang.String string()
Convert the context node. XXX Implement this by passing a NodeSet containing only the context node to the next function. XXX THE NODE BEING PASSED HERE IS NOT NECESSARILY THE CURRENT CONTEXT NODE


string

public static java.lang.String string(java.lang.Object e)
Convert objects to Java Strings in accordance with the spec. XXX This implementation is incomplete.


concat

public static java.lang.String concat(java.lang.String[] args)
This interface deviates from the specs, because Java 1.4 doesn't support variable length argument lists.

Returns:
the concatenation of its arguments

booleanFunc

public static boolean booleanFunc(java.lang.Object o)

number

public static java.lang.Double number(java.lang.Object o)
XPath numeric function 1, number()


number

public static java.lang.Double number()