1 18 19 package org.apache.tools.ant.util; 20 21 import java.util.Vector ; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.util.regexp.RegexpMatcher; 24 import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; 25 26 31 public class RegexpPatternMapper implements FileNameMapper { 32 protected RegexpMatcher reg = null; 34 protected char[] to = null; 35 protected StringBuffer result = new StringBuffer (); 36 38 42 public RegexpPatternMapper() throws BuildException { 43 reg = (new RegexpMatcherFactory()).newRegexpMatcher(); 44 } 45 46 private boolean handleDirSep = false; 47 private int regexpOptions = 0; 48 49 55 public void setHandleDirSep(boolean handleDirSep) { 56 this.handleDirSep = handleDirSep; 57 } 58 59 66 public void setCaseSensitive(boolean caseSensitive) { 67 if (!caseSensitive) { 68 regexpOptions = RegexpMatcher.MATCH_CASE_INSENSITIVE; 69 } else { 70 regexpOptions = 0; 71 } 72 } 73 74 79 public void setFrom(String from) throws BuildException { 80 try { 81 reg.setPattern(from); 82 } catch (NoClassDefFoundError e) { 83 throw new BuildException("Cannot load regular expression matcher", 86 e); 87 } 88 } 89 90 95 public void setTo(String to) { 96 this.to = to.toCharArray(); 97 } 98 99 107 public String [] mapFileName(String sourceFileName) { 108 if (handleDirSep) { 109 if (sourceFileName.indexOf("\\") != -1) { 110 sourceFileName = sourceFileName.replace('\\', '/'); 111 } 112 } 113 if (reg == null || to == null 114 || !reg.matches(sourceFileName, regexpOptions)) { 115 return null; 116 } 117 return new String [] {replaceReferences(sourceFileName)}; 118 } 119 120 126 protected String replaceReferences(String source) { 127 Vector v = reg.getGroups(source, regexpOptions); 128 129 result.setLength(0); 130 for (int i = 0; i < to.length; i++) { 131 if (to[i] == '\\') { 132 if (++i < to.length) { 133 int value = Character.digit(to[i], 10); 134 if (value > -1) { 135 result.append((String ) v.elementAt(value)); 136 } else { 137 result.append(to[i]); 138 } 139 } else { 140 result.append('\\'); 142 } 143 } else { 144 result.append(to[i]); 145 } 146 } 147 return result.substring(0); 148 } 149 150 } 151 | Popular Tags |