Package Torello.HTML
Class PrettyPrint
- java.lang.Object
-
- Torello.HTML.PrettyPrint
-
public class PrettyPrint extends java.lang.Object
Under-Development. Suite of Utilities for Cleaning up an HTML Page.
Hi-Lited Source-Code:- View Here: Torello/HTML/PrettyPrint.java
- Open New Browser-Tab: Torello/HTML/PrettyPrint.java
File Size: 7,740 Bytes Line Count: 191 '\n' Characters Found
-
-
Constructor Summary
Constructors Constructor PrettyPrint()
-
Method Summary
Ensure that all HTML-5 Block-Tags are placed on a New-Line ( '\n'
)Modifier and Type Method static int
ensureBlockTagsOnNewlines(Vector<HTMLNode> html)
static int
ensureBlockTagsOnNewlines(Vector<HTMLNode> html, int sPos, int ePos)
static int
ensureBlockTagsOnNewlines(Vector<HTMLNode> html, DotPair dp)
Remove all White-Space between each New-Line ( '\n'
) and non-White-SpaceModifier and Type Method static int
removeAllIndentation(Vector<HTMLNode> html)
static int
removeAllIndentation(Vector<HTMLNode> html, int sPos, int ePos)
static int
removeAllIndentation(Vector<HTMLNode> html, DotPair dp)
Validation-Helper: Generate a String from Text-Nodes, Only; Eliminate White-Space. Modifier and Type Method static String
textChecker(Vector<HTMLNode> html)
static String
textChecker(Vector<HTMLNode> html, int sPos, int ePos)
static String
textChecker(Vector<HTMLNode> html, DotPair dp)
Validation-Helper: Generate a String from Tag-Nodes, Only Modifier and Type Method static String
tagChecker(Vector<HTMLNode> html)
static String
tagChecker(Vector<HTMLNode> html, int sPos, int ePos)
static String
tagChecker(Vector<HTMLNode> html, DotPair dp)
-
-
-
Constructor Detail
-
PrettyPrint
public PrettyPrint()
-
-
Method Detail
-
removeAllIndentation
public static int removeAllIndentation(java.util.Vector<HTMLNode> html)
- Code:
- Exact Method Body:
return RemAllIndent.remove(html, 0, -1);
-
removeAllIndentation
public static int removeAllIndentation(java.util.Vector<HTMLNode> html, DotPair dp)
- Code:
- Exact Method Body:
return RemAllIndent.remove(html, dp.start, dp.end + 1);
-
removeAllIndentation
public static int removeAllIndentation(java.util.Vector<HTMLNode> html, int sPos, int ePos)
Any White-Space which immediately follows a'\n'
New-Line character is removed from allTextNode's
, by this method.- Parameters:
html
- This may be any Vectorized-HTML Web-Page (or sub-page).
The Variable-Type Wild-Card Expression'? extends HTMLNode'
means that aVector<TagNode>, Vector<TextNode>
orVector<CommentNode>
will all be accepted by this paramter without causing an exception throw.
These 'sub-type' Vectors are often returned as search results from the classes in the'NodeSearch'
vpackage.sPos
- This is the (integer)Vector
-index that sets a limit for the left-mostVector
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'inclusive' meaning that theHTMLNode
at thisVector
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Vector
, an exception will be thrown.ePos
- This is the (integer)Vector
-index that sets a limit for the right-mostVector
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the'HTMLNode'
at thisVector
-index will not be visited by this method.
NOTE: If this value is larger than the size of input theVector
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the size of the inputVector
-parameter.- Returns:
- The number of
TextNode's
that changed as a result of this method. - Throws:
java.lang.IndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thesize
of theVector
- If
'ePos'
is zero, or greater than the size of theVector
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toVector.size()
, before this check is done.
- If
- Code:
- Exact Method Body:
return RemAllIndent.remove(html, sPos, ePos);
-
ensureBlockTagsOnNewlines
public static int ensureBlockTagsOnNewlines (java.util.Vector<HTMLNode> html)
- Code:
- Exact Method Body:
return AddNewLines.beforeAllBlockTags(html, 0, -1);
-
ensureBlockTagsOnNewlines
public static int ensureBlockTagsOnNewlines (java.util.Vector<HTMLNode> html, DotPair dp)
- Code:
- Exact Method Body:
return AddNewLines.beforeAllBlockTags(html, dp.start, dp.end+1);
-
ensureBlockTagsOnNewlines
public static int ensureBlockTagsOnNewlines (java.util.Vector<HTMLNode> html, int sPos, int ePos)
Any Block-Tag identified on the page, or page sub-section, may only be preceeded by:- Optional White-Space of any type
- At least one, mandatory, new-line
'n'
character
If an HTML-5 Block-Tag (TagNode
) is identified that is preceeded by other HTML-Tags or Text:- If the preceeding node is a
TextNode
, a new-line'n'
is appended to the first non-white-space character at the end of thatTextNode
- If the preceeding node is another
TagNode
, a single-characterTextNode
containing the character'n'
(a new-line) is inserted into the input HTML-Vector
directly before that Block-TagNode
.
- Parameters:
html
- This may be any Vectorized-HTML Web-Page (or sub-page).
The Variable-Type Wild-Card Expression'? extends HTMLNode'
means that aVector<TagNode>, Vector<TextNode>
orVector<CommentNode>
will all be accepted by this paramter without causing an exception throw.
These 'sub-type' Vectors are often returned as search results from the classes in the'NodeSearch'
vpackage.sPos
- This is the (integer)Vector
-index that sets a limit for the left-mostVector
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'inclusive' meaning that theHTMLNode
at thisVector
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Vector
, an exception will be thrown.ePos
- This is the (integer)Vector
-index that sets a limit for the right-mostVector
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the'HTMLNode'
at thisVector
-index will not be visited by this method.
NOTE: If this value is larger than the size of input theVector
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the size of the inputVector
-parameter.- Returns:
- The number of new-line characters that have been inserted onto this page.
- Throws:
java.lang.IndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thesize
of theVector
- If
'ePos'
is zero, or greater than the size of theVector
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toVector.size()
, before this check is done.
- If
- Code:
- Exact Method Body:
return AddNewLines.beforeAllBlockTags(html, sPos, ePos);
-
textChecker
public static java.lang.String textChecker (java.util.Vector<HTMLNode> html)
- Code:
- Exact Method Body:
return textChecker(html, 0, -1);
-
textChecker
public static java.lang.String textChecker (java.util.Vector<HTMLNode> html, DotPair dp)
- Code:
- Exact Method Body:
return textChecker(html, dp.start, dp.end+1);
-
textChecker
public static java.lang.String textChecker (java.util.Vector<HTMLNode> html, int sPos, int ePos)
This method may be used to verify your page changes
This method does not actually perform any HTML-Modifications at all. The purpose of it is merely to facilitate checking that the results of your Pretty-Printing Desicions have done justice to the original, and intended, output.
This method attempts to verify that the resulting Output-Text from a Prettify-Operation on an HTML-Vector
was not changed during that clean-up. This method is intended for use with Testing-Suite Tools. This method ignores all instances ofCommentNode
andTagNode
that are present within the input HTML-Vector
. It iterates only theTextNode
instances.
During this iteration process, all identified White-Space is summarily removed from eachTextNode
. After this strip, the remaining characters (from eachTextNode
) are appended to a simple JavaStringBuilder
.
Applicability:
As a means of checking & testing your Pretty-Printed Output, this method can be used to facilitate a 'Before & After' comparison on your page when modifying a Web-Page for Pretty-Printing.
If your goal was merely to add or remove White-Space, then checking to ensure that all non-white-space characters are identical will help validate the modifications you have made. Please see the example code below:
Example:
// Strip everything but HTML Text-Nodes. Append all non-white-space characters to a String // This String can then be used to evaluate if any text was accidentally modified, removed or // added. This method 'textChecker' is essentially a Test-Framework Helper-Method. String textOnlyOld = PrettyPrint.textChecker(html, 0, -1); cleanIt(myHTML, 0, -1); // Repeat the previous process using the "Pretty-Printed Text" instead. String textOnlyNew = PrettyPrint.textChecker(html, 0, -1); System.out.println( textOnlyOld.equals(textOnlyNew) ? "The original & prettified text are identical" : "Houston, there may be a problem, you have modified your text" );
- Parameters:
html
- This may be any Vectorized-HTML Web-Page (or sub-page).
The Variable-Type Wild-Card Expression'? extends HTMLNode'
means that aVector<TagNode>, Vector<TextNode>
orVector<CommentNode>
will all be accepted by this paramter without causing an exception throw.
These 'sub-type' Vectors are often returned as search results from the classes in the'NodeSearch'
vpackage.sPos
- This is the (integer)Vector
-index that sets a limit for the left-mostVector
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'inclusive' meaning that theHTMLNode
at thisVector
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Vector
, an exception will be thrown.ePos
- This is the (integer)Vector
-index that sets a limit for the right-mostVector
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the'HTMLNode'
at thisVector
-index will not be visited by this method.
NOTE: If this value is larger than the size of input theVector
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the size of the inputVector
-parameter.- Returns:
- The number of new-line characters that have been inserted onto this page.
- Throws:
java.lang.IndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thesize
of theVector
- If
'ePos'
is zero, or greater than the size of theVector
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toVector.size()
, before this check is done.
- If
- Code:
- Exact Method Body:
LV l = new LV(html, sPos, ePos); StringBuilder sb = new StringBuilder(); HTMLNode n = null; for (int i=l.start; i < l.end; i++) if ((n = html.elementAt(i)).isTextNode()) sb.append(n.str.replaceAll("\\s+","")); return sb.toString();
-
tagChecker
public static java.lang.String tagChecker(java.util.Vector<HTMLNode> html)
- Code:
- Exact Method Body:
return tagChecker(html, 0, -1);
-
tagChecker
public static java.lang.String tagChecker(java.util.Vector<HTMLNode> html, DotPair dp)
- Code:
- Exact Method Body:
return tagChecker(html, dp.start, dp.end+1);
-
tagChecker
public static java.lang.String tagChecker(java.util.Vector<HTMLNode> html, int sPos, int ePos)
This method may be used to verify your page changes
This method does not actually perform any HTML-Modifications at all. The purpose of it is merely to facilitate checking that the results of your Pretty-Printing Desicions have done justice to the original, and intended, output.
This method attempts to verify that the resulting list of TagNode's in an HTML-Vector
has not been changed as a result f a Prettify-Operation. This method is intended for use with Testing-Suite Tools. This method ignores all instances ofCommentNode
andTextNode
that are present within the input HTML-Vector
. It iterates only theTagtNode
instances.
During this iteration process, the TagNode-Field'stok
andisClosing
are checked, and appending to aStringBuilder
. The Java-String
produced by thisStringBuilder
-Operation are the results returned by this method.
Applicability:
As a means of checking & testing your Pretty-Printed Output, this method can be used to facilitate a 'Before & After' comparison on your page when modifying a Web-Page for Pretty-Printing.
If your goal was merely to add or remove White-Space, then checking to ensure that all non-white-space characters are identical will help validate the modifications you have made. Please see the example code below:
Example:
// Strip everything but HTML Tag-Nodes. Append "TagNode.tok" String's into a Master // StringBuilder instance. This String can then be used to evaluate if any nodes were // accidentally modified, removed or added. This method 'tagChecker' is essentially a // Test-Framework Helper-Method. String tagListOld = PrettyPrint.textChecker(html, 0, -1); cleanIt(myHTML, 0, -1); // Repeat the previous process with the "Pretty-Printed Text" String tagListNew = PrettyPrint.textChecker(html, 0, -1); System.out.println( tagListOld.equals(tagListNew) ? "The original & prettified list of TagNode's are identical" : "Houston, there may be a problem, you have added or removed TagNode's" );
- Parameters:
html
- This may be any Vectorized-HTML Web-Page (or sub-page).
The Variable-Type Wild-Card Expression'? extends HTMLNode'
means that aVector<TagNode>, Vector<TextNode>
orVector<CommentNode>
will all be accepted by this paramter without causing an exception throw.
These 'sub-type' Vectors are often returned as search results from the classes in the'NodeSearch'
vpackage.sPos
- This is the (integer)Vector
-index that sets a limit for the left-mostVector
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'inclusive' meaning that theHTMLNode
at thisVector
-index will be visited by this method.
NOTE: If this value is negative, or larger than the length of the input-Vector
, an exception will be thrown.ePos
- This is the (integer)Vector
-index that sets a limit for the right-mostVector
-position to inspect/search inside the inputVector
-parameter.
This value is considered 'exclusive' meaning that the'HTMLNode'
at thisVector
-index will not be visited by this method.
NOTE: If this value is larger than the size of input theVector
-parameter, an exception will throw.
ALSO: Passing a negative value to this parameter,'ePos'
, will cause its value to be reset to the size of the inputVector
-parameter.- Returns:
- The number of new-line characters that have been inserted onto this page.
- Throws:
java.lang.IndexOutOfBoundsException
- This exception shall be thrown if any of the following are true:- If
'sPos'
is negative, or ifsPos
is greater-than-or-equal-to thesize
of theVector
- If
'ePos'
is zero, or greater than the size of theVector
- If the value of
'sPos'
is a larger integer than'ePos'
. If'ePos'
was negative, it is first reset toVector.size()
, before this check is done.
- If
- Code:
- Exact Method Body:
LV l = new LV(html, sPos, ePos); StringBuilder sb = new StringBuilder(); TagNode tn = null; for (int i=l.start; i < l.end; i++) if ((tn = html.elementAt(i).ifTagNode()) != null) sb.append((tn.isClosing ? " /" : " ") + tn.tok); return sb.toString();
-
-