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 | package Torello.Java; import Torello.Java.ReadOnly.ReadOnlySet; import Torello.Java.ReadOnly.ReadOnlyHashSet; import Torello.Java.ReadOnly.ReadOnlyList; import Torello.Java.ReadOnly.ReadOnlyArrayList; import Torello.Java.Additional.Counter; import Torello.JavaDoc.LinkJavaSource; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.stream.Stream; import java.util.function.Supplier; @Torello.JavaDoc.StaticFunctional public class StrSource { private StrSource() { } // ******************************************************************************************** // ******************************************************************************************** // FIELDS // ******************************************************************************************** // ******************************************************************************************** private static final char[] REGEX_ESCAPE_CHARS_ARR = { '\\', '/', '(', ')', '[', ']', '{', '}', '$', '^', '+', '*', '?', '-', '.' }; /** * These are 'control' characters (Reg Ex Code), so they must be escaped if the are to be * treated as their ASCII-equivalent values. */ public static final ReadOnlySet<Character> REGEX_ESCAPE_CHARS = new ReadOnlyHashSet<>(REGEX_ESCAPE_CHARS_ARR, null); private static final char[] JS_ESCAPE_CHARS_ARR = { '\\', '/', '\n', '\"' }; /** * When converting a {@code String} for a Java-Script {@code String}, these are the * characters that must be escaped. */ public static final ReadOnlySet<Character> JS_ESCAPE_CHARS = new ReadOnlyHashSet<>(JS_ESCAPE_CHARS_ARR, null); /** * The list of reserved Java Key-Words. This list was written by ChatGPT on February 1st, * 2024. */ public static final ReadOnlyList<String> reservedKeywords = new ReadOnlyArrayList<>( "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "permirs", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while" ); /** This will match the definition for a java {@code 'Generic'} class or interface */ public static final Pattern GENERIC_PARAMS = Pattern.compile("^.+?<([\\s\\w\\<>,\\?]+)>$"); /** This shall match a Java Package {@code String} */ public static final Pattern PACKAGE_NAME = Pattern.compile("([A-Za-z_]\\w*\\.)+"); // ******************************************************************************************** // ******************************************************************************************** // Searching for a tag in an HTML string (the early way - without regular expressions) // ******************************************************************************************** // ******************************************************************************************** /** * If parameter {@code String s} contains any tag within-which there is a valid * {@code "HREF"}, this will return the contents of the {@code HREF} Attribute/InnerTag. * * @param s This is usually some variant of an HTML element/tag {@code String}. This method * was the first one written for HTML in this scrape package, and is just kept here for legacy * reasons. The {@code class HTML.TagNode} has a number of options for extracting the * {@code 'HREF'} attribute from an HTML element. * * @return The attribute-value of an {@code HREF=...} attribute inside (usually an {@code <A>} * 'Anchor') HTML tag. This will return 'null' if there is no {@code HREF="..."} * attribute-value pair is found or identified. * * @throws IllegalArgumentException If there is no end-quote found for the {@code HREF="..."} * sub-string. */ @LinkJavaSource(handle="TagsGREP", name="grepIMG") public static String grep_HREF_tag(String s) { return TagsGREP.grepIMG(s); } /** * If parameter {@code String s} contains an HTML {@code "IMG"} tag, this will return the * contents of the {@code "SRC=..."} attribute tag-field. * * @param s This is usually some variant of an HTML element/tag {@code String}. This method * was the first one written for HTML in this scrape package, and is just kept here for legacy * reasons. The {@code class HTML.TagNode} has a number of options for extracting the * {@code 'SRC'} attribute from an HTML element. * * @return The attribute-value of a {@code SRC=...} attribute inside (usually an {@code <IMG>} * 'Image') HTML tag. 'null' is returned if: * * <BR /><BR /><OL CLASS=JDOL> * <LI>There is no HTML {@code 'IMG'} token found in the {@code String}</LI> * <LI>There is no {@code SRC='...'} attribute-value pair found.</LI> * </OL> */ @LinkJavaSource(handle="TagsGREP", name="grepHREF") public static String grep_IMG_SRC_tag(String s) { return TagsGREP.grepHREF(s); } // ******************************************************************************************** // ******************************************************************************************** // Java-Script & Reg-Ex String encoding (JSON.stringify()) // ******************************************************************************************** // ******************************************************************************************** /** * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_ESC_4JS_DESC> * * @param str This may be any String in java. It is intended to be inserted into a Java-Script * file between an open and close quotation marks. * * @return <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_ESC_4JS_RET> */ public static String escStrForJavaScript(String str) { return StrReplace.r(str, JS_ESCAPE_CHARS_ARR, '\\'); } /** * This method should only be used for a <B><I>precise {@code String} match</I></B> using a * regular-expression. This method shall 'escape' all characters that the JVM Regular * Expression Matcher in {@code package java.util.regex.*} would expect be escaped. If the * input parameter {@code 'str'} contains any regular-expression code, then this method would * <B>FAIL</B> as it would escape regular-expression code into unusable text. * * @param str This should be any {@code String} for which the user would like to find an * <B>exact match, as-is</B>. * * @return A regular-expression ready {@code String} */ public static String escStrForRegEx(String str) { return StrReplace.r(str, REGEX_ESCAPE_CHARS_ARR, '\\'); } // ******************************************************************************************** // ******************************************************************************************** // Java Code String-Functions // ******************************************************************************************** // ******************************************************************************************** /** * Parses a {@code String} such as {@code T extends TreeMap<Integer, List<String>>}. It is * strictly used, to <B><I>only parse</I></B> the generic-definition lists that are at the top * of generic <B>classes</B> and <B>interfaces</B>. * * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_PARSE_GENT DATA-NODE="An Example of Sorts"> * * @param genericTypeParamOrDefinition This should be {@code String} retrieved from inside the * less-than ({@code '<'}) and greater-than ({@code '>'}) symbols. For example, for * {@code SortedList<A extends Comparable, B>} the {@code String} passed to this method should * be {@code "A extends Comparable, B"} * * @return This should break down this {@code CSV} (comma separated value) list into * individual {@code String's}. * * @throws NoMatchException if the input {@code String} parameter does not match the * generics regular-expression {@link #GENERIC_PARAMS}. * * @throws StringFormatException If the input {@code String} could not be parsed. */ @LinkJavaSource(handle="ParseGenericType") public static String[] parseGenericType(String genericTypeParamOrDefinition) { return ParseGenericType.parse(genericTypeParamOrDefinition); } /** * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_CARET_BEN> * * @param str This may be any input-{@code String} that is less than 100 characters. * * @param strPos This must be a number between 0 and the length * * @return The same input-{@code String} with a second line appended underneath (using a * newline) having a <B>caret</B> ({@code '^'}) directly underneath the character at * {@code strPos}. * * @throws IllegalArgumentException If the input {@code String} is longer than * {@code 100 characters}. * * @throws StringFormatException If the input {@code String} contains any new-line {@code '\n'} * or tab {@code '\t'} characters. * * @throws StringIndexOutOfBoundsException If the value pased to {@code strPos} is negative or * greater than the length of the input-{@code String}. * * @see StringParse#nChars(char, int) */ public static String caretBeneath(String str, int strPos) { if (str.length() > 100) throw new IllegalArgumentException( "The length of the input-string must be less than 100. str has length: " + str.length() ); if (StrCmpr.containsOR(str, "\n", "\t")) throw new StringFormatException ("The input-string may not contain new-line or tab characters."); if (strPos >= str.length()) throw new StringIndexOutOfBoundsException( "The value you have passed to 'strPos' [" + strPos + "] is greater than the length " + "the input-string [" + str.length() + "]" ); if (strPos < 0) throw new StringIndexOutOfBoundsException ("You have passed a negative value to strPos [" + strPos + "]"); return str + "\n" + StringParse.nChars(' ', strPos) + '^'; } /** * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_REM_GEN_DESC> * * @param typeAsStr The "Reference Type" or "Declaration Type". * * @return The same {@code String}, having everything between the <B>outer-most, matching</B> * {@code '<'} and {@code '>'} symbols. * * <BR /><BR /><B>NOTE:</B> The returned {@code String} will not contain any leading or * trailing white-space. It is trimmed before being returned. * * @throws StringFormatException * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_STR_FORM_EX> */ @LinkJavaSource(handle="RemoveGeneric") public static String removeGeneric(String typeAsStr) { return RemoveGeneric.remove(typeAsStr); } /** * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_TTJI_DESC> * * @param typeStr * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_TTJI_TYPESTR> * * @return a Simplified version of the type that leaves out the scope, but provides a * simple Java Identifier, instead. Throws exceptions if not properly formatted. If any * array-bracket characters are passed, they is preserved, unless the arrays in this type * are part of the generic-type parameters; please see the examples above. * * @throws StringFormatException Please see the explanation provided in * {@link #removeGeneric(String)} under 'Throws'. * * @see #removeGeneric(String) */ @LinkJavaSource(handle="TypeToJavaIdentifier") public static String typeToJavaIdentifier(String typeStr) { return TypeToJavaIdentifier.convert(typeStr); } // This was designed while staring at the field retrieved from a JavaDoc HTML Page that // looked like this (from AbstractHNLI) // protected java.util.function.Predicate<E extends HTMLNode> p; // This puts a group (group 1) around the ( extends HTMLNode) part, so it can be removed. // JavaParser complained about it. private static final Pattern exClause = Pattern.compile("([A-Za-z][A-Za-z0-9]*)(\\s+extends\\s+[\\w\\.]+)"); /** * Removes the {@code 'extends'} part of a Java Generic * * <BR /><BR /><B STYLE='color:red;'>TO DO:</B> This will fail for a class such as: * <BR />{@code public class MyClass<T extends Vector<String>}, where the extends clause * also has a generic in it. Java HTML does not define such classes, but they are possible, * and this needs to be fixed, as soon as they let me! * * @param decl Any Type Declaration that includes has the word {{@code 'extends'}}, * followed by type-parameter information. * * @return The same {@code String} without the clause. */ public static String removeExtendsClause(String decl) { Matcher m = exClause.matcher(decl); while (m.find()) { decl = m.replaceFirst(m.group(1)); m.reset(decl); } return decl; } /** * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_JTYPE_STR> * * @param s Any Java {@code String}. * * @return {@code TRUE} if and only if the Java Compiler could interpret {@code 's'} as a valid * reference to a Java Type. In computer-programming, the world <B>{@code Type}</B> can have a * lot of meanings, but here, the word should be interpreted as a Java Class, Interface, * Enumeration (an {@code 'enum'}), Annotation or Record. * * <BR /><BR /><B>NOTE:</B> {@code 's'} may include the period {@code '.'} since inner classes, * enum's and interfaces are also valid Java Type's. Two consecutive period-characters, or a * period at the beginning or ending of {@code 's'} will result in this method returning * {@code FALSE}. */ @LinkJavaSource(handle="IsJavaTypeStr") public static boolean isJavaTypeStr(String s) { return IsJavaTypeStr.is(s); } /** * Checks whether an input {@code String} would be allowed as a Java Identifier - for instance, * whether the input would make a valid Field-Name, Variable-Name, Class-Name or Method-Name. * * <BR /><BR /><B CLASS=JDDescLabel>NOTE:</B> * * <BR />This class returns {@code FALSE} if it is passed 'null'. It does not throw a * {@code NullPointerException}. * * <BR /><BR /><B CLASS=JDDescLabel>ChatGPT Note:</B> * * <BR /><B>ChatGPT, 3.5</B> wrote this whole thing, including the in-line comments. I had to * write the Java-Doc Comments, but I guess I could have asked it to do that too. * * @param identifier Any Java {@code String} * * @return {@code TRUE} if-and-only-if parameter {@code 'identifier'} is a valid Java * Identifier. */ public static boolean isValidJavaIdentifier(String identifier) { // Check if the string is not null or empty if (identifier == null || identifier.isEmpty()) return false; // Check if the first character is a letter, underscore, or dollar sign if (! Character.isJavaIdentifierStart(identifier.charAt(0))) return false; // Check the remaining characters for (int i = 1; i < identifier.length(); i++) if (!Character.isJavaIdentifierPart(identifier.charAt(i))) return false; // Check if the identifier is a reserved keyword if (reservedKeywords.contains(identifier)) return false; // The string is a valid Java identifier return true; } /** * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_EX_TYPE_DESC> * * @param srcFileName This is expected to be the file-name of a {@code '.java'} or * {@code '.class'} File. * * @param throwOnBadTypeName When this is passed {@code TRUE}, this method throws an exception * if the Computed Type-Name is not a valid Java Identifier. * * @return <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_EX_TYPE_RET> * * @throws IllegalArgumentException If the file-name ends neither with the text {@code '.java'} * nor with {@code '.class'}. * * @throws JavaIdentifierException Throws if-and-only-if <B>BOTH</B> the returned * {@code String} would not constitute a valid Java-Identifier, <B>AND</B> the input * {@code boolean} parameter {@code throwOnBadTypeName} is passed {@code TRUE}. */ @LinkJavaSource(handle="ExtractTypeName") public static String extractTypeName (final String srcFileName, final boolean throwOnBadTypeName) { return ExtractTypeName.extract(srcFileName, throwOnBadTypeName); } // ******************************************************************************************** // ******************************************************************************************** // Replace Special-Character // ******************************************************************************************** // ******************************************************************************************** /** * There are actually people out there who are willing to put character {@code '160'} into * a file or document, instead of a simple {@code ' '} element. How rude. * Any instances of this character shall be replaced with the standard space character * {@code ASCII #32}. * * @param s Any {@code String} will pass. Generally {@code String's} that were converted from * HTML pages will contain {@code char #160} as it is occasionally translated from the HTML * escape sequence {@code } * * @return A String where any instance of white-space character {@code #160} have been * replaced with character {@code #32} */ public static String replaceNBSP(String s) { return s.replace((char) 160, ' '); } // { return s.replace(("" + ((char) 160)), " "); } /** * Even lower than {@code #160}, apparently is the {@code "Zero Width Space"} (character * {@code #8203}. This is actually inserted by the <B>JavaDoc Tool</B> (by * {@code Sun / Oracle}) into JavaDoc generated HTML Pages. Here, it shall be replaced by * character {@code #32} - the <I>space-character</I>. * * <BR /><BR /><B>A.K.A.:</B> <CODE>"\u200B"</CODE>. * * <BR /><BR /><B><I STYLE='color: red;'>Can you see the character, above?</I></B> No? * That's zero width space for you! If you ever sitting and wondering why a {@code String} * seems to be something else than what it looks like - you might have a zero-width * space in your {@code String}. If so, it will take a while to find the bug. * * @param s Any {@code String} will pass. Generally {@code String's} that were converted from * JavaDoc HTML pages will contain {@code char #8203}. * * @return A String where any instance of white-space character {@code #8203} have been * replaced with character {@code #32} */ public static String replaceZWSP(String s) { return s.replace((char) 8203, ' '); } // { return s.replace(("" + ((char) 8203)), " "); } // ******************************************************************************************** // ******************************************************************************************** // CSS Source // ******************************************************************************************** // ******************************************************************************************** /** * Checks if a Java-{@code String} constitutes a valid CSS Property-Name. Note that this * method, in no way consults any "complete list" of all known CSS-Properties. Instead, it * simply analyzes whether the name is conguent with the CSS-Property Validator Reg-ex. * * @param cssPropertyName Any Java-{@code String} * * @return {@code TRUE} if and ony if {@code 'attributeName'} is a valid HTML Atribute-Name, * according to the agreed upon CSS-Property Regular-Expression Validator. */ public static boolean isCSSPropertyName(String cssPropertyName) { if (cssPropertyName.length() == 0) return false; if (! isCSSPropertyNameStart(cssPropertyName.charAt(0))) return false; for (int i=1; i < cssPropertyName.length(); i++) { final char c = cssPropertyName.charAt(i); if ((c >= 'A') && (c <= 'Z')) continue; if ((c >= 'a') && (c <= 'z')) continue; if ((c >= '0') && (c <= '9')) continue; if ((c == '-') || (c == '_')) continue; return false; } return true; } /** * Checks whether parameter {@code 'c'} is one of the agreed-upon standard characters that are * allowed to begin CSS Property-Names. * * @param c Any Java {@code char}-primitive * * @return {@code TRUE} if and ony if {@code 'c'} is a character that would be allowed to begin * a CSS Property-Name */ public static boolean isCSSPropertyNameStart(char c) { if ((c >= 'A') && (c <= 'Z')) return true; if ((c >= 'a') && (c <= 'z')) return true; if ((c == '-') || (c == '_')) return true; return false; } /** * Checks whether parameter {@code 'c'} is one of the agreed-upon standard characters that are * permitted within CSS Property-Names, after the first character of the name. * * @param c Any Java {@code char}-primitive * * @return {@code TRUE} if and ony if {@code 'c'} is a character that would be allowed within a * valid CSS Property-Name. */ public static boolean isCSSPropertyNamePart(char c) { if ((c >= 'A') && (c <= 'Z')) return true; if ((c >= 'a') && (c <= 'z')) return true; if ((c >= '0') && (c <= '9')) return true; if ((c == '-') || (c == '_')) return true; return false; } // ******************************************************************************************** // ******************************************************************************************** // CSS Classes // ******************************************************************************************** // ******************************************************************************************** /** * Checks if a Java-{@code String} constitutes a valid CSS Class-Name. This method does not * consult any "complete list" of all known CSS classes but instead analyzes whether the name * adheres to standard CSS class naming conventions. * * <BR /><BR /><B CLASS=JDDescLable>Chat-GPT Note:</B> * * <BR />Chat-GPT wrote all three of these (including the JavaDoc). * Chat-GPT makes programming more fun. * * @param cssClassName Any Java-{@code String} * * @return {@code TRUE} if and only if {@code cssClassName} is a valid CSS Class-Name according * to the agreed-upon CSS naming rules. */ public static boolean isCSSClassName(String cssClassName) { if (cssClassName.length() == 0) return false; if (!isCSSClassNameStart(cssClassName.charAt(0))) return false; for (int i = 1; i < cssClassName.length(); i++) if (!isCSSClassNamePart(cssClassName.charAt(i))) return false; return true; } /** * Checks whether parameter {@code 'c'} is a valid character to begin a CSS Class-Name. * * <BR /><BR /><B CLASS=JDDescLable>Chat-GPT Note:</B> * * <BR />Chat-GPT wrote all three of these (including the JavaDoc). * Chat-GPT makes programming more fun. * * @param c Any Java {@code char}-primitive * * @return {@code TRUE} if and only if {@code c} is a character that can begin a CSS * Class-Name. */ public static boolean isCSSClassNameStart(char c) { if ((c >= 'A') && (c <= 'Z')) return true; if ((c >= 'a') && (c <= 'z')) return true; if (c == '-') return true; if (c == '_') return true; return false; } /** * Checks whether parameter {@code 'c'} is a valid character within a CSS Class-Name. * * <BR /><BR /><B CLASS=JDDescLable>Chat-GPT Note:</B> * * <BR />Chat-GPT wrote all three of these (including the JavaDoc). * Chat-GPT makes programming more fun. * * @param c Any Java {@code char}-primitive * * @return {@code TRUE} if and only if {@code c} is a character that can be part of a CSS * Class-Name. */ public static boolean isCSSClassNamePart(char c) { if ((c >= 'A') && (c <= 'Z')) return true; if ((c >= 'a') && (c <= 'z')) return true; if ((c >= '0') && (c <= '9')) return true; if (c == '-') return true; if (c == '_') return true; return false; } // ******************************************************************************************** // ******************************************************************************************** // More HTML Source // ******************************************************************************************** // ******************************************************************************************** /** * Checks if a Java-{@code String} constitutes a valid HTML Attibute-Name. Note that this * method, in no way consults any "complete list" of all know HTML-Attributes. Instead, it * simply analyzes whether the name is conguent with the Attribute-Name Validator Reg-ex. * * @param attributeName Any Java-{@code String} * * @return {@code TRUE} if and ony if {@code 'attributeName'} is a valid HTML Atribute-Name, * according to the agreed upon Attribute-Name Regular-Expression Validator. */ public static boolean isAttributeName(String attributeName) { if (attributeName.length() == 0) return false; if (! isAttributeNameStart(attributeName.charAt(0))) return false; for (int i=1; i < attributeName.length(); i++) { final char c = attributeName.charAt(i); if ((c >= 'A') && (c <= 'Z')) continue; if ((c >= 'a') && (c <= 'z')) continue; if ((c >= '0') && (c <= '9')) continue; if ((c == '-') || (c == '_')) continue; return false; } return true; } /** * Checks whether parameter {@code 'c'} is one of the agreed-upon standard characters that are * allowed to begin HTML Attribute-Names. * * @param c Any Java {@code char}-primitive * * @return {@code TRUE} if and ony if {@code 'c'} is a character that would be allowed to begin * an HTML Attribute-Name */ public static boolean isAttributeNameStart(char c) { if ((c >= 'A') && (c <= 'Z')) return true; if ((c >= 'a') && (c <= 'z')) return true; return false; } /** * Checks whether parameter {@code 'c'} is one of the agreed-upon standard characters that are * permitted within HTML Attribute-Names, after the first character of the name. * * @param c Any Java {@code char}-primitive * * @return {@code TRUE} if and ony if {@code 'c'} is a character that would be allowed within a * valid HTML Attribute-Name. */ public static boolean isAttributeNamePart(char c) { if ((c >= 'A') && (c <= 'Z')) return true; if ((c >= 'a') && (c <= 'z')) return true; if ((c >= '0') && (c <= '9')) return true; if ((c == '-') || (c == '_')) return true; return false; } /** * Simply removes the trailiing {@code '.java'} from the end of an input File-Name Parameter. * Also removes any leading directory information from the input {@code String}. * * @param javaFileName This is expected to be a legitamite '.java' Source-Code File-Name, as a * {@code java.lang.String}. * * @return Returns a {@code String} in which leading directory information has been truncated, * and the last five characters have been removed. * * <BR /><BR />Due to the highly repetitive nature of using this methpo within a loop, this * method's body <B STYLE='color: red;'><I>does not</I></B> perform any kind of error checking * on its input. * * <BR /><BR />If a null {@code String} is passed as input, this method will throw a * {@code NullPointerException}. If your best friend's address is passed as input, this method * will return a {@code String} in which the last 5 characters of text of that address have * been removed. * * <BR /><BR /><I>And if that address had a forward or backward slash ({@code '/'} or * {@code '\'}), everything prior to the last slash present within that input-mess would be * truncated.</I> * * @throws IndexOutOfBoundsException If, after truncating any leading directory information, * the resulting {@code String} is less than 5 characters, then this exception throws. */ public static String noPathNoExt(String javaFileName) { for (int i=(javaFileName.length()-1); i > 0; i--) { final char c = javaFileName.charAt(i); if ((c == '/') || (c == '\\')) { javaFileName = javaFileName.substring(i + 1); break; } } return javaFileName.substring(0, javaFileName.length() - 5); } } |