KickJava   Java API By Example, From Geeks To Geeks.

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


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.regex.Matcher JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23 import org.apache.tools.ant.BuildException;
24
25
26 /***
27  * Regular expression implementation using the JDK 1.4 regular expression package
28  */

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

42     protected int getSubsOptions(int options) {
43         int subsOptions = REPLACE_FIRST;
44         if (RegexpUtil.hasFlag(options, REPLACE_ALL)) {
45             subsOptions = 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         // translate \1 to $(1) so that the Matcher will work
61
StringBuffer JavaDoc subst = new StringBuffer JavaDoc();
62         for (int i = 0; i < argument.length(); i++) {
63             char c = argument.charAt(i);
64             if (c == '$') {
65                 subst.append('\\');
66                 subst.append('$');
67             } else if (c == '\\') {
68                 if (++i < argument.length()) {
69                     c = argument.charAt(i);
70                     int value = Character.digit(c, 10);
71                     if (value > -1) {
72                         subst.append("$").append(value);
73                     } else {
74                         subst.append(c);
75                     }
76                 } else {
77                     // XXX - should throw an exception instead?
78
subst.append('\\');
79                 }
80             } else {
81                 subst.append(c);
82             }
83         }
84         argument = subst.toString();
85
86         int sOptions = getSubsOptions(options);
87         Pattern JavaDoc p = getCompiledPattern(options);
88         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
89
90         Matcher JavaDoc m = p.matcher(input);
91         if (RegexpUtil.hasFlag(sOptions, REPLACE_ALL)) {
92             sb.append(m.replaceAll(argument));
93         } else {
94             boolean res = m.find();
95             if (res) {
96                 m.appendReplacement(sb, argument);
97                 m.appendTail(sb);
98             } else {
99                 sb.append(input);
100             }
101         }
102
103         return sb.toString();
104     }
105 }
106
Popular Tags