001package Torello.Java; 002 003import Torello.Java.ReadOnly.ReadOnlySet; 004import Torello.Java.ReadOnly.ReadOnlyHashSet; 005import Torello.Java.ReadOnly.ReadOnlyList; 006import Torello.Java.ReadOnly.ReadOnlyArrayList; 007 008import Torello.Java.Additional.Counter; 009 010import Torello.JavaDoc.LinkJavaSource; 011 012import java.util.regex.Pattern; 013import java.util.regex.Matcher; 014 015import java.util.stream.Stream; 016 017import java.util.function.Supplier; 018 019@Torello.JavaDoc.StaticFunctional 020public class StrSource 021{ 022 private StrSource() { } 023 024 025 // ******************************************************************************************** 026 // ******************************************************************************************** 027 // FIELDS 028 // ******************************************************************************************** 029 // ******************************************************************************************** 030 031 032 private static final char[] REGEX_ESCAPE_CHARS_ARR = 033 { '\\', '/', '(', ')', '[', ']', '{', '}', '$', '^', '+', '*', '?', '-', '.' }; 034 035 /** 036 * These are 'control' characters (Reg Ex Code), so they must be escaped if the are to be 037 * treated as their ASCII-equivalent values. 038 */ 039 public static final ReadOnlySet<Character> REGEX_ESCAPE_CHARS = 040 new ReadOnlyHashSet<>(REGEX_ESCAPE_CHARS_ARR, null); 041 042 private static final char[] JS_ESCAPE_CHARS_ARR = 043 { '\\', '/', '\n', '\"' }; 044 045 /** 046 * When converting a {@code String} for a Java-Script {@code String}, these are the 047 * characters that must be escaped. 048 */ 049 public static final ReadOnlySet<Character> JS_ESCAPE_CHARS = 050 new ReadOnlyHashSet<>(JS_ESCAPE_CHARS_ARR, null); 051 052 /** 053 * The list of reserved Java Key-Words. This list was written by ChatGPT on February 1st, 054 * 2024. 055 */ 056 public static final ReadOnlyList<String> reservedKeywords = new ReadOnlyArrayList<>( 057 "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", 058 "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", 059 "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", 060 "int", "interface", "long", "native", "new", "null", "package", "permirs", "private", 061 "protected", "public", "return", "short", "static", "strictfp", "super", "switch", 062 "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", 063 "while" 064 ); 065 066 /** This will match the definition for a java {@code 'Generic'} class or interface */ 067 public static final Pattern GENERIC_PARAMS = Pattern.compile("^.+?<([\\s\\w\\<>,\\?]+)>$"); 068 069 /** This shall match a Java Package {@code String} */ 070 public static final Pattern PACKAGE_NAME = Pattern.compile("([A-Za-z_]\\w*\\.)+"); 071 072 073 // ******************************************************************************************** 074 // ******************************************************************************************** 075 // Searching for a tag in an HTML string (the early way - without regular expressions) 076 // ******************************************************************************************** 077 // ******************************************************************************************** 078 079 080 /** 081 * If parameter {@code String s} contains any tag within-which there is a valid 082 * {@code "HREF"}, this will return the contents of the {@code HREF} Attribute/InnerTag. 083 * 084 * @param s This is usually some variant of an HTML element/tag {@code String}. This method 085 * was the first one written for HTML in this scrape package, and is just kept here for legacy 086 * reasons. The {@code class HTML.TagNode} has a number of options for extracting the 087 * {@code 'HREF'} attribute from an HTML element. 088 * 089 * @return The attribute-value of an {@code HREF=...} attribute inside (usually an {@code <A>} 090 * 'Anchor') HTML tag. This will return 'null' if there is no {@code HREF="..."} 091 * attribute-value pair is found or identified. 092 * 093 * @throws IllegalArgumentException If there is no end-quote found for the {@code HREF="..."} 094 * sub-string. 095 */ 096 @LinkJavaSource(handle="TagsGREP", name="grepIMG") 097 public static String grep_HREF_tag(String s) 098 { return TagsGREP.grepIMG(s); } 099 100 /** 101 * If parameter {@code String s} contains an HTML {@code "IMG"} tag, this will return the 102 * contents of the {@code "SRC=..."} attribute tag-field. 103 * 104 * @param s This is usually some variant of an HTML element/tag {@code String}. This method 105 * was the first one written for HTML in this scrape package, and is just kept here for legacy 106 * reasons. The {@code class HTML.TagNode} has a number of options for extracting the 107 * {@code 'SRC'} attribute from an HTML element. 108 * 109 * @return The attribute-value of a {@code SRC=...} attribute inside (usually an {@code <IMG>} 110 * 'Image') HTML tag. 'null' is returned if: 111 * 112 * <BR /><BR /><OL CLASS=JDOL> 113 * <LI>There is no HTML {@code 'IMG'} token found in the {@code String}</LI> 114 * <LI>There is no {@code SRC='...'} attribute-value pair found.</LI> 115 * </OL> 116 */ 117 @LinkJavaSource(handle="TagsGREP", name="grepHREF") 118 public static String grep_IMG_SRC_tag(String s) 119 { return TagsGREP.grepHREF(s); } 120 121 122 // ******************************************************************************************** 123 // ******************************************************************************************** 124 // Java-Script & Reg-Ex String encoding (JSON.stringify()) 125 // ******************************************************************************************** 126 // ******************************************************************************************** 127 128 129 /** 130 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_ESC_4JS_DESC> 131 * 132 * @param str This may be any String in java. It is intended to be inserted into a Java-Script 133 * file between an open and close quotation marks. 134 * 135 * @return <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_ESC_4JS_RET> 136 */ 137 public static String escStrForJavaScript(String str) 138 { return StrReplace.r(str, JS_ESCAPE_CHARS_ARR, '\\'); } 139 140 /** 141 * This method should only be used for a <B><I>precise {@code String} match</I></B> using a 142 * regular-expression. This method shall 'escape' all characters that the JVM Regular 143 * Expression Matcher in {@code package java.util.regex.*} would expect be escaped. If the 144 * input parameter {@code 'str'} contains any regular-expression code, then this method would 145 * <B>FAIL</B> as it would escape regular-expression code into unusable text. 146 * 147 * @param str This should be any {@code String} for which the user would like to find an 148 * <B>exact match, as-is</B>. 149 * 150 * @return A regular-expression ready {@code String} 151 */ 152 public static String escStrForRegEx(String str) 153 { return StrReplace.r(str, REGEX_ESCAPE_CHARS_ARR, '\\'); } 154 155 156 // ******************************************************************************************** 157 // ******************************************************************************************** 158 // Java Code String-Functions 159 // ******************************************************************************************** 160 // ******************************************************************************************** 161 162 163 /** 164 * Parses a {@code String} such as {@code T extends TreeMap<Integer, List<String>>}. It is 165 * strictly used, to <B><I>only parse</I></B> the generic-definition lists that are at the top 166 * of generic <B>classes</B> and <B>interfaces</B>. 167 * 168 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_PARSE_GENT DATA-NODE="An Example of Sorts"> 169 * 170 * @param genericTypeParamOrDefinition This should be {@code String} retrieved from inside the 171 * less-than ({@code '<'}) and greater-than ({@code '>'}) symbols. For example, for 172 * {@code SortedList<A extends Comparable, B>} the {@code String} passed to this method should 173 * be {@code "A extends Comparable, B"} 174 * 175 * @return This should break down this {@code CSV} (comma separated value) list into 176 * individual {@code String's}. 177 * 178 * @throws NoMatchException if the input {@code String} parameter does not match the 179 * generics regular-expression {@link #GENERIC_PARAMS}. 180 * 181 * @throws StringFormatException If the input {@code String} could not be parsed. 182 */ 183 @LinkJavaSource(handle="ParseGenericType") 184 public static String[] parseGenericType(String genericTypeParamOrDefinition) 185 { return ParseGenericType.parse(genericTypeParamOrDefinition); } 186 187 /** 188 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_CARET_BEN> 189 * 190 * @param str This may be any input-{@code String} that is less than 100 characters. 191 * 192 * @param strPos This must be a number between 0 and the length 193 * 194 * @return The same input-{@code String} with a second line appended underneath (using a 195 * newline) having a <B>caret</B> ({@code '^'}) directly underneath the character at 196 * {@code strPos}. 197 * 198 * @throws IllegalArgumentException If the input {@code String} is longer than 199 * {@code 100 characters}. 200 * 201 * @throws StringFormatException If the input {@code String} contains any new-line {@code '\n'} 202 * or tab {@code '\t'} characters. 203 * 204 * @throws StringIndexOutOfBoundsException If the value pased to {@code strPos} is negative or 205 * greater than the length of the input-{@code String}. 206 * 207 * @see StringParse#nChars(char, int) 208 */ 209 public static String caretBeneath(String str, int strPos) 210 { 211 if (str.length() > 100) throw new IllegalArgumentException( 212 "The length of the input-string must be less than 100. str has length: " + 213 str.length() 214 ); 215 216 if (StrCmpr.containsOR(str, "\n", "\t")) throw new StringFormatException 217 ("The input-string may not contain new-line or tab characters."); 218 219 if (strPos >= str.length()) throw new StringIndexOutOfBoundsException( 220 "The value you have passed to 'strPos' [" + strPos + "] is greater than the length " + 221 "the input-string [" + str.length() + "]" 222 ); 223 224 if (strPos < 0) throw new StringIndexOutOfBoundsException 225 ("You have passed a negative value to strPos [" + strPos + "]"); 226 227 return str + "\n" + StringParse.nChars(' ', strPos) + '^'; 228 } 229 230 /** 231 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_REM_GEN_DESC> 232 * 233 * @param typeAsStr The "Reference Type" or "Declaration Type". 234 * 235 * @return The same {@code String}, having everything between the <B>outer-most, matching</B> 236 * {@code '<'} and {@code '>'} symbols. 237 * 238 * <BR /><BR /><B>NOTE:</B> The returned {@code String} will not contain any leading or 239 * trailing white-space. It is trimmed before being returned. 240 * 241 * @throws StringFormatException 242 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_STR_FORM_EX> 243 */ 244 @LinkJavaSource(handle="RemoveGeneric") 245 public static String removeGeneric(String typeAsStr) 246 { return RemoveGeneric.remove(typeAsStr); } 247 248 /** 249 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_TTJI_DESC> 250 * 251 * @param typeStr 252 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_TTJI_TYPESTR> 253 * 254 * @return a Simplified version of the type that leaves out the scope, but provides a 255 * simple Java Identifier, instead. Throws exceptions if not properly formatted. If any 256 * array-bracket characters are passed, they is preserved, unless the arrays in this type 257 * are part of the generic-type parameters; please see the examples above. 258 * 259 * @throws StringFormatException Please see the explanation provided in 260 * {@link #removeGeneric(String)} under 'Throws'. 261 * 262 * @see #removeGeneric(String) 263 */ 264 @LinkJavaSource(handle="TypeToJavaIdentifier") 265 public static String typeToJavaIdentifier(String typeStr) 266 { return TypeToJavaIdentifier.convert(typeStr); } 267 268 269 // This was designed while staring at the field retrieved from a JavaDoc HTML Page that 270 // looked like this (from AbstractHNLI) 271 // protected java.util.function.Predicate<E extends HTMLNode> p; 272 // This puts a group (group 1) around the ( extends HTMLNode) part, so it can be removed. 273 // JavaParser complained about it. 274 275 private static final Pattern exClause = 276 Pattern.compile("([A-Za-z][A-Za-z0-9]*)(\\s+extends\\s+[\\w\\.]+)"); 277 278 /** 279 * Removes the {@code 'extends'} part of a Java Generic 280 * 281 * <BR /><BR /><B STYLE='color:red;'>TO DO:</B> This will fail for a class such as: 282 * <BR />{@code public class MyClass<T extends Vector<String>}, where the extends clause 283 * also has a generic in it. Java HTML does not define such classes, but they are possible, 284 * and this needs to be fixed, as soon as they let me! 285 * 286 * @param decl Any Type Declaration that includes has the word {{@code 'extends'}}, 287 * followed by type-parameter information. 288 * 289 * @return The same {@code String} without the clause. 290 */ 291 public static String removeExtendsClause(String decl) 292 { 293 Matcher m = exClause.matcher(decl); 294 295 while (m.find()) 296 { 297 decl = m.replaceFirst(m.group(1)); 298 m.reset(decl); 299 } 300 301 return decl; 302 } 303 304 /** 305 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_JTYPE_STR> 306 * 307 * @param s Any Java {@code String}. 308 * 309 * @return {@code TRUE} if and only if the Java Compiler could interpret {@code 's'} as a valid 310 * reference to a Java Type. In computer-programming, the world <B>{@code Type}</B> can have a 311 * lot of meanings, but here, the word should be interpreted as a Java Class, Interface, 312 * Enumeration (an {@code 'enum'}), Annotation or Record. 313 * 314 * <BR /><BR /><B>NOTE:</B> {@code 's'} may include the period {@code '.'} since inner classes, 315 * enum's and interfaces are also valid Java Type's. Two consecutive period-characters, or a 316 * period at the beginning or ending of {@code 's'} will result in this method returning 317 * {@code FALSE}. 318 */ 319 @LinkJavaSource(handle="IsJavaTypeStr") 320 public static boolean isJavaTypeStr(String s) 321 { return IsJavaTypeStr.is(s); } 322 323 /** 324 * Checks whether an input {@code String} would be allowed as a Java Identifier - for instance, 325 * whether the input would make a valid Field-Name, Variable-Name, Class-Name or Method-Name. 326 * 327 * <BR /><BR /><B CLASS=JDDescLabel>NOTE:</B> 328 * 329 * <BR />This class returns {@code FALSE} if it is passed 'null'. It does not throw a 330 * {@code NullPointerException}. 331 * 332 * <BR /><BR /><B CLASS=JDDescLabel>ChatGPT Note:</B> 333 * 334 * <BR /><B>ChatGPT, 3.5</B> wrote this whole thing, including the in-line comments. I had to 335 * write the Java-Doc Comments, but I guess I could have asked it to do that too. 336 * 337 * @param identifier Any Java {@code String} 338 * 339 * @return {@code TRUE} if-and-only-if parameter {@code 'identifier'} is a valid Java 340 * Identifier. 341 */ 342 public static boolean isValidJavaIdentifier(String identifier) 343 { 344 // Check if the string is not null or empty 345 if (identifier == null || identifier.isEmpty()) return false; 346 347 // Check if the first character is a letter, underscore, or dollar sign 348 if (! Character.isJavaIdentifierStart(identifier.charAt(0))) return false; 349 350 // Check the remaining characters 351 for (int i = 1; i < identifier.length(); i++) 352 if (!Character.isJavaIdentifierPart(identifier.charAt(i))) 353 return false; 354 355 // Check if the identifier is a reserved keyword 356 if (reservedKeywords.contains(identifier)) return false; 357 358 // The string is a valid Java identifier 359 return true; 360 } 361 362 /** 363 * <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_EX_TYPE_DESC> 364 * 365 * @param srcFileName This is expected to be the file-name of a {@code '.java'} or 366 * {@code '.class'} File. 367 * 368 * @param throwOnBadTypeName When this is passed {@code TRUE}, this method throws an exception 369 * if the Computed Type-Name is not a valid Java Identifier. 370 * 371 * @return <EMBED CLASS='external-html' DATA-FILE-ID=STRSRC_EX_TYPE_RET> 372 * 373 * @throws IllegalArgumentException If the file-name ends neither with the text {@code '.java'} 374 * nor with {@code '.class'}. 375 * 376 * @throws JavaIdentifierException Throws if-and-only-if <B>BOTH</B> the returned 377 * {@code String} would not constitute a valid Java-Identifier, <B>AND</B> the input 378 * {@code boolean} parameter {@code throwOnBadTypeName} is passed {@code TRUE}. 379 */ 380 @LinkJavaSource(handle="ExtractTypeName") 381 public static String extractTypeName 382 (final String srcFileName, final boolean throwOnBadTypeName) 383 { return ExtractTypeName.extract(srcFileName, throwOnBadTypeName); } 384 385 386 // ******************************************************************************************** 387 // ******************************************************************************************** 388 // Replace Special-Character 389 // ******************************************************************************************** 390 // ******************************************************************************************** 391 392 393 /** 394 * There are actually people out there who are willing to put character {@code '160'} into 395 * a file or document, instead of a simple {@code ' '} element. How rude. 396 * Any instances of this character shall be replaced with the standard space character 397 * {@code ASCII #32}. 398 * 399 * @param s Any {@code String} will pass. Generally {@code String's} that were converted from 400 * HTML pages will contain {@code char #160} as it is occasionally translated from the HTML 401 * escape sequence {@code } 402 * 403 * @return A String where any instance of white-space character {@code #160} have been 404 * replaced with character {@code #32} 405 */ 406 public static String replaceNBSP(String s) 407 { return s.replace((char) 160, ' '); } 408 // { return s.replace(("" + ((char) 160)), " "); } 409 410 /** 411 * Even lower than {@code #160}, apparently is the {@code "Zero Width Space"} (character 412 * {@code #8203}. This is actually inserted by the <B>JavaDoc Tool</B> (by 413 * {@code Sun / Oracle}) into JavaDoc generated HTML Pages. Here, it shall be replaced by 414 * character {@code #32} - the <I>space-character</I>. 415 * 416 * <BR /><BR /><B>A.K.A.:</B> <CODE>"\u200B"</CODE>. 417 * 418 * <BR /><BR /><B><I STYLE='color: red;'>Can you see the character, above?</I></B> No? 419 * That's zero width space for you! If you ever sitting and wondering why a {@code String} 420 * seems to be something else than what it looks like - you might have a zero-width 421 * space in your {@code String}. If so, it will take a while to find the bug. 422 * 423 * @param s Any {@code String} will pass. Generally {@code String's} that were converted from 424 * JavaDoc HTML pages will contain {@code char #8203}. 425 * 426 * @return A String where any instance of white-space character {@code #8203} have been 427 * replaced with character {@code #32} 428 */ 429 public static String replaceZWSP(String s) 430 { return s.replace((char) 8203, ' '); } 431 // { return s.replace(("" + ((char) 8203)), " "); } 432 433 434 // ******************************************************************************************** 435 // ******************************************************************************************** 436 // CSS Source 437 // ******************************************************************************************** 438 // ******************************************************************************************** 439 440 441 /** 442 * Checks if a Java-{@code String} constitutes a valid CSS Property-Name. Note that this 443 * method, in no way consults any "complete list" of all known CSS-Properties. Instead, it 444 * simply analyzes whether the name is conguent with the CSS-Property Validator Reg-ex. 445 * 446 * @param cssPropertyName Any Java-{@code String} 447 * 448 * @return {@code TRUE} if and ony if {@code 'attributeName'} is a valid HTML Atribute-Name, 449 * according to the agreed upon CSS-Property Regular-Expression Validator. 450 */ 451 public static boolean isCSSPropertyName(String cssPropertyName) 452 { 453 if (cssPropertyName.length() == 0) return false; 454 455 if (! isCSSPropertyNameStart(cssPropertyName.charAt(0))) return false; 456 457 for (int i=1; i < cssPropertyName.length(); i++) 458 { 459 final char c = cssPropertyName.charAt(i); 460 if ((c >= 'A') && (c <= 'Z')) continue; 461 if ((c >= 'a') && (c <= 'z')) continue; 462 if ((c >= '0') && (c <= '9')) continue; 463 if ((c == '-') || (c == '_')) continue; 464 return false; 465 } 466 467 return true; 468 } 469 470 /** 471 * Checks whether parameter {@code 'c'} is one of the agreed-upon standard characters that are 472 * allowed to begin CSS Property-Names. 473 * 474 * @param c Any Java {@code char}-primitive 475 * 476 * @return {@code TRUE} if and ony if {@code 'c'} is a character that would be allowed to begin 477 * a CSS Property-Name 478 */ 479 public static boolean isCSSPropertyNameStart(char c) 480 { 481 if ((c >= 'A') && (c <= 'Z')) return true; 482 if ((c >= 'a') && (c <= 'z')) return true; 483 if ((c == '-') || (c == '_')) return true; 484 return false; 485 } 486 487 /** 488 * Checks whether parameter {@code 'c'} is one of the agreed-upon standard characters that are 489 * permitted within CSS Property-Names, after the first character of the name. 490 * 491 * @param c Any Java {@code char}-primitive 492 * 493 * @return {@code TRUE} if and ony if {@code 'c'} is a character that would be allowed within a 494 * valid CSS Property-Name. 495 */ 496 public static boolean isCSSPropertyNamePart(char c) 497 { 498 if ((c >= 'A') && (c <= 'Z')) return true; 499 if ((c >= 'a') && (c <= 'z')) return true; 500 if ((c >= '0') && (c <= '9')) return true; 501 if ((c == '-') || (c == '_')) return true; 502 return false; 503 } 504 505 506 // ******************************************************************************************** 507 // ******************************************************************************************** 508 // CSS Classes 509 // ******************************************************************************************** 510 // ******************************************************************************************** 511 512 513 /** 514 * Checks if a Java-{@code String} constitutes a valid CSS Class-Name. This method does not 515 * consult any "complete list" of all known CSS classes but instead analyzes whether the name 516 * adheres to standard CSS class naming conventions. 517 * 518 * <BR /><BR /><B CLASS=JDDescLable>Chat-GPT Note:</B> 519 * 520 * <BR />Chat-GPT wrote all three of these (including the JavaDoc). 521 * Chat-GPT makes programming more fun. 522 * 523 * @param cssClassName Any Java-{@code String} 524 * 525 * @return {@code TRUE} if and only if {@code cssClassName} is a valid CSS Class-Name according 526 * to the agreed-upon CSS naming rules. 527 */ 528 public static boolean isCSSClassName(String cssClassName) 529 { 530 if (cssClassName.length() == 0) return false; 531 532 if (!isCSSClassNameStart(cssClassName.charAt(0))) return false; 533 534 for (int i = 1; i < cssClassName.length(); i++) 535 if (!isCSSClassNamePart(cssClassName.charAt(i))) return false; 536 537 return true; 538 } 539 540 /** 541 * Checks whether parameter {@code 'c'} is a valid character to begin a CSS Class-Name. 542 * 543 * <BR /><BR /><B CLASS=JDDescLable>Chat-GPT Note:</B> 544 * 545 * <BR />Chat-GPT wrote all three of these (including the JavaDoc). 546 * Chat-GPT makes programming more fun. 547 * 548 * @param c Any Java {@code char}-primitive 549 * 550 * @return {@code TRUE} if and only if {@code c} is a character that can begin a CSS 551 * Class-Name. 552 */ 553 public static boolean isCSSClassNameStart(char c) 554 { 555 if ((c >= 'A') && (c <= 'Z')) return true; 556 if ((c >= 'a') && (c <= 'z')) return true; 557 if (c == '-') return true; 558 if (c == '_') return true; 559 return false; 560 } 561 562 /** 563 * Checks whether parameter {@code 'c'} is a valid character within a CSS Class-Name. 564 * 565 * <BR /><BR /><B CLASS=JDDescLable>Chat-GPT Note:</B> 566 * 567 * <BR />Chat-GPT wrote all three of these (including the JavaDoc). 568 * Chat-GPT makes programming more fun. 569 * 570 * @param c Any Java {@code char}-primitive 571 * 572 * @return {@code TRUE} if and only if {@code c} is a character that can be part of a CSS 573 * Class-Name. 574 */ 575 public static boolean isCSSClassNamePart(char c) 576 { 577 if ((c >= 'A') && (c <= 'Z')) return true; 578 if ((c >= 'a') && (c <= 'z')) return true; 579 if ((c >= '0') && (c <= '9')) return true; 580 if (c == '-') return true; 581 if (c == '_') return true; 582 return false; 583 } 584 585 586 587 // ******************************************************************************************** 588 // ******************************************************************************************** 589 // More HTML Source 590 // ******************************************************************************************** 591 // ******************************************************************************************** 592 593 594 /** 595 * Checks if a Java-{@code String} constitutes a valid HTML Attibute-Name. Note that this 596 * method, in no way consults any "complete list" of all know HTML-Attributes. Instead, it 597 * simply analyzes whether the name is conguent with the Attribute-Name Validator Reg-ex. 598 * 599 * @param attributeName Any Java-{@code String} 600 * 601 * @return {@code TRUE} if and ony if {@code 'attributeName'} is a valid HTML Atribute-Name, 602 * according to the agreed upon Attribute-Name Regular-Expression Validator. 603 */ 604 public static boolean isAttributeName(String attributeName) 605 { 606 if (attributeName.length() == 0) return false; 607 608 if (! isAttributeNameStart(attributeName.charAt(0))) return false; 609 610 for (int i=1; i < attributeName.length(); i++) 611 { 612 final char c = attributeName.charAt(i); 613 if ((c >= 'A') && (c <= 'Z')) continue; 614 if ((c >= 'a') && (c <= 'z')) continue; 615 if ((c >= '0') && (c <= '9')) continue; 616 if ((c == '-') || (c == '_')) continue; 617 return false; 618 } 619 620 return true; 621 } 622 623 /** 624 * Checks whether parameter {@code 'c'} is one of the agreed-upon standard characters that are 625 * allowed to begin HTML Attribute-Names. 626 * 627 * @param c Any Java {@code char}-primitive 628 * 629 * @return {@code TRUE} if and ony if {@code 'c'} is a character that would be allowed to begin 630 * an HTML Attribute-Name 631 */ 632 public static boolean isAttributeNameStart(char c) 633 { 634 if ((c >= 'A') && (c <= 'Z')) return true; 635 if ((c >= 'a') && (c <= 'z')) return true; 636 return false; 637 } 638 639 /** 640 * Checks whether parameter {@code 'c'} is one of the agreed-upon standard characters that are 641 * permitted within HTML Attribute-Names, after the first character of the name. 642 * 643 * @param c Any Java {@code char}-primitive 644 * 645 * @return {@code TRUE} if and ony if {@code 'c'} is a character that would be allowed within a 646 * valid HTML Attribute-Name. 647 */ 648 public static boolean isAttributeNamePart(char c) 649 { 650 if ((c >= 'A') && (c <= 'Z')) return true; 651 if ((c >= 'a') && (c <= 'z')) return true; 652 if ((c >= '0') && (c <= '9')) return true; 653 if ((c == '-') || (c == '_')) return true; 654 return false; 655 } 656 657 658 /** 659 * Simply removes the trailiing {@code '.java'} from the end of an input File-Name Parameter. 660 * Also removes any leading directory information from the input {@code String}. 661 * 662 * @param javaFileName This is expected to be a legitamite '.java' Source-Code File-Name, as a 663 * {@code java.lang.String}. 664 * 665 * @return Returns a {@code String} in which leading directory information has been truncated, 666 * and the last five characters have been removed. 667 * 668 * <BR /><BR />Due to the highly repetitive nature of using this methpo within a loop, this 669 * method's body <B STYLE='color: red;'><I>does not</I></B> perform any kind of error checking 670 * on its input. 671 * 672 * <BR /><BR />If a null {@code String} is passed as input, this method will throw a 673 * {@code NullPointerException}. If your best friend's address is passed as input, this method 674 * will return a {@code String} in which the last 5 characters of text of that address have 675 * been removed. 676 * 677 * <BR /><BR /><I>And if that address had a forward or backward slash ({@code '/'} or 678 * {@code '\'}), everything prior to the last slash present within that input-mess would be 679 * truncated.</I> 680 * 681 * @throws IndexOutOfBoundsException If, after truncating any leading directory information, 682 * the resulting {@code String} is less than 5 characters, then this exception throws. 683 */ 684 public static String noPathNoExt(String javaFileName) 685 { 686 for (int i=(javaFileName.length()-1); i > 0; i--) 687 { 688 final char c = javaFileName.charAt(i); 689 690 if ((c == '/') || (c == '\\')) 691 { 692 javaFileName = javaFileName.substring(i + 1); 693 break; 694 } 695 } 696 697 return javaFileName.substring(0, javaFileName.length() - 5); 698 } 699}