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 | */
|
---|
17 | package org.apache.tools.ant.util.regexp;
|
---|
18 |
|
---|
19 |
|
---|
20 | import java.util.regex.Matcher;
|
---|
21 | import java.util.regex.Pattern;
|
---|
22 | import org.apache.tools.ant.BuildException;
|
---|
23 |
|
---|
24 |
|
---|
25 | /***
|
---|
26 | * Regular expression implementation using the JDK 1.4 regular expression package
|
---|
27 | */
|
---|
28 | public class Jdk14RegexpRegexp extends Jdk14RegexpMatcher implements Regexp {
|
---|
29 |
|
---|
30 | /** Constructor for Jdk14RegexpRegexp */
|
---|
31 | public Jdk14RegexpRegexp() {
|
---|
32 | super();
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Convert ant regexp substitution option to jdk1.4 options.
|
---|
37 | *
|
---|
38 | * @param options the ant regexp options
|
---|
39 | * @return the jdk14 substition options
|
---|
40 | */
|
---|
41 | protected int getSubsOptions(int options) {
|
---|
42 | int subsOptions = REPLACE_FIRST;
|
---|
43 | if (RegexpUtil.hasFlag(options, REPLACE_ALL)) {
|
---|
44 | subsOptions = REPLACE_ALL;
|
---|
45 | }
|
---|
46 | return subsOptions;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Perform a substitution on the regular expression.
|
---|
51 | * @param input The string to substitute on
|
---|
52 | * @param argument The string which defines the substitution
|
---|
53 | * @param options The list of options for the match and replace.
|
---|
54 | * @return the result of the operation
|
---|
55 | * @throws BuildException on error
|
---|
56 | */
|
---|
57 | public String substitute(String input, String argument, int options)
|
---|
58 | throws BuildException {
|
---|
59 | // translate \1 to $(1) so that the Matcher will work
|
---|
60 | StringBuffer subst = new StringBuffer();
|
---|
61 | for (int i = 0; i < argument.length(); i++) {
|
---|
62 | char c = argument.charAt(i);
|
---|
63 | if (c == '$') {
|
---|
64 | subst.append('\\');
|
---|
65 | subst.append('$');
|
---|
66 | } else if (c == '\\') {
|
---|
67 | if (++i < argument.length()) {
|
---|
68 | c = argument.charAt(i);
|
---|
69 | int value = Character.digit(c, 10);
|
---|
70 | if (value > -1) {
|
---|
71 | subst.append("$").append(value);
|
---|
72 | } else {
|
---|
73 | subst.append(c);
|
---|
74 | }
|
---|
75 | } else {
|
---|
76 | // XXX - should throw an exception instead?
|
---|
77 | subst.append('\\');
|
---|
78 | }
|
---|
79 | } else {
|
---|
80 | subst.append(c);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | argument = subst.toString();
|
---|
84 |
|
---|
85 | int sOptions = getSubsOptions(options);
|
---|
86 | Pattern p = getCompiledPattern(options);
|
---|
87 | StringBuffer sb = new StringBuffer();
|
---|
88 |
|
---|
89 | Matcher m = p.matcher(input);
|
---|
90 | if (RegexpUtil.hasFlag(sOptions, REPLACE_ALL)) {
|
---|
91 | sb.append(m.replaceAll(argument));
|
---|
92 | } else {
|
---|
93 | boolean res = m.find();
|
---|
94 | if (res) {
|
---|
95 | m.appendReplacement(sb, argument);
|
---|
96 | m.appendTail(sb);
|
---|
97 | } else {
|
---|
98 | sb.append(input);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | return sb.toString();
|
---|
103 | }
|
---|
104 | }
|
---|