KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > regexp > JakartaOroRegexp


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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
27 /***
28  * Regular expression implementation using the Jakarta Oro package
29  */

30 public class JakartaOroRegexp extends JakartaOroMatcher implements Regexp {
31
32     /** Constructor for JakartaOroRegexp */
33     public JakartaOroRegexp() {
34         super();
35     }
36
37     /**
38      * Perform a substitution on the regular expression.
39      * @param input The string to substitute on
40      * @param argument The string which defines the substitution
41      * @param options The list of options for the match and replace.
42      * @return the result of the operation
43      * @throws BuildException on error
44      */

45     public String JavaDoc substitute(String JavaDoc input, String JavaDoc argument, int options)
46         throws BuildException {
47         // translate \1 to $1 so that the Perl5Substitution will work
48
StringBuffer JavaDoc subst = new StringBuffer JavaDoc();
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
72
73         // Do the substitution
74
Substitution s =
75             new Perl5Substitution(subst.toString(),
76                                   Perl5Substitution.INTERPOLATE_ALL);
77         return Util.substitute(matcher,
78                                getCompiledPattern(options),
79                                s,
80                                input,
81                                getSubsOptions(options));
82     }
83
84     /**
85      * Convert ant regexp substitution option to oro options.
86      *
87      * @param options the ant regexp options
88      * @return the oro substition options
89      */

90     protected int getSubsOptions(int options) {
91         boolean replaceAll = RegexpUtil.hasFlag(options, REPLACE_ALL);
92         int subsOptions = 1;
93         if (replaceAll) {
94             subsOptions = Util.SUBSTITUTE_ALL;
95         }
96         return subsOptions;
97     }
98
99 }
100
Popular Tags