1 18 19 package org.apache.tools.ant.types.selectors; 20 21 import java.io.File ; 22 import java.util.StringTokenizer ; 23 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.types.Parameter; 26 27 33 public class DepthSelector extends BaseExtendSelector { 34 35 37 38 public int min = -1; 39 40 public int max = -1; 41 42 44 45 public static final String MIN_KEY = "min"; 46 47 public static final String MAX_KEY = "max"; 48 49 53 public DepthSelector() { 54 } 55 56 59 public String toString() { 60 StringBuffer buf = new StringBuffer ("{depthselector min: "); 61 buf.append(min); 62 buf.append(" max: "); 63 buf.append(max); 64 buf.append("}"); 65 return buf.toString(); 66 } 67 68 73 public void setMin(int min) { 74 this.min = min; 75 } 76 77 82 public void setMax(int max) { 83 this.max = max; 84 } 85 86 92 public void setParameters(Parameter[] parameters) { 93 super.setParameters(parameters); 94 if (parameters != null) { 95 for (int i = 0; i < parameters.length; i++) { 96 String paramname = parameters[i].getName(); 97 if (MIN_KEY.equalsIgnoreCase(paramname)) { 98 try { 99 setMin(Integer.parseInt(parameters[i].getValue())); 100 } catch (NumberFormatException nfe1) { 101 setError("Invalid minimum value " 102 + parameters[i].getValue()); 103 } 104 } else if (MAX_KEY.equalsIgnoreCase(paramname)) { 105 try { 106 setMax(Integer.parseInt(parameters[i].getValue())); 107 } catch (NumberFormatException nfe1) { 108 setError("Invalid maximum value " 109 + parameters[i].getValue()); 110 } 111 } else { 112 setError("Invalid parameter " + paramname); 113 } 114 } 115 } 116 } 117 118 122 public void verifySettings() { 123 if (min < 0 && max < 0) { 124 setError("You must set at least one of the min or the " 125 + "max levels."); 126 } 127 if (max < min && max > -1) { 128 setError("The maximum depth is lower than the minimum."); 129 } 130 } 131 132 144 public boolean isSelected(File basedir, String filename, File file) { 145 146 validate(); 148 149 int depth = -1; 150 String absBase = basedir.getAbsolutePath(); 152 String absFile = file.getAbsolutePath(); 153 StringTokenizer tokBase = new StringTokenizer (absBase, 154 File.separator); 155 StringTokenizer tokFile = new StringTokenizer (absFile, 156 File.separator); 157 while (tokFile.hasMoreTokens()) { 158 String filetoken = tokFile.nextToken(); 159 if (tokBase.hasMoreTokens()) { 160 String basetoken = tokBase.nextToken(); 161 if (!basetoken.equals(filetoken)) { 163 throw new BuildException("File " + filename 164 + " does not appear within " + absBase 165 + "directory"); 166 } 167 } else { 168 depth += 1; 169 if (max > -1 && depth > max) { 170 return false; 171 } 172 } 173 } 174 if (tokBase.hasMoreTokens()) { 175 throw new BuildException("File " + filename 176 + " is outside of " + absBase + "directory tree"); 177 } 178 if (min > -1 && depth < min) { 179 return false; 180 } 181 return true; 182 } 183 184 } 185 186 | Popular Tags |