Package org.htmlcleaner
Class XPather
java.lang.Object
org.htmlcleaner.XPather
Utility 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.2invalid input: '<'position()]/@id
- data(//a['v' invalid input: '<' @id])
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected CollectionevaluateAgainst(Collection object, int from, int to, boolean isRecursive, int position, int last, boolean isFilterContext, Collection filterSource) Object[]evaluateAgainstNode(TagNode node) Main public method for this class - a way to execute XPath expression against specified TagNode instance.protected CollectionevaluateFunction(Collection source, int from, int to, int position, int last, boolean isFilterContext) Evaluates specified function.protected booleanevaluateLogic(Collection first, Collection second, String logicOperator) Evaluates logic operation on two collections.protected CollectionfilterByCondition(Collection source, int from, int to) Filter nodes satisfying the conditionprivate intfindClosingIndex(int from, int to) private Stringflatten(int from, int to) private CollectiongetElementsByName(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 booleanChecks if token is attribute (starts with @)private booleanisFunctionCall(int from, int to) Checks if tokens in specified range represents valid function call.private booleanChecks if given string is valid identifier.private booleanprivate booleanisValidDouble(String value) private static booleanisValidInteger(String value) private CollectionCreates one-element collection for the specified object.private voidprivate String
-
Field Details
-
C0
private static final int C0- See Also:
-
C9
private static final int C9- See Also:
-
CD
private static final int CD- See Also:
-
CP
private static final int CP- See Also:
-
CM
private static final int CM- See Also:
-
CS
private static final int CS- See Also:
-
tokenArray
-
-
Constructor Details
-
XPather
Constructor - creates XPather instance with specified XPath expression.- Parameters:
expression-
-
-
Method Details
-
evaluateAgainstNode
Main public method for this class - a way to execute XPath expression against specified TagNode instance.- Parameters:
node-- Throws:
XPatherException
-
throwStandardException
- Throws:
XPatherException
-
evaluateAgainst
protected Collection evaluateAgainst(Collection object, int from, int to, boolean isRecursive, int position, int last, boolean isFilterContext, Collection filterSource) throws XPatherException - Throws:
XPatherException
-
flatten
-
isValidInteger
-
isValidDouble
-
isIdentifier
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 Collection evaluateFunction(Collection source, int from, int to, int position, int last, boolean isFilterContext) throws XPatherException Evaluates 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
Filter nodes satisfying the condition- Parameters:
source-from-to-- Throws:
XPatherException
-
isToken
-
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
Checks if token is attribute (starts with @)- Parameters:
token-
-
singleton
Creates one-element collection for the specified object.- Parameters:
element-
-
getElementsByName
private Collection getElementsByName(Collection source, int from, int to, boolean isRecursive, boolean isFilterContext) throws XPatherException For 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
Evaluates logic operation on two collections.- Parameters:
first-second-logicOperator-- Returns:
- Result of logic operation
-
toText
-