1 18 package org.apache.tools.ant.util.regexp; 19 20 21 import java.util.regex.Matcher ; 22 import java.util.regex.Pattern ; 23 import org.apache.tools.ant.BuildException; 24 25 26 29 public class Jdk14RegexpRegexp extends Jdk14RegexpMatcher implements Regexp { 30 31 32 public Jdk14RegexpRegexp() { 33 super(); 34 } 35 36 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 58 public String substitute(String input, String argument, int options) 59 throws BuildException { 60 StringBuffer subst = new StringBuffer (); 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 subst.append('\\'); 79 } 80 } else { 81 subst.append(c); 82 } 83 } 84 argument = subst.toString(); 85 86 int sOptions = getSubsOptions(options); 87 Pattern p = getCompiledPattern(options); 88 StringBuffer sb = new StringBuffer (); 89 90 Matcher 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 |