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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | package Torello.HTML.NodeSearch; import java.util.Vector; import java.util.function.Predicate; import java.util.regex.Pattern; import Torello.HTML.*; import Torello.Java.StrFilter; /** * A functional-interface / lambda-target, and several {@code static}-builders for generating * instances of them, which extends {@code java.util.function.Predicate} and encapsulates * search-criteria into a <CODE>Predicate<{@link TagNode}></CODE> * * <BR /><BR /><EMBED CLASS='external-html' DATA-FILE-ID=AVT> */ @FunctionalInterface public interface AVT extends Predicate<TagNode>, java.io.Serializable { /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUIDFI> */ public static final long serialVersionUID = 1; // ****************************************************************************************** // Functional-Interface Method // ****************************************************************************************** /** * <B><SPAN STYLE="color: red;">FUNCTIONAL-INTERFACE BOOLEAN METHOD:</SPAN></B> This is the * method that fulfils this {@code functional-interface 'test'} method. * * @param tn This method will be called - once for each {@code TagNode} found inside of a * vectorized HTML page. * * @return If the {@code TagNode} meets the test's "inclusion requirements", then this method * should return {@code TRUE}. */ public boolean test(TagNode tn); // ****************************************************************************************** // TextComparitor factory builders // ****************************************************************************************** /** * This is a {@code static} factory method that generates {@code AVT-Predicate's} * ({@code Predicate<TagNode>}). It saves the user of typing the lambda information by hand, * and does a validation check too. The primary use of this class is that the results of one * factory method may be "AND-chained" or "OR-chained" with another to make search requirements * more specific. * * @param innerTag This also goes by the term "attribute" in many HTML specifications. It is * the <B STYLE="color: red;">name</B> of the attribute, not it's * <B STYLE="color: red;">value</B>. The <B STYLE="color: red;">value</B> will be found (if the * {@code TagNode} contains this attribute), and the parameter {@code 'TextComparitor'} will be * used to compare this <B STYLE="color: red;">value</B> - <I>dependent upon which * {@code 'TextComparitor'} is used</I> against the Compare-Strings * * @param tc This may be any of the listed {@code TextComparitor's} in the class. There are * quite a few "pre-defined" {@code static} members in the {@link TextComparitor} class. There * are many that have both long names, and abbreviated names which can be interchangeably used * for readability purposes. * * @param compareStr These are passed to the {@code 'TextComparitor'} when using to perform * tests on the attribute <B STYLE="color: red;">value</B>. * * @return <EMBED CLASS='external-html' DATA-FILE-ID=RETCMP1> * * @see ARGCHECK#innerTag(String) * @see ARGCHECK#TC(TextComparitor, String[]) * @see TagNode#AV(String) * @see TextComparitor#test(String, String[]) * * @throws InnerTagKeyException <EMBED CLASS='external-html' DATA-FILE-ID=ITKEYEX> * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT cmp(String innerTag, TextComparitor tc, String... compareStr) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If these tests fail, the returned predicate would absolutely fail. final String innerTagLC = ARGCHECK.innerTag(innerTag); ARGCHECK.TC(tc, compareStr); // Minimum length for field TagNode.str to have before it could possible contain the attribute // Obviously, the TagNode would have to have a min-length that includes the // attribute-name length + '< ' and '>' final int MIN_LEN = innerTag.length() + 3; // Java's "Lambda-Expression" Syntax (like an "anonymous method"). // AVT extends functional-interface Predicate<TagNode> return (TagNode tn) -> { // This eliminates testing any TagNode that simply COULD NOT contain the // specified attribute. (an optimization) if (tn.isClosing || (tn.str.length() <= (tn.tok.length() + MIN_LEN))) return false; // Retrieve the value of the requested "inner-tag" (HTML Attribute) Key-Value Pair // from the input HTML-Element (TagNode) String itv = tn.AV(innerTagLC); // REG-EX MATCHER, MORE EXPENSIVE // If the innerTag's value is null, then the inner-tag was not a key-value // found inside the TagNode: return false. // Otherwise return the 'tc' test-results on that value using the named 'tc' // comparison on the compare-strings. return (itv == null) ? false : tc.test(itv, compareStr); }; } /** * <EMBED CLASS='external-html' DATA-FILE-ID=CMPKIITNF1> * * @param innerTag This also goes by the term "attribute" in many HTML specifications. It is * the <B STYLE="color: red;">name</B> of the attribute, not it's * <B STYLE="color: red;">value</B>. The <B STYLE="color: red;">value</B> will be found (if the * {@code TagNode} contains this attribute), and the parameter {@link TextComparitor} will be * used to compare this <B STYLE="color: red;">value</B> - <I>dependent upon which * {@code 'TextComparitor'} is used</I> against the Compare-{@code String's} * * @param tc This may be any of the listed {@code TextComparitor's} in the class. There are * quite a few "pre-defined" {@code static} members in the {@code TextComparitor} class. There * are many that have both long names, and abbreviated names which can be interchangeably used * for readability purposes. * * @param compareStr These are passed to the {@code 'TextComparitor'} to perform tests on the * attribute <B STYLE="color: red;">value</B>. * * @return An instance of {@code 'AVT'} that can be passed to the NodeSearch classes * search-methods via any one of the methods that accepts a {@code Predicate<TagNode>} as a * parameter in the search criteria. * * @see #cmp(String, TextComparitor, String[]) * * @throws InnerTagKeyException <EMBED CLASS='external-html' DATA-FILE-ID=ITKEYEX> * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT cmpKIITNF(String innerTag, TextComparitor tc, String... compareStr) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If these tests fail, the returned predicate would absolutely fail. final String innerTagLC = ARGCHECK.innerTag(innerTag); ARGCHECK.TC(tc, compareStr); // Java's "Lambda-Expression" Syntax (like an "anonymous method"). // AVT extends functional-interface Predicate<TagNode> return (TagNode tn) -> { // This eliminates testing any TagNode that simply COULD NOT contain // attributes. (an optimization) // // KIITNF -> Empty Opening HTML TagNode Elements cannot be eliminated! // HOWEVER, Closing TagNodes are never included if (tn.isClosing) return false; // Retrieve the value of the requested "inner-tag" (HTML Attribute) Key-Value Pair // from the input HTML-Element (TagNode) String itv = tn.AV(innerTagLC); // REG-EX MATCHER, MORE EXPENSIVE // If the innerTag's value is null, then the inner-tag was not a key-value pair // found inside the TagNode. // // BECAUSE the user requested to "Keep If Inner-Tag Not Found", we must return TRUE // in that case. // In Java '||' uses short-circuit boolean-evaluation, while '|' requires // full-evaluation. // // OTHERWISE return the 'tc' test-results on that value using the named 'tc' comparison // on the compare-strings. return (itv == null) || tc.test(itv, compareStr); }; } // ******************************************************************************************** // Regular-Expression (Pattern) factory builders. // ******************************************************************************************** /** * This is a {@code static} factory method that generates {@code AVT-Predicate's} * ({@code Predicate<TagNode>}). It saves the user of typing the lambda information by hand, * and does a validation check too. The primary use of this class is that the results of one * factory method may be "AND-chained" or "OR-chained" with another to make search requirements * more specific. * * @param innerTag This also goes by the term "attribute" in many HTML specifications. It is * the <B STYLE="color: red;">name</B> of the attribute, not it's * <B STYLE="color: red;">value</B>. The <B STYLE="color: red;">value</B> will be found (if the * {@code TagNode} contains this attribute), and then tested using the Regular-Expression * {@code p.matcher(tag_value).find()} method. * * @param p This may be any regular expression {@code Pattern}. This {@code Pattern} will be * executed against the <B STYLE="color: red;">value</B> of the inner-tag specified by * parameter {@code 'innerTag'}. * * @return <EMBED CLASS='external-html' DATA-FILE-ID=RETCMP2> * * @see ARGCHECK#innerTag(String) * @see ARGCHECK#REGEX(Pattern) * @see TagNode#AV(String) * * @throws InnerTagKeyException <EMBED CLASS='external-html' DATA-FILE-ID=ITKEYEX> * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT cmp(String innerTag, Pattern p) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If these tests fail, the returned predicate would absolutely fail. final String innerTagLC = ARGCHECK.innerTag(innerTag); final Predicate<String> pred = ARGCHECK.REGEX(p); // Minimum length for field TagNode.str to have before it could possible contain the attribute // Obviously, the TagNode would have to have a min-length that includes the attribute-name // length + '< ' and '>' final int MIN_LEN = innerTag.length() + 3; // Java's "Lambda-Expression" Syntax (like an "anonymous method"). // AVT extends functional-interface Predicate<TagNode> return (TagNode tn) -> { // This eliminates testing any TagNode that simply COULD NOT contain the // attribute. (an optimization) if (tn.isClosing || (tn.str.length() <= (tn.tok.length() + MIN_LEN))) return false; // Retrieve the value of the requested "inner-tag" (HTML Attribute) Key-Value Pair // from the input HTML-Element (TagNode) // // REG-EX MATCHER, MORE EXPENSIVE String itv = tn.AV(innerTagLC); // If the innerTag's value is null, then the inner-tag was not a key-value pair // found inside the TagNode: return false. // Otherwise return the results of running the Regular-Expression matcher using the // input 'Pattern' instance. return (itv == null) ? false : pred.test(itv); }; } /** * This is a {@code static} factory method that generates {@code AVT-Predicate's} * ({@code Predicate<TagNode>}). It saves the user of typing the lambda information by hand, * and does a validation check too. The primary use of this class is that the results of one * factory method may be "AND-chained" or "OR-chained" with another to make search requirements * more specific. * * @param innerTag This also goes by the term "attribute" in many HTML specifications. It is * the <B STYLE="color: red;">name</B> of the attribute, not it's * <B STYLE="color: red;">value</B>. The <B STYLE="color: red;">value</B> will be found (if the * {@code TagNode} contains this attribute), and then tested using the Regular-Expression * {@code p.matcher(tag_value).find()} method. * * @param p This may be any regular expression {@code Pattern}. This {@code Pattern} will be * executed against the <B STYLE="color: red;">value</B> of the inner-tag specified by * parameter {@code 'innerTag'}. * * @param keepOnMatch There may be times when it is necessary to specify that a * Regular-Expression match should cause the search-filter to reject a {@code TagNode}, rather * than keeping it as a search-result match. In this case, the programmer can utilize this * variable to indicate whether matches should cause this method to return {@code TRUE} or * {@code FALSE}. If this variable is set to {@code FALSE}, then the {@code Predicate<TagNode>} * that is generated will return {@code FALSE}, whenever the regular-expression matches the * Attribute-<B STYLE="color: red;">Value</B>. * * <BR /><BR /><B><SPAN STYLE="color: red;">DEFAULT BEHAVIOR NOTE:</B></SPAN> The classes and * methods in this Node Search Package that accept regular-expressions as search-parameters * will always treat a match to indicate that the {@code TagNode} (or {@code TextNode}) in * question <B><I>has passed</I></B> the search-filter criteria. This method, therefore, * provides a way to bypass this default behavior. * * @param keepOnNull This parameter allows the user to specify whether the absence of an HTML * Inner-Tag should indicate that the TagNode being tested should pass or fail (keep or * reject) the search-filter criteria. * * <BR /><BR /><B><SPAN STYLE="color: red;">DEFAULT BEHAVIOR NOTE:</B></SPAN> The default * filter-results for the search classes and search methods of the Node-Search Package are such * that if an inner-tag is simply not available ... or 'not present' within an HTML Element, * then that element <I><B>will not be included</I></B> in the search results for that class or * method. <B><I>By using this particular {@code AVT} factory-method, a programmer can by-pass * that default behavior.</I></B> * * @return An instance of {@code 'AVT'} that can be passed to the NodeSearch classes * search-methods via any one of the methods that accepts a {@code Predicate<TagNode>} as a * parameter in the search criteria. * * @see ARGCHECK#innerTag(String) * @see ARGCHECK#REGEX(Pattern) * @see TagNode#AV(String) * * @throws InnerTagKeyException <EMBED CLASS='external-html' DATA-FILE-ID=ITKEYEX> * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT cmp (String innerTag, Pattern p, final boolean keepOnMatch, final boolean keepOnNull) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If these tests fail, the returned predicate would absolutely fail. final String innerTagLC = ARGCHECK.innerTag(innerTag); final Predicate<String> pred = ARGCHECK.REGEX(p); // Java's "Lambda-Expression" Syntax (like an "anonymous method"). // AVT extends functional-interface Predicate<TagNode> return (TagNode tn) -> { // This eliminates testing any TagNode that simply COULD NOT contain // attributes. (an optimization) // // keepOnNull -> Empty Opening HTML TagNode Elements cannot be eliminated! // HOWEVER, Closing TagNodes are never included if (tn.isClosing) return false; // Retrieve the value of the requested "inner-tag" (HTML Attribute) Key-Value Pair // from the input HTML-Element (TagNode) // // REG-EX MATCHER, MORE EXPENSIVE String itv = tn.AV(innerTagLC); // If the Attribute is simply not present in the HTML Element if (itv == null) return keepOnNull; if (pred.test(itv)) return keepOnMatch; // if the Regular-Expression succeeded else return ! keepOnMatch; // If the Regular-Expression failed }; } /** * <EMBED CLASS='external-html' DATA-FILE-ID=CMPKIITNF2> * * @param innerTag This also goes by the term "attribute" in many HTML specifications. It is * the <B STYLE="color: red;">name</B> of the attribute, not it's * <B STYLE="color: red;">value</B>. The <B STYLE="color: red;">value</B> will be found (if the * {@code TagNode} contains this attribute), and then tested using the Regular-Expression * {@code p.matcher(tag_value).find()} method. * * @param p This may be any regular expression {@code Pattern}. This {@code Pattern} will be * executed against the <B STYLE="color: red;">value</B> of the inner-tag specified by * parameter {@code 'innerTag'}. * * @return An instance of {@code 'AVT'} that can be passed to the NodeSearch classes * search-methods via any one of the methods that accepts a {@code Predicate<TagNode>} as a * parameter in the search parameter-list. * * @see #cmp(String, Pattern) * * @throws InnerTagKeyException <EMBED CLASS='external-html' DATA-FILE-ID=ITKEYEX> * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT cmpKIITNF(String innerTag, Pattern p) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If these tests fail, the returned predicate would absolutely fail. final String innerTagLC = ARGCHECK.innerTag(innerTag); final Predicate<String> pred = ARGCHECK.REGEX(p); // Java's "Lambda-Expression" Syntax (like an "anonymous method"). // AVT extends functional-interface Predicate<TagNode> return (TagNode tn) -> { // This eliminates testing any TagNode that simply COULD NOT contain // attributes. (an optimization) // // KIITNF -> Empty Opening HTML TagNode Elements cannot be eliminated! // HOWEVER, Closing TagNodes are never included if (tn.isClosing) return false; // Retrieve the value of the requested "inner-tag" (HTML Attribute) Key-Value Pair // from the input HTML-Element (TagNode) String itv = tn.AV(innerTagLC); // REG-EX MATCHER, MORE EXPENSIVE // If the innerTag's value is null, then the inner-tag was not a key-value pair // found inside the TagNode. // // BECAUSE the user requested to "Keep If Inner-Tag Not Found", we must return // in that case. // In Java '||' uses short-circuit boolean-evaluation, while '|' requires // full-evaluation. // // OTHERWISE return the results of running the Regular-Expression matcher using the // input 'Pattern' instance. return (itv == null) || pred.test(itv); }; } // ******************************************************************************************** // Predicate<String> factory builders. // ******************************************************************************************** /** * Convenience Method. * <BR />Invokes: {@link #cmp(String, Predicate)} * <BR />Converts: {@link StrFilter} to simple {@code String-Predicate} */ public static AVT cmp(String innerTag, StrFilter innerTagValueTest) { return cmp(innerTag, (Predicate<String>) innerTagValueTest::test); } /** * This is a {@code static} factory method that generates {@code AVT-Predicate's} - * ({@code Predicate<TagNode>}). It saves the user of typing the lambda information by hand, * and does a validation check too. The primary use of this class is that the results of one * factory method may be "AND-chained" or "OR-chained" with another to make search requirements * more specific. * * <BR /><BR /><B>NOTE:</B> The astute observer might wonder why change from a * {@code String-Predicate} to a {@code TagNode-Predicate}, with the answer being that * predicate-chaining on <I>different, multiple inner-tags (and their * <B STYLE="color: red;">values</B>)</I> can only be accomplished by using a * {@code TagNode-Predicate}, rather than a {@code String-Predicate} * * @param innerTag This also goes by the term "attribute" in many HTML specifications. It is * the <B STYLE="color: red;">name</B> of the attribute, not it's * <B STYLE="color: red;">value</B>. The <B STYLE="color: red;">value</B> will be found (if the * {@code TagNode} contains this attribute), and then tested against the * {@code String-Predicate} in parameter {@code 'innerTagValueTest'}. * * @param innerTagValueTest This may be any Java {@code String-Predicate} with a * {@code test(...) / accept} method. It will be used to accept or reject the inner-tag's * <B STYLE="color: red;">value</B> * * @return <EMBED CLASS='external-html' DATA-FILE-ID=RETCMP3> * * @see InnerTagFind * @see ARGCHECK#innerTag(String) * @see TagNode#AV(String) * * @throws InnerTagKeyException <EMBED CLASS='external-html' DATA-FILE-ID=ITKEYEX> * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT cmp(String innerTag, Predicate<String> innerTagValueTest) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If these tests fail, the returned predicate would absolutely fail. final String innerTagLC = ARGCHECK.innerTag(innerTag); if (innerTagValueTest == null) throw new NullPointerException ("Parameter innerTagValueTest was passed null, but this is not allowed here."); // Minimum length for field TagNode.str to have before it could possible contain the attribute // Obviously, the TagNode would have to have a min-length that includes the attribute-name // length + '< ' and '>' final int MIN_LEN = innerTag.length() + 3; // Java's "Lambda-Expression" Syntax (like an "anonymous method"). // AVT extends functional-interface Predicate<TagNode> return (TagNode tn) -> { // This eliminates testing any TagNode that simply COULD NOT contain the // attribute. (an optimization) if (tn.isClosing || (tn.str.length() <= (tn.tok.length() + MIN_LEN))) return false; // Retrieve the value of the requested "inner-tag" (HTML Attribute) Key-Value Pair // from the input HTML-Element (TagNode) String itv = tn.AV(innerTagLC); // REG-EX MATCHER, MORE EXPENSIVE // If the innerTag's value is null, then the inner-tag was not a key-value pair // found inside the TagNode: return false. // Otherwise return the results of the Predicate<String> provided on that // attribute-value. return (itv == null) ? false : innerTagValueTest.test(itv); }; } /** * Convenience Method. * <BR />Invokes: {@link #cmpKIITNF(String, Predicate)} * <BR />Converts: {@link StrFilter} to {@code String-Predicate} */ public static AVT cmpKIITNF(String innerTag, StrFilter innerTagValueTest) { return cmpKIITNF(innerTag, (Predicate<String>) innerTagValueTest::test); } /** * <EMBED CLASS='external-html' DATA-FILE-ID=CMPKIITNF3> * * @param innerTag This also goes by the term "attribute" in many HTML specifications. It is * the <B STYLE="color: red;">name</B> of the attribute, not it's * <B STYLE="color: red;">value</B>. The <B STYLE="color: red;">value</B> will be found (if the * {@code TagNode} contains this attribute), and then tested against the * {@code String-Predicate} parameter {@code 'innerTagValueTest'}. * * @param innerTagValueTest This may be any Java {@code String-Predicate} with a * {@code test(...) / accept} method. It will be used to accept or reject the inner-tag's * value. * * @return An instance of {@code 'AVT'} that can be passed to the NodeSearch classes * search-methods via any one of the methods that accepts a {@code Predicate<TagNode>} as a * parameter in the search criteria. * * @see #cmp(String, Predicate) * * @throws InnerTagKeyException <EMBED CLASS='external-html' DATA-FILE-ID=ITKEYEX> * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT cmpKIITNF(String innerTag, Predicate<String> innerTagValueTest) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If these tests fail, the returned predicate would absolutely fail. final String innerTagLC = ARGCHECK.innerTag(innerTag); if (innerTagValueTest == null) throw new NullPointerException ("Parameter innerTagValueTest was passed null, but this is not allowed here."); // Java's "Lambda-Expression" Syntax (like an "anonymous method"). // AVT extends functional-interface Predicate<TagNode> return (TagNode tn) -> { // This eliminates testing any TagNode that simply COULD NOT contain // attributes. (an optimization) // // KIITNF -> Empty Opening HTML TagNode Elements cannot be eliminated! // HOWEVER, Closing TagNodes are never included if (tn.isClosing) return false; // Retrieve the value of the requested "inner-tag" (HTML Attribute) Key-Value Pair // from the input HTML-Element (TagNode) // // REG-EX MATCHER, MORE EXPENSIVE String itv = tn.AV(innerTagLC); // If the innerTag's value is null, then the inner-tag was not a key-value pair // found inside the TagNode. // // BECAUSE the user requested to "Keep If Inner-Tag Not Found", we must return TRUE // in that case. // In Java '||' uses short-circuit boolean-evaluation, while '|' requires // full-evaluation. // // OTHERWISE return the results of the Predicate<String> provided on that // attribute-value. return (itv == null) || innerTagValueTest.test(itv); }; } // ******************************************************************************************** // Simple Present-Or-Not-Present test // ******************************************************************************************** /** * This is a {@code static} factory method that generates {@code AVT-Predicate's} - * ({@code Predicate<TagNode>}). It saves the user of typing the lambda information by hand, * and does a validation check too. The primary use of this class is that the results of one * factory method may be "AND-chained" or "OR-chained" with another to make search requirements * more specific. * * @param innerTag This also goes by the term "attribute" in many HTML specifications. It is * the <B STYLE="color: red;">name</B> of the attribute, not it's * <B STYLE="color: red;">value</B>. If this attribute is found, this {@code Predicate} will * always return {@code TRUE} regardless of it's <B STYLE="color: red;">value</B> - so long as * it is not null. * * <BR /><BR /><B><SPAN STYLE="color:red;">IMPORTANT NOTE:</B></SPAN> There is a subtlety here * between inner-tag's that have a <B STYLE="color: red;">value</B> of {@code the-empty-string, * a zero-length-string}, and attributes that are "null" or not found at all. Though rare, it * is sometimes the case that an HTML Attribute may have a <B STYLE="color: red;">value</B> of * {@code <SOME-TAG SOME-INNER-TAG="">}. There can be other versions that leave the quotes off * entirely such as: {@code <OTHER-ELEMENT OTHER-ATTRIBUTE=>} - where there are no quotes at * all. If the attribute is found, <I>with an equals sign</I> it will evaluate to the * <B><I>{@code the zero-length-string}</I></B>, but if the attribute is not found at all, * searching for it will return null, and this {@code Predicate} will return {@code FALSE}. * * @return <EMBED CLASS='external-html' DATA-FILE-ID=RETCMP4> * * @see InnerTagFind * @see ARGCHECK#innerTag(String) * @see TagNode#AV(String) * * @throws InnerTagKeyException <EMBED CLASS='external-html' DATA-FILE-ID=ITKEYEX> * * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT cmp(String innerTag) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If this test fails, the returned predicate would absolutely fail. final String innerTagLC = ARGCHECK.innerTag(innerTag); // SIMPLIFIED LAMBDA: The contents of this "anonymous method" can be expressed in a // single-statement. No need for 'return' or 'curly-braces' // Returns TRUE if the HTML Element contained a copy of the named inner-tag, and false // otherwise. return (TagNode tn) -> tn.AV(innerTagLC) != null; } // ****************************************************************************************** // The basic-required methods, of a "Functional-Interface Predicate" // ****************************************************************************************** /** * Generates a new {@code 'AVT'} predicate test that {@code logically-AND's} the results of * {@code 'this' Predicate} with the results of the new, additional passed parameter * {@code Predicate 'additionalTest'}. * * @param additionalTest This is an additional test of the inner-tag * <B STYLE="color: red;">key-value pair</B> (also known as the * <B STYLE="color: red;">"attribute-value pair"</B>) of HTML {@code TagNode's}. * * @return A new {@code Predicate<TagNode>} that will use two tests: * {@code 'this'} and {@code 'additionalTest'} and subsequently perform a logical-AND on the * result. Short-circuit evaluation is used (specifically, the {@code '&&'} operator, * rather than the {@code '&'} operator are utilized). The {@code Predicate} that is returned * will perform the {@code 'this'} test first, and then the {@code 'additionalTest'} second. * The returned {@code Predicate} will return the {@code 'AND'} of both of them. * * @see TagNode * * @throws NullPointerException If any of the provided input reference parameters are null. */ default AVT and(AVT additionalTest) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If this test fails, the returned predicate would absolutely fail. if (additionalTest == null) throw new NullPointerException ("The parameter 'additionalTest' passed to method 'AVT.and(additionalTest)' was null"); // SIMPLIFIED LAMBDA: The contents of this "anonymous method" can be expressed in a // single-statement. No need for 'return' or 'curly-braces' // Returns TRUE if both 'this' evaluates to true on an input HTML Element, // and 'other' also evaluates to true for the same element. return (TagNode tn) -> this.test(tn) && additionalTest.test(tn); } /** * Generates a new {@code 'AVT'} predicate test that {@code logically-OR's} the results of * {@code 'this' Predicate} with the results of the new, additional passed parameter * {@code Predicate 'additionalTest'}. * * @param additionalTest This is an additional test of the inner-tag * <B STYLE="color: red;">key-value pair</B> (also known as the * <B STYLE="color: red;">"attribute-value pair"</B>) of HTML {@code TagNode's}. * * @return A new {@code Predicate<TagNode>} that will use two tests: * {@code 'this'} and {@code 'additionalTest'} and subsequently perform a logical-OR on the * result. Short-circuit evaluation is used (specifically, the {@code '||'} operator, * rather than the {@code '|'} operator are utilized). The {@code Predicate} that is returned * will perform the {@code 'this'} test first, and then the {@code 'additionalTest'} second. * The returned {@code Predicate} will return the {@code 'OR'} of both of them. * * @see TagNode * * @throws NullPointerException If any of the provided input reference parameters are null. */ default AVT or(AVT additionalTest) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If this test fails, the returned predicate would absolutely fail. if (additionalTest == null) throw new NullPointerException ("The parameter 'additionalTest' passed to method 'AVT.or(additionalTest)' was null"); // SIMPLIFIED LAMBDA: The contents of this "anonymous method" can be expressed in a // single-statement. No need for 'return' or 'curly-braces' // Returns TRUE if either 'this' evaluates to true on an input HTML Element, // and 'other' also evaluates to true for the same element. return (TagNode tn) -> this.test(tn) || additionalTest.test(tn); } /** * Generates a new {@code 'AVT'} predicate test that is the {@code logical-NOT} of * {@code 'this' Predicate}. * * @return A new {@code Predicate<TagNode>} that will simply just calls {@code 'this' * Predicate}, and puts an exclamation point ({@code logical 'NOT'}) in front of the result. * * @see TagNode */ default AVT negate() { // SIMPLIFIED LAMBDA: The contents of this "anonymous method" can be expressed in a // single-statement. No need for 'return' or 'curly-braces' // Returns the opposite of whatever result 'this' evaluates using the input HTML Element. return (TagNode tn) -> ! this.test(tn); } /** * This is a {@code static} factory method that generates {@code AVT-Predicate's} * ({@code Predicate<TagNode>}). It saves the user of typing the lambda information by hand, * and does a validation check too. * * <BR /><BR />If the {@code expectedTN.equals(tn)} fails - specifically using the * java-built-in equality-test method {@code 'equals(...)'}, then the generated / returned * {@code Predicate} would return {@code TRUE}, and the {@code TagNode} in question would be * included in the results. * * @param expectedTN This is compared against {@code TagNode's} found in the * page-{@code Vector} for equality. * * @return A {@code Predicate<TagNode>} that compares for equality with parameter * {@code 'expectedTN'} * * @see TagNode * * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT isEqualKEEP(TagNode expectedTN) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If this test fails, the returned predicate would absolutely fail. if (expectedTN == null) throw new NullPointerException ("The parameter 'expectedTN' passed to method 'AVT.isEqualKEEP(expectedTN)' was null"); // SIMPLIFIED LAMBDA: The contents of this "anonymous method" can be expressed in a // single-statement. No need for 'return' or 'curly-braces' // Returns true if the HTML Element passed to this (anonymous) method is the same as the // one passed to 'isEqualsKEEP' // Identical to: (TagNode tn) -> tn.str.equals(expectedTN.str); return (TagNode tn) -> tn.equals(expectedTN); } /** * This is a {@code static} factory method that generates {@code AVT-Predicate's} * ({@code Predicate<TagNode>}). It saves the user of typing the lambda information by hand, * and does a validation check too. * * <BR /><BR />If the {@code expectedTN.equals(tn)} fails - specifically using the * java-built-in equality-test method {@code equals(...)} - then the generated / returned * {@code Predicate} would return {@code FALSE}, and the {@code TagNode} in question would be * filtered from the results. * * @param expectedTN This is compared against {@code TagNode's} found in the * page-{@code Vector} for equality. * * @return A {@code Predicate<TagNode>} that compares for equality with parameter * {@code 'expectedTN'} * * @see TagNode * * @throws NullPointerException If any of the provided input reference parameters are null. */ public static AVT isEqualREJECT(TagNode expectedTN) { // FAIL-FAST: It is helpful for the user to test the data before building the Predicate. // If this test fails, the returned predicate would absolutely fail. if (expectedTN == null) throw new NullPointerException( "The parameter 'expectedTN' passed to method 'AVT.isEqualREJECT(expectedTN)' "+ "was null" ); // SIMPLIFIED LAMBDA: The contents of this "anonymous method" can be expressed in a // single-statement. No need for 'return' or 'curly-braces' // Returns TRUE if the HTML Element passed to this (anonymous) method is the same as the // one passed to 'isEqualsKEEP' // Identical to: (TagNode tn) -> ! tn.str.equals(expectedTN.str); return (TagNode tn) -> ! tn.equals(expectedTN); } } |