Package org.htmlunit.xpath
Class Expression
- java.lang.Object
-
- org.htmlunit.xpath.Expression
-
- All Implemented Interfaces:
javax.xml.transform.SourceLocator
,ExpressionNode
,XPathVisitable
- Direct Known Subclasses:
Function
,NodeTest
,Operation
,UnaryOperation
,UnionPattern
,XObject
public abstract class Expression extends java.lang.Object implements ExpressionNode, XPathVisitable
This abstract class serves as the base for all expression objects. An Expression can be executed to return aXObject
, normally has a location within a document or DOM, can send error and warning events, and normally do not hold state and are meant to be immutable once construction has completed. An exception to the immutibility rule is iterators and walkers, which must be cloned in order to be used -- the original must still be immutable.
-
-
Field Summary
Fields Modifier and Type Field Description private ExpressionNode
m_parent
The location where this expression was built from.
-
Constructor Summary
Constructors Constructor Description Expression()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description DTMIterator
asIterator(XPathContext xctxt, int contextNode)
Given an select expression and a context, evaluate the XPath and return the resulting iterator.int
asNode(XPathContext xctxt)
Return the first node out of the nodeset, if this expression is a nodeset expression.void
assertion(boolean b, java.lang.String msg)
Tell the user of an assertion error, and probably throw an exception.boolean
bool(XPathContext xctxt)
Evaluate expression to a boolean.boolean
canTraverseOutsideSubtree()
Tell if this expression or it's subexpressions can traverse outside the current subtree.abstract boolean
deepEquals(Expression expr)
Compare this object with another object and see if they are equal, include the sub heararchy.void
error(XPathContext xctxt, java.lang.String msg, java.lang.Object[] args)
Tell the user of an error, and probably throw an exception.abstract XObject
execute(XPathContext xctxt)
Execute an expression in the XPath runtime context, and return the result of the expression.XObject
execute(XPathContext xctxt, boolean destructiveOK)
Execute an expression in the XPath runtime context, and return the result of the expression, but tell that a "safe" object doesn't have to be returned.XObject
execute(XPathContext xctxt, int currentNode)
Execute an expression in the XPath runtime context, and return the result of the expression.XObject
execute(XPathContext xctxt, int currentNode, DTM dtm, int expType)
Execute an expression in the XPath runtime context, and return the result of the expression.ExpressionNode
exprGetParent()
void
exprSetParent(ExpressionNode n)
This pair of methods are used to inform the node of its parent.int
getColumnNumber()
ExpressionNode
getExpressionOwner()
Get the first non-Expression parent of this node.int
getLineNumber()
java.lang.String
getPublicId()
java.lang.String
getSystemId()
protected boolean
isSameClass(Expression expr)
This is a utility method to tell if the passed in class is the same class as this.boolean
isStableNumber()
Tell if this expression returns a stable number that will not change during iterations within the expression.double
num(XPathContext xctxt)
Evaluate expression to a number.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.htmlunit.xpath.XPathVisitable
callVisitors
-
-
-
-
Field Detail
-
m_parent
private ExpressionNode m_parent
The location where this expression was built from. Need for diagnostic messages. May be null.
-
-
Method Detail
-
canTraverseOutsideSubtree
public boolean canTraverseOutsideSubtree()
Tell if this expression or it's subexpressions can traverse outside the current subtree.- Returns:
- true if traversal outside the context node's subtree can occur.
-
execute
public XObject execute(XPathContext xctxt, int currentNode) throws javax.xml.transform.TransformerException
Execute an expression in the XPath runtime context, and return the result of the expression.- Parameters:
xctxt
- The XPath runtime context.currentNode
- The currentNode.- Returns:
- The result of the expression in the form of a
XObject
. - Throws:
javax.xml.transform.TransformerException
- if a runtime exception occurs.
-
execute
public XObject execute(XPathContext xctxt, int currentNode, DTM dtm, int expType) throws javax.xml.transform.TransformerException
Execute an expression in the XPath runtime context, and return the result of the expression.- Parameters:
xctxt
- The XPath runtime context.currentNode
- The currentNode.dtm
- The DTM of the current node.expType
- The expanded type ID of the current node.- Returns:
- The result of the expression in the form of a
XObject
. - Throws:
javax.xml.transform.TransformerException
- if a runtime exception occurs.
-
execute
public abstract XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
Execute an expression in the XPath runtime context, and return the result of the expression.- Parameters:
xctxt
- The XPath runtime context.- Returns:
- The result of the expression in the form of a
XObject
. - Throws:
javax.xml.transform.TransformerException
- if a runtime exception occurs.
-
execute
public XObject execute(XPathContext xctxt, boolean destructiveOK) throws javax.xml.transform.TransformerException
Execute an expression in the XPath runtime context, and return the result of the expression, but tell that a "safe" object doesn't have to be returned. The default implementation just calls execute(xctxt).- Parameters:
xctxt
- The XPath runtime context.destructiveOK
- true if a "safe" object doesn't need to be returned.- Returns:
- The result of the expression in the form of a
XObject
. - Throws:
javax.xml.transform.TransformerException
- if a runtime exception occurs.
-
num
public double num(XPathContext xctxt) throws javax.xml.transform.TransformerException
Evaluate expression to a number.- Parameters:
xctxt
- The XPath runtime context.- Returns:
- The expression evaluated as a double.
- Throws:
javax.xml.transform.TransformerException
- if any
-
bool
public boolean bool(XPathContext xctxt) throws javax.xml.transform.TransformerException
Evaluate expression to a boolean.- Parameters:
xctxt
- The XPath runtime context.- Returns:
- false
- Throws:
javax.xml.transform.TransformerException
- if any
-
asNode
public int asNode(XPathContext xctxt) throws javax.xml.transform.TransformerException
Return the first node out of the nodeset, if this expression is a nodeset expression.- Parameters:
xctxt
- The XPath runtime context.- Returns:
- the first node out of the nodeset, or DTM.NULL.
- Throws:
javax.xml.transform.TransformerException
- if any
-
asIterator
public DTMIterator asIterator(XPathContext xctxt, int contextNode) throws javax.xml.transform.TransformerException
Given an select expression and a context, evaluate the XPath and return the resulting iterator.- Parameters:
xctxt
- The execution context.contextNode
- The node that "." expresses.- Returns:
- A valid DTMIterator.
- Throws:
javax.xml.transform.TransformerException
- thrown if the active ProblemListener decides the error condition is severe enough to halt processing.javax.xml.transform.TransformerException
- if any
-
isStableNumber
public boolean isStableNumber()
Tell if this expression returns a stable number that will not change during iterations within the expression. This is used to determine if a proximity position predicate can indicate that no more searching has to occur.- Returns:
- true if the expression represents a stable number.
-
deepEquals
public abstract boolean deepEquals(Expression expr)
Compare this object with another object and see if they are equal, include the sub heararchy.- Parameters:
expr
- Another expression object.- Returns:
- true if this objects class and the expr object's class are the same, and the data contained within both objects are considered equal.
-
isSameClass
protected final boolean isSameClass(Expression expr)
This is a utility method to tell if the passed in class is the same class as this. It is to be used by the deepEquals method. I'm bottlenecking it here because I'm not totally confident that comparing the class objects is the best way to do this.- Returns:
- true of the passed in class is the exact same class as this class.
-
assertion
public void assertion(boolean b, java.lang.String msg)
Tell the user of an assertion error, and probably throw an exception.- Parameters:
b
- If false, a runtime exception will be thrown.msg
- The assertion message, which should be informative.- Throws:
java.lang.RuntimeException
- if the b argument is false.
-
error
public void error(XPathContext xctxt, java.lang.String msg, java.lang.Object[] args) throws javax.xml.transform.TransformerException
Tell the user of an error, and probably throw an exception.- Parameters:
xctxt
- The XPath runtime context.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.javax.xml.transform.TransformerException
- if any
-
getExpressionOwner
public ExpressionNode getExpressionOwner()
Get the first non-Expression parent of this node.- Returns:
- null or first ancestor that is not an Expression.
-
exprSetParent
public void exprSetParent(ExpressionNode n)
This pair of methods are used to inform the node of its parent.- Specified by:
exprSetParent
in interfaceExpressionNode
-
exprGetParent
public ExpressionNode exprGetParent()
- Specified by:
exprGetParent
in interfaceExpressionNode
-
getPublicId
public java.lang.String getPublicId()
- Specified by:
getPublicId
in interfacejavax.xml.transform.SourceLocator
-
getSystemId
public java.lang.String getSystemId()
- Specified by:
getSystemId
in interfacejavax.xml.transform.SourceLocator
-
getLineNumber
public int getLineNumber()
- Specified by:
getLineNumber
in interfacejavax.xml.transform.SourceLocator
-
getColumnNumber
public int getColumnNumber()
- Specified by:
getColumnNumber
in interfacejavax.xml.transform.SourceLocator
-
-