|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Regular Expression Field Add-on |
| 4 | + * %% |
| 5 | + * Copyright (C) 2025 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.flowingcode.vaadin.addons.regex; |
| 21 | + |
| 22 | +import static com.flowingcode.vaadin.addons.regex.RegularExpressionOperator.ADVANCED; |
| 23 | +import static com.flowingcode.vaadin.addons.regex.RegularExpressionOperator.CONTAINS; |
| 24 | +import static com.flowingcode.vaadin.addons.regex.RegularExpressionOperator.ENDS_WITH; |
| 25 | +import static com.flowingcode.vaadin.addons.regex.RegularExpressionOperator.STARTS_WITH; |
| 26 | +import java.util.regex.Pattern; |
| 27 | +import java.util.regex.PatternSyntaxException; |
| 28 | +import lombok.AccessLevel; |
| 29 | +import lombok.AllArgsConstructor; |
| 30 | +import lombok.EqualsAndHashCode; |
| 31 | +import lombok.Getter; |
| 32 | +import lombok.NonNull; |
| 33 | + |
| 34 | +/** |
| 35 | + * Represents a regular expression. |
| 36 | + * |
| 37 | + * <p> |
| 38 | + * This class assists in the creation and handling of regular expressions by providing pre-defined |
| 39 | + * operators for common patterns such as "starts with", "ends with", and "contains". It also |
| 40 | + * supports an advanced mode where users can input custom regular expressions. |
| 41 | + * </p> |
| 42 | + * |
| 43 | + * <p> |
| 44 | + * Instances of this class are immutable and validated upon creation. |
| 45 | + * </p> |
| 46 | + * |
| 47 | + * @author Javier Godoy |
| 48 | + */ |
| 49 | +@Getter |
| 50 | +@EqualsAndHashCode(onlyExplicitlyIncluded = true) |
| 51 | +@AllArgsConstructor(access = AccessLevel.PRIVATE) |
| 52 | +public final class RegularExpression { |
| 53 | + |
| 54 | + private static final String ANY = ".*"; |
| 55 | + |
| 56 | + /** The operator defining the type of regular expression. */ |
| 57 | + @EqualsAndHashCode.Include |
| 58 | + private final RegularExpressionOperator operator; |
| 59 | + |
| 60 | + /** The input string used for generating the pattern. */ |
| 61 | + @EqualsAndHashCode.Include |
| 62 | + private final String input; |
| 63 | + |
| 64 | + /** The compiled {@code Pattern} for the regular expression. */ |
| 65 | + private final Pattern pattern; |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a new {@code RegularExpression} based on the specified {@code operator} and |
| 69 | + * {@code input}. |
| 70 | + * |
| 71 | + * @param operator the type of regular expression (e.g., |
| 72 | + * {@link RegularExpressionOperator#CONTAINS}) |
| 73 | + * @param input the string to be used for pattern creation |
| 74 | + * @throws PatternSyntaxException if the resulting pattern is invalid. |
| 75 | + */ |
| 76 | + public RegularExpression(@NonNull RegularExpressionOperator operator, @NonNull String input) |
| 77 | + throws PatternSyntaxException { |
| 78 | + this.operator = operator; |
| 79 | + this.input = input; |
| 80 | + |
| 81 | + String regex = switch (operator) { |
| 82 | + case ADVANCED -> input; |
| 83 | + case CONTAINS -> ANY + quote(input) + ANY; |
| 84 | + case ENDS_WITH -> ANY + quote(input); |
| 85 | + case STARTS_WITH -> quote(input) + ANY; |
| 86 | + }; |
| 87 | + |
| 88 | + pattern = Pattern.compile(regex); |
| 89 | + } |
| 90 | + |
| 91 | + private final static String CHARS = ".?+*\\[({$^|\\\\"; |
| 92 | + |
| 93 | + private final static Pattern SIMPLE_PATTERN = |
| 94 | + Pattern.compile("(?:[^CHARS]|\\\\[CHARS])+".replace("CHARS", CHARS)); |
| 95 | + |
| 96 | + private final static Pattern ESCAPE_PATTERN = |
| 97 | + Pattern.compile("[CHARS]".replace("CHARS", CHARS)); |
| 98 | + |
| 99 | + private final static Pattern UNQUOTE_PATTERN = |
| 100 | + Pattern.compile("\\\\([CHARS])".replace("CHARS", CHARS)); |
| 101 | + |
| 102 | + private static String quote(String input) { |
| 103 | + String s1 = ESCAPE_PATTERN.matcher(input).replaceAll("\\\\$0"); |
| 104 | + String s2 = "\\Q" + input.replace("\\E", "\\E\\\\E\\Q") + "\\E"; |
| 105 | + return (s1.length() < s2.length()) ? s1 : s2; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Creates a {@code RegularExpression} instance from a given {@link Pattern}. |
| 110 | + * |
| 111 | + * <p> |
| 112 | + * This method attempts to determine if the pattern corresponds to a simple |
| 113 | + * {@link RegularExpressionOperator} like "starts with", "ends with", or "contains". If it does, a |
| 114 | + * corresponding {@code RegularExpression} is returned. Otherwise, an advanced mode expression is |
| 115 | + * created. |
| 116 | + * </p> |
| 117 | + * |
| 118 | + * @param pattern the pattern to analyze |
| 119 | + * |
| 120 | + * @throws NullPointerException if {@code pattern} is {@code null}. |
| 121 | + * |
| 122 | + * @return a {@code RegularExpression} instance |
| 123 | + */ |
| 124 | + public static RegularExpression of(Pattern pattern) { |
| 125 | + |
| 126 | + if (pattern == null) { |
| 127 | + throw new NullPointerException("Pattern cannot be null"); |
| 128 | + } |
| 129 | + |
| 130 | + String regex = pattern.pattern(); |
| 131 | + boolean hasLeadingWildcard = false; |
| 132 | + boolean hasTrailingWildcard = false; |
| 133 | + if (regex.startsWith(ANY)) { |
| 134 | + hasLeadingWildcard = true; |
| 135 | + regex = regex.substring(2); |
| 136 | + } |
| 137 | + |
| 138 | + if (regex.endsWith(ANY)) { |
| 139 | + hasTrailingWildcard = true; |
| 140 | + regex = regex.substring(0, regex.length() - 2); |
| 141 | + } |
| 142 | + |
| 143 | + String input = null; |
| 144 | + if (hasLeadingWildcard || hasTrailingWildcard) { |
| 145 | + if (SIMPLE_PATTERN.matcher(regex).matches()) { |
| 146 | + input = UNQUOTE_PATTERN.matcher(regex).replaceAll("$1"); |
| 147 | + } else if (regex.startsWith("\\Q") && regex.endsWith("\\E")) { |
| 148 | + input = regex.substring(2, regex.length() - 2).replace("\\E\\\\E\\Q", "\\E"); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (input != null) { |
| 153 | + if (hasLeadingWildcard && hasTrailingWildcard) { |
| 154 | + return new RegularExpression(CONTAINS, input); |
| 155 | + } else if (hasLeadingWildcard) { |
| 156 | + return new RegularExpression(ENDS_WITH, input); |
| 157 | + } else if (hasTrailingWildcard) { |
| 158 | + return new RegularExpression(STARTS_WITH, input); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return new RegularExpression(ADVANCED, pattern.pattern(), pattern); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public String toString() { |
| 167 | + return operator + " " + input; |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments