1 18 package org.apache.tools.ant.types.resources.selectors; 19 20 import java.text.DateFormat ; 21 import java.text.SimpleDateFormat ; 22 import java.text.ParseException ; 23 import java.util.Locale ; 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.types.Resource; 27 import org.apache.tools.ant.types.TimeComparison; 28 import org.apache.tools.ant.util.FileUtils; 29 30 38 public class Date implements ResourceSelector { 39 private static final String MILLIS_OR_DATETIME 40 = "Either the millis or the datetime attribute must be set."; 41 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 42 43 private Long millis = null; 44 private String dateTime = null; 45 private String pattern = null; 46 private TimeComparison when = TimeComparison.EQUAL; 47 private long granularity = FILE_UTILS.getFileTimestampGranularity(); 48 49 53 public synchronized void setMillis(long m) { 54 millis = new Long (m); 55 } 56 57 61 public synchronized long getMillis() { 62 return millis == null ? -1L : millis.longValue(); 63 } 64 65 69 public synchronized void setDateTime(String s) { 70 dateTime = s; 71 millis = null; 72 } 73 74 78 public synchronized String getDatetime() { 79 return dateTime; 80 } 81 82 86 public synchronized void setGranularity(long g) { 87 granularity = g; 88 } 89 90 94 public synchronized long getGranularity() { 95 return granularity; 96 } 97 98 102 public synchronized void setPattern(String p) { 103 pattern = p; 104 } 105 106 110 public synchronized String getPattern() { 111 return pattern; 112 } 113 114 118 public synchronized void setWhen(TimeComparison c) { 119 when = c; 120 } 121 122 126 public synchronized TimeComparison getWhen() { 127 return when; 128 } 129 130 135 public synchronized boolean isSelected(Resource r) { 136 if (dateTime == null && millis == null) { 137 throw new BuildException(MILLIS_OR_DATETIME); 138 } 139 if (millis == null) { 140 DateFormat df = ((pattern == null) 141 ? DateFormat.getDateTimeInstance( 142 DateFormat.SHORT, DateFormat.SHORT, Locale.US) 143 : new SimpleDateFormat (pattern)); 144 try { 145 long m = df.parse(dateTime).getTime(); 146 if (m < 0) { 147 throw new BuildException("Date of " + dateTime 148 + " results in negative milliseconds value" 149 + " relative to epoch (January 1, 1970, 00:00:00 GMT)."); 150 } 151 setMillis(m); 152 } catch (ParseException pe) { 153 throw new BuildException("Date of " + dateTime 154 + " Cannot be parsed correctly. It should be in" 155 + (pattern == null 156 ? " MM/DD/YYYY HH:MM AM_PM" : pattern) + " format."); 157 } 158 } 159 return when.evaluate(r.getLastModified(), millis.longValue(), granularity); 160 } 161 162 } 163 | Popular Tags |