01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.tools.ant.util.regexp;
19:
20: import org.apache.oro.text.regex.Perl5Substitution;
21: import org.apache.oro.text.regex.Substitution;
22: import org.apache.oro.text.regex.Util;
23: import org.apache.tools.ant.BuildException;
24:
25: /***
26: * Regular expression implementation using the Jakarta Oro package
27: */
28: public class JakartaOroRegexp extends JakartaOroMatcher implements
29: 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: // Do the substitution
72: Substitution s = new Perl5Substitution(subst.toString(),
73: Perl5Substitution.INTERPOLATE_ALL);
74: return Util.substitute(matcher, getCompiledPattern(options), s,
75: input, getSubsOptions(options));
76: }
77:
78: /**
79: * Convert ant regexp substitution option to oro options.
80: *
81: * @param options the ant regexp options
82: * @return the oro substition options
83: */
84: protected int getSubsOptions(int options) {
85: boolean replaceAll = RegexpUtil.hasFlag(options, REPLACE_ALL);
86: int subsOptions = 1;
87: if (replaceAll) {
88: subsOptions = Util.SUBSTITUTE_ALL;
89: }
90: return subsOptions;
91: }
92:
93: }
|