Package org.htmlcleaner
Class XPather
- java.lang.Object
-
- org.htmlcleaner.XPather
-
public class XPather extends java.lang.ObjectUtility for searching cleaned document tree with XPath expressions.
Examples of supported axes:- //div//a
- //div//a[@id][@class]
- /body/*[1]/@type
- //div[3]//a[@id][@href='r/n4']
- //div[last() >= 4]//./div[position() = last()])[position() > 22]//li[2]//a
- //div[2]/@*[2]
- data(//div//a[@id][@class])
- //p/last()
- //body//div[3][@class]//span[12.2
- data(//a['v' < @id])
-
-
Constructor Summary
Constructors Constructor Description XPather(java.lang.String expression)Constructor - creates XPather instance with specified XPath expression.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.CollectionevaluateAgainst(java.util.Collection object, int from, int to, boolean isRecursive, int position, int last, boolean isFilterContext, java.util.Collection filterSource)java.lang.Object[]evaluateAgainstNode(TagNode node)Main public method for this class - a way to execute XPath expression against specified TagNode instance.protected java.util.CollectionevaluateFunction(java.util.Collection source, int from, int to, int position, int last, boolean isFilterContext)Evaluates specified function.protected booleanevaluateLogic(java.util.Collection first, java.util.Collection second, java.lang.String logicOperator)Evaluates logic operation on two collections.protected java.util.CollectionfilterByCondition(java.util.Collection source, int from, int to)Filter nodes satisfying the conditionprivate intfindClosingIndex(int from, int to)private java.lang.Stringflatten(int from, int to)private java.util.CollectiongetElementsByName(java.util.Collection source, int from, int to, boolean isRecursive, boolean isFilterContext)For the given source collection and specified name, returns collection of subnodes or attribute values.private booleanisAtt(java.lang.String token)Checks if token is attribute (starts with @)private booleanisFunctionCall(int from, int to)Checks if tokens in specified range represents valid function call.private booleanisIdentifier(java.lang.String s)Checks if given string is valid identifier.private booleanisToken(java.lang.String token, int index)private booleanisValidDouble(java.lang.String value)private static booleanisValidInteger(java.lang.String value)private java.util.Collectionsingleton(java.lang.Object element)Creates one-element collection for the specified object.private voidthrowStandardException()private java.lang.StringtoText(java.lang.Object o)
-
-
-
Field Detail
-
C0
private static final int C0
- See Also:
- Constant Field Values
-
C9
private static final int C9
- See Also:
- Constant Field Values
-
CD
private static final int CD
- See Also:
- Constant Field Values
-
CP
private static final int CP
- See Also:
- Constant Field Values
-
CM
private static final int CM
- See Also:
- Constant Field Values
-
CS
private static final int CS
- See Also:
- Constant Field Values
-
tokenArray
private java.lang.String[] tokenArray
-
-
Method Detail
-
evaluateAgainstNode
public java.lang.Object[] evaluateAgainstNode(TagNode node) throws XPatherException
Main public method for this class - a way to execute XPath expression against specified TagNode instance.- Parameters:
node-- Throws:
XPatherException
-
throwStandardException
private void throwStandardException() throws XPatherException- Throws:
XPatherException
-
evaluateAgainst
protected java.util.Collection evaluateAgainst(java.util.Collection object, int from, int to, boolean isRecursive, int position, int last, boolean isFilterContext, java.util.Collection filterSource) throws XPatherException- Throws:
XPatherException
-
flatten
private java.lang.String flatten(int from, int to)
-
isValidInteger
private static boolean isValidInteger(java.lang.String value)
-
isValidDouble
private boolean isValidDouble(java.lang.String value)
-
isIdentifier
private boolean isIdentifier(java.lang.String s)
Checks if given string is valid identifier.- Parameters:
s-
-
isFunctionCall
private boolean isFunctionCall(int from, int to)Checks if tokens in specified range represents valid function call.- Parameters:
from-to-- Returns:
- True if it is valid function call, false otherwise.
-
evaluateFunction
protected java.util.Collection evaluateFunction(java.util.Collection source, int from, int to, int position, int last, boolean isFilterContext) throws XPatherExceptionEvaluates specified function. Currently, following XPath functions are supported: last, position, text, count, data- Parameters:
source-from-to-position-last-- Returns:
- Collection as the result of evaluation.
- Throws:
XPatherException
-
filterByCondition
protected java.util.Collection filterByCondition(java.util.Collection source, int from, int to) throws XPatherExceptionFilter nodes satisfying the condition- Parameters:
source-from-to-- Throws:
XPatherException
-
isToken
private boolean isToken(java.lang.String token, int index)
-
findClosingIndex
private int findClosingIndex(int from, int to)- Parameters:
from-to-- Returns:
- matching closing index in the token array for the current token, or -1 if there is no closing token within expected bounds.
-
isAtt
private boolean isAtt(java.lang.String token)
Checks if token is attribute (starts with @)- Parameters:
token-
-
singleton
private java.util.Collection singleton(java.lang.Object element)
Creates one-element collection for the specified object.- Parameters:
element-
-
getElementsByName
private java.util.Collection getElementsByName(java.util.Collection source, int from, int to, boolean isRecursive, boolean isFilterContext) throws XPatherExceptionFor the given source collection and specified name, returns collection of subnodes or attribute values.- Parameters:
source-from-to-isRecursive-- Returns:
- Colection of TagNode instances or collection of String instances.
- Throws:
XPatherException
-
evaluateLogic
protected boolean evaluateLogic(java.util.Collection first, java.util.Collection second, java.lang.String logicOperator)Evaluates logic operation on two collections.- Parameters:
first-second-logicOperator-- Returns:
- Result of logic operation
-
toText
private java.lang.String toText(java.lang.Object o)
-
-