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