Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 368   Methods: 16
NCLOC: 179   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
Tstamp.java 40.9% 44.3% 56.2% 45.4%
 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 "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   
 
 55   
 package org.apache.tools.ant.taskdefs;
 56   
 
 57   
 import java.text.SimpleDateFormat;
 58   
 import java.util.Calendar;
 59   
 import java.util.Date;
 60   
 import java.util.Enumeration;
 61   
 import java.util.Hashtable;
 62   
 import java.util.Locale;
 63   
 import java.util.NoSuchElementException;
 64   
 import java.util.StringTokenizer;
 65   
 import java.util.TimeZone;
 66   
 import java.util.Vector;
 67   
 import org.apache.tools.ant.BuildException;
 68   
 import org.apache.tools.ant.Location;
 69   
 import org.apache.tools.ant.Project;
 70   
 import org.apache.tools.ant.Task;
 71   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 72   
 
 73   
 /**
 74   
  * Sets properties to the current time, or offsets from the current time.
 75   
  * The default properties are TSTAMP, DSTAMP and TODAY;
 76   
  *
 77   
  * @author costin@dnt.ro
 78   
  * @author stefano@apache.org
 79   
  * @author roxspring@yahoo.com
 80   
  * @author Conor MacNeill
 81   
  * @author Magesh Umasankar
 82   
  * @since Ant 1.1
 83   
  * @ant.task category="utility"
 84   
  */
 85   
 public class Tstamp extends Task {
 86   
 
 87   
     private Vector customFormats = new Vector();
 88   
     private String prefix = "";
 89   
 
 90   
     /**
 91   
      * Set a prefix for the properties. If the prefix does not end with a "."
 92   
      * one is automatically added
 93   
      * @since Ant 1.5
 94   
      */
 95  3
     public void setPrefix(String prefix) {
 96  3
         this.prefix = prefix;
 97  3
         if (!this.prefix.endsWith(".")) {
 98  3
             this.prefix += ".";
 99   
         }
 100   
     }
 101   
 
 102   
     /**
 103   
      * create the timestamps. Custom ones are done before
 104   
      * the standard ones, to get their retalation in early.
 105   
      * @throws BuildException
 106   
      */
 107  6
     public void execute() throws BuildException {
 108  6
         try {
 109  6
             Date d = new Date();
 110   
 
 111  6
             Enumeration i = customFormats.elements();
 112  6
             while (i.hasMoreElements()) {
 113  2
                 CustomFormat cts = (CustomFormat) i.nextElement();
 114  2
                 cts.execute(getProject(), d, getLocation());
 115   
             }
 116   
 
 117  6
             SimpleDateFormat dstamp = new SimpleDateFormat ("yyyyMMdd");
 118  6
             setProperty("DSTAMP", dstamp.format(d));
 119   
 
 120  6
             SimpleDateFormat tstamp = new SimpleDateFormat ("HHmm");
 121  6
             setProperty("TSTAMP", tstamp.format(d));
 122   
 
 123  6
             SimpleDateFormat today
 124   
                 = new SimpleDateFormat ("MMMM d yyyy", Locale.US);
 125  6
             setProperty("TODAY", today.format(d));
 126   
 
 127   
         } catch (Exception e) {
 128  0
             throw new BuildException(e);
 129   
         }
 130   
     }
 131   
 
 132   
     /**
 133   
      * create a custom format with the the current prefix.
 134   
      * @return a ready to fill-in format
 135   
      */
 136  4
     public CustomFormat createFormat() {
 137  4
         CustomFormat cts = new CustomFormat();
 138  4
         customFormats.addElement(cts);
 139  4
         return cts;
 140   
     }
 141   
 
 142   
     /**
 143   
      * helper that encapsulates prefix logic and property setting
 144   
      * policy (i.e. we use setNewProperty instead of setProperty).
 145   
      */
 146  22
     private void setProperty(String name, String value) {
 147  22
         getProject().setNewProperty(prefix + name, value);
 148   
     }
 149   
 
 150   
     /**
 151   
      * This nested element that allows a property to be set
 152   
      * to the current date and time in a given format.
 153   
      * The date/time patterns are as defined in the
 154   
      * Java SimpleDateFormat class.
 155   
      * The format element also allows offsets to be applied to
 156   
      * the time to generate different time values.
 157   
      * @todo consider refactoring out into a re-usable element.
 158   
      */
 159   
     public class CustomFormat {
 160   
         private TimeZone timeZone;
 161   
         private String propertyName;
 162   
         private String pattern;
 163   
         private String language;
 164   
         private String country;
 165   
         private String variant;
 166   
         private int offset = 0;
 167   
         private int field = Calendar.DATE;
 168   
 
 169   
         /**
 170   
          * Create a format
 171   
          */
 172  4
         public CustomFormat() {
 173   
         }
 174   
 
 175   
         /**
 176   
          *  The property to receive the date/time string in the given pattern
 177   
          * @param propertyName
 178   
          */
 179  4
         public void setProperty(String propertyName) {
 180  4
             this.propertyName = propertyName;
 181   
         }
 182   
 
 183   
         /**
 184   
          * The date/time pattern to be used. The values are as
 185   
          * defined by the Java SimpleDateFormat class.
 186   
          * @param pattern
 187   
          * @see java.text.SimpleDateFormat
 188   
          */
 189  4
         public void setPattern(String pattern) {
 190  4
             this.pattern = pattern;
 191   
         }
 192   
 
 193   
         /**
 194   
          * The locale used to create date/time string.
 195   
          * The general form is "language, country, variant" but
 196   
          * either variant or variant and country may be omitted.
 197   
          * For more information please refer to documentation
 198   
          * for the java.util.Locale  class.
 199   
          * @param locale
 200   
          * @see java.util.Locale
 201   
          */
 202  0
         public void setLocale(String locale) {
 203  0
             StringTokenizer st = new StringTokenizer(locale, " \t\n\r\f,");
 204  0
             try {
 205  0
                 language = st.nextToken();
 206  0
                 if (st.hasMoreElements()) {
 207  0
                     country = st.nextToken();
 208  0
                     if (st.hasMoreElements()) {
 209  0
                         variant = st.nextToken();
 210  0
                         if (st.hasMoreElements()) {
 211  0
                             throw new BuildException("bad locale format",
 212   
                                                       getLocation());
 213   
                         }
 214   
                     }
 215   
                 } else {
 216  0
                     country = "";
 217   
                 }
 218   
             } catch (NoSuchElementException e) {
 219  0
                 throw new BuildException("bad locale format", e,
 220   
                                          getLocation());
 221   
             }
 222   
         }
 223   
 
 224   
         /**
 225   
          * The timezone to use for displaying time.
 226   
          * The values are as defined by the Java TimeZone class.
 227   
          * @param id
 228   
          * @see java.util.TimeZone
 229   
          */
 230  3
         public void setTimezone(String id){
 231  3
             timeZone = TimeZone.getTimeZone(id);
 232   
         }
 233   
 
 234   
         /**
 235   
          * The numeric offset to the current time.
 236   
          * @param offset
 237   
          */
 238  0
         public void setOffset(int offset) {
 239  0
             this.offset = offset;
 240   
         }
 241   
 
 242   
         /**
 243   
          * @deprecated setUnit(String) is deprecated and is replaced with
 244   
          *             setUnit(Tstamp.Unit) to make Ant's
 245   
          *             Introspection mechanism do the work and also to
 246   
          *             encapsulate operations on the unit in its own
 247   
          *             class.
 248   
          */
 249  0
         public void setUnit(String unit) {
 250  0
             log("DEPRECATED - The setUnit(String) method has been deprecated."
 251   
                 + " Use setUnit(Tstamp.Unit) instead.");
 252  0
             Unit u = new Unit();
 253  0
             u.setValue(unit);
 254  0
             field = u.getCalendarField();
 255   
         }
 256   
 
 257   
         /**
 258   
          * The unit of the offset to be applied to the current time.
 259   
          * Valid Values are
 260   
          * <ul>
 261   
          *    <li>millisecond</li>
 262   
          *    <li>second</li>
 263   
          *    <li>minute</li>
 264   
          *    <li>hour</li>
 265   
          *    <li>day</li>
 266   
          *    <li>week</li>
 267   
          *    <li>month</li>
 268   
          *    <li>year</li>
 269   
          * </ul>
 270   
          * The default unit is day.
 271   
          * @param unit
 272   
          */
 273  0
         public void setUnit(Unit unit) {
 274  0
             field = unit.getCalendarField();
 275   
         }
 276   
 
 277   
         /**
 278   
          * validate parameter and execute the format
 279   
          * @param project project to set property in
 280   
          * @param date date to use as a starting point
 281   
          * @param location line in file (for errors)
 282   
          */
 283  4
         public void execute(Project project, Date date, Location location) {
 284  4
             if (propertyName == null) {
 285  0
                 throw new BuildException("property attribute must be provided",
 286   
                                          location);
 287   
             }
 288   
 
 289  4
             if (pattern == null) {
 290  0
                 throw new BuildException("pattern attribute must be provided",
 291   
                                          location);
 292   
             }
 293   
 
 294  4
             SimpleDateFormat sdf;
 295  4
             if (language == null) {
 296  4
                 sdf = new SimpleDateFormat(pattern);
 297  0
             } else if (variant == null) {
 298  0
                 sdf = new SimpleDateFormat(pattern,
 299   
                                            new Locale(language, country));
 300   
             } else {
 301  0
                 sdf = new SimpleDateFormat(pattern,
 302   
                                            new Locale(language, country,
 303   
                                                       variant));
 304   
             }
 305  4
             if (offset != 0) {
 306  0
                 Calendar calendar = Calendar.getInstance();
 307  0
                 calendar.setTime(date);
 308  0
                 calendar.add(field, offset);
 309  0
                 date = calendar.getTime();
 310   
             }
 311  4
             if (timeZone != null){
 312  3
                 sdf.setTimeZone(timeZone);
 313   
             }
 314  4
             Tstamp.this.setProperty(propertyName, sdf.format(date));
 315   
         }
 316   
     }
 317   
 
 318   
     /**
 319   
      * set of valid units to use for time offsets.
 320   
      */
 321   
     public static class Unit extends EnumeratedAttribute {
 322   
 
 323   
         private static final String MILLISECOND = "millisecond";
 324   
         private static final String SECOND = "second";
 325   
         private static final String MINUTE = "minute";
 326   
         private static final String HOUR = "hour";
 327   
         private static final String DAY = "day";
 328   
         private static final String WEEK = "week";
 329   
         private static final String MONTH = "month";
 330   
         private static final String YEAR = "year";
 331   
 
 332   
         private static final String[] units = {
 333   
                                                 MILLISECOND,
 334   
                                                 SECOND,
 335   
                                                 MINUTE,
 336   
                                                 HOUR,
 337   
                                                 DAY,
 338   
                                                 WEEK,
 339   
                                                 MONTH,
 340   
                                                 YEAR
 341   
                                               };
 342   
 
 343   
         private Hashtable calendarFields = new Hashtable();
 344   
 
 345  0
         public Unit() {
 346  0
             calendarFields.put(MILLISECOND,
 347   
                                new Integer(Calendar.MILLISECOND));
 348  0
             calendarFields.put(SECOND, new Integer(Calendar.SECOND));
 349  0
             calendarFields.put(MINUTE, new Integer(Calendar.MINUTE));
 350  0
             calendarFields.put(HOUR, new Integer(Calendar.HOUR_OF_DAY));
 351  0
             calendarFields.put(DAY, new Integer(Calendar.DATE));
 352  0
             calendarFields.put(WEEK, new Integer(Calendar.WEEK_OF_YEAR));
 353  0
             calendarFields.put(MONTH, new Integer(Calendar.MONTH));
 354  0
             calendarFields.put(YEAR, new Integer(Calendar.YEAR));
 355   
         }
 356   
 
 357  0
         public int getCalendarField() {
 358  0
             String key = getValue().toLowerCase();
 359  0
             Integer i = (Integer) calendarFields.get(key);
 360  0
             return i.intValue();
 361   
         }
 362   
 
 363  0
         public String[] getValues() {
 364  0
             return units;
 365   
         }
 366   
     }
 367   
 }
 368