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