Package org.htmlunit.xpath.compiler
Class XPathParser
- java.lang.Object
-
- org.htmlunit.xpath.compiler.XPathParser
-
public class XPathParser extends java.lang.ObjectTokenizes 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 java.lang.StringCONTINUE_AFTER_FATAL_ERRORprotected static intFILTER_MATCH_FAILEDResults from checking FilterExpr syntaxprotected static intFILTER_MATCH_PREDICATESprotected static intFILTER_MATCH_PRIMARYprivate javax.xml.transform.ErrorListenerm_errorListenerThe error listener where syntax errors are to be sent.private FunctionTablem_functionTableThe table contains build-in functions and customized functions(package private) PrefixResolverm_namespaceContextThe prefix resolver to map prefixes to namespaces in the OpMap.private OpMapm_opsThe XPath to be processed.(package private) intm_queueMarkThe position in the token queue is tracked by m_queueMark.(package private) java.lang.Stringm_tokenThe next token in the pattern.(package private) charm_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.
-
Constructor Summary
Constructors Constructor Description XPathParser(javax.xml.transform.ErrorListener errorListener)The parser constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanAbbreviatedNodeTestStep(boolean isLeadingSlashPermitted)AbbreviatedNodeTestStep ::= '@'? NodeTest Predicateprotected intAdditiveExpr(int addPos)This has to handle construction of the operations so that they are evaluated in pre-fix order.protected voidAndExpr()AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr(package private) voidappendOp(int length, int op)Insert room for operation.protected voidArgument()Argument ::= Exprprotected intAxisName()Basis ::= AxisName '::' NodeTest | AbbreviatedBasisprotected voidBasis()Basis ::= AxisName '::' NodeTest | AbbreviatedBasisprotected voidBooleanExpr()StringExpr ::= Exprprivate voidconsumeExpected(char expected)Consume an expected token, throwing an exception if it isn't there.protected java.lang.StringdumpRemainingTokenQueue()Dump the remaining token queue.protected intEqualityExpr(int addPos)EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr(package private) voiderror(java.lang.String msg, java.lang.Object[] args)Notify the user of an error, and probably throw an exception.protected voidExpr()Expr ::= OrExprprotected intFilterExpr()FilterExpr ::= PrimaryExpr | FilterExpr Predicateprotected booleanFunctionCall()FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'javax.xml.transform.ErrorListenergetErrorListener()Return the current error listener.(package private) intgetFunctionToken(java.lang.String key)Given a string, return the corresponding function token.protected voidIdKeyPattern()IdKeyPattern ::= 'id' '(' Literal ')' | 'key' '(' Literal ',' Literal ')' (Also handle doc())voidinitMatchPattern(Compiler compiler, java.lang.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.voidinitXPath(Compiler compiler, java.lang.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) voidinsertOp(int pos, int length, int op)Insert room for operation.protected voidLiteral()The value of the Literal is the sequence of characters inside the " or ' characters.protected voidLocationPath()LocationPath ::= RelativeLocationPath | AbsoluteLocationPathprotected voidLocationPathPattern()LocationPathPattern ::= '/' RelativePathPattern? | IdKeyPattern (('/' | '//') RelativePathPattern)? | '//'? RelativePathPattern(package private) booleanlookahead(char c, int n)Look ahead of the current token in order to make a branching decision.private booleanlookahead(java.lang.String s, int n)Look ahead of the current token in order to make a branching decision.protected intMultiplicativeExpr(int addPos)This has to handle construction of the operations so that they are evaluated in pre-fix order.protected voidNCName()NCName ::= (Letter | '_') (NCNameChar) NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extenderprivate voidnextToken()Retrieve the next token from the command and store it in m_token string.protected voidNodeTest(int axesType)NodeTest ::= WildcardName | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'protected voidNumber()Number ::= [0-9]+('.'[0-9]+)? | '.'[0-9]+protected voidNumberExpr()NumberExpr ::= Exprprotected voidOrExpr()OrExpr ::= AndExpr | OrExpr 'or' AndExprprotected voidPathExpr()PathExpr ::= LocationPath | FilterExpr | FilterExpr '/' RelativeLocationPath | FilterExpr '//' RelativeLocationPathprotected voidPattern()Pattern ::= LocationPathPattern | Pattern '|' LocationPathPatternprotected voidPredicate()Predicate ::= '[' PredicateExpr ']'protected voidPredicateExpr()PredicateExpr ::= Exprprotected booleanPrimaryExpr()PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCallprotected voidQName()QName ::= (Prefix ':')? LocalPart Prefix ::= NCName LocalPart ::= NCNameprotected intRelationalExpr(int addPos)RelationalExpr ::= AdditiveExpr | RelationalExpr '<' AdditiveExpr | RelationalExpr '>' AdditiveExpr | RelationalExpr '<=' AdditiveExpr | RelationalExpr '>=' AdditiveExprprotected booleanRelativeLocationPath()RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPathprotected voidRelativePathPattern()RelativePathPattern ::= StepPattern | RelativePathPattern '/' StepPattern | RelativePathPattern '//' StepPatternvoidsetErrorHandler(javax.xml.transform.ErrorListener handler)Allow an application to register an error event handler, where syntax errors will be sent.protected booleanStep()Step ::= Basis Predicate | AbbreviatedStepprotected booleanStepPattern(boolean isLeadingSlashPermitted)StepPattern ::= AbbreviatedNodeTestStepprotected voidStringExpr()StringExpr ::= Expr(package private) booleantokenIs(char c)Check whether m_tokenChar==c.(package private) booleantokenIs(java.lang.String s)Check whether m_token matches the target string.protected voidUnaryExpr()UnaryExpr ::= UnionExpr | '-' UnaryExprprotected voidUnionExpr()The context of the right hand side expressions is the context of the left hand side expression.
-
-
-
Field Detail
-
CONTINUE_AFTER_FATAL_ERROR
public static final java.lang.String CONTINUE_AFTER_FATAL_ERROR
- See Also:
- Constant Field Values
-
m_ops
private OpMap m_ops
The XPath to be processed.
-
m_token
transient java.lang.String m_token
The next token in the pattern.
-
m_tokenChar
transient char m_tokenChar
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.
-
m_queueMark
int m_queueMark
The position in the token queue is tracked by m_queueMark.
-
FILTER_MATCH_FAILED
protected static final int FILTER_MATCH_FAILED
Results from checking FilterExpr syntax- See Also:
- Constant Field Values
-
FILTER_MATCH_PRIMARY
protected static final int FILTER_MATCH_PRIMARY
- See Also:
- Constant Field Values
-
FILTER_MATCH_PREDICATES
protected static final int FILTER_MATCH_PREDICATES
- See Also:
- Constant Field Values
-
m_namespaceContext
PrefixResolver m_namespaceContext
The prefix resolver to map prefixes to namespaces in the OpMap.
-
m_errorListener
private javax.xml.transform.ErrorListener m_errorListener
The error listener where syntax errors are to be sent.
-
m_functionTable
private FunctionTable m_functionTable
The table contains build-in functions and customized functions
-
-
Method Detail
-
initXPath
public void initXPath(Compiler compiler, java.lang.String expression, PrefixResolver namespaceContext) throws javax.xml.transform.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:
javax.xml.transform.TransformerException- in case of error
-
initMatchPattern
public void initMatchPattern(Compiler compiler, java.lang.String expression, PrefixResolver namespaceContext) throws javax.xml.transform.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:
javax.xml.transform.TransformerException- in case of error
-
setErrorHandler
public void setErrorHandler(javax.xml.transform.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 javax.xml.transform.ErrorListener getErrorListener()
Return the current error listener.- Returns:
- The error listener, which should not normally be null, but may be.
-
tokenIs
final boolean tokenIs(java.lang.String s)
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
private boolean lookahead(java.lang.String s, int n)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
private void consumeExpected(char expected) throws javax.xml.transform.TransformerExceptionConsume an expected token, throwing an exception if it isn't there.- Parameters:
expected- the character to be expected.- Throws:
javax.xml.transform.TransformerException- in case of error
-
error
void error(java.lang.String msg, java.lang.Object[] args) throws javax.xml.transform.TransformerExceptionNotify 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:
javax.xml.transform.TransformerException- if the current ErrorListoner determines to throw an exception.
-
dumpRemainingTokenQueue
protected java.lang.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.
-
getFunctionToken
final int getFunctionToken(java.lang.String key)
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
protected void Expr() throws javax.xml.transform.TransformerExceptionExpr ::= OrExpr- Throws:
javax.xml.transform.TransformerException- in case of error
-
OrExpr
protected void OrExpr() throws javax.xml.transform.TransformerExceptionOrExpr ::= AndExpr | OrExpr 'or' AndExpr- Throws:
javax.xml.transform.TransformerException- in case of error
-
AndExpr
protected void AndExpr() throws javax.xml.transform.TransformerExceptionAndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr- Throws:
javax.xml.transform.TransformerException- in case of error
-
EqualityExpr
protected int EqualityExpr(int addPos) throws javax.xml.transform.TransformerExceptionEqualityExpr ::= 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:
javax.xml.transform.TransformerException- in case of error
-
RelationalExpr
protected int RelationalExpr(int addPos) throws javax.xml.transform.TransformerExceptionRelationalExpr ::= 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:
javax.xml.transform.TransformerException- in case of error
-
AdditiveExpr
protected int AdditiveExpr(int addPos) throws javax.xml.transform.TransformerExceptionThis 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:
javax.xml.transform.TransformerException- in case of error
-
MultiplicativeExpr
protected int MultiplicativeExpr(int addPos) throws javax.xml.transform.TransformerExceptionThis 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:
javax.xml.transform.TransformerException- in case of error
-
UnaryExpr
protected void UnaryExpr() throws javax.xml.transform.TransformerExceptionUnaryExpr ::= UnionExpr | '-' UnaryExpr- Throws:
javax.xml.transform.TransformerException- in case of error
-
StringExpr
protected void StringExpr() throws javax.xml.transform.TransformerExceptionStringExpr ::= Expr- Throws:
javax.xml.transform.TransformerException- in case of error
-
BooleanExpr
protected void BooleanExpr() throws javax.xml.transform.TransformerExceptionStringExpr ::= Expr- Throws:
javax.xml.transform.TransformerException- in case of error
-
NumberExpr
protected void NumberExpr() throws javax.xml.transform.TransformerExceptionNumberExpr ::= Expr- Throws:
javax.xml.transform.TransformerException- in case of error
-
UnionExpr
protected void UnionExpr() throws javax.xml.transform.TransformerExceptionThe 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:
javax.xml.transform.TransformerException- in case of error
-
PathExpr
protected void PathExpr() throws javax.xml.transform.TransformerExceptionPathExpr ::= LocationPath | FilterExpr | FilterExpr '/' RelativeLocationPath | FilterExpr '//' RelativeLocationPath- Throws:
javax.xml.transform.TransformerException- in case of error
-
FilterExpr
protected int FilterExpr() throws javax.xml.transform.TransformerExceptionFilterExpr ::= 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:
javax.xml.transform.TransformerException- in case of error
-
PrimaryExpr
protected boolean PrimaryExpr() throws javax.xml.transform.TransformerExceptionPrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall- Returns:
- true if this method successfully matched a PrimaryExpr
- Throws:
javax.xml.transform.TransformerException- in case of error
-
Argument
protected void Argument() throws javax.xml.transform.TransformerExceptionArgument ::= Expr- Throws:
javax.xml.transform.TransformerException- in case of error
-
FunctionCall
protected boolean FunctionCall() throws javax.xml.transform.TransformerExceptionFunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'- Returns:
- true if, and only if, a FunctionCall was matched
- Throws:
javax.xml.transform.TransformerException- in case of error
-
LocationPath
protected void LocationPath() throws javax.xml.transform.TransformerExceptionLocationPath ::= RelativeLocationPath | AbsoluteLocationPath- Throws:
javax.xml.transform.TransformerException- in case of error
-
RelativeLocationPath
protected boolean RelativeLocationPath() throws javax.xml.transform.TransformerExceptionRelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath- Returns:
- true if, and only if, a RelativeLocationPath was matched
- Throws:
javax.xml.transform.TransformerException- in case of error
-
Step
protected boolean Step() throws javax.xml.transform.TransformerExceptionStep ::= Basis Predicate | AbbreviatedStep- Returns:
- false if step was empty (or only a '/'); true, otherwise
- Throws:
javax.xml.transform.TransformerException- in case of error
-
Basis
protected void Basis() throws javax.xml.transform.TransformerExceptionBasis ::= AxisName '::' NodeTest | AbbreviatedBasis- Throws:
javax.xml.transform.TransformerException- in case of error
-
AxisName
protected int AxisName() throws javax.xml.transform.TransformerExceptionBasis ::= AxisName '::' NodeTest | AbbreviatedBasis- Returns:
- FROM_XXX axes type, found in
Keywords. - Throws:
javax.xml.transform.TransformerException- in case of error
-
NodeTest
protected void NodeTest(int axesType) throws javax.xml.transform.TransformerExceptionNodeTest ::= WildcardName | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'- Parameters:
axesType- FROM_XXX axes type, found inKeywords.- Throws:
javax.xml.transform.TransformerException- in case of error
-
Predicate
protected void Predicate() throws javax.xml.transform.TransformerExceptionPredicate ::= '[' PredicateExpr ']'- Throws:
javax.xml.transform.TransformerException- in case of error
-
PredicateExpr
protected void PredicateExpr() throws javax.xml.transform.TransformerExceptionPredicateExpr ::= Expr- Throws:
javax.xml.transform.TransformerException- in case of error
-
QName
protected void QName() throws javax.xml.transform.TransformerExceptionQName ::= (Prefix ':')? LocalPart Prefix ::= NCName LocalPart ::= NCName- Throws:
javax.xml.transform.TransformerException- in case of error
-
NCName
protected void NCName()
NCName ::= (Letter | '_') (NCNameChar) NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender
-
Literal
protected void Literal() throws javax.xml.transform.TransformerExceptionThe value of the Literal is the sequence of characters inside the " or ' characters.Literal ::= '"' [^"]* '"' | "'" [^']* "'"
- Throws:
javax.xml.transform.TransformerException- in case of error
-
Number
protected void Number() throws javax.xml.transform.TransformerExceptionNumber ::= [0-9]+('.'[0-9]+)? | '.'[0-9]+- Throws:
javax.xml.transform.TransformerException- in case of error
-
Pattern
protected void Pattern() throws javax.xml.transform.TransformerExceptionPattern ::= LocationPathPattern | Pattern '|' LocationPathPattern- Throws:
javax.xml.transform.TransformerException- in case of error
-
LocationPathPattern
protected void LocationPathPattern() throws javax.xml.transform.TransformerExceptionLocationPathPattern ::= '/' RelativePathPattern? | IdKeyPattern (('/' | '//') RelativePathPattern)? | '//'? RelativePathPattern- Throws:
javax.xml.transform.TransformerException- in case of error
-
IdKeyPattern
protected void IdKeyPattern() throws javax.xml.transform.TransformerExceptionIdKeyPattern ::= 'id' '(' Literal ')' | 'key' '(' Literal ',' Literal ')' (Also handle doc())- Throws:
javax.xml.transform.TransformerException- in case of error
-
RelativePathPattern
protected void RelativePathPattern() throws javax.xml.transform.TransformerExceptionRelativePathPattern ::= StepPattern | RelativePathPattern '/' StepPattern | RelativePathPattern '//' StepPattern- Throws:
javax.xml.transform.TransformerException- in case of error
-
StepPattern
protected boolean StepPattern(boolean isLeadingSlashPermitted) throws javax.xml.transform.TransformerExceptionStepPattern ::= 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:
javax.xml.transform.TransformerException- in case of error
-
AbbreviatedNodeTestStep
protected boolean AbbreviatedNodeTestStep(boolean isLeadingSlashPermitted) throws javax.xml.transform.TransformerExceptionAbbreviatedNodeTestStep ::= '@'? 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:
javax.xml.transform.TransformerException- in case of error
-
-