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 | package Torello.CSS; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.Objects; /** * An exception for using with a Validity-Check of a CSS Class-Name. * * <BR /><BR /><B CLASS=JDDescLabel2>Regular-Expression:</B> * * <BR />The following is considered the validator for this exception's * {@link #check(String) Validity-Checker} method. * * <DIV CLASS=REGEX>{@code * ^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$ * }</DIV> * * <EMBED CLASS='external-html' DATA-FILE-ID=CLASS_NAME_CHaRT> * @see #VALID_CSS_CLASS_NAME * @see #VALID_CSS_CLASS_NAME_PRED */ public class ClassNameCSSException extends RuntimeException { /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUIDEX> */ public static final long serialVersionUID = 1; /** * This is a validator regular-expression. It is intended to check whether or not a * CSS Class-Name is valid or not. * * @see VALID_CSS_CLASS_NAME_PRED */ public static final Pattern VALID_CSS_CLASS_NAME = // Pattern.compile("^([_a-zA-Z]|-[_a-zA-Z])[_\\-a-zA-Z0-9]*$"); Pattern.compile("^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$"); /** * This is the validator {@link #VALID_CSS_CLASS_NAME Reg-Ex} after having been converted into * a {@code java.util.function.Predicate<String>}. * * @see VALID_CSS_CLASS_NAME */ public static final Predicate<String> VALID_CSS_CLASS_NAME_PRED = VALID_CSS_CLASS_NAME.asPredicate(); /** Constructs a {@code ClassNameCSSException} with no detail message. */ public ClassNameCSSException() { super(); } /** * Constructs a {@code ClassNameCSSException} with the specified detail message. * @param message the detail message. */ public ClassNameCSSException(String message) { super(message); } /** * Constructs a new exception with the specified detail message and cause. * * <BR /><BR /><B CLASS=JDDescLabel>NOTE:</B> * * <BR /><BR />The detail message associated with cause is not automatically incorporated into * this exception's detail message. * * @param message The detail message (which is saved for later retrieval by the * {@code Throwable.getMessage()} method). * * @param cause the cause (which is saved for later retrieval by the * {@code Throwable.getCause()} method). (A null value is permitted, and indicates that the * cause is non-existent or unknown.) */ public ClassNameCSSException(String message, Throwable cause) { super(message, cause); } /** * Constructs a new exception with the specified cause and a detail message of * {@code (cause==null ? null : cause.toString())} (which typically contains the class and * detail message of cause). This constructor is useful for exceptions that are little more * than wrappers for other throwables. * * @param cause The cause (which is saved for later retrieval by the * {@code Throwable.getCause()} method). (A null value is permitted, and indicates that the * cause is non-existent or unknown.) */ public ClassNameCSSException(Throwable cause) { super(cause); } /** * Checks if a the provided {@code cssClassName} is a valid name according the CSS * Regular-Expression that's incorporated into this class. * * @param cssClassName Any Java-{@code String}, but only a valid CSS Class-Name can prevent * this method from throwing this class' exception. * * @throws ClassNameCSSException if the provided {@code cssClassName} isn't a proper CSS * Class-Name, according to the Regular-Expression Predicate {@link #VALID_CSS_CLASS_NAME_PRED} * * @throws NullPointerException if {@code cssClassName} is null, then NPE throws. * * @see #VALID_CSS_CLASS_NAME * @see #VALID_CSS_CLASS_NAME_PRED */ public static void check(String cssClassName) { Objects.requireNonNull(cssClassName, "The CSS Class-Name which was passed is null"); if (! VALID_CSS_CLASS_NAME_PRED.test(cssClassName)) throw new ClassNameCSSException( "The CSS Class-Name that was passed: " + '[' + cssClassName + "], " + "is not a valid CSS Class-Name.\n" + "It did not match the Reg-Ex: " + VALID_CSS_CLASS_NAME.pattern() ); } } |