1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | package Torello.HTML; import java.util.Vector; import Torello.Java.LV; /* removeAllIndentation(v) removeDuplicateNewLines(v) addNewsLinesIf(Vector v, String tags...) // For tags that don't already have one indent(v, tags, numSpaces) // Specify which are "Indent able Block Tags" // Only Indent tags that are on a new-line setAllQuotes(v, SD, Consumer<? extends Exception> failPolicy) removeQuotes(v, Consumer<? extends Exception> failPolicy, String tags...) removeQuotes(v, String tags...) // Remove whenever possible setAllCase(v, boolean lowerOrUpper, boolean tagOrTagPlusAttr) */ /** * Under-Development. Suite of Utilities for Cleaning up an HTML Page. */ public class PrettyPrint { // ******************************************************************************************** // ******************************************************************************************** // Remove All Indentation // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Identical To: {@link #removeAllIndentation(Vector, int, int)} */ public static int removeAllIndentation(Vector<HTMLNode> html) { return RemAllIndent.remove(html, 0, -1); } /** * Convenience Method. * <BR />Receives: {@code DotPair} * <BR />Identical To: {@link #removeAllIndentation(Vector, int, int)} */ public static int removeAllIndentation(Vector<HTMLNode> html, DotPair dp) { return RemAllIndent.remove(html, dp.start, dp.end + 1); } /** * Any White-Space <B STYLE='color: red;'><I>which immediately follows a {@code '\n'} * New-Line character</B></I> is removed from all {@link TextNode TextNode's}, by this method. * * @param html <EMBED CLASS='external-html' DATA-FILE-ID=HTMLVEC> * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSVEC> * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSVEC> * * @return The number of {@code TextNode's} that changed as a result of this method. * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX> */ public static int removeAllIndentation(Vector<HTMLNode> html, int sPos, int ePos) { return RemAllIndent.remove(html, sPos, ePos); } // ******************************************************************************************** // ******************************************************************************************** // Remove All Indentation // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Identical To: {@link #ensureBlockTagsOnNewlines(Vector, int, int)} */ public static int ensureBlockTagsOnNewlines(Vector<HTMLNode> html) { return AddNewLines.beforeAllBlockTags(html, 0, -1); } /** * Convenience Method. * <BR />Receives: {@code DotPair} * <BR />Identical To: {@link #ensureBlockTagsOnNewlines(Vector, int, int)} */ public static int ensureBlockTagsOnNewlines(Vector<HTMLNode> html, DotPair dp) { return AddNewLines.beforeAllBlockTags(html, dp.start, dp.end+1); } /** * <EMBED CLASS='external-html' DATA-FILE-ID=PRPR_ENSURE_BTNL> * @param html <EMBED CLASS='external-html' DATA-FILE-ID=HTMLVEC> * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSVEC> * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSVEC> * * @return The number of new-line characters that have been inserted onto this page. * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX> */ public static int ensureBlockTagsOnNewlines(Vector<HTMLNode> html, int sPos, int ePos) { return AddNewLines.beforeAllBlockTags(html, sPos, ePos); } // ******************************************************************************************** // ******************************************************************************************** // CHECKER: Text-Nodes & Remove All White-Space // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Invokes: {@link #textChecker(Vector, int, int)} */ public static String textChecker(Vector<HTMLNode> html) { return textChecker(html, 0, -1); } /** * Convenience Method. * <BR />Receives: {@code DotPair} * <BR />Invokes: {@link #textChecker(Vector, int, int)} */ public static String textChecker(Vector<HTMLNode> html, DotPair dp) { return textChecker(html, dp.start, dp.end+1); } /** * <EMBED CLASS='external-html' DATA-FILE-ID=PRPR_TXT_CHECKER> * @param html <EMBED CLASS='external-html' DATA-FILE-ID=HTMLVEC> * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSVEC> * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSVEC> * * @return The number of new-line characters that have been inserted onto this page. * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX> */ public static String textChecker(Vector<HTMLNode> html, int sPos, int ePos) { 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(); } // ******************************************************************************************** // ******************************************************************************************** // CHECKER: Tag-Nodes Only // ******************************************************************************************** // ******************************************************************************************** /** * Convenience Method. * <BR />Invokes: {@link #tagChecker(Vector, int, int)} */ public static String tagChecker(Vector<HTMLNode> html) { return tagChecker(html, 0, -1); } /** * Convenience Method. * <BR />Receives: {@code DotPair} * <BR />Invokes: {@link #tagChecker(Vector, int, int)} */ public static String tagChecker(Vector<HTMLNode> html, DotPair dp) { return tagChecker(html, dp.start, dp.end+1); } /** * <EMBED CLASS='external-html' DATA-FILE-ID=PRPR_TAG_CHECKER> * @param html <EMBED CLASS='external-html' DATA-FILE-ID=HTMLVEC> * @param sPos <EMBED CLASS='external-html' DATA-FILE-ID=SPOSVEC> * @param ePos <EMBED CLASS='external-html' DATA-FILE-ID=EPOSVEC> * * @return The number of new-line characters that have been inserted onto this page. * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX> */ public static String tagChecker(Vector<HTMLNode> html, int sPos, int ePos) { 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(); } } |