1 | public class JakartaOroMatcher implements RegexpMatcher {↵ | | 1 | public class JakartaRegexpMatcher implements RegexpMatcher {↵
|
|
2 | private String pattern;↵ | | 2 | private String pattern;↵
|
3 | // CheckStyle:VisibilityModifier OFF - bc↵ | | |
|
4 | protected final Perl5Compiler compiler = new Perl5Compiler();↵ | | |
|
5 | protected final Perl5Matcher matcher = new Perl5Matcher();↵ | | |
|
6 | // CheckStyle:VisibilityModifier ON↵ | | |
|
|
7 | /**↵ | | |
|
8 | * Constructor for JakartaOroMatcher.↵ | | |
|
9 | */↵ | | |
|
10 | public JakartaOroMatcher() {↵ | | |
|
11 | }↵ | | |
|
|
12 | /**↵ | | 3 | /**↵
|
13 | * Set the regexp pattern from the String description.↵ | | 4 | * Set the regexp pattern from the String description.↵
|
14 | * @param pattern the pattern to match↵ | | 5 | * @param pattern the pattern to match↵
|
15 | */↵ | | 6 | */↵
|
16 | public void setPattern(String pattern) {↵ | | 7 | public void setPattern(String pattern) {↵
|
17 | this.pattern = pattern;↵ | | 8 | this.pattern = pattern;↵
|
18 | }↵ | | 9 | }↵
|
|
19 | /**↵ | | 10 | /**↵
|
20 | * Get a String representation of the regexp pattern↵ | | 11 | * Get a String representation of the regexp pattern↵
|
21 | * @return the pattern↵ | | 12 | * @return the pattern↵
|
22 | */↵ | | 13 | */↵
|
23 | public String getPattern() {↵ | | 14 | public String getPattern() {↵
|
24 | return this.pattern;↵ | | 15 | return pattern;↵
|
25 | }↵ | | 16 | }↵
|
|
26 | /**↵ | | 17 | /**↵
|
27 | * Get a compiled representation of the regexp pattern↵ | | 18 | * Compile the pattern.↵
|
| | | 19 | *↵
|
28 | * @param options the options↵ | | 20 | * @param options the ant regexp options↵
|
29 | * @return the compiled pattern↵ | | 21 | * @return a compiled pattern↵
|
30 | * @throws BuildException on error↵ | | 22 | * @exception BuildException if an error occurs↵
|
31 | */↵ | | 23 | */↵
|
32 | protected Pattern getCompiledPattern(int options)↵ | | 24 | protected RE getCompiledPattern(int options)↵
|
33 | throws BuildException {↵ | | 25 | throws BuildException {↵
|
34 | try {↵ | | 26 | ↵
|
35 | // compute the compiler options based on the input options first↵ | | 27 | int cOptions = getCompilerOptions↵
|
36 | Pattern p = compiler.compile(pattern, getCompilerOptions(o↵ | | 28 | (options);↵
|
| | | 29 | try {↵
|
| | | 30 | RE reg = new RE(pattern);↵
|
37 | ptions));↵ | | 31 | reg.setMatchFlags(cOptions);↵
|
38 | return p;↵ | | 32 | return reg;↵
|
39 | } catch (Exception e) {↵ | | 33 | } catch (RESyntaxException e) {↵
|
40 | throw new BuildException(e);↵ | | 34 | throw new BuildException(e);↵
|
41 | }↵ | | 35 | }↵
|
42 | }↵ | | 36 | }↵
|
|
43 | /**↵ | | 37 | /**↵
|
44 | * Does the given argument match the pattern using default options?↵ | | 38 | * Does the given argument match the pattern?↵
|
45 | * @param argument the string to match against↵ | | 39 | * @param argument the string to match against↵
|
46 | * @return true if the pattern matches↵ | | 40 | * @return true if the pattern matches↵
|
47 | * @throws BuildException on error↵ | | 41 | * @throws BuildException on error↵
|
48 | */↵ | | 42 | */↵
|
49 | public boolean matches(String argument) throws BuildException {↵ | | 43 | public boolean matches(String argument) throws BuildException {↵
|
50 | return matches(argument, MATCH_DEFAULT);↵ | | 44 | return matches(argument, MATCH_DEFAULT);↵
|
51 | }↵ | | 45 | }↵
|
|
52 | /**↵ | | 46 | /**↵
|
53 | * Does the given argument match the pattern?↵ | | 47 | * Does the given argument match the pattern?↵
|
54 | * @param input the string to match against↵ | | 48 | * @param input the string to match against↵
|
55 | * @param options the regex options to use↵ | | 49 | * @param options the regex options to use↵
|
56 | * @return true if the pattern matches↵ | | 50 | * @return true if the pattern matches↵
|
57 | * @throws BuildException on error↵ | | 51 | * @throws BuildException on error↵
|
58 | */↵ | | 52 | */↵
|
59 | public boolean matches(String input, int options)↵ | | 53 | public boolean matches(String input, int options)↵
|
60 | throws BuildException {↵ | | 54 | throws BuildException {↵
|
61 | Pattern p = getCompiledPattern(options);↵ | | 55 | return matches(input, getCompiledPattern(options));↵
|
62 | ↵ | | 56 | }↵
|
|
63 | return matcher.contains(input, p↵ | | 57 | private boolean matches(String input, RE reg) {↵
|
64 | );↵ | | 58 | return reg.match(input);↵
|
65 | }↵ | | 59 | }↵
|
|
66 | /**↵ | | 60 | /**↵
|
67 | * Returns a Vector of matched groups found in the argument↵ | | 61 | * Returns a Vector of matched groups found in the argument↵
|
68 | * using default options.↵ | | 62 | * using default options.↵
|
69 | *↵ | | 63 | *↵
|
70 | * <p>Group 0 will be the full match, the rest are the↵ | | 64 | * <p>Group 0 will be the full match, the rest are the↵
|
71 | * parenthesized subexpressions</p>.↵ | | 65 | * parenthesized subexpressions</p>.↵
|
72 | *↵ | | 66 | *↵
|
73 | * @param argument the string to match against↵ | | 67 | * @param argument the string to match against↵
|
74 | * @return the vector of groups↵ | | 68 | * @return the vector of groups↵
|
75 | * @throws BuildException on error↵ | | 69 | * @throws BuildException on error↵
|
76 | */↵ | | 70 | */↵
|
77 | public Vector getGroups(String argument) throws BuildException {↵ | | 71 | public Vector getGroups(String argument) throws BuildException {↵
|
78 | return getGroups(argument, MATCH_DEFAULT);↵ | | 72 | return getGroups(argument, MATCH_DEFAULT);↵
|
79 | }↵ | | 73 | }↵
|
|
80 | /**↵ | | 74 | /**↵
|
81 | * Returns a Vector of matched groups found in the argument.↵ | | 75 | * Returns a Vector of matched groups found in the argument.↵
|
82 | *↵ | | 76 | *↵
|
83 | * <p>Group 0 will be the full match, the rest are the↵ | | 77 | * <p>Group 0 will be the full match, the rest are the↵
|
84 | * parenthesized subexpressions</p>.↵ | | 78 | * parenthesized subexpressions</p>.↵
|
85 | *↵ | | 79 | *↵
|
86 | * @param input the string to match against↵ | | 80 | * @param input the string to match against↵
|
87 | * @param options the regex options to use↵ | | 81 | * @param options the regex options to use↵
|
88 | * @return the vector of groups↵ | | 82 | * @return the vector of groups↵
|
89 | * @throws BuildException on error↵ | | 83 | * @throws BuildException on error↵
|
90 | */↵ | | 84 | */↵
|
91 | public Vector getGroups(String input, int options)↵ | | 85 | public Vector getGroups(String input, int options)↵
|
92 | throws BuildException {↵ | | 86 | throws BuildException {↵
|
| | | 87 | RE reg = getCompiledPattern(options);↵
|
93 | if (!matches(input, options)) {↵ | | 88 | if (!matches(input, reg)) {↵
|
94 | return null;↵ | | 89 | return null;↵
|
95 | }↵ | | 90 | }↵
|
96 | Vector v = new Vector();↵ | | 91 | Vector v = new Vector();↵
|
97 | MatchResult mr = matcher.getMatch();↵ | | 92 | ↵
|
98 | int cnt = mr.groups();↵ | | 93 | int cnt = reg.getParenCount();↵
|
99 | for (int i = 0; i < cnt; i++) {↵ | | 94 | for (int i = 0; i < cnt; i++) {↵
|
100 | String match = mr.group(i);↵ | | 95 | String match = reg.getParen(i);↵
|
101 | // treat non-matching groups as empty matches↵ | | 96 | // treat non-matching groups as empty matches↵
|
102 | if (match == null) {↵ | | 97 | if (match == null) {↵
|
103 | match = "";↵ | | 98 | match = "";↵
|
104 | }↵ | | 99 | }↵
|
105 | v.addElement(match);↵ | | 100 | v.addElement(match);↵
|
106 | }↵ | | 101 | }↵
|
107 | return v;↵ | | 102 | return v;↵
|
108 | }↵ | | 103 | }↵
|
|
109 | /**↵ | | 104 | /**↵
|
110 | * Convert the generic options to the regex compiler specific options.↵ | | 105 | * Convert the generic options to the regex compiler specific options.↵
|
111 | * @param options the generic options↵ | | 106 | * @param options the generic options↵
|
112 | * @return the specific options↵ | | 107 | * @return the specific options↵
|
113 | */↵ | | 108 | */↵
|
114 | protected int getCompilerOptions(int options) {↵ | | 109 | protected int getCompilerOptions(int options) {↵
|
115 | int cOptions = Perl5Compiler.DEFAULT_MASK;↵ | | 110 | int cOptions = RE.MATCH_NORMAL;↵
|
|
116 | if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {↵ | | 111 | if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {↵
|
117 | cOptions |= Perl5Compiler.CASE_INSENSITIVE_MASK;↵ | | 112 | cOptions |= RE.MATCH_CASEINDEPENDENT;↵
|
118 | }↵ | | 113 | }↵
|
119 | if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {↵ | | 114 | if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {↵
|
120 | cOptions |= Perl5Compiler.MULTILINE_MASK;↵ | | 115 | cOptions |= RE.MATCH_MULTILINE;↵
|
121 | }↵ | | 116 | }↵
|
122 | if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {↵ | | 117 | if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {↵
|
123 | cOptions |= Perl5Compiler.SINGLELINE_MASK;↵ | | 118 | cOptions |= RE.MATCH_SINGLELINE;↵
|
124 | }↵ | | 119 | }↵
|
|
125 | return cOptions;↵ | | 120 | return cOptions;↵
|
126 | | | 121 |
|