Package Torello.HTML

Interface ReplaceFunction

  • All Superinterfaces:
    java.io.Serializable
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface ReplaceFunction
    extends java.io.Serializable
    A function-pointer definition that facilitates the substituting of HTMLNode elements in Vectorized-HTML with other, user-provided, elements.

    Overload-able using lambda. This simple interface needs to receive an HTMLNode and also the position in a Vector where this node was retrieved. It is up to the user to compute / instantiate / provide a new node that can be used to replace the old one.

    This is actually a pseudo-trivial class. The reason it was written is to make swapping out <IMG SRC="..."> nodes with image TagNode's that have updated SRC URL's. It is mostly just a "one-line for-loop."

    The vast majority of the classes in this HTML scrape and search package are written to eliminate the complex need for worrying too much about for-loops. All the NodeSearch package's Find / Get / Remove classes do is cycle through Vector's of HTML pages looking for matches.

    Though the concept of a for-loop is easy for even the novice in computer-programming, when writing code, worrying about exception cases, boundary cases, case (upper-case and lower-case) all add up to make a very cumbersome search. The list of classes in NodeSearch are just glorified argument-parameter marshalling, and validity & boundary checking. This can make writing a scrape, search, or replace HTML program of your own hundreds of times more straight-forward.

    Below is an example of the simplest of replacer-methods in this class:

    Example:
    // The intent of the classes: ReplaceFunction, and ReplaceNodes
    // ... are to make the following example a one-line command, rather than a for-loop:
    
    if (posArr.length != newNodes.size()) throw new ArrayIndexOutOfBoundsException(
        "The pointer array 'posArr', and the replacement-node array 'newNodes' do not have equal lengths!\n" +
        "posArr.length=" + posArr.length + ", newNodes.size()=" + newNodes.size()
    );
    
    int newNodesPos = 0;
    for (int pos : posArr) v.setElementAt(newNodes.elementAt(newNodesPos++), pos);
    


    The code (above) could be "replaced" with the single line:

    Java Line of Code:
    ReplaceNodes.r(page, imagePosArr, newHTMLImageNodes);
    


    • Field Detail

      • serialVersionUID

        🡇    
        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.

        Functional Interfaces are usually not thought of as Data Objects that need to be saved, stored and retrieved; however, having the ability to store intermediate results along with the lambda-functions that helped get those results can make debugging easier.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
         public static final long serialVersionUID = 1;
        
    • Method Detail

      • getReplacement

        🡅    
        HTMLNode getReplacement​(HTMLNode n,
                                int curVecPos,
                                int iterationCount)
        The intention here is to provide a "replace node" for a particular position in the original-Vector<HTMLNode>
        Parameters:
        n - This is the "old node" that needs replacing. It will have been obtained from the original HTML page Vector.
        curVecPos - The position in the original Vector. This value may be ignored, but is provided as a matter of convenience.
        iterationCount - The for-loop that will be making calls to 'getReplacement(...)' provides the a loop-count to this method. All that means is there is a "loop variable" that is updated-by-one (incremented) each time this method is called.
        Returns:
        A new node to be substituted for the current node at the position identified by {code 'curArrPos'}.