Package Torello.HTML
Class SubSection
- java.lang.Object
-
- Torello.HTML.SubSection
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.CharSequence,java.lang.Cloneable,java.lang.Comparable<Replaceable>,Replaceable
public class SubSection extends java.lang.Object implements java.lang.CharSequence, java.io.Serializable, java.lang.Cloneable, Replaceable
Allows the NodeSearch Package to simultaneously return both an HTML-Vectorsublist, and the location where that sub-list was located (as an instance ofDotPair) where that sublist was located.
This class is is a simple data-structure-class which is used to represent vectorized-html web-page "sub-sections." This class keeps a copy of both the html (as a page-Vector), and the location of this sub-page from whence this html was copied from the original page. The location is saved as an instance of DotPair.
If the above sounds like technical-jargon, please review class DotPair and notice that the start and end are merely pointers into a vectorized-html web-page. They 'point' to the starting and ending places on the main html web-page that stored this "sub-section" or "sub-list" html-contents.
This class implements the Replaceable interface which can encapsulate both the location of a portion of html, and its location inside of a main page - from which it was copied, as public fields in this data-structure.
STALE-DATA NOTE:
The burden of ensuring that stale-data is not contained inside an instance of classSubSectionis left as an exercise for the programmer using this class. If the original page vector is modified, even the portion of the original page being modified does not overlap this sub-section the values inside the location field could become stale - with respect to the original page. This will happen if any nodes are added or removed from the original page. Thelocationpointers would no longer have indices that represent the original sub-section intended by the original instantiation of this class.
Implements Replaceable:
This class implements theReplaceableinterface. This means that it may be used for efficiently modifying, updating, or replacing many segments of an HTML-Page using the method:
ReplaceNodes.r(Vector<HTMLNode>, Iterable<Replaceable>, boolean)
Whenever the Java HTML JAR Library's HTML-Vector'sare being used to modify or update an HTML-Page, it can sometimes help to remember that shifting elements in a list (in this package aVector<HTMLNode>) can be somewhat inefficient if there are going to be many nodes inserted and removed. This is because inserting a node into aVectordoes, indeed, require shifting all nodes that occur after the insertion index-location forward!
By first extracting HTML nodes or sub-lists using the NodeSearch-PackagePeek-Operations(all of which returnReplaceableinstances), a user can operate on much smaller HTML Pieces. Once all updates have been made, the originalVectorcan be instantly rebuilt using the efficient updater method (link above).
TheReplaceableinterface provides quite a number of HTML Modification methods to add, change or eliminate the original HTML found on a page. Note that when operating on a piece of HTML that has been extracted, the effects of stale index-pointers become irrelevant!- See Also:
HTMLNode,DotPair,NodeIndex,TagNodePeekInclusive,InnerTagPeekInclusive, Serialized Form
Hi-Lited Source-Code:- View Here: Torello/HTML/SubSection.java
- Open New Browser-Tab: Torello/HTML/SubSection.java
File Size: 12,092 Bytes Line Count: 286 '\n' Characters Found
-
-
Field Summary
Serializable ID Modifier and Type Field static longserialVersionUIDAlternate Comparator Modifier and Type Field static Comparator<SubSection>comp2SubSection Fields Modifier and Type Field Vector<HTMLNode>htmlDotPairlocation
-
Constructor Summary
Constructors Constructor SubSection(DotPair location, Vector<HTMLNode> html)
-
Method Summary
Methods: interface Torello.HTML.Replaceable Modifier and Type Method booleanaddAllInto(int index, Vector<HTMLNode> fileVec)booleanaddAllInto(Vector<HTMLNode> fileVec)Vector<HTMLNode>currentNodes()intcurrentSize()HTMLNodefirstCurrentNode()HTMLNodelastCurrentNode()intoriginalLocationEnd()intoriginalLocationStart()intoriginalSize()intupdate(Vector<HTMLNode> originalHTML)Methods: interface java.lang.CharSequence Modifier and Type Method charcharAt(int index)intlength()CharSequencesubSequence(int start, int end)StringtoString()Methods: interface java.lang.Cloneable Modifier and Type Method SubSectionclone()Methods: class java.lang.Object Modifier and Type Method inthashCode()-
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface Torello.HTML.Replaceable
clearHTML, compareTo, isSynthetic, moveAndUpdate, setHTML, setHTML, summarize
-
-
-
-
Field Detail
-
serialVersionUID
public static final long serialVersionUID
This fulfils the SerialVersion UID requirement for all classes that implement Java'sinterface java.io.Serializable. Using theSerializableImplementation 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;
-
location
public final DotPair location
This public field identifies the sub-section location of a particular sub-section from a vectorized-html webpage. The location of the sub-page is specified by theclass DotPairpublic-fields:public final int 'start'andpublic final int 'end'- See Also:
DotPair- Code:
- Exact Field Declaration Expression:
public final DotPair location;
-
html
-
comp2
public static java.util.Comparator<SubSection> comp2
This is an "alternative Comparitor" that can be used for sorting instances of this class. It should work with theCollections.sort(List, Comparator)method in the standard JDK packagejava.util.*;
Comparator Heuristic:
This simply compares thepublicDotPair-Typed fieldlocationto each-other using that class' secondary instance ofComparator.DotPair'ssecondary-comparitor may be viewed at:DotPair.comp2.- See Also:
DotPair.comp2- Code:
- Exact Field Declaration Expression:
public static Comparator<SubSection> comp2 = (SubSection ss1, SubSection ss2) -> DotPair.comp2.compare(ss1.location, ss2.location);
-
-
Constructor Detail
-
SubSection
public SubSection(DotPair location, java.util.Vector<HTMLNode> html)
This just builds a new instance of this class. It represents a 'sub-section' of the html-page that needs to encapsulated into an object-instance. The contents of this data-structure are merely these two parameters that are passed to this constructor.- Parameters:
location- This parameter value will be assigned immediately to the internal-fieldpublic DotPair location.It is a two-integerVector-index class that points to the starting index-position, inside the main html-Vector, of the htmlclass 'SubSection'being constructed here.html- This parameter may be any vectorized-html web-page, but the intention is that thisVectoris an exact "cloned range" (a copy of a portion of the web-page) whose starting and ending integer indexVector-positions are demarcated by the contents of the parameter'location'- Throws:
java.lang.IllegalArgumentException- This exception will throw if either of these two scenarios occur:- If the input
Vector<HTMLNode> 'html'hashtml.size() == 0. - If
html.size() != location.size()
- If the input
- See Also:
location,html,DotPair,NodeIndex,HTMLNode- Code:
- Exact Constructor Body:
if (location == null) throw new NullPointerException ("Parameter 'DotPair location' to SubSection constructor was null."); if (html == null) throw new NullPointerException ("Parameter 'Vector<HTMLNode> html' to SubSection constructor was null."); if (html.size() == 0) throw new IllegalArgumentException( "Parameter 'Vector<HTMLNode> html' to SubSection constructor has size zero, but " + "this is not allowed here." ); if (location.size() != html.size()) throw new IllegalArgumentException( "Field 'public final int end' [value=" + location.end + "] of passed-parameter " + "'DotPair location' to SubSection constructor is different than the length of the " + "html-vector [" + html.size() + "]." ); this.location = location; this.html = html;
-
-
Method Detail
-
clone
public SubSection clone()
Java'sinterface Cloneablerequirements. This instantiates a newSubSectionwith identicalVector<HTMLNode> htmlandDotPair locationfields.- Overrides:
clonein classjava.lang.Object- Returns:
- A new
SubSectionwhose internal fields are identical to this one. - Code:
- Exact Method Body:
return new SubSection(location, html);
-
hashCode
public int hashCode()
Java's hash-code requirement.- Overrides:
hashCodein classjava.lang.Object- Returns:
- A hash-code that may be used when storing this node in a java hashed-collection.
The starting location of this
SubSectionought to be be a unique hash - Code:
- Exact Method Body:
return location.start;
-
toString
public final java.lang.String toString()
Java'stoString()requirement.
Final Method:
This method is final, and cannot be modified by sub-classes.- Specified by:
toStringin interfacejava.lang.CharSequence- Overrides:
toStringin classjava.lang.Object- Returns:
- A
String-representation of thisHTMLNode. - See Also:
toString()- Code:
- Exact Method Body:
return Util.pageToString(html);
-
charAt
public final char charAt(int index)
Returns the char value at the specified index of the String defined-by an invokation of the method:Util.pageToString(html). An index ranges from'0'(zero) tolength() - 1.The firstcharvalue of the sequence is at index'0', 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:
charAtin interfacejava.lang.CharSequence- Parameters:
index- The index of the char value to be returned- Returns:
- The specified char value
- See Also:
toString()- Code:
- Exact Method Body:
return toString().charAt(index);
-
length
public final int length()
Returns the length of theStringdefined-by an invokation of the method:Util.pageToString(html). The length is the number of 16-bitchar'sin the sequence.
Final Method:
This method is final, and cannot be modified by sub-classes.- Specified by:
lengthin interfacejava.lang.CharSequence- Returns:
- the number of
charsinthis.n.str - See Also:
toString()- Code:
- Exact Method Body:
return toString().length();
-
subSequence
public final java.lang.CharSequence subSequence(int start, int end)
Returns ajava.lang.CharSequencethat is a subsequence of theStringdefined-by an invokation of the method:Util.pageToString(html). The subsequence starts with thecharvalue at the specified index and ends with thecharvalue at indexend - 1.The length (inchar's) of the returned sequence isend - start, so ifstart == endthen an empty sequence is returned.
Final Method:
This method is final, and cannot be modified by sub-classes.- Specified by:
subSequencein interfacejava.lang.CharSequence- Parameters:
start- The start index, inclusiveend- The end index, exclusive- Returns:
- The specified subsequence
- See Also:
toString()- Code:
- Exact Method Body:
return toString().substring(start, end);
-
originalSize
public int originalSize()
Description copied from interface:ReplaceableReports how many nodes were copied intothisinstance. For implementing classes that inheritNodeIndex, this value will always be one. For others, it should report exactly how manyHTMLNode'swere copied.- Specified by:
originalSizein interfaceReplaceable- Returns:
- Number of nodes originally contained by
thisinstance.
The purpose ofReplaceable'sis to allow a user to modify HTML using a smaller sub-list, without having to operate on the entire HTML-Vectorsince adding & removing nodes is one variant ofVector-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 location.size();
-
currentSize
public int currentSize()
Description copied from interface:ReplaceableReturns how many nodes are currently inthisinstance.- Specified by:
currentSizein interfaceReplaceable- Returns:
- Number of nodes. See explanation of the original size,
versus the current size
here - Code:
- Exact Method Body:
return html.size();
-
originalLocationStart
public int originalLocationStart()
Description copied from interface:ReplaceableReturns the start-location within the original page-Vectorfrom whence the HTML contents ofthisinstance were retrieved.
Start is Inclusive:
The returned value is inclusive of the actual, original-range ofthisinstance. This means the firstHTMLNodecopied intothisinstance' internal data-structure was atoriginalLocationStart().
Implementations of Replaceable:
The two concrete implementatons of this interface (NodeIndexandSubSection) - both enforce the'final'modifier on their location-fields. (See:NodeIndex.indexandlocation).- Specified by:
originalLocationStartin interfaceReplaceable- Returns:
- The
Vectorstart-index from whence this HTML was copied. - Code:
- Exact Method Body:
return location.start;
-
originalLocationEnd
public int originalLocationEnd()
Description copied from interface:ReplaceableReturns the end-location within the original page-Vectorfrom whence the HTML contents ofthisinstance were retrieved.
Start is Exclusive:
The returned value is exclusive of the actual, original-range ofthisinstance. This means the lastHTMLNodecopied intothisinstance' internal data-structure was atoriginalLocationEnd() - 1
Implementations of Replaceable:
The two concrete implementatons of this interface (NodeIndexandSubSection) - both enforce the'final'modifier on their location-fields. (See:NodeIndex.indexandlocation).- Specified by:
originalLocationEndin interfaceReplaceable- Returns:
- The
Vectorend-index from whence this HTML was copied. - Code:
- Exact Method Body:
return location.end + 1;
-
currentNodes
public java.util.Vector<HTMLNode> currentNodes()
Description copied from interface:ReplaceableAll nodes currently contained by thisReplaceable. The concrete-classes which implementReplaceable(SubSection&TagNodeIndex) allow for the html they hold to be modified. The modification to aReplaceablehappens independently from the original HTML Page out of which it was copied.Replaceable'sare, sort-of, the exact opposite of Java'sListmethod'subList'. According to the Sun / Oracle Documentation forjava.util.List.subList(int fromIndex, int toIndex), any changes made to an instance of a'subList'are immediately reflected back into the originalListfrom where they were created.
TheList.subListoperation has the advantage of being extremely easy to work with - however, an HTML-PageVectorhas 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 aReplaceableis modified - nothing happens to the originalVectorwhatsoever!. Until a user requests that the original HTML-Vectorbe 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 - seeReplacement.runto understand how quick updates on HTML-Pages is done using theReplaceableinterface.- Specified by:
currentNodesin interfaceReplaceable- Returns:
- An HTML-
Vectorof the nodes.
The HTML-Vectorwhich is returned by this method may be modified in any way that is necessary! If or when a user requests that the original HTML-Vectorbe updated to accomodate the changes that have been made, the contents of theVectorwhich is returned by this method will be used to replace the original-HTML.
If this method is invoked more than once, the same exactVectorwill be returned each time that the Current-Nodes are requested. The internal "Current-Nodes" HTML-Vectoris a "per instance" Singleton-InstanceVector. - Code:
- Exact Method Body:
return html;
-
firstCurrentNode
public HTMLNode firstCurrentNode()
Description copied from interface:ReplaceableThe first node currently contained by thisReplaceable- Specified by:
firstCurrentNodein interfaceReplaceable- Returns:
- The First Node
- Code:
- Exact Method Body:
return currentNodes().elementAt(0);
-
lastCurrentNode
public HTMLNode lastCurrentNode()
Description copied from interface:ReplaceableThe last node currently contained by thisReplaceable- Specified by:
lastCurrentNodein interfaceReplaceable- Returns:
- The last node
- Code:
- Exact Method Body:
return html.elementAt(html.size() - 1);
-
addAllInto
public boolean addAllInto(java.util.Vector<HTMLNode> fileVec)
Description copied from interface:ReplaceableAdd all nodes currently retained inthisinstance into the HTML-Vectorparameterhtml. The nodes are appended to the end of'html'. Implementing classesNodeIndexandSubSectionsimply use the JavaVectormethod'sadd(forNodeIndex) andaddAll(forSubSection).- Specified by:
addAllIntoin interfaceReplaceable- Parameters:
fileVec- The HTML-Vectorinto which the nodes will be appended (to the end of thisVector, usingVectormethodsaddoraddAlldependent upon whether one or more-than-one nodes are being inserted).- Returns:
- The result of
Vectormethodadd, or methodallAll - Code:
- Exact Method Body:
return fileVec.addAll(html);
-
addAllInto
public boolean addAllInto(int index, java.util.Vector<HTMLNode> fileVec)
Description copied from interface:ReplaceableAdd all nodes currently retained inthisinstance into the HTML-Vectorparameterhtml.- Specified by:
addAllIntoin interfaceReplaceable- Parameters:
index- The'html'parameter'sVector-index where these nodes are to be insertedfileVec- The HTML-Vectorinto which the nodes will be appended (to the end of thisVector, usingVectormethodsaddoraddAlldependent upon whether one or more-than-one nodes are being inserted).- Returns:
- The result of
Vectormethodadd, or methodallAll - Code:
- Exact Method Body:
return fileVec.addAll(index, html);
-
update
public int update(java.util.Vector<HTMLNode> originalHTML)
Description copied from interface:ReplaceableReplaces the original range of nodes insideoriginalHTMLwith the current-nodes ofthisinstance, using the original-location of the node(s).
Replaceable's Primary Value:
The main value of using theReplaceableinterface 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 intoReplaceable'sinstances (using the Peek operations in package NodeSearch), and then re-copying those sections back into the original page-Vectorafter 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 aRelaceable. 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...) usingReplaceable'sin 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:
updatein interfaceReplaceable- Parameters:
originalHTML- The original page-Vectorwhere the nodes inthisinstance were retrieved- Returns:
- The change in the size of the
Vector - See Also:
Replacement.run(Vector, Iterable, boolean)- Code:
- Exact Method Body:
return ReplaceNodes.r(originalHTML, location, html);
-
-