Coverage Report - org.simpleframework.xml.transform.DateType
 
Classes in this File Line Coverage Branch Coverage Complexity
DateType
77%
17/22
33%
1/3
0
DateType$DateFormat
100%
5/5
N/A
0
 
 1  
 /*
 2  
  * DateType.java May 2007
 3  
  *
 4  
  * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * This library is free software; you can redistribute it and/or
 7  
  * modify it under the terms of the GNU Lesser General Public
 8  
  * License as published by the Free Software Foundation.
 9  
  *
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  
  * GNU Lesser General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU Lesser General 
 16  
  * Public License along with this library; if not, write to the 
 17  
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 18  
  * Boston, MA  02111-1307  USA
 19  
  */
 20  
 
 21  
 package org.simpleframework.xml.transform;
 22  
 
 23  
 import java.text.SimpleDateFormat;
 24  
 import java.util.Date;
 25  
 
 26  
 /**
 27  
  * The <code>DateType</code> enumeration provides a set of known date
 28  
  * formats supported by the date transformer. This allows the XML
 29  
  * representation of a date to come in several formats, from most 
 30  
  * accurate to least. Enumerating the dates ensures that resolution
 31  
  * of the format is fast by enabling inspection of the date string. 
 32  
  * 
 33  
  * @author Niall Gallagher
 34  
  */
 35  4
 enum DateType {
 36  
    
 37  
    /**
 38  
     * This is the default date format used by the date transform.
 39  
     */
 40  4
    FULL("yyyy-MM-dd HH:mm:ss.S z"),
 41  
    
 42  
    /**
 43  
     * This is the date type without millisecond resolution.
 44  
     */
 45  4
    LONG("yyyy-MM-dd HH:mm:ss z"),
 46  
    
 47  
    /**
 48  
     * This date type enables only the specific date to be used.
 49  
     */
 50  4
    NORMAL("yyyy-MM-dd z"),
 51  
    
 52  
    /**
 53  
     * This is the shortest format that relies on the date locale.
 54  
     */
 55  4
    SHORT("yyyy-MM-dd");
 56  
 
 57  
    /**
 58  
     * This is the date formatter that is used to parse the date.
 59  
     */
 60  
    private DateFormat format;
 61  
 
 62  
    /**
 63  
     * Constructor for the <code>DateType</code> enumeration. This
 64  
     * will accept a simple date format pattern, which is used to
 65  
     * parse an input string and convert it to a usable date.
 66  
     * 
 67  
     * @param format this is the format to use to parse the date
 68  
     */
 69  16
    private DateType(String format) {
 70  16
       this.format = new DateFormat(format);         
 71  16
    }
 72  
 
 73  
    /**
 74  
     * Acquires the date format from the date type. This is then 
 75  
     * used to parse the date string and convert it to a usable
 76  
     * date. The format returned is synchronized for safety.
 77  
     * 
 78  
     * @return this returns the date format to be used
 79  
     */
 80  
    private DateFormat getFormat() {
 81  35
       return format;         
 82  
    }
 83  
    
 84  
    /**
 85  
     * This is used to convert the date to a string value. The 
 86  
     * string value can then be embedded in to the generated XML in
 87  
     * such a way that it can be recovered as a <code>Date</code>
 88  
     * when the value is transformed by the date transform.
 89  
     * 
 90  
     * @param date this is the date that is converted to a string
 91  
     * 
 92  
     * @return this returns the string to represent the date
 93  
     */
 94  
    public static String getText(Date date) throws Exception {
 95  24
       DateFormat format = FULL.getFormat();
 96  
       
 97  24
       return format.getText(date);
 98  
    }
 99  
    
 100  
    /**
 101  
     * This is used to convert the string to a date value. The 
 102  
     * date value can then be recovered from the generated XML by
 103  
     * parsing the text with one of the known date formats. This
 104  
     * allows bidirectional transformation of dates to strings.
 105  
     * 
 106  
     * @param text this is the date that is converted to a date
 107  
     * 
 108  
     * @return this returns the date parsed from the string value
 109  
     */
 110  
    public static Date getDate(String text) throws Exception {
 111  11
       DateType type = getType(text);
 112  11
       DateFormat format = type.getFormat();
 113  
       
 114  11
       return format.getDate(text);
 115  
    }
 116  
 
 117  
    /**
 118  
     * This is used to acquire a date type using the specified text
 119  
     * as input. This will perform some checks on the raw string to
 120  
     * match it to the appropriate date type. Resolving the date type
 121  
     * in this way ensures that only one date type needs to be used.
 122  
     * 
 123  
     * @param text this is the text to be matched with a date type
 124  
     * 
 125  
     * @return the most appropriate date type for the given string
 126  
     */
 127  
    public static DateType getType(String text) {
 128  11
       int length = text.length();
 129  
 
 130  11
       if(length > 23) {
 131  11
          return FULL;
 132  
       }
 133  0
       if(length > 20) {
 134  0
          return LONG;
 135  
       }
 136  0
       if(length > 11) {
 137  0
          return NORMAL;
 138  
       }
 139  0
       return SHORT;
 140  
    }
 141  
    
 142  
    /**
 143  
     * The <code>DateFormat</code> provides a synchronized means for
 144  
     * using the simple date format object. It ensures that should 
 145  
     * there be many threads trying to gain access to the formatter 
 146  
     * that they will not collide causing a race condition.
 147  
     * 
 148  
     * @author Niall Gallagher
 149  
     */
 150  
    private static class DateFormat {
 151  
       
 152  
       /**
 153  
        * This is the simple date format used to parse the string.
 154  
        */
 155  
       private SimpleDateFormat format;
 156  
       
 157  
       /**
 158  
        * Constructor for the <code>DateFormat</code> object. This will
 159  
        * wrap a simple date format, providing access to the conversion
 160  
        * functions which allow date to string and string to date.
 161  
        * 
 162  
        * @param format this is the pattern to use for the date type
 163  
        */
 164  16
       public DateFormat(String format) {         
 165  16
          this.format = new SimpleDateFormat(format);
 166  16
       }
 167  
       
 168  
       /**
 169  
        * This is used to provide a transformation from a date to a string.
 170  
        * It ensures that there is a bidirectional transformation process
 171  
        * which allows dates to be serialized and deserialized with XML.
 172  
        * 
 173  
        * @param date this is the date to be converted to a string value
 174  
        * 
 175  
        * @return returns the string that has be converted from a date
 176  
        */
 177  
       public synchronized String getText(Date date) throws Exception {
 178  24
          return format.format(date);
 179  
       }
 180  
       
 181  
       /**
 182  
        * This is used to provide a transformation from a string to a date.
 183  
        * It ensures that there is a bidirectional transformation process
 184  
        * which allows dates to be serialized and deserialized with XML.
 185  
        * 
 186  
        * @param text this is the string to be converted to a date value
 187  
        * 
 188  
        * @return returns the date that has be converted from a string
 189  
        */
 190  
       public synchronized Date getDate(String text) throws Exception {
 191  11
          return format.parse(text);
 192  
       }
 193  
    }
 194  
 }