Class 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.
    • Constructor Detail

      • XPathHelper

        private XPathHelper()
    • 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)
      • getSkeletonXpath

        public static String getSkeletonXpath​(Node node)
      • getXPathExpression

        public static String getXPathExpression​(Node node)
      • 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​(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 in
        xpathExpr - 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
      • lowerCaseAttributes

        private static String lowerCaseAttributes​(String formatted)
      • capitalizeTagNames

        private static String capitalizeTagNames​(String xpath)
      • 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 in
        xpath - 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()