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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | package Torello.HTML; import java.util.*; import java.util.function.BiConsumer; import java.util.concurrent.locks.*; import static Torello.Java.C.*; import Torello.Java.LV; import Torello.JavaDoc.StaticFunctional; import Torello.JavaDoc.Excuse; /** * A fast way to print Web-Page {@code Vector's} to a {@code String}. * * <EMBED CLASS='external-html' DATA-FILE-ID=DEBUG> */ @StaticFunctional(Excused="lastWasNewLine", Excuses=Excuse.FLAG) public class Debug { private Debug() { } /** * Convenience Method. * <BR />Invokes: {@link #print(Vector, int, int, BiConsumer)} * <BR />Requests: Print entire {@code 'page' (Vector)} */ public static String print (Vector<? extends HTMLNode> page, BiConsumer<HTMLNode, StringBuffer> printerVersion) { return print(page, 0, -1, printerVersion); } /** * Convenience Method. * <BR />Invokes: {@link #print(Vector, int, int, BiConsumer)} * <BR />Requests: Print entire {@code SubSection} */ public static String print(SubSection sec, BiConsumer<HTMLNode, StringBuffer> printerVersion) { return print(sec.html, 0, -1, printerVersion); } /** * Convenience Method. * <BR />Invokes: {@link #print(Vector, int, int, BiConsumer)} * <BR />Requests: Print {@code 'page'} sub-range defined by parameter {@code 'dp'} */ public static String print( Vector<? extends HTMLNode> page, DotPair dp, BiConsumer<HTMLNode, StringBuffer> printerVersion ) { return print(page, dp.start, dp.end + 1, printerVersion); } /** * Converts HTML to a {@code String} - adding readable notes & messages. This will print * every node in the vectorized-html between starting-index {@code int sPos} (inclusively) and * ending-index {@code int ePos}, exclusively. * * @param page Any vectorized-html page or sub-page. * * @param sPos The printing of nodes will begin at position {@code 'sPos'} - inclusively. * * @param ePos The node at element-position will not be printed (exclusive), and the one * previous shall print. * * @param printerVersion The complicated-looking function-pointer comes from the package * {@code 'java.util.function.'} The literal code to pass to this parameter would be: * <SPAN STYLE="color: blue; font-weight: bold;">Debug::A, Debug::B ... Debug::K</SPAN>. The * programmer may also create a print-function if so desired, and pass that using a * function-pointer or a {@code lambda-expression} * * @return a pretty printed {@code String} of the nodes in this {@code Vector}. * * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=VIOOBEX> * * @see LV */ public static String print( Vector<? extends HTMLNode> page, int sPos, int ePos, BiConsumer<HTMLNode, StringBuffer> printerVersion ) { LV l = new LV(page, sPos, ePos); StringBuffer sb = new StringBuffer(); for (int i=l.start; i < l.end; i++) printerVersion.accept(page.elementAt(i), sb); return sb.toString(); } /** * Converts HTML to a {@code String} - adding readable notes & messages. This version * shall only print nodes whose index "is pointed to" by the elements of the position-index * array parameter {@code int[] posArr}. * * @param page Any vectorized-html page or sub-page. * * @param posArr This is an array of index-pointers into the vectorized-html webpage * parameter 'page.' These arrays are usually created by calls to the {@code package * NodeSearch} using classes with the <B>{@code 'Find'} Key-Word</B>. Such classes return * integer-arrays of pointers to nodes that matched a specific search criteria. * * @param printerVersion The complicated-looking function-pointer comes from the package * 'java.util.function.' The literal code to pass to this parameter would be: * <SPAN STYLE="color: blue; font-weight: bold;">Debug::A, Debug::B ... Debug::K</SPAN>. The * programmer may also create a print-function if so desired, and pass that using a * function-pointer or a {@code lambda-expression}. * * @return a pretty printed {@code String} of the nodes in this {@code Vector} pointed-to by * the elements of {@code 'posArr'} * * @throws ArrayIndexOutOfBoundsException If any of the elements in {@code 'posArr'} contain * index-pointers that are out of range of {@code Vector} parameter {@code 'page'}, then java * will, naturally, throw this exception. */ public static String print( Vector<? extends HTMLNode> page, int[] posArr, BiConsumer<HTMLNode, StringBuffer> printerVersion ) { StringBuffer sb = new StringBuffer(); for (int i : posArr) printerVersion.accept(page.elementAt(i), sb); return sb.toString(); } /** * Converts HTML to a {@code String} - adding readable notes & messages. This version * shall only print nodes whose index "is pointed to" by the elements of the position-index * array parameter {@code int[] posArr}. * * <BR /><BR /><B CLASS=JDDescLabel>Note about Usability:</B> * * <BR />This version of print can be invaluable during debugging when trolling through pages * of HTML is reduced to only the indexes that are indicated by an index-pointer array. The * addition of a "radix" means that some nodes before and after the identified node can be * looked at too. * * @param page Any vectorized-html page or sub-page. * * @param posArr This is an array of index-pointers into the vectorized-html webpage * parameter {@code 'page'}. These arrays are usually created by calls to the {@code package * NodeSearch} using classes with the <B>{@code 'Find'} Key-Word</B>. Such classes return * integer-arrays of pointers to nodes that matched a specific search criteria. * * @param radix The number of nodes, in both the forward and reverse directions, to include * in the output for a match. * * @param nodesHaveBeenSkippedString This will be printed whenever the print-logic has skipped * nodes in the list during printing. An example of a good {@code String} to use would just be * {@code Sting s = "***** Nodes Skipped *****\n";}, but any choice will do - so long as it * gives the appearance the during printing there are "blank spots" that skipped some of the * content of the html. * * @param addColorCurlyBracesToRequestedNodes If this is {@code TRUE}, and the * {@code class Shell.C} has its color {@code String} evaluated, because the program is running * on some acceptable version of UNIX, then a bright-green colored curly-brace set will * surround each node that is explicitly a member of the index-pointer-array, parameter * {@code 'posArr'} * * <BR /><BR /><B>NOTE:</B> If this is unclear, this method prints every node that is * pointed-to by the {@code 'posArr'} parameter. In addition to these nodes, it will print * {@code 'radix'} nodes before and {@code 'radix'} nodes after each of the elements pointed to * by the {@code 'posArr'} parameter. When viewing the output, if it is important to know * which nodes are explicitly in the 'request-list' then adding bright-green curly braces to * those output nodes will differentiate them from other nodes that are being printed because * of the {@code radix} parameter. * * @param printerVersion The complicated-looking function-pointer comes from the package * {@code 'java.util.function.'} The literal code to pass to this parameter would be: * <SPAN STYLE="color: blue; font-weight: bold;">Debug::A, Debug::B ... Debug::K</SPAN>. The * programmer may also create a print-function if so desired, and pass that using a * function-pointer or a {@code lambda-expression} * * @return a pretty printed {@code String} of the nodes in this {@code Vector} pointed-to by * the elements of {@code 'posArr'}, surrounded by {@code 'radix'} number of nodes in either * direction. * * @throws ArrayIndexOutOfBoundsException If any of the elements in {@code posArr} contain * index-pointers that are out of range of {@code Vector}-parameter {@code 'page'}, then java * will, naturally, throw this exception * * @see Torello.Java.C */ public static String print( Vector<? extends HTMLNode> page, int[] posArr, int radix, String nodesHaveBeenSkippedString, boolean addColorCurlyBracesToRequestedNodes, BiConsumer<HTMLNode, StringBuffer> printerVersion ) { StringBuffer sb = new StringBuffer(); // Used internally to print-curly braces TreeSet<Integer> ts = null; // Initialize it if the curly-braces are requested if (addColorCurlyBracesToRequestedNodes) { ts = new TreeSet<>(); for (int pos : posArr) ts.add(Integer.valueOf(pos)); } int start = 0; int end = 0; for (int i : posArr) { // Start at precisely the current index-pointer MINUS the radix. // OR ... If the loop is about to "reprint" a node because the radix is larger than the // space between two consecutive nodes - then start at the node just after the // last node that was printed. start = Math.max(i - radix, end + 1); // A large radix might get the loop past the end of the vector, AVOID THAT. if (start > (page.size() - 1)) break; // If the next node to be printed is immediately after the previous node that was // printed IN THE UNDERLYING HTML, then no nodes have been skipped, so don't print the // "nodesHaveBeenSkippedString" ... otherwise print it! if (start > (end + 1)) sb.append(nodesHaveBeenSkippedString); // Make sure not to go past the last node in the string, or exception will throw. end = Math.min(i + radix, (page.size() - 1)); // It is possible that 'start' is greater than 'end' (too much overlap), in which case // the outer loop will continue on to the next index-pointer. // NOTE: uses 'j <= end' NOT 'j < end' !!! (TRICKY) if (addColorCurlyBracesToRequestedNodes) for (int j=start; j <= end; j++) { boolean addColorBrackets = ts.contains(Integer.valueOf(j)); if (addColorBrackets) sb.append(BGREEN + "{" + RESET); printerVersion.accept(page.elementAt(j), sb); if (addColorBrackets) sb.append(BGREEN + "}" + RESET); } else for (int j=start; j <= end; j++) printerVersion.accept(page.elementAt(j), sb); } return sb.toString(); } // ******************************************************************************************** // ******************************************************************************************** // Protected, Internal Print Methods // ******************************************************************************************** // ******************************************************************************************** /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'A'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * * @param sb the {@code StringBuffer} that is receiving the print command. * * @see TagNode#str */ public static void A(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; sb.append("[" + name + ":" + n.str + "]"); } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'B'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * * @param sb the {@code StringBuffer} that is receiving the print command. * * @see TagNode#str */ public static void B(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; sb.append("[" + name + ", StrLen=" + n.str.length() + ": " + n.str + "]"); } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'C'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * * @param sb the {@code StringBuffer} that is receiving the print command. * * @see TagNode#str * @see TagNode#tok * @see TagNode#isClosing */ public static void C(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; if (n.isTagNode()) sb.append( "[" + name + ", n.tok=" + ((TagNode) n).tok + ", " + "n.isClosing=" + ((TagNode) n).isClosing + ": " + n.str + "]" ); else sb.append("[" + name + ": " + n.str + "]"); } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'D'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * @param sb the {@code StringBuffer} that is receiving the print command. * * @see TagNode#str * @see TagNode#tok * @see TagNode#isClosing */ public static void D(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; if (n.isTagNode()) sb.append( "[" + name + ", StrLen=" + n.str.length() + ", n.tok=" + ((TagNode) n).tok + ", " + "n.isClosing=" + ((TagNode) n).isClosing + ": " + n.str + "]" ); else sb.append("[" + name + ", StrLen=" + n.str.length() + ": " + n.str + "]"); } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'E'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * @param sb the {@code StringBuffer} that is receiving the print command. * * @see TagNode#str * @see TagNode#tok * @see TagNode#isClosing */ public static void E(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; if (n.isTagNode()) sb.append( "[" + name + ", StrLen=" + n.str.length() + ", n.tok=" + ((TagNode) n).tok + ", " + "n.isClosing=" + ((TagNode) n).isClosing + "]\n[" + n.str + "]\n" ); else sb.append("[" + name + ", StrLen=" + n.str.length() + "]\n[" + n.str + "]\n"); } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'F'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * @param sb the {@code StringBuffer} that is receiving the print command. * * @see TagNode#str * @see TagNode#isClosing * @see TagNode#toStringAV() */ public static void F(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; if (n.isTagNode() && (! ((TagNode) n).isClosing)) sb.append(((TagNode) n).toStringAV()); else sb.append("[" + name + ", StrLen=" + n.str.length() + "]\n[" + n.str + "]\n"); } /** Method 'G' is a hack - uses a global variable - THIS IS A HACK. DO NOT WORRY ABOUT IT. */ private static boolean lastWasNewLine = false; /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'G'</B></CODE> * * <BR /><BR /><B CLASS=JDDescLabel>Thread Safety Point:</B> * * <BR />Print Style G <I>is not {@code Thread} safe!</I>. * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * @param sb the {@code StringBuffer} that is receiving the print command. * * @see TagNode#str * @see TagNode#tok * @see TagNode#isClosing * @see TagNode#toStringAV() */ public static void G(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; TagNode tn = null; if ( n.isTagNode() && (! (tn = (TagNode) n).isClosing) && (tn.str.length() > (tn.tok.length() + 4)) ) { sb.append((lastWasNewLine ? "" : "\n") + ((TagNode) n).toStringAV()); lastWasNewLine = true; } else { sb.append("[" + name + ", StrLen=" + n.str.length() + ": " + n.str + "]"); lastWasNewLine = false; }; } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'H'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * @param sb the {@code StringBuffer} that is receiving the print command. * * @see C * @see C#RESET * @see C#BRED * @see C#BCYAN * @see TagNode#str * @see TagNode#tok * @see TagNode#isClosing */ public static void H(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; if (n.isTagNode()) sb.append( "[" + name + ", StrLen=" + BCYAN + n.str.length() + RESET + ", " + "n.tok=" + BCYAN + ((TagNode) n).tok + RESET +", " + "n.isClosing=" + BCYAN + ((TagNode) n).isClosing + RESET + "]" + "[" + BRED + n.str + RESET + "]" ); else sb.append( "[" + name + ", StrLen=" + BCYAN + n.str.length() + RESET + ": " + BRED + n.str + RESET + "]" ); } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'I'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * @param sb the {@code StringBuffer} that is receiving the print command. * * @see C * @see C#RESET * @see C#BRED * @see C#BCYAN * @see TagNode#str * @see TagNode#tok */ public static void I(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; if (n.isTagNode()) sb.append( "[" + name + ", StrLen=" + BCYAN + n.str.length() + RESET + ", " + "n.tok=" + BCYAN + ((TagNode) n).tok + RESET + ", " + "n.isClosing=" + BCYAN + ((TagNode) n).isClosing + RESET + "]\n" + "[" + BRED + n.str + RESET + "]\n" ); else sb.append( "[" + name + ", StrLen=" + BCYAN + n.str.length() + RESET + "]\n" + "[" + BRED + n.str + RESET + "]\n" ); } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'J'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * @param sb the {@code StringBuffer} that is receiving the print command. * * @see C * @see C#RESET * @see C#BRED * @see C#BYELLOW * @see TagNode#str */ public static void J(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent // exception. if (name.equals("")) name = "Anonymous Class"; if (n.isTagNode()) sb.append("[" + BRED + n.str + RESET + "]"); else if (n.isCommentNode()) sb.append("[" + BYELLOW + n.str + RESET + "]"); else sb.append("[" + n.str + "]"); } /** * Prints an HTML Element ({@code TagNode, TextNode} or {@code CommentNode}) using * <CODE><B>Print Style 'K'</B></CODE> * * @param n An {@code HTMLNode} that will print to the {@code StringBuffer} * @param sb the {@code StringBuffer} that is receiving the print command. * * @see C * @see C#RESET * @see C#BRED * @see C#BYELLOW * @see TagNode#str */ public static void K(HTMLNode n, StringBuffer sb) { String name = n.getClass().getSimpleName(); // Extremely Unlikely Programmer has extended HTMLNode, but just in case, to prevent exception. if (name.equals("")) name = "Anonymous Class"; if (n.isTagNode()) sb.append("[" + BRED + n.str + RESET + "]\n"); else if (n.isCommentNode()) sb.append("[" + BYELLOW + n.str + RESET + "]\n"); else sb.append("[" + n.str + "]\n"); } } |