001package Torello.HTML;
002
003import java.util.Vector;
004
005import Torello.Java.LV;
006
007/*
008removeAllIndentation(v)
009
010removeDuplicateNewLines(v)
011
012addNewsLinesIf(Vector v, String tags...)
013    // For tags that don't already have one
014
015indent(v, tags, numSpaces)
016    // Specify which are "Indent able Block Tags"
017    // Only Indent tags that are on a new-line
018
019setAllQuotes(v, SD, Consumer<? extends Exception> failPolicy)
020removeQuotes(v, Consumer<? extends Exception> failPolicy, String tags...)
021removeQuotes(v, String tags...) // Remove whenever possible
022
023setAllCase(v, boolean lowerOrUpper, boolean tagOrTagPlusAttr)
024*/
025
026/**
027 * Under-Development.  Suite of Utilities for Cleaning up an HTML Page.
028 */
029public class PrettyPrint
030{
031    // ********************************************************************************************
032    // ********************************************************************************************
033    // Remove All Indentation
034    // ********************************************************************************************
035    // ********************************************************************************************
036
037
038    /**
039     * Convenience Method.
040     * <BR />Identical To: {@link #removeAllIndentation(Vector, int, int)}
041     */
042    public static int removeAllIndentation(Vector<HTMLNode> html)
043    { return RemAllIndent.remove(html, 0, -1); }
044
045    /**
046     * Convenience Method.
047     * <BR />Receives: {@code DotPair} 
048     * <BR />Identical To: {@link #removeAllIndentation(Vector, int, int)}
049     */
050    public static int removeAllIndentation(Vector<HTMLNode> html, DotPair dp)
051    { return RemAllIndent.remove(html, dp.start, dp.end + 1); }
052
053    /**
054     * Any White-Space <B STYLE='color: red;'><I>which immediately follows a {@code '\n'}
055     * New-Line character</B></I> is removed from all {@link TextNode TextNode's}, by this method.
056     * 
057     * @param html <EMBED CLASS='external-html' DATA-FILE-ID=HTMLVEC>
058     * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSVEC>
059     * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSVEC>
060     * 
061     * @return The number of {@code TextNode's} that changed as a result of this method.
062     * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX>
063     */
064    public static int removeAllIndentation(Vector<HTMLNode> html, int sPos, int ePos)
065    { return RemAllIndent.remove(html, sPos, ePos); }
066
067
068    // ********************************************************************************************
069    // ********************************************************************************************
070    // Remove All Indentation
071    // ********************************************************************************************
072    // ********************************************************************************************
073
074
075    /**
076     * Convenience Method.
077     * <BR />Identical To: {@link #ensureBlockTagsOnNewlines(Vector, int, int)}
078     */
079    public static int ensureBlockTagsOnNewlines(Vector<HTMLNode> html)
080    { return AddNewLines.beforeAllBlockTags(html, 0, -1); }
081
082    /**
083     * Convenience Method.
084     * <BR />Receives: {@code DotPair} 
085     * <BR />Identical To: {@link #ensureBlockTagsOnNewlines(Vector, int, int)}
086     */
087    public static int ensureBlockTagsOnNewlines(Vector<HTMLNode> html, DotPair dp)
088    { return AddNewLines.beforeAllBlockTags(html, dp.start, dp.end+1); }
089
090    /**
091     * <EMBED CLASS='external-html' DATA-FILE-ID=PRPR_ENSURE_BTNL>
092     * @param html <EMBED CLASS='external-html' DATA-FILE-ID=HTMLVEC>
093     * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSVEC>
094     * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSVEC>
095     * 
096     * @return The number of new-line characters that have been inserted onto this page.
097     * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX>
098     */
099    public static int ensureBlockTagsOnNewlines(Vector<HTMLNode> html, int sPos, int ePos)
100    { return AddNewLines.beforeAllBlockTags(html, sPos, ePos); }
101
102
103    // ********************************************************************************************
104    // ********************************************************************************************
105    // CHECKER: Text-Nodes & Remove All White-Space
106    // ********************************************************************************************
107    // ********************************************************************************************
108
109
110    /**
111     * Convenience Method.
112     * <BR />Invokes: {@link #textChecker(Vector, int, int)}
113     */
114    public static String textChecker(Vector<HTMLNode> html)
115    { return textChecker(html, 0, -1); }
116
117    /**
118     * Convenience Method.
119     * <BR />Receives: {@code DotPair} 
120     * <BR />Invokes: {@link #textChecker(Vector, int, int)}
121     */
122    public static String textChecker(Vector<HTMLNode> html, DotPair dp)
123    { return textChecker(html, dp.start, dp.end+1); }
124
125    /**
126     * <EMBED CLASS='external-html' DATA-FILE-ID=PRPR_TXT_CHECKER>
127     * @param html <EMBED CLASS='external-html' DATA-FILE-ID=HTMLVEC>
128     * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSVEC>
129     * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSVEC>
130     * 
131     * @return The number of new-line characters that have been inserted onto this page.
132     * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX>
133     */
134    public static String textChecker(Vector<HTMLNode> html, int sPos, int ePos)
135    {
136        LV              l   = new LV(html, sPos, ePos);
137        StringBuilder   sb  = new StringBuilder();
138        HTMLNode        n   = null;
139
140        for (int i=l.start; i < l.end; i++)
141            if ((n = html.elementAt(i)).isTextNode())
142                sb.append(n.str.replaceAll("\\s+",""));
143
144        return sb.toString();
145    }
146
147
148    // ********************************************************************************************
149    // ********************************************************************************************
150    // CHECKER: Tag-Nodes Only
151    // ********************************************************************************************
152    // ********************************************************************************************
153
154
155    /**
156     * Convenience Method.
157     * <BR />Invokes: {@link #tagChecker(Vector, int, int)}
158     */
159    public static String tagChecker(Vector<HTMLNode> html)
160    { return tagChecker(html, 0, -1); }
161
162    /**
163     * Convenience Method.
164     * <BR />Receives: {@code DotPair} 
165     * <BR />Invokes: {@link #tagChecker(Vector, int, int)}
166     */
167    public static String tagChecker(Vector<HTMLNode> html, DotPair dp)
168    { return tagChecker(html, dp.start, dp.end+1); }
169
170    /**
171     * <EMBED CLASS='external-html' DATA-FILE-ID=PRPR_TAG_CHECKER>
172     * @param html <EMBED CLASS='external-html' DATA-FILE-ID=HTMLVEC>
173     * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSVEC>
174     * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSVEC>
175     * 
176     * @return The number of new-line characters that have been inserted onto this page.
177     * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX>
178     */
179    public static String tagChecker(Vector<HTMLNode> html, int sPos, int ePos)
180    {
181        LV              l   = new LV(html, sPos, ePos);
182        StringBuilder   sb  = new StringBuilder();
183        TagNode         tn  = null;
184
185        for (int i=l.start; i < l.end; i++)
186            if ((tn = html.elementAt(i).ifTagNode()) != null)
187                sb.append((tn.isClosing ? " /" : " ") + tn.tok);
188
189        return sb.toString();
190    }
191}