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 ofHTMLNode
elements in Vectorized-HTML with other, user-provided, elements.
Overload-able using lambda. This simple interface needs to receive anHTMLNode
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 imageTagNode's
that have updatedSRC
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 theNodeSearch
package'sFind / Get / Remove
classes do is cycle throughVector'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);
Hi-Lited Source-Code:- View Here: Torello/HTML/ReplaceFunction.java
- Open New Browser-Tab: Torello/HTML/ReplaceFunction.java
-
-
Field Summary
Fields Modifier and Type Field static long
serialVersionUID
-
Method Summary
@FunctionalInterface (Lambda) Method Modifier and Type Method HTMLNode
getReplacement(HTMLNode n, int curVecPos, int iterationCount)
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
This fulfils the SerialVersion UID requirement for all classes that implement Java'sinterface java.io.Serializable
. Using theSerializable
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 pageVector
.curVecPos
- The position in the originalVector
. 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'}.
-
-