Package Torello.HTML

Class NodeIndex<NODE extends HTMLNode>

  • Type Parameters:
    NODE - The class of HTMLNode represented by this NodeIndex instance.
    All Implemented Interfaces:
    java.io.Serializable, java.lang.CharSequence, java.lang.Cloneable, java.lang.Comparable<Replaceable>, Replaceable
    Direct Known Subclasses:
    CommentNodeIndex, TagNodeIndex, TextNodeIndex

    public abstract class NodeIndex<NODE extends HTMLNode>
    extends java.lang.Object
    implements java.lang.CharSequence, java.io.Serializable, java.lang.Cloneable, Replaceable
    The abstract parent class of all three NodeIndex classes, TagNodeIndex, TextNodeIndex and CommentNodeIndex.

    NodeIndex: HTMLNode 'Plus' the Vector-Index

    This class is just an extremely simple data-structure-class used, generally, for returning both the index of an instance-node of class 'HTMLNode' inside a vectorized-html web-page, and also the node itself. This class is the "parent class" of the extending classes: TextNodeIndex, TagNodeIndex and CommentNodeIndex. These instances are returned by all PEEK operations in the node-search package. The constructor of this class accepts an index, and a node and saves them as public fields of this class.


    STALE DATA NOTE:
    If a vectorized-html webpage is modified after any of these Node + Index classes are created / instantiated, and nodes are added or removed from the webpage, then the (integer) index data inside these classes would have become stale when they are next accessed.

    It is important to remember that Vector-position (a.k.a. "Vector-index") information that is stored inside instances of these (extremely-simple) classes will become stale, immediately if nodes are ever added or removed to the underlying Vector from which these Node + Index object-classes are created.
    See Also:
    HTMLNode, CommentNodeIndex, TagNodeIndex, TextNodeIndex, Serialized Form


    • Field Detail

      • serialVersionUID

        🡇     🗕  🗗  🗖
        public static final long serialVersionUID
        This fulfils the SerialVersion UID requirement for all classes that implement Java's interface java.io.Serializable. Using the Serializable Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
         public static final long serialVersionUID = 1;
        
      • index

        🡅  🡇     🗕  🗗  🗖
        public final int index
        An index to a node from a web-page. This index must point to a the exact same node inside of a vectorized-html page as the node stored in member-field HTMLNode 'n'.
      • n

        🡅  🡇     🗕  🗗  🗖
        public NODE extends HTMLNode n
        A HTMLNode from a web-page. This node is supposed to be the same node stored at the index specified by member-field int 'index' of some vectorized-html web-page in memory or on disk.
      • comp2

        🡅  🡇     🗕  🗗  🗖
        public static final java.util.Comparator<TextNodeIndex> comp2
        This is an "alternative Comparitor" that can be used for sorting instances of this class. It should work with the Collections.sort(List, Comparator) method in the standard JDK package java.util.*;

        Comparitor Heuristic:
        This version utilizes the standard JDK method String.compareTo(String).
        See Also:
        HTMLNode.str
        Code:
        Exact Field Declaration Expression:
         public static final Comparator<TextNodeIndex> comp2 =
                 (TextNodeIndex txni1, TextNodeIndex txni2) -> txni1.n.str.compareTo(txni2.n.str);
        
      • comp3

        🡅  🡇     🗕  🗗  🗖
        public static final java.util.Comparator<TextNodeIndex> comp3
        This is an "alternative Comparitor" that can be used for sorting instances of this class. It should work with the Collections.sort(List, Comparator) method in the standard JDK package java.util.*;

        Comparitor Heuristic:
        This version utilizes the standard JDK method String.compareToIgnoreCase(String).
        See Also:
        HTMLNode.str
        Code:
        Exact Field Declaration Expression:
         public static final Comparator<TextNodeIndex> comp3 =
                 (TextNodeIndex txni1, TextNodeIndex txni2) -> txni1.n.str.compareToIgnoreCase(txni2.n.str);
        
    • Constructor Detail

      • NodeIndex

        🡅  🡇     🗕  🗗  🗖
        protected NodeIndex​(int index,
                            NODE n)
        a default constructor. This assigns a value to the index field.
        Parameters:
        index - This is the vector-index location of HTMLNode 'n' inside of a vectorized-HTML web-page.

        STALE DATA NOTE: This class is a minor-use class, not one of the primary data classes. This instance shall become 'useless' the moment the vector that was used to instantiate this class is modified, and the node 'n' is no longer at vector-index 'index.' These "NodeIndex" classes are retained, not deprecated due to the fundamental nature of using the classes of the NodeSearch Package. Data is easily made stale. Generally, when modifying HTML Vectors, the easiest thing to do is to remember to modify a vector at specific locations by iterating from the end of the vector, back to the beginning. This will generally prevent "state-data vector-indexes" from rearing their ugly head.
        n - An HTMLNode that needs to be the node found in the underlying vector at vector-index 'index.'
        Throws:
        java.lang.IndexOutOfBoundsException - if index is negative, this exception is thrown.
        java.lang.NullPointerException - if n is null.
        Code:
        Exact Constructor Body:
         this.index = index;
         this.n = n;
        
         if (n == null)  throw new NullPointerException(
             "HTMLNode parameter 'n' to this constructor was passed a null value, but this " +
             "is not allowed here."
         );
        
         if (index < 0)  throw new IndexOutOfBoundsException(
             "Integer parameter 'index' to this constructor was passed a negative value: " +
             index
         );
        
    • Method Detail

      • newNodeIndex

        🡅  🡇     🗕  🗗  🗖
        public static final NodeIndex<?> newNodeIndex​(int index,
                                                      HTMLNode n)
        Simple dispatch method helper that switches on the class of input parameter 'n'.
        Parameters:
        n - Any of the three Java HTML defined HTMLNode subclasses - TagNode, TextNode or CommentNode
        Returns:
        A NodeIndex inheriting class that is appropriate to 'n'.
        Throws:
        java.lang.IllegalArgumentException - If the user has extended class HTMLNode, and passed this unrecognized HTMLNode Type.
        Code:
        Exact Method Body:
         Class<?> newNodeClass = n.getClass();
        
         if (TagNode.class.isAssignableFrom(newNodeClass))
             return new TagNodeIndex(index, (TagNode) n);
        
         if (TextNode.class.isAssignableFrom(newNodeClass))
             return new TextNodeIndex(index, (TextNode) n);
        
         if (CommentNode.class.isAssignableFrom(newNodeClass))
             return new CommentNodeIndex(index, (CommentNode) n);
        
         throw new IllegalArgumentException
             ("Parameter 'n' has a Type that is an Unrecognized HTMLNode-SubClass Type");
        
      • equals

        🡅  🡇     🗕  🗗  🗖
        public final boolean equals​(java.lang.Object o)
        Java's public boolean equals(Object o) requirements.

        Final Method:
        This method is final, and cannot be modified by sub-classes.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - This may be any Java Object, but only ones of 'this' type whose internal-values are identical will bring this method to return true.
        Returns:
        TRUE If 'this' equals another object HTMLNode.
        Code:
        Exact Method Body:
         if (o == null) return false;
         if (o == this) return true;
        
         if (! this.getClass().equals(o.getClass())) return false;
        
         NodeIndex<?> other = (NodeIndex) o;
        
         return other.n.str.equals(this.n.str) && (other.index == this.index);
        
      • hashCode

        🡅  🡇     🗕  🗗  🗖
        public int hashCode()
        Java's hash-code requirement.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        A hash-code that may be used when storing this node in a java hashed-collection. The index of this NodeIndex ought to be be a unique hash.
        Code:
        Exact Method Body:
         return index;
        
      • charAt

        🡅  🡇     🗕  🗗  🗖
        public final char charAt​(int index)
        Returns the char value at the specified index of the public final String str field of 'this' field public final HTMLNode n. An index ranges from zero to length() - 1. The first char value of the sequence is at index zero, the next at index one, and so on, as for array indexing.

        If the char value specified by the index is a surrogate, the surrogate value is returned.

        Final Method:
        This method is final, and cannot be modified by sub-classes.
        Specified by:
        charAt in interface java.lang.CharSequence
        Parameters:
        index - The index of the char value to be returned
        Returns:
        The specified char value
        Code:
        Exact Method Body:
         return n.str.charAt(index);
        
      • length

        🡅  🡇     🗕  🗗  🗖
        public final int length()
        Returns the length of the public final String str field of 'this' field public final HTMLNode n. The length is the number of 16-bit chars in the sequence.

        Final Method:
        This method is final, and cannot be modified by sub-classes.
        Specified by:
        length in interface java.lang.CharSequence
        Returns:
        the number of chars in this.n.str
        Code:
        Exact Method Body:
         return n.str.length();
        
      • subSequence

        🡅  🡇     🗕  🗗  🗖
        public final java.lang.CharSequence subSequence​(int start,
                                                        int end)
        Returns a CharSequence that is a subsequence of the public final String str field of 'this' field public final HTMLNode n. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1. The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.

        Final Method:
        This method is final, and cannot be modified by sub-classes.
        Specified by:
        subSequence in interface java.lang.CharSequence
        Parameters:
        start - The start index, inclusive
        end - The end index, exclusive
        Returns:
        The specified subsequence
        Code:
        Exact Method Body:
         return n.str.substring(start, end);
        
      • toString

        🡅  🡇     🗕  🗗  🗖
        public final java.lang.String toString()
        Returns the public final String str field of 'this' field public final HTMLNode n.

        Final Method:
        This method is final, and cannot be modified by sub-classes.
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string consisting of exactly this sequence of characters.
        See Also:
        HTMLNode.str
        Code:
        Exact Method Body:
         return n.str;
        
      • originalSize

        🡅  🡇     🗕  🗗  🗖
        public int originalSize()
        Description copied from interface: Replaceable
        Reports how many nodes were copied into this instance. For implementing classes that inherit NodeIndex, this value will always be one. For others, it should report exactly how many HTMLNode's were copied.
        Specified by:
        originalSize in interface Replaceable
        Returns:
        Number of nodes originally contained by this instance.

        The purpose of Replaceable's is to allow a user to modify HTML using a smaller sub-list, without having to operate on the entire HTML-Vector since adding & removing nodes is one variant of Vector-modification, the original-size may often differ from the current-size.

        When modifying HTML, if a web-page is broken into smaller-pieces, and changes are restricted to those smaller sub-lists (and the original page is rebuilt, all at once, after all changes have been made) then those modifications should require far-fewer time-consuming list-shift operations, tremendously improving the performance of the code.
        Code:
        Exact Method Body:
         return 1;
        
      • originalLocationStart

        🡅  🡇     🗕  🗗  🗖
        public int originalLocationStart()
        Description copied from interface: Replaceable
        Returns the start-location within the original page-Vector from whence the HTML contents of this instance were retrieved.

        Start is Inclusive:
        The returned value is inclusive of the actual, original-range of this instance. This means the first HTMLNode copied into this instance' internal data-structure was at originalLocationStart().

        Implementations of Replaceable:
        The two concrete implementatons of this interface (NodeIndex and SubSection) - both enforce the 'final' modifier on their location-fields. (See: index and SubSection.location).
        Specified by:
        originalLocationStart in interface Replaceable
        Returns:
        The Vector start-index from whence this HTML was copied.
        Code:
        Exact Method Body:
         return index;
        
      • originalLocationEnd

        🡅  🡇     🗕  🗗  🗖
        public int originalLocationEnd()
        Description copied from interface: Replaceable
        Returns the end-location within the original page-Vector from whence the HTML contents of this instance were retrieved.

        Start is Exclusive:
        The returned value is exclusive of the actual, original-range of this instance. This means the last HTMLNode copied into this instance' internal data-structure was at originalLocationEnd() - 1

        Implementations of Replaceable:
        The two concrete implementatons of this interface (NodeIndex and SubSection) - both enforce the 'final' modifier on their location-fields. (See: index and SubSection.location).
        Specified by:
        originalLocationEnd in interface Replaceable
        Returns:
        The Vector end-index from whence this HTML was copied.
        Code:
        Exact Method Body:
         return index + 1;
        
      • currentNodes

        🡅  🡇     🗕  🗗  🗖
        public java.util.Vector<HTMLNodecurrentNodes()
        Description copied from interface: Replaceable
        All nodes currently contained by this Replaceable. The concrete-classes which implement Replaceable (SubSection & TagNodeIndex) allow for the html they hold to be modified. The modification to a Replaceable happens independently from the original HTML Page out of which it was copied.

        Replaceable's are, sort-of, the exact opposite of Java's List method 'subList'. According to the Sun / Oracle Documentation for java.util.List.subList(int fromIndex, int toIndex), any changes made to an instance of a 'subList' are immediately reflected back into the original List from where they were created.

        The List.subList operation has the advantage of being extremely easy to work with - however, an HTML-Page Vector has the potential of being hundreds of nodes long. Any operations that involve insertion or deletion will likely be terribly inefficient.

        When the HTML inside of a Replaceable is modified - nothing happens to the original Vector whatsoever!. Until a user requests that the original HTML-Vector be updated to reflect all changes that he or she has made, the original HTML remains untouched. When an update request is finally issued, all changes are made all at once, and at the same time!

        Again - see ReplaceNodes.r(Vector, Iterable, boolean) to understand how quick updates on HTML-Pages is done using the Replaceable interface.
        Specified by:
        currentNodes in interface Replaceable
        Returns:
        An HTML-Vector of the nodes.
        Code:
        Exact Method Body:
         if (CURRENT_NODES == null)  CURRENT_NODES = new Vector<>(1);
         CURRENT_NODES.add(n);
         return CURRENT_NODES;
        
      • addAllInto

        🡅  🡇     🗕  🗗  🗖
        public boolean addAllInto​(java.util.Vector<HTMLNode> fileVec)
        Description copied from interface: Replaceable
        Add all nodes currently retained in this instance into the HTML-Vector parameter html. The nodes are appended to the end of 'html'. Implementing classes NodeIndex and SubSection simply use the Java Vector method's add (for NodeIndex) and addAll (for SubSection).
        Specified by:
        addAllInto in interface Replaceable
        Parameters:
        fileVec - The HTML-Vector into which the nodes will be appended (to the end of this Vector, using Vector methods add or addAll dependent upon whether one or more-than-one nodes are being inserted).
        Returns:
        The result of Vector method add, or method allAll
        Code:
        Exact Method Body:
         return fileVec.add(n);
        
      • addAllInto

        🡅  🡇     🗕  🗗  🗖
        public boolean addAllInto​(int index,
                                  java.util.Vector<HTMLNode> fileVec)
        Description copied from interface: Replaceable
        Add all nodes currently retained in this instance into the HTML-Vector parameter html.
        Specified by:
        addAllInto in interface Replaceable
        Parameters:
        index - The 'html' parameter's Vector-index where these nodes are to be inserted
        fileVec - The HTML-Vector into which the nodes will be appended (to the end of this Vector, using Vector methods add or addAll dependent upon whether one or more-than-one nodes are being inserted).
        Returns:
        The result of Vector method add, or method allAll
        Code:
        Exact Method Body:
         fileVec.insertElementAt(n, index);
         return true;
        
      • update

        🡅     🗕  🗗  🗖
        public int update​(java.util.Vector<HTMLNode> fileVec)
        Description copied from interface: Replaceable
        Replaces the original range of nodes inside originalHTML with the current-nodes of this instance, using the original-location of the node(s).

        Replaceable's Primary Value:
        The main value of using the Replaceable interface is to allow for more expedient replacing / modifying HTML Pages. If many changes need to be made to a page, first extracting and copying the sub-sections that need changing into Replaceable's instances (using the Peek operations in package NodeSearch), and then re-copying those sections back into the original page-Vector after changing them - avoids the cost that would be incurred from repeatedly inserting and shifting a long list of nodes in a large HTML Page.

        Therefore, this method is probably best avoided, as it is defeating the entire-purpose of a Relaceable. This method will update the nodes at the location in the original-Vector, which is fine, but if more than one update / change is needed, using this method over-and-over again will re-introduce the exact shifting that was supposed to be avoided by (and is the whole reason for...) using Replaceable's in the first place!

        The following example should make this clear:

        Example:
        Vector<HTMLNode>    page        = HTMLPage.getPageTokens(new URL("http://some.url.com/"), false);
        Vector<SubSection>  myTableRows = TagNodePeekInclusive.all(page, "tr");
        TagNode             OPEN_SPAN   = HTMLTags.hasTag("SPAN", TC.OpeningTags);
        TagNode             CLOSE_SPAN  = HTMLTags.hasTag("SPAN", TC.ClosingTags);
        int                 counter     = 1;
        
        for (SubSection tableRow : myTableRows)
        {
            // Retrieve the <TR> Tag & Give it a CSS-ID
            TagNode tr = tableRow.html.elementAt(0).asTagNode().setID("ROW" + counter++, null);
        
            // Put the newly created <TR ID=..> into the vector.  It was the first-element in the SubSection
            tableRow.html.setElementAt(tr, 0);
        
            // Add a <SPAN>...</SPAN> surrounding the first line of text
            // NOTE: This assumes that tableRow[1] (second SubSection node) is a TextNode with text
        
            tableRow.html.insertElementAt(OPEN_SPAN, 1);
            tableRow.html.insertElementAt(CLOSE_SPAN, 3);
        }
        
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // This version DESTROYS THE BENEFIT of using TagNodePeekInclusive
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        //
        // Here, if the original html-page was thousands of nodes long, every table-row
        // update will force thousands of nodes to be shifted to the right over-and-over
        // again!
        
        for (SubSection tableRow : myTableRows) tableRow.update(page);
        
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // This builds a new Vector much more efficiently, avoiding costly node-shifting
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
        page = ReplaceNodes.r(page, myTableRows, false).a;
        
        Specified by:
        update in interface Replaceable
        Parameters:
        fileVec - The original page-Vector where the nodes in this instance were retrieved
        Returns:
        The change in the size of the Vector
        See Also:
        ReplaceNodes.r(Vector, Iterable, boolean)
        Code:
        Exact Method Body:
         fileVec.setElementAt(n, index);  return 0;