1 /*
2  * Copyright  2001-2004 The Apache Software Foundation
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 *
16 */
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/***
29 * Regular expression implementation using the JDK 1.4 regular expression package
30 */
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        // translate \1 to $(1) so that the Matcher will work
48        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                    // XXX - should throw an exception instead?
65                    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