Coverage Report - org.simpleframework.xml.transform.GregorianCalendarTransform
 
Classes in this File Line Coverage Branch Coverage Complexity
GregorianCalendarTransform
92%
11/12
100%
1/1
0
 
 1  
 /*
 2  
  * GregorialCalendarTransform.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.util.GregorianCalendar;
 24  
 import java.util.Date;
 25  
 
 26  
 /**
 27  
  * The <code>DateTransform</code> is used to transform calendar
 28  
  * values to and from string representations, which will be inserted
 29  
  * in the generated XML document as the value place holder. The
 30  
  * value must be readable and writable in the same format. Fields
 31  
  * and methods annotated with the XML attribute annotation will use
 32  
  * this to persist and retrieve the value to and from the XML source.
 33  
  * <pre>
 34  
  * 
 35  
  *    &#64;Attribute
 36  
  *    private GregorianCalendar date;
 37  
  *    
 38  
  * </pre>
 39  
  * As well as the XML attribute values using transforms, fields and
 40  
  * methods annotated with the XML element annotation will use this.
 41  
  * Aside from the obvious difference, the element annotation has an
 42  
  * advantage over the attribute annotation in that it can maintain
 43  
  * any references using the <code>CycleStrategy</code> object. 
 44  
  * 
 45  
  * @author Niall Gallagher
 46  
  */
 47  0
 class GregorianCalendarTransform implements Transform<GregorianCalendar> {
 48  
    
 49  
    /**
 50  
     * This is the date transform used to parse and format dates.
 51  
     */  
 52  
    private DateTransform transform;
 53  
    
 54  
    /**
 55  
     * Constructor for the <code>GregorianCalendarTransform</code> 
 56  
     * object. This is used to create a transform using a default 
 57  
     * date format pattern. The format chosen for the default date 
 58  
     * uses <code>2007-05-02 12:22:10.000 GMT</code> like dates.
 59  
     */
 60  
    public GregorianCalendarTransform() throws Exception {
 61  1
       this(Date.class);
 62  1
    }
 63  
    
 64  
    /**
 65  
     * Constructor for the <code>GregorianCalendarTransform</code> 
 66  
     * object. This is used to create a transform using a default 
 67  
     * date format pattern. The format should typically contain 
 68  
     * enough information to create the date using a different 
 69  
     * locale or time zone between read and write operations.
 70  
     * 
 71  
     * @param format this is the date format that is to be used
 72  
     */
 73  1
    public GregorianCalendarTransform(Class type) throws Exception {
 74  1
       this.transform = new DateTransform(type);      
 75  1
    }
 76  
    
 77  
    /**
 78  
     * This method is used to convert the string value given to an
 79  
     * appropriate representation. This is used when an object is
 80  
     * being deserialized from the XML document and the value for
 81  
     * the string representation is required.
 82  
     * 
 83  
     * @param date the string representation of the date value 
 84  
     * 
 85  
     * @return this returns an appropriate instanced to be used
 86  
     */  
 87  
    public GregorianCalendar read(String date) throws Exception {
 88  1
       return read(transform.read(date));      
 89  
    }
 90  
    
 91  
    /**
 92  
     * This method is used to convert the string value given to an
 93  
     * appropriate representation. This is used when an object is
 94  
     * being deserialized from the XML document and the value for
 95  
     * the string representation is required.
 96  
     * 
 97  
     * @param date the string representation of the date value 
 98  
     * 
 99  
     * @return this returns an appropriate instanced to be used
 100  
     */  
 101  
    private GregorianCalendar read(Date date) throws Exception {
 102  1
       GregorianCalendar calendar = new GregorianCalendar();
 103  
       
 104  1
       if(date != null) {
 105  1
          calendar.setTime(date);
 106  
       }
 107  1
       return calendar;
 108  
    }
 109  
    
 110  
    /**
 111  
     * This method is used to convert the provided value into an XML
 112  
     * usable format. This is used in the serialization process when
 113  
     * there is a need to convert a field value in to a string so 
 114  
     * that that value can be written as a valid XML entity.
 115  
     * 
 116  
     * @param date this is the value to be converted to a string
 117  
     * 
 118  
     * @return this is the string representation of the given date
 119  
     */
 120  
    public String write(GregorianCalendar date) throws Exception {
 121  1
       return transform.write(date.getTime());
 122  
    }
 123  
 }