1 18 19 package org.apache.tools.ant.types.selectors; 20 21 import java.io.File ; 22 import java.text.DateFormat ; 23 import java.text.SimpleDateFormat ; 24 import java.text.ParseException ; 25 import java.util.Locale ; 26 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.types.Parameter; 29 import org.apache.tools.ant.types.TimeComparison; 30 import org.apache.tools.ant.util.FileUtils; 31 32 37 public class DateSelector extends BaseExtendSelector { 38 39 40 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 41 42 private long millis = -1; 43 private String dateTime = null; 44 private boolean includeDirs = false; 45 private long granularity = 0; 46 private String pattern; 47 private TimeComparison when = TimeComparison.EQUAL; 48 49 50 public static final String MILLIS_KEY = "millis"; 51 52 public static final String DATETIME_KEY = "datetime"; 53 54 public static final String CHECKDIRS_KEY = "checkdirs"; 55 56 public static final String GRANULARITY_KEY = "granularity"; 57 58 public static final String WHEN_KEY = "when"; 59 60 public static final String PATTERN_KEY = "pattern"; 61 62 66 public DateSelector() { 67 granularity = FILE_UTILS.getFileTimestampGranularity(); 68 } 69 70 73 public String toString() { 74 StringBuffer buf = new StringBuffer ("{dateselector date: "); 75 buf.append(dateTime); 76 buf.append(" compare: ").append(when.getValue()); 77 buf.append(" granularity: "); 78 buf.append(granularity); 79 if (pattern != null) { 80 buf.append(" pattern: ").append(pattern); 81 } 82 buf.append("}"); 83 return buf.toString(); 84 } 85 86 92 public void setMillis(long millis) { 93 this.millis = millis; 94 } 95 96 100 public long getMillis() { 101 if (dateTime != null) { 102 validate(); 103 } 104 return millis; 105 } 106 107 113 public void setDatetime(String dateTime) { 114 this.dateTime = dateTime; 115 millis = -1; 116 } 117 118 123 public void setCheckdirs(boolean includeDirs) { 124 this.includeDirs = includeDirs; 125 } 126 127 132 public void setGranularity(int granularity) { 133 this.granularity = granularity; 134 } 135 136 142 public void setWhen(TimeComparisons tcmp) { 143 setWhen((TimeComparison) tcmp); 144 } 145 146 150 public void setWhen(TimeComparison t) { 151 when = t; 152 } 153 154 159 public void setPattern(String pattern) { 160 this.pattern = pattern; 161 } 162 163 169 public void setParameters(Parameter[] parameters) { 170 super.setParameters(parameters); 171 if (parameters != null) { 172 for (int i = 0; i < parameters.length; i++) { 173 String paramname = parameters[i].getName(); 174 if (MILLIS_KEY.equalsIgnoreCase(paramname)) { 175 try { 176 setMillis(new Long (parameters[i].getValue() 177 ).longValue()); 178 } catch (NumberFormatException nfe) { 179 setError("Invalid millisecond setting " 180 + parameters[i].getValue()); 181 } 182 } else if (DATETIME_KEY.equalsIgnoreCase(paramname)) { 183 setDatetime(parameters[i].getValue()); 184 } else if (CHECKDIRS_KEY.equalsIgnoreCase(paramname)) { 185 setCheckdirs(Project.toBoolean(parameters[i].getValue())); 186 } else if (GRANULARITY_KEY.equalsIgnoreCase(paramname)) { 187 try { 188 setGranularity(new Integer (parameters[i].getValue() 189 ).intValue()); 190 } catch (NumberFormatException nfe) { 191 setError("Invalid granularity setting " 192 + parameters[i].getValue()); 193 } 194 } else if (WHEN_KEY.equalsIgnoreCase(paramname)) { 195 setWhen(new TimeComparison(parameters[i].getValue())); 196 } else if (PATTERN_KEY.equalsIgnoreCase(paramname)) { 197 setPattern(parameters[i].getValue()); 198 } else { 199 setError("Invalid parameter " + paramname); 200 } 201 } 202 } 203 } 204 205 209 public void verifySettings() { 210 if (dateTime == null && millis < 0) { 211 setError("You must provide a datetime or the number of " 212 + "milliseconds."); 213 } else if (millis < 0 && dateTime != null) { 214 DateFormat df = ((pattern == null) 216 ? DateFormat.getDateTimeInstance( 217 DateFormat.SHORT, DateFormat.SHORT, Locale.US) 218 : new SimpleDateFormat (pattern)); 219 220 try { 221 setMillis(df.parse(dateTime).getTime()); 222 if (millis < 0) { 223 setError("Date of " + dateTime 224 + " results in negative milliseconds value" 225 + " relative to epoch (January 1, 1970, 00:00:00 GMT)."); 226 } 227 } catch (ParseException pe) { 228 setError("Date of " + dateTime 229 + " Cannot be parsed correctly. It should be in" 230 + ((pattern == null) 231 ? " MM/DD/YYYY HH:MM AM_PM" : pattern) + " format."); 232 } 233 } 234 } 235 236 245 public boolean isSelected(File basedir, String filename, File file) { 246 247 validate(); 248 249 return (file.isDirectory() && !includeDirs) 250 || when.evaluate(file.lastModified(), millis, granularity); 251 } 252 253 257 public static class TimeComparisons extends TimeComparison { 258 } 259 260 } 261 262 263 | Popular Tags |