net.sf.saxon.expr
Class ExpressionParser

java.lang.Object
  extended by net.sf.saxon.expr.ExpressionParser
Direct Known Subclasses:
SelectionParser

public class ExpressionParser
extends Object

Parser for XPath expressions and XSLT patterns. This code was originally inspired by James Clark's xt but has been totally rewritten (several times)

Author:
Michael Kay

Nested Class Summary
static class ExpressionParser.ForClause
           
 
Constructor Summary
ExpressionParser()
          Create an expression parser
 
Method Summary
 Stack getRangeVariableStack()
          Get the range variable stack.
 Tokenizer getTokenizer()
          Get the tokenizer (the lexical analyzer)
 boolean isCompileWithTracing()
          Determine whether trace hooks are included in the compiled code.
 LocalNameTest makeLocalNameTest(short nodeType, String localName)
          Make a LocalNameTest (*:name)
 int makeNameCode(String qname, boolean useDefault)
          Make a NameCode, using the static context for namespace resolution
 int makeNameCodeSilently(String qname, boolean useDefault)
          Make a NameCode, using the static context for namespace resolution.
 NamespaceTest makeNamespaceTest(short nodeType, String prefix)
          Make a NamespaceTest (name:*)
 NameTest makeNameTest(short nodeType, String qname, boolean useDefault)
          Make a NameTest, using the static context for namespace resolution
 StructuredQName makeStructuredQName(String qname, boolean useDefault)
          Make a Structured QName, using the static context for namespace resolution
 Expression parse(String expression, int start, int terminator, int lineNumber, StaticContext env)
          Parse a string representing an expression
 Pattern parsePattern(String pattern, StaticContext env)
          Parse a string representing an XSLT pattern
 SequenceType parseSequenceType(String input, StaticContext env)
          Parse a string representing a sequence type
 void setCompileWithTracing(boolean trueOrFalse)
          Set whether trace hooks are to be included in the compiled code.
 void setRangeVariableStack(Stack stack)
          Set the range variable stack.
 void setScanOnly(boolean scanOnly)
          Set that we are parsing in "scan only"
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionParser

public ExpressionParser()
Create an expression parser

Method Detail

setCompileWithTracing

public void setCompileWithTracing(boolean trueOrFalse)
Set whether trace hooks are to be included in the compiled code. To use tracing, it is necessary both to compile the code with trace hooks included, and to supply a TraceListener at run-time

Parameters:
trueOrFalse - true if trace code is to be compiled in, false otherwise

isCompileWithTracing

public boolean isCompileWithTracing()
Determine whether trace hooks are included in the compiled code.

Returns:
true if trace hooks are included, false if not.

getTokenizer

public Tokenizer getTokenizer()
Get the tokenizer (the lexical analyzer)

Returns:
the tokenizer (the lexical analyzer)

parse

public Expression parse(String expression,
                        int start,
                        int terminator,
                        int lineNumber,
                        StaticContext env)
                 throws XPathException
Parse a string representing an expression

Parameters:
expression - the expression expressed as a String
start - offset within the string where parsing is to start
terminator - character to treat as terminating the expression
lineNumber - location of the start of the expression, for diagnostics
env - the static context for the expression
Returns:
an Expression object representing the result of parsing
Throws:
XPathException - if the expression contains a syntax error

parsePattern

public Pattern parsePattern(String pattern,
                            StaticContext env)
                     throws XPathException
Parse a string representing an XSLT pattern

Parameters:
pattern - the pattern expressed as a String
env - the static context for the pattern
Returns:
a Pattern object representing the result of parsing
Throws:
XPathException - if the pattern contains a syntax error

parseSequenceType

public SequenceType parseSequenceType(String input,
                                      StaticContext env)
                               throws XPathException
Parse a string representing a sequence type

Parameters:
input - the string, which should conform to the XPath SequenceType production
env - the static context
Returns:
a SequenceType object representing the type
Throws:
XPathException - if any error is encountered

getRangeVariableStack

public Stack getRangeVariableStack()
Get the range variable stack. Used when parsing a nested subexpression inside an attribute constructor

Returns:
the stack used for locally-declared variables

setRangeVariableStack

public void setRangeVariableStack(Stack stack)
Set the range variable stack. Used when parsing a nested subexpression inside an attribute constructor.

Parameters:
stack - the stack to be used for local variables declared within the expression

makeNameCode

public final int makeNameCode(String qname,
                              boolean useDefault)
                       throws XPathException
Make a NameCode, using the static context for namespace resolution

Parameters:
qname - The name as written, in the form "[prefix:]localname"
useDefault - Defines the action when there is no prefix. If true, use the default namespace URI for element names. If false, use no namespace URI (as for attribute names).
Returns:
the namecode, which can be used to identify this name in the name pool
Throws:
XPathException - if the name is invalid, or the prefix undeclared

makeNameCodeSilently

public final int makeNameCodeSilently(String qname,
                                      boolean useDefault)
                               throws XPathException,
                                      QNameException
Make a NameCode, using the static context for namespace resolution. This variant of the method does not call "grumble" to report any errors to the ErrorListener, it only reports errors by throwing exceptions. This allows the caller to control the message output.

Parameters:
qname - The name as written, in the form "[prefix:]localname"
useDefault - Defines the action when there is no prefix. If true, use the default namespace URI for element names. If false, use no namespace URI (as for attribute names).
Returns:
the namecode, which can be used to identify this name in the name pool
Throws:
XPathException - if the name is invalid, or the prefix undeclared
QNameException

makeStructuredQName

public final StructuredQName makeStructuredQName(String qname,
                                                 boolean useDefault)
                                          throws XPathException
Make a Structured QName, using the static context for namespace resolution

Parameters:
qname - The name as written, in the form "[prefix:]localname"
useDefault - Defines the action when there is no prefix. If true, use the default namespace URI for element names. If false, use no namespace URI (as for attribute names).
Returns:
the namecode, which can be used to identify this name in the name pool
Throws:
XPathException - if the name is invalid, or the prefix undeclared

makeNameTest

public NameTest makeNameTest(short nodeType,
                             String qname,
                             boolean useDefault)
                      throws XPathException
Make a NameTest, using the static context for namespace resolution

Parameters:
nodeType - the type of node required (identified by a constant in class Type)
qname - the lexical QName of the required node
useDefault - true if the default namespace should be used when the QName is unprefixed
Returns:
a NameTest, representing a pattern that tests for a node of a given node kind and a given name
Throws:
XPathException - if the QName is invalid

makeNamespaceTest

public NamespaceTest makeNamespaceTest(short nodeType,
                                       String prefix)
                                throws XPathException
Make a NamespaceTest (name:*)

Parameters:
nodeType - integer code identifying the type of node required
prefix - the namespace prefix
Returns:
the NamespaceTest, a pattern that matches all nodes in this namespace
Throws:
XPathException - if the namespace prefix is not declared

makeLocalNameTest

public LocalNameTest makeLocalNameTest(short nodeType,
                                       String localName)
                                throws XPathException
Make a LocalNameTest (*:name)

Parameters:
nodeType - the kind of node to be matched
localName - the requred local name
Returns:
a LocalNameTest, a pattern which matches all nodes of a given local name, regardless of namespace
Throws:
XPathException - if the local name is invalid

setScanOnly

public void setScanOnly(boolean scanOnly)
Set that we are parsing in "scan only"

Parameters:
scanOnly - true if parsing is to proceed in scan-only mode. In this mode namespace bindings are not yet known, so no attempt is made to look up namespace prefixes.


Copyright (C) Michael H. Kay. All rights reserved.