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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.Task;
  56. import org.apache.tools.ant.BuildException;
  57. import org.apache.tools.ant.Project;
  58. import org.apache.tools.ant.Location;
  59. import org.apache.tools.ant.types.EnumeratedAttribute;
  60. import java.util.Calendar;
  61. import java.util.Date;
  62. import java.util.Enumeration;
  63. import java.util.Hashtable;
  64. import java.util.Locale;
  65. import java.util.NoSuchElementException;
  66. import java.util.StringTokenizer;
  67. import java.util.TimeZone;
  68. import java.util.Vector;
  69. import java.text.SimpleDateFormat;
  70. /**
  71. * Sets TSTAMP, DSTAMP and TODAY
  72. *
  73. * @author costin@dnt.ro
  74. * @author stefano@apache.org
  75. * @author roxspring@yahoo.com
  76. * @author Conor MacNeill
  77. * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
  78. * @since Ant 1.1
  79. * @ant.task category="utility"
  80. */
  81. public class Tstamp extends Task {
  82. private Vector customFormats = new Vector();
  83. private String prefix = "";
  84. /**
  85. * @since Ant 1.5
  86. */
  87. public void setPrefix(String prefix) {
  88. this.prefix = prefix;
  89. if (!this.prefix.endsWith(".")) {
  90. this.prefix += ".";
  91. }
  92. }
  93. public void execute() throws BuildException {
  94. try {
  95. Date d = new Date();
  96. SimpleDateFormat dstamp = new SimpleDateFormat ("yyyyMMdd");
  97. project.setNewProperty(prefix + "DSTAMP", dstamp.format(d));
  98. SimpleDateFormat tstamp = new SimpleDateFormat ("HHmm");
  99. project.setNewProperty(prefix + "TSTAMP", tstamp.format(d));
  100. SimpleDateFormat today
  101. = new SimpleDateFormat ("MMMM d yyyy", Locale.US);
  102. project.setNewProperty(prefix + "TODAY", today.format(d));
  103. Enumeration i = customFormats.elements();
  104. while (i.hasMoreElements()) {
  105. CustomFormat cts = (CustomFormat) i.nextElement();
  106. cts.execute(project, d, location);
  107. }
  108. } catch (Exception e) {
  109. throw new BuildException(e);
  110. }
  111. }
  112. public CustomFormat createFormat() {
  113. CustomFormat cts = new CustomFormat(prefix);
  114. customFormats.addElement(cts);
  115. return cts;
  116. }
  117. public class CustomFormat {
  118. private TimeZone timeZone;
  119. private String propertyName;
  120. private String pattern;
  121. private String language;
  122. private String country;
  123. private String variant;
  124. private int offset = 0;
  125. private int field = Calendar.DATE;
  126. private String prefix = "";
  127. public CustomFormat(String prefix) {
  128. this.prefix = prefix;
  129. }
  130. public void setProperty(String propertyName) {
  131. this.propertyName = prefix + propertyName;
  132. }
  133. public void setPattern(String pattern) {
  134. this.pattern = pattern;
  135. }
  136. public void setLocale(String locale) {
  137. StringTokenizer st = new StringTokenizer(locale, " \t\n\r\f,");
  138. try {
  139. language = st.nextToken();
  140. if (st.hasMoreElements()) {
  141. country = st.nextToken();
  142. if (st.hasMoreElements()) {
  143. variant = st.nextToken();
  144. if (st.hasMoreElements()) {
  145. throw new BuildException("bad locale format",
  146. getLocation());
  147. }
  148. }
  149. } else {
  150. country = "";
  151. }
  152. } catch (NoSuchElementException e) {
  153. throw new BuildException("bad locale format", e,
  154. getLocation());
  155. }
  156. }
  157. public void setTimezone(String id){
  158. timeZone = TimeZone.getTimeZone(id);
  159. }
  160. public void setOffset(int offset) {
  161. this.offset = offset;
  162. }
  163. /**
  164. * @deprecated setUnit(String) is deprecated and is replaced with
  165. * setUnit(Tstamp.Unit) to make Ant's
  166. * Introspection mechanism do the work and also to
  167. * encapsulate operations on the unit in its own
  168. * class.
  169. */
  170. public void setUnit(String unit) {
  171. log("DEPRECATED - The setUnit(String) method has been deprecated."
  172. + " Use setUnit(Tstamp.Unit) instead.");
  173. Unit u = new Unit();
  174. u.setValue(unit);
  175. field = u.getCalendarField();
  176. }
  177. public void setUnit(Unit unit) {
  178. field = unit.getCalendarField();
  179. }
  180. public void execute(Project project, Date date, Location location) {
  181. if (propertyName == null) {
  182. throw new BuildException("property attribute must be provided",
  183. location);
  184. }
  185. if (pattern == null) {
  186. throw new BuildException("pattern attribute must be provided",
  187. location);
  188. }
  189. SimpleDateFormat sdf;
  190. if (language == null) {
  191. sdf = new SimpleDateFormat(pattern);
  192. } else if (variant == null) {
  193. sdf = new SimpleDateFormat(pattern,
  194. new Locale(language, country));
  195. } else {
  196. sdf = new SimpleDateFormat(pattern,
  197. new Locale(language, country,
  198. variant));
  199. }
  200. if (offset != 0) {
  201. Calendar calendar = Calendar.getInstance();
  202. calendar.setTime(date);
  203. calendar.add(field, offset);
  204. date = calendar.getTime();
  205. }
  206. if (timeZone != null){
  207. sdf.setTimeZone(timeZone);
  208. }
  209. project.setNewProperty(propertyName, sdf.format(date));
  210. }
  211. }
  212. public static class Unit extends EnumeratedAttribute {
  213. private static final String MILLISECOND = "millisecond";
  214. private static final String SECOND = "second";
  215. private static final String MINUTE = "minute";
  216. private static final String HOUR = "hour";
  217. private static final String DAY = "day";
  218. private static final String WEEK = "week";
  219. private static final String MONTH = "month";
  220. private static final String YEAR = "year";
  221. private static final String[] units = {
  222. MILLISECOND,
  223. SECOND,
  224. MINUTE,
  225. HOUR,
  226. DAY,
  227. WEEK,
  228. MONTH,
  229. YEAR
  230. };
  231. private Hashtable calendarFields = new Hashtable();
  232. public Unit() {
  233. calendarFields.put(MILLISECOND,
  234. new Integer(Calendar.MILLISECOND));
  235. calendarFields.put(SECOND, new Integer(Calendar.SECOND));
  236. calendarFields.put(MINUTE, new Integer(Calendar.MINUTE));
  237. calendarFields.put(HOUR, new Integer(Calendar.HOUR_OF_DAY));
  238. calendarFields.put(DAY, new Integer(Calendar.DATE));
  239. calendarFields.put(WEEK, new Integer(Calendar.WEEK_OF_YEAR));
  240. calendarFields.put(MONTH, new Integer(Calendar.MONTH));
  241. calendarFields.put(YEAR, new Integer(Calendar.YEAR));
  242. }
  243. public int getCalendarField() {
  244. String key = getValue().toLowerCase();
  245. Integer i = (Integer) calendarFields.get(key);
  246. return i.intValue();
  247. }
  248. public String[] getValues() {
  249. return units;
  250. }
  251. }
  252. }