Package org.htmlunit.xpath.compiler
Class XPathParser
java.lang.Object
org.htmlunit.xpath.compiler.XPathParser
Tokenizes and parses XPath expressions. This should really be named XPathParserImpl, and may be
renamed in the future.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
protected static final int
Results from checking FilterExpr syntaxprotected static final int
protected static final int
private ErrorListener
The error listener where syntax errors are to be sent.private FunctionTable
The table contains build-in functions and customized functions(package private) PrefixResolver
The prefix resolver to map prefixes to namespaces in the OpMap.private OpMap
The XPath to be processed.(package private) int
The position in the token queue is tracked by m_queueMark.(package private) String
The next token in the pattern.(package private) char
The first char in m_token, the theory being that this is an optimization because we won't have to do charAt(0) as often. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
AbbreviatedNodeTestStep
(boolean isLeadingSlashPermitted) AbbreviatedNodeTestStep ::= '@'? NodeTest Predicateprotected int
AdditiveExpr
(int addPos) This has to handle construction of the operations so that they are evaluated in pre-fix order.protected void
AndExpr()
AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr(package private) void
appendOp
(int length, int op) Insert room for operation.protected void
Argument()
Argument ::= Exprprotected int
AxisName()
Basis ::= AxisName '::' NodeTest | AbbreviatedBasisprotected void
Basis()
Basis ::= AxisName '::' NodeTest | AbbreviatedBasisprotected void
StringExpr ::= Exprprivate void
consumeExpected
(char expected) Consume an expected token, throwing an exception if it isn't there.protected String
Dump the remaining token queue.protected int
EqualityExpr
(int addPos) EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr(package private) void
Notify the user of an error, and probably throw an exception.protected void
Expr()
Expr ::= OrExprprotected int
FilterExpr ::= PrimaryExpr | FilterExpr Predicateprotected boolean
FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'Return the current error listener.(package private) final int
getFunctionToken
(String key) Given a string, return the corresponding function token.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.(package private) void
insertOp
(int pos, int length, int op) Insert room for operation.protected void
Literal()
The value of the Literal is the sequence of characters inside the " or ' characters.protected void
LocationPath ::= RelativeLocationPath | AbsoluteLocationPathprotected void
LocationPathPattern ::= '/' RelativePathPattern? | IdKeyPattern (('/' | '//') RelativePathPattern)? | '//'? RelativePathPattern(package private) final boolean
lookahead
(char c, int n) Look ahead of the current token in order to make a branching decision.private boolean
Look ahead of the current token in order to make a branching decision.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()
NCName ::= (Letter | '_') (NCNameChar) NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extenderprivate void
Retrieve the next token from the command and store it in m_token string.protected void
NodeTest
(int axesType) NodeTest ::= WildcardName | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'protected void
Number()
Number ::= [0-9]+('.'[0-9]+)? | '.'[0-9]+protected void
NumberExpr ::= Exprprotected void
OrExpr()
OrExpr ::= AndExpr | OrExpr 'or' AndExprprotected void
PathExpr()
PathExpr ::= LocationPath | FilterExpr | FilterExpr '/' RelativeLocationPath | FilterExpr '//' RelativeLocationPathprotected void
Pattern()
Pattern ::= LocationPathPattern | Pattern '|' LocationPathPatternprotected void
Predicate ::= '[' PredicateExpr ']'protected void
PredicateExpr ::= Exprprotected boolean
PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCallprotected void
QName()
QName ::= (Prefix ':')? LocalPart Prefix ::= NCName LocalPart ::= NCNameprotected int
RelationalExpr
(int addPos) RelationalExpr ::= AdditiveExpr | RelationalExpr '<' AdditiveExpr | RelationalExpr '>' AdditiveExpr | RelationalExpr '<=' AdditiveExpr | RelationalExpr '>=' AdditiveExprprotected boolean
RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPathprotected void
RelativePathPattern ::= StepPattern | RelativePathPattern '/' StepPattern | RelativePathPattern '//' StepPatternvoid
setErrorHandler
(ErrorListener handler) Allow an application to register an error event handler, where syntax errors will be sent.protected boolean
Step()
Step ::= Basis Predicate | AbbreviatedStepprotected boolean
StepPattern
(boolean isLeadingSlashPermitted) StepPattern ::= AbbreviatedNodeTestStepprotected void
StringExpr ::= Expr(package private) final boolean
tokenIs
(char c) Check whether m_tokenChar==c.(package private) final boolean
Check whether m_token matches the target string.protected void
UnaryExpr ::= UnionExpr | '-' UnaryExprprotected void
The context of the right hand side expressions is the context of the left hand side expression.
-
Field Details
-
CONTINUE_AFTER_FATAL_ERROR
- See Also:
-
m_ops
The XPath to be processed. -
m_token
The next token in the pattern. -
m_tokenChar
transient char m_tokenCharThe first char in m_token, the theory being that this is an optimization because we won't have to do charAt(0) as often. -
m_queueMark
int m_queueMarkThe position in the token queue is tracked by m_queueMark. -
FILTER_MATCH_FAILED
protected static final int FILTER_MATCH_FAILEDResults 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:
-
m_namespaceContext
PrefixResolver m_namespaceContextThe prefix resolver to map prefixes to namespaces in the OpMap. -
m_errorListener
The error listener where syntax errors are to be sent. -
m_functionTable
The table contains build-in functions and customized functions
-
-
Constructor Details
-
XPathParser
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
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
Return the current error listener.- Returns:
- The error listener, which should not normally be null, but may be.
-
tokenIs
Check whether m_token matches the target string.- Parameters:
s
- A string reference or null.- Returns:
- If m_token is null, returns false (or true if s is also null), or return true if the current token matches the string, else false.
-
tokenIs
final boolean tokenIs(char c) Check whether m_tokenChar==c.- Parameters:
c
- A character to be tested.- Returns:
- If m_token is null, returns false, or return true if c matches the current token.
-
lookahead
final boolean lookahead(char c, int n) Look ahead of the current token in order to make a branching decision.- Parameters:
c
- the character to be tested for.n
- number of tokens to look ahead. Must be greater than 1.- Returns:
- true if the next token matches the character argument.
-
lookahead
Look ahead of the current token in order to make a branching decision.- Parameters:
s
- the string to compare it to.n
- number of tokens to lookahead. Must be greater than 1.- Returns:
- true if the token behind the current token matches the string argument.
-
nextToken
private void nextToken()Retrieve the next token from the command and store it in m_token string. -
consumeExpected
Consume an expected token, throwing an exception if it isn't there.- Parameters:
expected
- the character to be expected.- Throws:
TransformerException
- in case of error
-
error
Notify the user of an error, and probably throw an exception.- Parameters:
msg
- An error msgkey that corresponds to one of the constants found inXPATHErrorResources
, which is a key for a format string.args
- An array of arguments represented in the format string, which may be null.- Throws:
TransformerException
- if the current ErrorListoner determines to throw an exception.
-
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.
-
getFunctionToken
Given a string, return the corresponding function token.- Parameters:
key
- A local name of a function.- Returns:
- The function ID, which may correspond to one of the FUNC_XXX values found in
FunctionTable
, but may be a value installed by an external module.
-
insertOp
void insertOp(int pos, int length, int op) Insert room for operation. This will NOT set the length value of the operation, but will update the length value for the total expression.- Parameters:
pos
- The position where the op is to be inserted.length
- The length of the operation space in the op map.op
- The op code to the inserted.
-
appendOp
void appendOp(int length, int op) Insert room for operation. This WILL set the length value of the operation, and will update the length value for the total expression.- Parameters:
length
- The length of the operation.op
- The op code to the inserted.
-
Expr
Expr ::= OrExpr- Throws:
TransformerException
- in case of error
-
OrExpr
OrExpr ::= AndExpr | OrExpr 'or' AndExpr- Throws:
TransformerException
- in case of error
-
AndExpr
AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr- Throws:
TransformerException
- in case of error
-
EqualityExpr
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
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
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
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
UnaryExpr ::= UnionExpr | '-' UnaryExpr- Throws:
TransformerException
- in case of error
-
StringExpr
StringExpr ::= Expr- Throws:
TransformerException
- in case of error
-
BooleanExpr
StringExpr ::= Expr- Throws:
TransformerException
- in case of error
-
NumberExpr
NumberExpr ::= Expr- Throws:
TransformerException
- in case of error
-
UnionExpr
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
PathExpr ::= LocationPath | FilterExpr | FilterExpr '/' RelativeLocationPath | FilterExpr '//' RelativeLocationPath- Throws:
TransformerException
- in case of error
-
FilterExpr
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
PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall- Returns:
- true if this method successfully matched a PrimaryExpr
- Throws:
TransformerException
- in case of error
-
Argument
Argument ::= Expr- Throws:
TransformerException
- in case of error
-
FunctionCall
FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'- Returns:
- true if, and only if, a FunctionCall was matched
- Throws:
TransformerException
- in case of error
-
LocationPath
LocationPath ::= RelativeLocationPath | AbsoluteLocationPath- Throws:
TransformerException
- in case of error
-
RelativeLocationPath
RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath- Returns:
- true if, and only if, a RelativeLocationPath was matched
- Throws:
TransformerException
- in case of error
-
Step
Step ::= Basis Predicate | AbbreviatedStep- Returns:
- false if step was empty (or only a '/'); true, otherwise
- Throws:
TransformerException
- in case of error
-
Basis
Basis ::= AxisName '::' NodeTest | AbbreviatedBasis- Throws:
TransformerException
- in case of error
-
AxisName
Basis ::= AxisName '::' NodeTest | AbbreviatedBasis- Returns:
- FROM_XXX axes type, found in
Keywords
. - Throws:
TransformerException
- in case of error
-
NodeTest
NodeTest ::= WildcardName | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'- Parameters:
axesType
- FROM_XXX axes type, found inKeywords
.- Throws:
TransformerException
- in case of error
-
Predicate
Predicate ::= '[' PredicateExpr ']'- Throws:
TransformerException
- in case of error
-
PredicateExpr
PredicateExpr ::= Expr- Throws:
TransformerException
- in case of error
-
QName
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
The value of the Literal is the sequence of characters inside the " or ' characters.Literal ::= '"' [^"]* '"' | "'" [^']* "'"
- Throws:
TransformerException
- in case of error
-
Number
Number ::= [0-9]+('.'[0-9]+)? | '.'[0-9]+- Throws:
TransformerException
- in case of error
-
Pattern
Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern- Throws:
TransformerException
- in case of error
-
LocationPathPattern
LocationPathPattern ::= '/' RelativePathPattern? | IdKeyPattern (('/' | '//') RelativePathPattern)? | '//'? RelativePathPattern- Throws:
TransformerException
- in case of error
-
IdKeyPattern
IdKeyPattern ::= 'id' '(' Literal ')' | 'key' '(' Literal ',' Literal ')' (Also handle doc())- Throws:
TransformerException
- in case of error
-
RelativePathPattern
RelativePathPattern ::= StepPattern | RelativePathPattern '/' StepPattern | RelativePathPattern '//' StepPattern- Throws:
TransformerException
- in case of error
-
StepPattern
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
-