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