1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.text.SimpleDateFormat ; 22 import java.util.Calendar ; 23 import java.util.Date ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 import java.util.NoSuchElementException ; 29 import java.util.StringTokenizer ; 30 import java.util.TimeZone ; 31 import java.util.Vector ; 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.Location; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.Task; 36 import org.apache.tools.ant.types.EnumeratedAttribute; 37 38 45 public class Tstamp extends Task { 46 47 private Vector customFormats = new Vector (); 48 private String prefix = ""; 49 50 56 public void setPrefix(String prefix) { 57 this.prefix = prefix; 58 if (!this.prefix.endsWith(".")) { 59 this.prefix += "."; 60 } 61 } 62 63 68 public void execute() throws BuildException { 69 try { 70 Date d = new Date (); 71 72 Enumeration i = customFormats.elements(); 73 while (i.hasMoreElements()) { 74 CustomFormat cts = (CustomFormat) i.nextElement(); 75 cts.execute(getProject(), d, getLocation()); 76 } 77 78 SimpleDateFormat dstamp = new SimpleDateFormat ("yyyyMMdd"); 79 setProperty("DSTAMP", dstamp.format(d)); 80 81 SimpleDateFormat tstamp = new SimpleDateFormat ("HHmm"); 82 setProperty("TSTAMP", tstamp.format(d)); 83 84 SimpleDateFormat today 85 = new SimpleDateFormat ("MMMM d yyyy", Locale.US); 86 setProperty("TODAY", today.format(d)); 87 88 } catch (Exception e) { 89 throw new BuildException(e); 90 } 91 } 92 93 97 public CustomFormat createFormat() { 98 CustomFormat cts = new CustomFormat(); 99 customFormats.addElement(cts); 100 return cts; 101 } 102 103 107 private void setProperty(String name, String value) { 108 getProject().setNewProperty(prefix + name, value); 109 } 110 111 120 public class CustomFormat { 121 private TimeZone timeZone; 122 private String propertyName; 123 private String pattern; 124 private String language; 125 private String country; 126 private String variant; 127 private int offset = 0; 128 private int field = Calendar.DATE; 129 130 133 public CustomFormat() { 134 } 135 136 140 public void setProperty(String propertyName) { 141 this.propertyName = propertyName; 142 } 143 144 150 public void setPattern(String pattern) { 151 this.pattern = pattern; 152 } 153 154 163 public void setLocale(String locale) { 164 StringTokenizer st = new StringTokenizer (locale, " \t\n\r\f,"); 165 try { 166 language = st.nextToken(); 167 if (st.hasMoreElements()) { 168 country = st.nextToken(); 169 if (st.hasMoreElements()) { 170 variant = st.nextToken(); 171 if (st.hasMoreElements()) { 172 throw new BuildException("bad locale format", 173 getLocation()); 174 } 175 } 176 } else { 177 country = ""; 178 } 179 } catch (NoSuchElementException e) { 180 throw new BuildException("bad locale format", e, 181 getLocation()); 182 } 183 } 184 185 191 public void setTimezone(String id) { 192 timeZone = TimeZone.getTimeZone(id); 193 } 194 195 199 public void setOffset(int offset) { 200 this.offset = offset; 201 } 202 203 213 public void setUnit(String unit) { 214 log("DEPRECATED - The setUnit(String) method has been deprecated." 215 + " Use setUnit(Tstamp.Unit) instead."); 216 Unit u = new Unit(); 217 u.setValue(unit); 218 field = u.getCalendarField(); 219 } 220 221 237 public void setUnit(Unit unit) { 238 field = unit.getCalendarField(); 239 } 240 241 247 public void execute(Project project, Date date, Location location) { 248 if (propertyName == null) { 249 throw new BuildException("property attribute must be provided", 250 location); 251 } 252 253 if (pattern == null) { 254 throw new BuildException("pattern attribute must be provided", 255 location); 256 } 257 258 SimpleDateFormat sdf; 259 if (language == null) { 260 sdf = new SimpleDateFormat (pattern); 261 } else if (variant == null) { 262 sdf = new SimpleDateFormat (pattern, 263 new Locale (language, country)); 264 } else { 265 sdf = new SimpleDateFormat (pattern, 266 new Locale (language, country, 267 variant)); 268 } 269 if (offset != 0) { 270 Calendar calendar = Calendar.getInstance(); 271 calendar.setTime(date); 272 calendar.add(field, offset); 273 date = calendar.getTime(); 274 } 275 if (timeZone != null) { 276 sdf.setTimeZone(timeZone); 277 } 278 Tstamp.this.setProperty(propertyName, sdf.format(date)); 279 } 280 } 281 282 285 public static class Unit extends EnumeratedAttribute { 286 287 private static final String MILLISECOND = "millisecond"; 288 private static final String SECOND = "second"; 289 private static final String MINUTE = "minute"; 290 private static final String HOUR = "hour"; 291 private static final String DAY = "day"; 292 private static final String WEEK = "week"; 293 private static final String MONTH = "month"; 294 private static final String YEAR = "year"; 295 296 private static final String [] UNITS = { 297 MILLISECOND, 298 SECOND, 299 MINUTE, 300 HOUR, 301 DAY, 302 WEEK, 303 MONTH, 304 YEAR 305 }; 306 307 private Map calendarFields = new HashMap (); 308 309 310 public Unit() { 311 calendarFields.put(MILLISECOND, 312 new Integer (Calendar.MILLISECOND)); 313 calendarFields.put(SECOND, new Integer (Calendar.SECOND)); 314 calendarFields.put(MINUTE, new Integer (Calendar.MINUTE)); 315 calendarFields.put(HOUR, new Integer (Calendar.HOUR_OF_DAY)); 316 calendarFields.put(DAY, new Integer (Calendar.DATE)); 317 calendarFields.put(WEEK, new Integer (Calendar.WEEK_OF_YEAR)); 318 calendarFields.put(MONTH, new Integer (Calendar.MONTH)); 319 calendarFields.put(YEAR, new Integer (Calendar.YEAR)); 320 } 321 322 326 public int getCalendarField() { 327 String key = getValue().toLowerCase(); 328 Integer i = (Integer ) calendarFields.get(key); 329 return i.intValue(); 330 } 331 332 336 public String [] getValues() { 337 return UNITS; 338 } 339 } 340 } 341 | Popular Tags |