You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tstamp.java 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.Enumeration;
  23. import java.util.HashMap;
  24. import java.util.Locale;
  25. import java.util.Map;
  26. import java.util.NoSuchElementException;
  27. import java.util.StringTokenizer;
  28. import java.util.TimeZone;
  29. import java.util.Vector;
  30. import org.apache.tools.ant.BuildException;
  31. import org.apache.tools.ant.Location;
  32. import org.apache.tools.ant.Project;
  33. import org.apache.tools.ant.Task;
  34. import org.apache.tools.ant.types.EnumeratedAttribute;
  35. /**
  36. * Sets properties to the current time, or offsets from the current time.
  37. * The default properties are TSTAMP, DSTAMP and TODAY;
  38. *
  39. * @since Ant 1.1
  40. * @ant.task category="utility"
  41. */
  42. public class Tstamp extends Task {
  43. private Vector customFormats = new Vector();
  44. private String prefix = "";
  45. /**
  46. * Set a prefix for the properties. If the prefix does not end with a "."
  47. * one is automatically added.
  48. * @param prefix the prefix to use.
  49. * @since Ant 1.5
  50. */
  51. public void setPrefix(String prefix) {
  52. this.prefix = prefix;
  53. if (!this.prefix.endsWith(".")) {
  54. this.prefix += ".";
  55. }
  56. }
  57. /**
  58. * create the timestamps. Custom ones are done before
  59. * the standard ones, to get their retaliation in early.
  60. * @throws BuildException on error.
  61. */
  62. public void execute() throws BuildException {
  63. try {
  64. Date d = new Date();
  65. Enumeration i = customFormats.elements();
  66. while (i.hasMoreElements()) {
  67. CustomFormat cts = (CustomFormat) i.nextElement();
  68. cts.execute(getProject(), d, getLocation());
  69. }
  70. SimpleDateFormat dstamp = new SimpleDateFormat ("yyyyMMdd");
  71. setProperty("DSTAMP", dstamp.format(d));
  72. SimpleDateFormat tstamp = new SimpleDateFormat ("HHmm");
  73. setProperty("TSTAMP", tstamp.format(d));
  74. SimpleDateFormat today
  75. = new SimpleDateFormat ("MMMM d yyyy", Locale.US);
  76. setProperty("TODAY", today.format(d));
  77. } catch (Exception e) {
  78. throw new BuildException(e);
  79. }
  80. }
  81. /**
  82. * create a custom format with the current prefix.
  83. * @return a ready to fill-in format
  84. */
  85. public CustomFormat createFormat() {
  86. CustomFormat cts = new CustomFormat();
  87. customFormats.addElement(cts);
  88. return cts;
  89. }
  90. /**
  91. * helper that encapsulates prefix logic and property setting
  92. * policy (i.e. we use setNewProperty instead of setProperty).
  93. */
  94. private void setProperty(String name, String value) {
  95. getProject().setNewProperty(prefix + name, value);
  96. }
  97. /**
  98. * This nested element that allows a property to be set
  99. * to the current date and time in a given format.
  100. * The date/time patterns are as defined in the
  101. * Java SimpleDateFormat class.
  102. * The format element also allows offsets to be applied to
  103. * the time to generate different time values.
  104. * @todo consider refactoring out into a re-usable element.
  105. */
  106. public class CustomFormat {
  107. private TimeZone timeZone;
  108. private String propertyName;
  109. private String pattern;
  110. private String language;
  111. private String country;
  112. private String variant;
  113. private int offset = 0;
  114. private int field = Calendar.DATE;
  115. /**
  116. * Create a format
  117. */
  118. public CustomFormat() {
  119. }
  120. /**
  121. * The property to receive the date/time string in the given pattern
  122. * @param propertyName the name of the property.
  123. */
  124. public void setProperty(String propertyName) {
  125. this.propertyName = propertyName;
  126. }
  127. /**
  128. * The date/time pattern to be used. The values are as
  129. * defined by the Java SimpleDateFormat class.
  130. * @param pattern the pattern to use.
  131. * @see java.text.SimpleDateFormat
  132. */
  133. public void setPattern(String pattern) {
  134. this.pattern = pattern;
  135. }
  136. /**
  137. * The locale used to create date/time string.
  138. * The general form is "language, country, variant" but
  139. * either variant or variant and country may be omitted.
  140. * For more information please refer to documentation
  141. * for the java.util.Locale class.
  142. * @param locale the locale to use.
  143. * @see java.util.Locale
  144. */
  145. public void setLocale(String locale) {
  146. StringTokenizer st = new StringTokenizer(locale, " \t\n\r\f,");
  147. try {
  148. language = st.nextToken();
  149. if (st.hasMoreElements()) {
  150. country = st.nextToken();
  151. if (st.hasMoreElements()) {
  152. variant = st.nextToken();
  153. if (st.hasMoreElements()) {
  154. throw new BuildException("bad locale format",
  155. getLocation());
  156. }
  157. }
  158. } else {
  159. country = "";
  160. }
  161. } catch (NoSuchElementException e) {
  162. throw new BuildException("bad locale format", e,
  163. getLocation());
  164. }
  165. }
  166. /**
  167. * The timezone to use for displaying time.
  168. * The values are as defined by the Java TimeZone class.
  169. * @param id the timezone value.
  170. * @see java.util.TimeZone
  171. */
  172. public void setTimezone(String id) {
  173. timeZone = TimeZone.getTimeZone(id);
  174. }
  175. /**
  176. * The numeric offset to the current time.
  177. * @param offset the offset to use.
  178. */
  179. public void setOffset(int offset) {
  180. this.offset = offset;
  181. }
  182. /**
  183. * Set the unit type (using String).
  184. * @param unit the unit to use.
  185. * @deprecated since 1.5.x.
  186. * setUnit(String) is deprecated and is replaced with
  187. * setUnit(Tstamp.Unit) to make Ant's
  188. * Introspection mechanism do the work and also to
  189. * encapsulate operations on the unit in its own
  190. * class.
  191. */
  192. public void setUnit(String unit) {
  193. log("DEPRECATED - The setUnit(String) method has been deprecated."
  194. + " Use setUnit(Tstamp.Unit) instead.");
  195. Unit u = new Unit();
  196. u.setValue(unit);
  197. field = u.getCalendarField();
  198. }
  199. /**
  200. * The unit of the offset to be applied to the current time.
  201. * Valid Values are
  202. * <ul>
  203. * <li>millisecond</li>
  204. * <li>second</li>
  205. * <li>minute</li>
  206. * <li>hour</li>
  207. * <li>day</li>
  208. * <li>week</li>
  209. * <li>month</li>
  210. * <li>year</li>
  211. * </ul>
  212. * The default unit is day.
  213. * @param unit the unit to use.
  214. */
  215. public void setUnit(Unit unit) {
  216. field = unit.getCalendarField();
  217. }
  218. /**
  219. * validate parameter and execute the format.
  220. * @param project project to set property in.
  221. * @param date date to use as a starting point.
  222. * @param location line in file (for errors)
  223. */
  224. public void execute(Project project, Date date, Location location) {
  225. if (propertyName == null) {
  226. throw new BuildException("property attribute must be provided",
  227. location);
  228. }
  229. if (pattern == null) {
  230. throw new BuildException("pattern attribute must be provided",
  231. location);
  232. }
  233. SimpleDateFormat sdf;
  234. if (language == null) {
  235. sdf = new SimpleDateFormat(pattern);
  236. } else if (variant == null) {
  237. sdf = new SimpleDateFormat(pattern,
  238. new Locale(language, country));
  239. } else {
  240. sdf = new SimpleDateFormat(pattern,
  241. new Locale(language, country,
  242. variant));
  243. }
  244. if (offset != 0) {
  245. Calendar calendar = Calendar.getInstance();
  246. calendar.setTime(date);
  247. calendar.add(field, offset);
  248. date = calendar.getTime();
  249. }
  250. if (timeZone != null) {
  251. sdf.setTimeZone(timeZone);
  252. }
  253. Tstamp.this.setProperty(propertyName, sdf.format(date));
  254. }
  255. }
  256. /**
  257. * set of valid units to use for time offsets.
  258. */
  259. public static class Unit extends EnumeratedAttribute {
  260. private static final String MILLISECOND = "millisecond";
  261. private static final String SECOND = "second";
  262. private static final String MINUTE = "minute";
  263. private static final String HOUR = "hour";
  264. private static final String DAY = "day";
  265. private static final String WEEK = "week";
  266. private static final String MONTH = "month";
  267. private static final String YEAR = "year";
  268. private static final String[] UNITS = {
  269. MILLISECOND,
  270. SECOND,
  271. MINUTE,
  272. HOUR,
  273. DAY,
  274. WEEK,
  275. MONTH,
  276. YEAR
  277. };
  278. private Map calendarFields = new HashMap();
  279. /** Constructor for Unit enumerated type. */
  280. public Unit() {
  281. calendarFields.put(MILLISECOND,
  282. new Integer(Calendar.MILLISECOND));
  283. calendarFields.put(SECOND, new Integer(Calendar.SECOND));
  284. calendarFields.put(MINUTE, new Integer(Calendar.MINUTE));
  285. calendarFields.put(HOUR, new Integer(Calendar.HOUR_OF_DAY));
  286. calendarFields.put(DAY, new Integer(Calendar.DATE));
  287. calendarFields.put(WEEK, new Integer(Calendar.WEEK_OF_YEAR));
  288. calendarFields.put(MONTH, new Integer(Calendar.MONTH));
  289. calendarFields.put(YEAR, new Integer(Calendar.YEAR));
  290. }
  291. /**
  292. * Convert the value to int unit value.
  293. * @return an int value.
  294. */
  295. public int getCalendarField() {
  296. String key = getValue().toLowerCase();
  297. Integer i = (Integer) calendarFields.get(key);
  298. return i.intValue();
  299. }
  300. /**
  301. * Get the valid values.
  302. * @return the value values.
  303. */
  304. public String[] getValues() {
  305. return UNITS;
  306. }
  307. }
  308. }