1
17
18package org.tp23.antinstaller.antmod;
19
20import java.util.Vector;
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23import java.util.regex.PatternSyntaxException;
24
25import org.apache.tools.ant.BuildException;
26import org.apache.tools.ant.util.regexp.RegexpMatcher;
27import org.apache.tools.ant.util.regexp.RegexpUtil;
28
29
34public class Jdk14RegexpMatcher implements RegexpMatcher {
35
36 private String pattern;
37
38 public Jdk14RegexpMatcher() {
39 }
40
41
44 public void setPattern(String pattern) {
45 this.pattern = pattern;
46 }
47
48
51 public String getPattern() {
52 return pattern;
53 }
54
55 protected Pattern getCompiledPattern(int options)
56 throws BuildException {
57 int cOptions = getCompilerOptions(options);
58 try {
59 Pattern p = Pattern.compile(this.pattern, cOptions);
60 return p;
61 } catch (PatternSyntaxException e) {
62 throw new BuildException(e);
63 }
64 }
65
66
69 public boolean matches(String argument) throws BuildException {
70 return matches(argument, MATCH_DEFAULT);
71 }
72
73
76 public boolean matches(String input, int options)
77 throws BuildException {
78 try {
79 Pattern p = getCompiledPattern(options);
80 return p.matcher(input).find();
81 } catch (Exception e) {
82 throw new BuildException(e);
83 }
84 }
85
86
92 public Vector getGroups(String argument) throws BuildException {
93 return getGroups(argument, MATCH_DEFAULT);
94 }
95
96
02 public Vector getGroups(String input, int options)
03 throws BuildException {
04 Pattern p = getCompiledPattern(options);
05 Matcher matcher = p.matcher(input);
06 if (!matcher.find()) {
07 return null;
08 }
09 Vector v = new Vector();
10 int cnt = matcher.groupCount();
11 for (int i = 0; i <= cnt; i++) {
12 String match = matcher.group(i);
13 if (match == null) {
15 match = "";
16 }
17 v.addElement(match);
18 }
19 return v;
20 }
21
22 protected int getCompilerOptions(int options) {
23 int cOptions = Pattern.UNIX_LINES;
25
26 if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
27 cOptions |= Pattern.CASE_INSENSITIVE;
28 }
29 if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
30 cOptions |= Pattern.MULTILINE;
31 }
32 if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
33 cOptions |= Pattern.DOTALL;
34 }
35
36 return cOptions;
37 }
38
39}
40