Package com.crawljax.util
Class XPathHelper
- java.lang.Object
-
- com.crawljax.util.XPathHelper
-
public final class XPathHelper extends Object
Utility class that contains methods used by Crawljax and some plugin to deal with XPath resolving, constructing etc.
-
-
Field Summary
Fields Modifier and Type Field Description private static StringFULL_XPATH_CACHEprivate static PatternID_PATTERNprivate static org.slf4j.LoggerLOGprivate static intMAX_SEARCH_LOOPSprivate static StringSKEL_XPATH_CACHEprivate static PatternTAG_PATTERN
-
Constructor Summary
Constructors Modifier Constructor Description privateXPathHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static StringcapitalizeTagNames(String xpath)static NodeListevaluateXpathExpression(String domStr, String xpathExpr)Returns the list of nodes which match the expression xpathExpr in the String domStr.static NodeListevaluateXpathExpression(Document dom, String xpathExpr)Returns the list of nodes which match the expression xpathExpr in the Document dom.static StringformatXPath(String xpath)static NodegetBelongingNode(FormInput input, Document dom)static intgetCloseElementLocation(String dom, int pos, String element)static intgetCloseElementLocation(String dom, String xpath)static StringgetLastElementXPath(String xpath)static NodegetNodeFromSpecificParent(Node fragmentParentNode, String relativeXpath)static List<Node>getSiblings(Node parent, Node element)Get siblings of the same type as element from parent.static StringgetSkeletonXpath(Node node)static StringgetXPathExpression(Node node)static StringgetXPathExpression_other(Node node)Reverse Engineers an XPath Expression of a given Node in the DOM.static com.google.common.collect.ImmutableList<String>getXpathForXPathExpressions(Document dom, String xpathExpression)Returns the XPaths of all nodes retrieved by xpathExpression.static StringgetXPathFromSpecificParent(Node child, Node parent)get relative xpath from specific parentstatic intgetXPathLocation(String dom, String xpath)returns position of xpath element which match the expression xpath in the String dom.private static StringlowerCaseAttributes(String formatted)private static StringstripEndSquareBrackets(String string)static StringstripXPathToElement(String xpath)
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
TAG_PATTERN
private static final Pattern TAG_PATTERN
-
ID_PATTERN
private static final Pattern ID_PATTERN
-
FULL_XPATH_CACHE
private static final String FULL_XPATH_CACHE
- See Also:
- Constant Field Values
-
SKEL_XPATH_CACHE
private static final String SKEL_XPATH_CACHE
- See Also:
- Constant Field Values
-
MAX_SEARCH_LOOPS
private static final int MAX_SEARCH_LOOPS
- See Also:
- Constant Field Values
-
-
Method Detail
-
getXPathFromSpecificParent
public static String getXPathFromSpecificParent(Node child, Node parent)
get relative xpath from specific parent- Parameters:
child-parent-- Returns:
-
getNodeFromSpecificParent
public static Node getNodeFromSpecificParent(Node fragmentParentNode, String relativeXpath)
-
getXPathExpression_other
public static String getXPathExpression_other(Node node)
Reverse Engineers an XPath Expression of a given Node in the DOM.- Parameters:
node- the given node.- Returns:
- string xpath expression (e.g., "/html[1]/body[1]/div[3]").
-
getSiblings
public static List<Node> getSiblings(Node parent, Node element)
Get siblings of the same type as element from parent.- Parameters:
parent- parent node.element- element.- Returns:
- List of sibling (from element) under parent
-
evaluateXpathExpression
public static NodeList evaluateXpathExpression(String domStr, String xpathExpr) throws XPathExpressionException, IOException
Returns the list of nodes which match the expression xpathExpr in the String domStr.- Returns:
- the list of nodes which match the query
- Throws:
XPathExpressionExceptionIOException
-
evaluateXpathExpression
public static NodeList evaluateXpathExpression(Document dom, String xpathExpr) throws XPathExpressionException
Returns the list of nodes which match the expression xpathExpr in the Document dom.- Parameters:
dom- the Document to search inxpathExpr- the xpath query- Returns:
- the list of nodes which match the query
- Throws:
XPathExpressionException- On error.
-
getXpathForXPathExpressions
public static com.google.common.collect.ImmutableList<String> getXpathForXPathExpressions(Document dom, String xpathExpression) throws XPathExpressionException
Returns the XPaths of all nodes retrieved by xpathExpression. Example: //DIV[@id='foo'] returns /HTM[1]/BODY[1]/DIV[2]- Parameters:
dom- The dom.xpathExpression- The expression to find the element.- Returns:
- list of XPaths retrieved by xpathExpression.
- Throws:
XPathExpressionException
-
formatXPath
public static String formatXPath(String xpath)
- Parameters:
xpath- The xpath to format.- Returns:
- formatted xpath with tag names in uppercase and attributes in lowercase
-
getLastElementXPath
public static String getLastElementXPath(String xpath)
- Parameters:
xpath- The xpath expression to find the last element of.- Returns:
- returns the last element in the xpath expression
-
stripEndSquareBrackets
private static String stripEndSquareBrackets(String string)
- Parameters:
string-- Returns:
- string without the before [
-
getXPathLocation
public static int getXPathLocation(String dom, String xpath)
returns position of xpath element which match the expression xpath in the String dom.- Parameters:
dom- the Document to search inxpath- the xpath query- Returns:
- position of xpath element, if fails returns -1
-
getCloseElementLocation
public static int getCloseElementLocation(String dom, int pos, String element)
- Parameters:
dom- The dom string.pos- Position where to start searching.element- The element.- Returns:
- the position where the close element is
-
getCloseElementLocation
public static int getCloseElementLocation(String dom, String xpath)
- Parameters:
dom- The dom.xpath- The xpath expression.- Returns:
- the position where the close element is
-
stripXPathToElement
public static String stripXPathToElement(String xpath)
- Parameters:
xpath- The xpath expression.- Returns:
- the xpath expression for only the element location. Leaves out the attributes and text()
-
getBelongingNode
public static Node getBelongingNode(FormInput input, Document dom) throws XPathExpressionException
- Throws:
XPathExpressionException
-
-