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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 = new SimpleDateFormat ("MMMM d yyyy", Locale.US);
  101. project.setNewProperty(prefix + "TODAY", today.format(d));
  102. Enumeration i = customFormats.elements();
  103. while (i.hasMoreElements()) {
  104. CustomFormat cts = (CustomFormat) i.nextElement();
  105. cts.execute(project, d, location);
  106. }
  107. } catch (Exception e) {
  108. throw new BuildException(e);
  109. }
  110. }
  111. public CustomFormat createFormat() {
  112. CustomFormat cts = new CustomFormat(prefix);
  113. customFormats.addElement(cts);
  114. return cts;
  115. }
  116. public class CustomFormat {
  117. private TimeZone timeZone;
  118. private String propertyName;
  119. private String pattern;
  120. private String language;
  121. private String country;
  122. private String variant;
  123. private int offset = 0;
  124. private int field = Calendar.DATE;
  125. private String prefix = "";
  126. public CustomFormat(String prefix) {
  127. this.prefix = prefix;
  128. }
  129. public void setProperty(String propertyName) {
  130. this.propertyName = prefix + propertyName;
  131. }
  132. public void setPattern(String pattern) {
  133. this.pattern = pattern;
  134. }
  135. public void setLocale(String locale) {
  136. StringTokenizer st = new StringTokenizer(locale, " \t\n\r\f,");
  137. try {
  138. language = st.nextToken();
  139. if (st.hasMoreElements()) {
  140. country = st.nextToken();
  141. if (st.hasMoreElements()) {
  142. variant = st.nextToken();
  143. if (st.hasMoreElements()) {
  144. throw new BuildException("bad locale format",
  145. getLocation());
  146. }
  147. }
  148. } else {
  149. country = "";
  150. }
  151. } catch (NoSuchElementException e) {
  152. throw new BuildException("bad locale format", e,
  153. getLocation());
  154. }
  155. }
  156. public void setTimezone(String id){
  157. timeZone = TimeZone.getTimeZone(id);
  158. }
  159. public void setOffset(int offset) {
  160. this.offset = offset;
  161. }
  162. /**
  163. * @deprecated setUnit(String) is deprecated and is replaced with
  164. * setUnit(Tstamp.Unit) to make Ant's
  165. * Introspection mechanism do the work and also to
  166. * encapsulate operations on the unit in its own
  167. * class.
  168. */
  169. public void setUnit(String unit) {
  170. log("DEPRECATED - The setUnit(String) method has been deprecated."
  171. + " Use setUnit(Tstamp.Unit) instead.");
  172. Unit u = new Unit();
  173. u.setValue(unit);
  174. field = u.getCalendarField();
  175. }
  176. public void setUnit(Unit unit) {
  177. field = unit.getCalendarField();
  178. }
  179. public void execute(Project project, Date date, Location location) {
  180. if (propertyName == null) {
  181. throw new BuildException("property attribute must be provided",
  182. location);
  183. }
  184. if (pattern == null) {
  185. throw new BuildException("pattern attribute must be provided",
  186. location);
  187. }
  188. SimpleDateFormat sdf;
  189. if (language == null) {
  190. sdf = new SimpleDateFormat(pattern);
  191. } else if (variant == null) {
  192. sdf = new SimpleDateFormat(pattern,
  193. new Locale(language, country));
  194. } else {
  195. sdf = new SimpleDateFormat(pattern,
  196. new Locale(language, country,
  197. variant));
  198. }
  199. if (offset != 0) {
  200. Calendar calendar = Calendar.getInstance();
  201. calendar.setTime(date);
  202. calendar.add(field, offset);
  203. date = calendar.getTime();
  204. }
  205. if (timeZone != null){
  206. sdf.setTimeZone(timeZone);
  207. }
  208. project.setNewProperty(propertyName, sdf.format(date));
  209. }
  210. }
  211. public static class Unit extends EnumeratedAttribute {
  212. private static final String MILLISECOND = "millisecond";
  213. private static final String SECOND = "second";
  214. private static final String MINUTE = "minute";
  215. private static final String HOUR = "hour";
  216. private static final String DAY = "day";
  217. private static final String WEEK = "week";
  218. private static final String MONTH = "month";
  219. private static final String YEAR = "year";
  220. private static final String[] units = {
  221. MILLISECOND,
  222. SECOND,
  223. MINUTE,
  224. HOUR,
  225. DAY,
  226. WEEK,
  227. MONTH,
  228. YEAR
  229. };
  230. private Hashtable calendarFields = new Hashtable();
  231. public Unit() {
  232. calendarFields.put(MILLISECOND,
  233. new Integer(Calendar.MILLISECOND));
  234. calendarFields.put(SECOND, new Integer(Calendar.SECOND));
  235. calendarFields.put(MINUTE, new Integer(Calendar.MINUTE));
  236. calendarFields.put(HOUR, new Integer(Calendar.HOUR_OF_DAY));
  237. calendarFields.put(DAY, new Integer(Calendar.DATE));
  238. calendarFields.put(WEEK, new Integer(Calendar.WEEK_OF_YEAR));
  239. calendarFields.put(MONTH, new Integer(Calendar.MONTH));
  240. calendarFields.put(YEAR, new Integer(Calendar.YEAR));
  241. }
  242. public int getCalendarField() {
  243. String key = getValue().toLowerCase();
  244. Integer i = (Integer) calendarFields.get(key);
  245. return i.intValue();
  246. }
  247. public String[] getValues() {
  248. return units;
  249. }
  250. }
  251. }