001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.util.regexp;
019:
020: import java.util.regex.Matcher;
021: import java.util.regex.Pattern;
022: import org.apache.tools.ant.BuildException;
023:
024: /***
025: * Regular expression implementation using the JDK 1.4 regular expression package
026: */
027: public class Jdk14RegexpRegexp extends Jdk14RegexpMatcher implements
028: Regexp {
029:
030: /** Constructor for Jdk14RegexpRegexp */
031: public Jdk14RegexpRegexp() {
032: super ();
033: }
034:
035: /**
036: * Convert ant regexp substitution option to jdk1.4 options.
037: *
038: * @param options the ant regexp options
039: * @return the jdk14 substition options
040: */
041: protected int getSubsOptions(int options) {
042: int subsOptions = REPLACE_FIRST;
043: if (RegexpUtil.hasFlag(options, REPLACE_ALL)) {
044: subsOptions = REPLACE_ALL;
045: }
046: return subsOptions;
047: }
048:
049: /**
050: * Perform a substitution on the regular expression.
051: * @param input The string to substitute on
052: * @param argument The string which defines the substitution
053: * @param options The list of options for the match and replace.
054: * @return the result of the operation
055: * @throws BuildException on error
056: */
057: public String substitute(String input, String argument, int options)
058: throws BuildException {
059: // translate \1 to $(1) so that the Matcher will work
060: StringBuffer subst = new StringBuffer();
061: for (int i = 0; i < argument.length(); i++) {
062: char c = argument.charAt(i);
063: if (c == '$') {
064: subst.append('\\');
065: subst.append('$');
066: } else if (c == '\\') {
067: if (++i < argument.length()) {
068: c = argument.charAt(i);
069: int value = Character.digit(c, 10);
070: if (value > -1) {
071: subst.append("$").append(value);
072: } else {
073: subst.append(c);
074: }
075: } else {
076: // XXX - should throw an exception instead?
077: subst.append('\\');
078: }
079: } else {
080: subst.append(c);
081: }
082: }
083: argument = subst.toString();
084:
085: int sOptions = getSubsOptions(options);
086: Pattern p = getCompiledPattern(options);
087: StringBuffer sb = new StringBuffer();
088:
089: Matcher m = p.matcher(input);
090: if (RegexpUtil.hasFlag(sOptions, REPLACE_ALL)) {
091: sb.append(m.replaceAll(argument));
092: } else {
093: boolean res = m.find();
094: if (res) {
095: m.appendReplacement(sb, argument);
096: m.appendTail(sb);
097: } else {
098: sb.append(input);
099: }
100: }
101:
102: return sb.toString();
103: }
104: }
|