1 | public static class ReplaceRegex extends ChainableReaderFilter {↵ | | 1 | public static class ContainsRegex extends ChainableReaderFilter {↵
|
2 | private String from;↵ | | 2 | private String from;↵
|
3 | private String to;↵ | | 3 | private String to;↵
|
4 | private RegularExpression regularExpression;↵ | | 4 | private RegularExpression regularExpression;↵
|
5 | private Substitution substitution;↵ | | 5 | private Substitution substitution;↵
|
6 | private boolean initialized = false;↵ | | 6 | private boolean initialized = false;↵
|
7 | private String flags = "";↵ | | 7 | private String flags = "";↵
|
8 | private int options;↵ | | 8 | private int options;↵
|
9 | private Regexp regexp;↵ | | 9 | private Regexp regexp;↵
|
|
|
10 | /**↵ | | 10 | /**↵
|
11 | * the from attribute↵ | | |
|
12 | * @param from the regex string↵ | | 11 | * @param from the regex pattern↵
|
13 | */↵ | | 12 | */↵
|
14 | public void setPattern(String from) {↵ | | 13 | public void setPattern(String from) {↵
|
15 | this.from = from;↵ | | 14 | this.from = from;↵
|
16 | }↵ | | 15 | }↵
|
|
17 | /**↵ | | 16 | /**↵
|
18 | * the to attribute↵ | | |
|
19 | * @param to the replacement string↵ | | 17 | * @param to the replacement string↵
|
20 | */↵ | | 18 | */↵
|
21 | public void setReplace(String to) {↵ | | 19 | public void setReplace(String to) {↵
|
22 | this.to = to;↵ | | 20 | this.to = to;↵
|
23 | }↵ | | 21 | }↵
|
|
24 | /**↵ | | 22 | /**↵
|
25 | * @param flags the regex flags↵ | | 23 | * @param flags the regex flags↵
|
26 | */↵ | | 24 | */↵
|
27 | public void setFlags(String flags) {↵ | | 25 | public void setFlags(String flags) {↵
|
28 | this.flags = flags;↵ | | 26 | this.flags = flags;↵
|
29 | }↵ | | 27 | }↵
|
|
30 | private void initialize() {↵ | | 28 | private void initialize() {↵
|
31 | if (initialized) {↵ | | 29 | if (initialized) {↵
|
32 | return;↵ | | 30 | return;↵
|
33 | }↵ | | 31 | }↵
|
34 | options = convertRegexOptions(flags);↵ | | 32 | options = convertRegexOptions(flags);↵
|
35 | if (from == null) {↵ | | 33 | if (from == null) {↵
|
36 | throw new BuildException("Missing pattern in replaceregex");↵ | | 34 | throw new BuildException("Missing from in containsregex");↵
|
37 | }↵ | | 35 | }↵
|
38 | regularExpression = new RegularExpression();↵ | | 36 | regularExpression = new RegularExpression();↵
|
39 | regularExpression.setPattern(from);↵ | | 37 | regularExpression.setPattern(from);↵
|
40 | regexp = regularExpression.getRegexp(getProject());↵ | | 38 | regexp = regularExpression.getRegexp(getProject());↵
|
41 | if (to == null) {↵ | | 39 | if (to == null) {↵
|
42 | to = "";↵ | | 40 | return;↵
|
43 | }↵ | | 41 | }↵
|
44 | substitution = new Substitution();↵ | | 42 | substitution = new Substitution();↵
|
45 | substitution.setExpression(to);↵ | | 43 | substitution.setExpression(to);↵
|
46 | }↵ | | 44 | }↵
|
|
47 | /**↵ | | 45 | /**↵
|
| | | 46 | * apply regex and substitution on a string↵
|
48 | * @param line the string to modify↵ | | 47 | * @param string the string to apply filter on↵
|
49 | * @return the modified string↵ | | 48 | * @return the filtered string↵
|
50 | */↵ | | 49 | */↵
|
51 | public String filter(String line) {↵ | | 50 | public String filter(String string) {↵
|
52 | initialize();↵ | | 51 | initialize();↵
|
|
53 | if (!regexp.matches(line, options)) {↵ | | 52 | if (!regexp.matches(string, options)) {↵
|
54 | return line↵ | | 53 | return null;↵
|
| | | 54 | }↵
|
| | | 55 | if (substitution == null) {↵
|
55 | ;↵ | | 56 | return string;↵
|
56 | }↵ | | 57 | }↵
|
57 | return regexp.substitute(↵ | | 58 | return regexp.substitute(↵
|
58 | line, substitution.getExpression(getProject()), options);↵ | | 59 | string, substitution.getExpression(getProject()), options);↵
|
59 | | | 60 |
|