1
17package org.tp23.antinstaller.antmod;
18
19
20import java.util.regex.Matcher;
21import java.util.regex.Pattern;
22
23import org.apache.tools.ant.BuildException;
24import org.apache.tools.ant.util.regexp.Regexp;
25import org.apache.tools.ant.util.regexp.RegexpUtil;
26
27
28
31public class Jdk14RegexpRegexp extends Jdk14RegexpMatcher implements Regexp {
32
33 public Jdk14RegexpRegexp() {
34 super();
35 }
36
37 protected int getSubsOptions(int options) {
38 int subsOptions = REPLACE_FIRST;
39 if (RegexpUtil.hasFlag(options, REPLACE_ALL)) {
40 subsOptions = REPLACE_ALL;
41 }
42 return subsOptions;
43 }
44
45 public String substitute(String input, String argument, int options)
46 throws BuildException {
47 StringBuffer subst = new StringBuffer();
49 for (int i = 0; i < argument.length(); i++) {
50 char c = argument.charAt(i);
51 if (c == '$') {
52 subst.append('\\');
53 subst.append('$');
54 } else if (c == '\\') {
55 if (++i < argument.length()) {
56 c = argument.charAt(i);
57 int value = Character.digit(c, 10);
58 if (value > -1) {
59 subst.append("$").append(value);
60 } else {
61 subst.append(c);
62 }
63 } else {
64 subst.append('\\');
66 }
67 } else {
68 subst.append(c);
69 }
70 }
71 argument = subst.toString();
72
73 int sOptions = getSubsOptions(options);
74 Pattern p = getCompiledPattern(options);
75 StringBuffer sb = new StringBuffer();
76
77 Matcher m = p.matcher(input);
78 if (RegexpUtil.hasFlag(sOptions, REPLACE_ALL)) {
79 sb.append(m.replaceAll(argument));
80 } else {
81 boolean res = m.find();
82 if (res) {
83 m.appendReplacement(sb, argument);
84 m.appendTail(sb);
85 } else {
86 sb.append(input);
87 }
88 }
89
90 return sb.toString();
91 }
92}
93