Class 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 static int C0  
      private static int C9  
      private static int CD  
      private static int CM  
      private static int CP  
      private static int CS  
      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 Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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)  
      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.Collection evaluateFunction​(java.util.Collection source, int from, int to, int position, int last, boolean isFilterContext)
      Evaluates specified function.
      protected boolean evaluateLogic​(java.util.Collection first, java.util.Collection second, java.lang.String logicOperator)
      Evaluates logic operation on two collections.
      protected java.util.Collection filterByCondition​(java.util.Collection source, int from, int to)
      Filter nodes satisfying the condition
      private 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 value)  
      private static boolean isValidInteger​(java.lang.String value)  
      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)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • XPather

        public XPather​(java.lang.String expression)
        Constructor - creates XPather instance with specified XPath expression.
        Parameters:
        expression -
    • 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
      • 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 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

        protected 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

        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)