Package org.htmlcleaner
Class XPather
- java.lang.Object
-
- org.htmlcleaner.XPather
-
public class XPather extends java.lang.Object
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.2
- data(//a['v' < @id])
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String[]
tokenArray
-
Constructor Summary
Constructors Constructor Description XPather(java.lang.String expression)
Constructor - creates XPather instance with specified XPath expression.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.util.Collection
evaluateAgainst(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.private java.util.Collection
evaluateFunction(java.util.Collection source, int from, int to, int position, int last, boolean isFilterContext)
Evaluates specified function.private boolean
evaluateLogic(java.util.Collection first, java.util.Collection second, java.lang.String logicOperator)
Evaluates logic operation on two collections.private java.util.Collection
filterByCondition(java.util.Collection source, int from, int to)
Filter nodes satisfying the conditionprivate int
findClosingIndex(int from, int to)
private java.lang.String
flatten(int from, int to)
private java.util.Collection
getElementsByName(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 boolean
isAtt(java.lang.String token)
Checks if token is attribute (starts with @)private boolean
isFunctionCall(int from, int to)
Checks if tokens in specified range represents valid function call.private boolean
isIdentifier(java.lang.String s)
Checks if given string is valid identifier.private boolean
isToken(java.lang.String token, int index)
private boolean
isValidDouble(java.lang.String s)
private boolean
isValidInteger(java.lang.String s)
private java.util.Collection
singleton(java.lang.Object element)
Creates one-element collection for the specified object.private void
throwStandardException()
private java.lang.String
toText(java.lang.Object o)
-
-
-
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
private 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 boolean isValidInteger(java.lang.String s)
-
isValidDouble
private boolean isValidDouble(java.lang.String s)
-
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
private java.util.Collection evaluateFunction(java.util.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
private java.util.Collection filterByCondition(java.util.Collection source, int from, int to) throws XPatherException
Filter 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 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
private 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)
-
-