Package org.htmlunit.xpath.compiler
Class Compiler
- java.lang.Object
-
- org.htmlunit.xpath.compiler.OpMap
-
- org.htmlunit.xpath.compiler.Compiler
-
public class Compiler extends OpMap
An instance of this class compiles an XPath string expression into a Expression object. This class compiles the string into a sequence of operation codes (op map) and then builds from that into an Expression tree.
-
-
Field Summary
Fields Modifier and Type Field Description private static booleanDEBUGprivate intlocPathDepthprivate PrefixResolverm_currentPrefixResolverThe current prefixResolver for the execution context.(package private) javax.xml.transform.ErrorListenerm_errorHandlerThe error listener where errors will be sent.private FunctionTablem_functionTableThe FunctionTable for all xpath build-in functions-
Fields inherited from class org.htmlunit.xpath.compiler.OpMap
BLOCKTOKENQUEUESIZE, m_currentPattern, m_opMap, m_tokenQueue, MAPINDEX_LENGTH, MAXTOKENQUEUESIZE
-
-
Constructor Summary
Constructors Constructor Description Compiler(javax.xml.transform.ErrorListener errorHandler, FunctionTable fTable)Construct a Compiler object with a specific ErrorListener.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Expressionand(int opPos)Compile an 'and' operation.protected Expressionarg(int opPos)Compile a function argument.protected Expressionbool(int opPos)Compile a 'boolean(...)' operation.Expressioncompile(int opPos)Execute the XPath object from a given opcode position.(package private) ExpressioncompileFunction(int opPos)Compile a built-in XPath function.private ExpressioncompileOperation(Operation operation, int opPos)Bottle-neck compilation of an operation with left and right operands.private voidcompilePredicates(int opPos, Expression[] predicates)Compiles predicates in the step.private ExpressioncompileUnary(UnaryOperation unary, int opPos)Bottle-neck compilation of a unary operation.intcountPredicates(int opPos)Count the number of predicates in the step.protected Expressiondiv(int opPos)Compile a 'div' operation.protected Expressionequals(int opPos)Compile a '=' operation.voiderror(java.lang.String msg, java.lang.Object[] args)Tell the user of an error, and probably throw an exception.Expression[]getCompiledPredicates(int opPos)Compile a zero or more predicates for a given match pattern.(package private) FunctionTablegetFunctionTable()Get the function tableintgetLocationPathDepth()Get the level of the location path or union being constructed.PrefixResolvergetNamespaceContext()Get the current namespace context for the xpath.intgetWhatToShow(int opPos)Get aNodeFilterbit set that tells what to show for a given node test.protected Expressiongroup(int opPos)Compile an expression group.protected Expressiongt(int opPos)Compile a '&gr;' operation.protected Expressiongte(int opPos)Compile a '>=' operation.protected Expressionliteral(int opPos)Compile a literal string value.ExpressionlocationPath(int opPos)Compile a location path.ExpressionlocationPathPattern(int opPos)Compile a location match pattern unit expression.protected Expressionlt(int opPos)Compile a '<' operation.protected Expressionlte(int opPos)Compile a '<=' operation.protected ExpressionmatchPattern(int opPos)Compile an entire match pattern expression.protected Expressionminus(int opPos)Compile a '-' operation.protected Expressionmod(int opPos)Compile a 'mod' operation.protected Expressionmult(int opPos)Compile a '*' operation.protected Expressionneg(int opPos)Compile a unary '-' operation.protected Expressionnotequals(int opPos)Compile a '!=' operation.protected Expressionnumber(int opPos)Compile a 'number(...)' operation.protected Expressionnumberlit(int opPos)Compile a literal number value.protected Expressionor(int opPos)Compile an 'or' operation.protected Expressionplus(int opPos)Compile a '+' operation.Expressionpredicate(int opPos)Compile a location step predicate expression.voidsetNamespaceContext(PrefixResolver pr)Set the current namespace context for the xpath.protected StepPatternstepPattern(int opPos, int stepCount, StepPattern ancestorPattern)Compile a step pattern unit expression, used for both location paths and match patterns.protected Expressionstring(int opPos)Compile a 'string(...)' operation.protected Expressionunion(int opPos)Compile a location path union.-
Methods inherited from class org.htmlunit.xpath.compiler.OpMap
getArgLengthOfStep, getFirstChildPos, getFirstChildPosOfStep, getFirstPredicateOpPos, getNextOpPos, getNextStepPos, getOp, getPatternString, getStepLocalName, getStepNS, getStepTestType, getTokenQueue, getTokenQueueSize, setOp, shrink, toString
-
-
-
-
Field Detail
-
locPathDepth
private int locPathDepth
-
DEBUG
private static final boolean DEBUG
- See Also:
- Constant Field Values
-
m_currentPrefixResolver
private PrefixResolver m_currentPrefixResolver
The current prefixResolver for the execution context.
-
m_errorHandler
final javax.xml.transform.ErrorListener m_errorHandler
The error listener where errors will be sent. If this is null, errors and warnings will be sent to System.err. May be null.
-
m_functionTable
private final FunctionTable m_functionTable
The FunctionTable for all xpath build-in functions
-
-
Constructor Detail
-
Compiler
public Compiler(javax.xml.transform.ErrorListener errorHandler, FunctionTable fTable)Construct a Compiler object with a specific ErrorListener.- Parameters:
errorHandler- Error listener where messages will be sent, or null if messages should be sent to System err.fTable- The FunctionTable object where the xpath build-in functions are stored.
-
-
Method Detail
-
compile
public Expression compile(int opPos) throws javax.xml.transform.TransformerException
Execute the XPath object from a given opcode position.- Parameters:
opPos- The current position in the xpath.m_opMap array.- Returns:
- The result of the XPath.
- Throws:
javax.xml.transform.TransformerException- if there is a syntax or other error.
-
compileOperation
private Expression compileOperation(Operation operation, int opPos) throws javax.xml.transform.TransformerException
Bottle-neck compilation of an operation with left and right operands.- Parameters:
operation- non-null reference to parent operation.opPos- The op map position of the parent operation.- Returns:
- reference to
Operationinstance. - Throws:
javax.xml.transform.TransformerException- if there is a syntax or other error.
-
compileUnary
private Expression compileUnary(UnaryOperation unary, int opPos) throws javax.xml.transform.TransformerException
Bottle-neck compilation of a unary operation.- Parameters:
unary- The parent unary operation.opPos- The position in the op map of the parent operation.- Returns:
- The unary argument.
- Throws:
javax.xml.transform.TransformerException- if syntax or other error occurs.
-
or
protected Expression or(int opPos) throws javax.xml.transform.TransformerException
Compile an 'or' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Orinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
and
protected Expression and(int opPos) throws javax.xml.transform.TransformerException
Compile an 'and' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Andinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
notequals
protected Expression notequals(int opPos) throws javax.xml.transform.TransformerException
Compile a '!=' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
NotEqualsinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
equals
protected Expression equals(int opPos) throws javax.xml.transform.TransformerException
Compile a '=' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Equalsinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
lte
protected Expression lte(int opPos) throws javax.xml.transform.TransformerException
Compile a '<=' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Lteinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
lt
protected Expression lt(int opPos) throws javax.xml.transform.TransformerException
Compile a '<' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Ltinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
gte
protected Expression gte(int opPos) throws javax.xml.transform.TransformerException
Compile a '>=' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Gteinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
gt
protected Expression gt(int opPos) throws javax.xml.transform.TransformerException
Compile a '&gr;' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Gtinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
plus
protected Expression plus(int opPos) throws javax.xml.transform.TransformerException
Compile a '+' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Plusinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
minus
protected Expression minus(int opPos) throws javax.xml.transform.TransformerException
Compile a '-' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Minusinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
mult
protected Expression mult(int opPos) throws javax.xml.transform.TransformerException
Compile a '*' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Multinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
div
protected Expression div(int opPos) throws javax.xml.transform.TransformerException
Compile a 'div' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Divinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
mod
protected Expression mod(int opPos) throws javax.xml.transform.TransformerException
Compile a 'mod' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Modinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
neg
protected Expression neg(int opPos) throws javax.xml.transform.TransformerException
Compile a unary '-' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Neginstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
string
protected Expression string(int opPos) throws javax.xml.transform.TransformerException
Compile a 'string(...)' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Stringinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
bool
protected Expression bool(int opPos) throws javax.xml.transform.TransformerException
Compile a 'boolean(...)' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Boolinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
number
protected Expression number(int opPos) throws javax.xml.transform.TransformerException
Compile a 'number(...)' operation.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Numberinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
literal
protected Expression literal(int opPos)
Compile a literal string value.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
XStringinstance.
-
numberlit
protected Expression numberlit(int opPos)
Compile a literal number value.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
XNumberinstance.
-
group
protected Expression group(int opPos) throws javax.xml.transform.TransformerException
Compile an expression group.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to the contained expression.
- Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
arg
protected Expression arg(int opPos) throws javax.xml.transform.TransformerException
Compile a function argument.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to the argument expression.
- Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
union
protected Expression union(int opPos) throws javax.xml.transform.TransformerException
Compile a location path union. The UnionPathIterator itself may createLocPathIteratorchildren.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
LocPathIteratorinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
getLocationPathDepth
public int getLocationPathDepth()
Get the level of the location path or union being constructed.- Returns:
- 0 if it is a top-level path.
-
getFunctionTable
FunctionTable getFunctionTable()
Get the function table
-
locationPath
public Expression locationPath(int opPos) throws javax.xml.transform.TransformerException
Compile a location path. The LocPathIterator itself may createAxesWalkerchildren.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
LocPathIteratorinstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
predicate
public Expression predicate(int opPos) throws javax.xml.transform.TransformerException
Compile a location step predicate expression.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- the contained predicate expression.
- Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
matchPattern
protected Expression matchPattern(int opPos) throws javax.xml.transform.TransformerException
Compile an entire match pattern expression.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
UnionPatterninstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
locationPathPattern
public Expression locationPathPattern(int opPos) throws javax.xml.transform.TransformerException
Compile a location match pattern unit expression.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
StepPatterninstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
getWhatToShow
public int getWhatToShow(int opPos)
Get aNodeFilterbit set that tells what to show for a given node test.- Parameters:
opPos- the op map position for the location step.- Returns:
NodeFilterbit set that tells what to show for a given node test.
-
stepPattern
protected StepPattern stepPattern(int opPos, int stepCount, StepPattern ancestorPattern) throws javax.xml.transform.TransformerException
Compile a step pattern unit expression, used for both location paths and match patterns.- Parameters:
opPos- The current position in the m_opMap array.stepCount- The number of steps to expect.ancestorPattern- The owning StepPattern, which may be null.- Returns:
- reference to
StepPatterninstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
getCompiledPredicates
public Expression[] getCompiledPredicates(int opPos) throws javax.xml.transform.TransformerException
Compile a zero or more predicates for a given match pattern.- Parameters:
opPos- The position of the first predicate the m_opMap array.- Returns:
- reference to array of
Expressioninstances. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
countPredicates
public int countPredicates(int opPos) throws javax.xml.transform.TransformerExceptionCount the number of predicates in the step.- Parameters:
opPos- The position of the first predicate the m_opMap array.- Returns:
- The number of predicates for this step.
- Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
compilePredicates
private void compilePredicates(int opPos, Expression[] predicates) throws javax.xml.transform.TransformerExceptionCompiles predicates in the step.- Parameters:
opPos- The position of the first predicate the m_opMap array.predicates- An empty pre-determined array ofExpressions, that will be filled in.- Throws:
javax.xml.transform.TransformerException- if any
-
compileFunction
Expression compileFunction(int opPos) throws javax.xml.transform.TransformerException
Compile a built-in XPath function.- Parameters:
opPos- The current position in the m_opMap array.- Returns:
- reference to
Functioninstance. - Throws:
javax.xml.transform.TransformerException- if a error occurs creating the Expression.
-
error
public void error(java.lang.String msg, java.lang.Object[] args) throws javax.xml.transform.TransformerExceptionTell the user of an error, and probably throw an exception.- Overrides:
errorin classOpMap- 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.
-
getNamespaceContext
public PrefixResolver getNamespaceContext()
Get the current namespace context for the xpath.- Returns:
- The current prefix resolver, *may* be null, though hopefully not.
-
setNamespaceContext
public void setNamespaceContext(PrefixResolver pr)
Set the current namespace context for the xpath.- Parameters:
pr- The resolver for prefixes in the XPath expression.
-
-