Package Torello.HTML
Class CommentNode
- java.lang.Object
-
- Torello.HTML.HTMLNode
-
- Torello.HTML.CommentNode
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.CharSequence
,java.lang.Cloneable
,java.lang.Comparable<CommentNode>
public final class CommentNode extends HTMLNode implements java.lang.CharSequence, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<CommentNode>
Concrete HTMLNode SubClasses:
The three inherited classes ofabstract class HTMLNode
are very light-weight, and contain some amount ofpublic
methods, but do not have heavy internal-state (either static, or non-static). Below is a list of the internal field's that are added to each of the three instantiations of the ancestorHTMLNode class
:class TagNode
adds a fieldpublic final boolean isClosing
- which tells a user if this tag has a forward-slash immediately following the '<' (less-than symbol) at character position 2. This is how one identifies a 'closing-version' of the element, for instance: '</DIV>
' and '</SPAN>
' would both have theirpublic final boolean isClosing
fields set to TRUE. There is also apublic final String tok
field added to instances ofTagNode
that identify what html element the TagNode represents. For example an HTML Element such as:<A HREF="http://My.URL.com" TARGET=_blank>
, would have it'sString 'tok'
field set to'a'
class TextNode
this inherited class fromclass HTMLNode
does not add any internal state at all. It has the exact same internally-maintained fields as its parent-class. Thepublic final String str
field merely states what text this text-node actually represents.
class CommentNode
for searching-purposes, and ease-of-use,class CommentNode
, which is the third and final class to inheritHTMLNode
keeps one extra internal-field, which ispublic final String body
. This field is a redundant, duplicate, of the internal stringpublic final String str
- which is inherited from the HTML Node class. The subtle difference is that, since comment nodes represent the HTML<!-- and -->
symbols, the'body'
of the comment sometimes needs to be searched, quickly. Thepublic final String body
leaves off these leading and ending comment delimiter symbols:<!-- and -->
Represents HTML Comments, and is one of only three HTML Element Classes provided by the Java HTML Library Tool, and also one of the three data-classes that can be generated by the HTML Parser.
This is referring to HTML comments which have the form:<!-- This is an HTML-styled COMMENT -->
. This node will store such text in the parent-class field:str
This class inherits fromabstract
parent-classHTMLNode
Inheritance Tree Diagram:
Below is the inheritance diagram (with fields) of the three concrete-classes that extend theabstract
classHTMLNode:
- See Also:
TagNode
,TextNode
,HTMLNode
, Serialized Form
Hi-Lited Source-Code:- View Here: Torello/HTML/CommentNode.java
- Open New Browser-Tab: Torello/HTML/CommentNode.java
File Size: 6,028 Bytes Line Count: 131 '\n' Characters Found
-
-
Field Summary
Serializable ID Modifier and Type Field static long
serialVersionUID
Data Fields Modifier and Type Field String
body
-
Constructor Summary
Constructors Constructor Description CommentNode(String s)
This constructor simply makes a call tosuper(s);
a.k.a.class HTML.HTMLNode
This constructor also checks to ensure that the internalString
-field (public final String str
) contains beginning and ending comment markers:'<!--'
and'-->'
-
Method Summary
Methods: interface java.lang.Cloneable Modifier and Type Method CommentNode
clone()
Methods: interface java.lang.Comparable Modifier and Type Method int
compareTo(CommentNode cn)
'instanceof' Operator Replacement Methods Modifier and Type Method CommentNode
ifCommentNode()
boolean
isCommentNode()
-
Methods inherited from class Torello.HTML.HTMLNode
asCommentNode, asTagNode, asTextNode, charAt, equals, hashCode, ifTagNode, ifTextNode, isOpenTag, isOpenTagPWA, isTagNode, isTextNode, length, openTag, openTagPWA, subSequence, toString
-
-
-
-
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 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.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final long serialVersionUID = 1;
-
body
public final java.lang.String body
This stores a copy of the body of the comment. Specifically, (and simply) the java string methodbody = str.substring(4, str.length() - 3);
is called and stored here. Yes, this does mean that extra memory / double memory is used to store comments, however the trade-offs are somewhat high.
TRADEOFFS: If the programmer or user ever wishes to perform a search, it becomes obvious that leaving off the beginning and trailing'<!--'
and'-->'
markers when specifying a search provides more easily readable code, and less error prone code. Thus, when using theCommentNodeFind, Get, Peek, Poll, etc...
methods in thepackage NodeSearch
, a Java Stringsubstring(...)
would have to be invoked on every search comparison loop invocation. Primarily, keepingclass HTMLNode
and it's descendants all immutable is a much higher priority to ensure clean code, it becomes necessary to keep a redundant copy of the bodyString
.
-
-
Constructor Detail
-
CommentNode
public CommentNode(java.lang.String s)
This constructor simply makes a call tosuper(s);
a.k.a.class HTML.HTMLNode
This constructor also checks to ensure that the internalString
-field (public final String str
) contains beginning and ending comment markers:'<!--'
and'-->'
- Throws:
java.lang.IllegalArgumentException
- If the passed string does not start and end with the appropriate HTML comment-markers:<!--
and-->
- Code:
- Exact Constructor Body:
super(s); if (! s.startsWith("<!--")) throw new IllegalArgumentException ("The passed HTML string does not start with comment marker '<!--'"); if (! s.endsWith("-->")) throw new IllegalArgumentException ("The passed HTML string does not end with comment marker '-->'"); body = str.substring(4, str.length() - 3); if (body.contains("-->")) throw new IllegalArgumentException ("The passed HTML string has multiple occurrences of substring '-->'");
-
-
Method Detail
-
isCommentNode
public final boolean isCommentNode()
This method identifies that'this'
instance of (abstract parent-class)HTMLNode
is, indeed, an instance of sub-classCommentNode
.
Final Method:
This method is final, and cannot be modified by sub-classes.- Overrides:
isCommentNode
in classHTMLNode
- Returns:
- This method shall always return
TRUE
It overrides the parent-classHTMLNode
methodisCommentNode()
, which always returnsFALSE
. - See Also:
isCommentNode()
- Code:
- Exact Method Body:
return true;
-
ifCommentNode
public final CommentNode ifCommentNode()
This method identifies that'this'
instance of (abstract parent-class)HTMLNode
is, indeed, an instance of sub-classCommentNode
.
Final Method:
This method is final, and cannot be modified by sub-classes.- Overrides:
ifCommentNode
in classHTMLNode
- Returns:
'this'
reference. This method can be used inside loops for improving the readability of loop-condition expressions. See example below:
Example:
Vector<HTMLNode> fileVec = HTMLPage.getPageTokens(new URL("http://some.url.com"), false); CommentNode c; // NOTE: The casting to class CommentNode is automatically acheived with this method, // which can make loops a lot easier to read. Only a CommentNode instance will have // a '.body' field. Attempting to read a '.body' field from an instance of HTMLNode // would immediately generate a compile-time error. for (HTMLNode n : fileVec) if ((c = myHTMLVector.elementAt(i).ifCommentNode()) != null) if (c.body.equals("Some Comment Node Text")) ...
This method-version overrides the parent-class-version, which always returns null. This method is not overriden by otherHTMLNode
sub-classes.- See Also:
ifCommentNode()
- Code:
- Exact Method Body:
return this;
-
clone
public CommentNode clone()
-
compareTo
public int compareTo(CommentNode cn)
Java'sinterface Comparable<T>
requirements. This does a very simple comparison using the underlying fieldfinal String str
that allHTMLNode's
contain.- Specified by:
compareTo
in interfacejava.lang.Comparable<CommentNode>
- Parameters:
cn
- Any otherCommentNode
to be compared to'this' CommentNode
- Returns:
- An integer that fulfils Java's
interface Comparable<T> public boolean compareTo(T t)
method requirements. - Code:
- Exact Method Body:
return this.body.compareToIgnoreCase(cn.body);
-
-