KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import java.util.Vector JavaDoc;
22 import org.apache.regexp.RE;
23 import org.apache.tools.ant.BuildException;
24
25 /***
26  * Regular expression implementation using the Jakarta Regexp package
27  */

28 public class JakartaRegexpRegexp extends JakartaRegexpMatcher
29     implements Regexp {
30
31     /** Constructor for JakartaRegexpRegexp */
32     public JakartaRegexpRegexp() {
33         super();
34     }
35
36     /**
37      * Convert ant regexp substitution option to apache regex options.
38      *
39      * @param options the ant regexp options
40      * @return the apache regex substition options
41      */

42     protected int getSubsOptions(int options) {
43         int subsOptions = RE.REPLACE_FIRSTONLY;
44         if (RegexpUtil.hasFlag(options, REPLACE_ALL)) {
45             subsOptions = RE.REPLACE_ALL;
46         }
47         return subsOptions;
48     }
49
50     /**
51      * Perform a substitution on the regular expression.
52      * @param input The string to substitute on
53      * @param argument The string which defines the substitution
54      * @param options The list of options for the match and replace.
55      * @return the result of the operation
56      * @throws BuildException on error
57      */

58     public String JavaDoc substitute(String JavaDoc input, String JavaDoc argument, int options)
59         throws BuildException {
60         Vector JavaDoc v = getGroups(input, options);
61
62         // replace \1 with the corresponding group
63
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
64         for (int i = 0; i < argument.length(); i++) {
65             char c = argument.charAt(i);
66             if (c == '\\') {
67                 if (++i < argument.length()) {
68                     c = argument.charAt(i);
69                     int value = Character.digit(c, 10);
70                     if (value > -1) {
71                         result.append((String JavaDoc) v.elementAt(value));
72                     } else {
73                         result.append(c);
74                     }
75                 } else {
76                     // XXX - should throw an exception instead?
77
result.append('\\');
78                 }
79             } else {
80                 result.append(c);
81             }
82         }
83         argument = result.toString();
84
85         RE reg = getCompiledPattern(options);
86         int sOptions = getSubsOptions(options);
87         return reg.subst(input, argument, sOptions);
88     }
89 }
90
Popular Tags