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