1 18 19 package org.apache.tools.ant.util; 20 21 34 public class GlobPatternMapper implements FileNameMapper { 35 36 40 protected String fromPrefix = null; 41 42 45 protected String fromPostfix = null; 46 47 50 protected int prefixLength; 51 52 55 protected int postfixLength; 56 57 60 protected String toPrefix = null; 61 62 65 protected String toPostfix = null; 66 67 69 private boolean handleDirSep = false; 70 private boolean caseSensitive = true; 71 72 78 public void setHandleDirSep(boolean handleDirSep) { 79 this.handleDirSep = handleDirSep; 80 } 81 82 89 public void setCaseSensitive(boolean caseSensitive) { 90 this.caseSensitive = caseSensitive; 91 } 92 93 97 public void setFrom(String from) { 98 int index = from.lastIndexOf("*"); 99 if (index == -1) { 100 fromPrefix = from; 101 fromPostfix = ""; 102 } else { 103 fromPrefix = from.substring(0, index); 104 fromPostfix = from.substring(index + 1); 105 } 106 prefixLength = fromPrefix.length(); 107 postfixLength = fromPostfix.length(); 108 } 109 110 114 public void setTo(String to) { 115 int index = to.lastIndexOf("*"); 116 if (index == -1) { 117 toPrefix = to; 118 toPostfix = ""; 119 } else { 120 toPrefix = to.substring(0, index); 121 toPostfix = to.substring(index + 1); 122 } 123 } 124 125 132 public String [] mapFileName(String sourceFileName) { 133 if (fromPrefix == null 134 || !modifyName(sourceFileName).startsWith(modifyName(fromPrefix)) 135 || !modifyName(sourceFileName).endsWith(modifyName(fromPostfix))) { 136 return null; 137 } 138 return new String [] {toPrefix 139 + extractVariablePart(sourceFileName) 140 + toPostfix}; 141 } 142 143 149 protected String extractVariablePart(String name) { 150 return name.substring(prefixLength, 151 name.length() - postfixLength); 152 } 153 154 159 private String modifyName(String name) { 160 if (!caseSensitive) { 161 name = name.toLowerCase(); 162 } 163 if (handleDirSep) { 164 if (name.indexOf('\\') != -1) { 165 name = name.replace('\\', '/'); 166 } 167 } 168 return name; 169 } 170 } 171 | Popular Tags |