Coverage Report - org.simpleframework.xml.transform.DateTransform
 
Classes in this File Line Coverage Branch Coverage Complexity
DateTransform
100%
8/8
N/A
1
 
 1  
 /*
 2  
  * DateTransform.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.Date;
 22  
 
 23  
 /**
 24  
  * The <code>DateTransform</code> object is used to transform date
 25  
  * values to and from string representations, which will be inserted
 26  
  * in the generated XML document as the value place holder. The
 27  
  * value must be readable and writable in the same format. Fields
 28  
  * and methods annotated with the XML attribute annotation will use
 29  
  * this to persist and retrieve the value to and from the XML source.
 30  
  * <pre>
 31  
  * 
 32  
  *    &#64;Attribute
 33  
  *    private Date date;
 34  
  *    
 35  
  * </pre>
 36  
  * As well as the XML attribute values using transforms, fields and
 37  
  * methods annotated with the XML element annotation will use this.
 38  
  * Aside from the obvious difference, the element annotation has an
 39  
  * advantage over the attribute annotation in that it can maintain
 40  
  * any references using the <code>CycleStrategy</code> object. 
 41  
  * 
 42  
  * @author Niall Gallagher
 43  
  */
 44  4105
 class DateTransform<T extends Date> implements Transform<T> {
 45  
    
 46  
    /**
 47  
     * This represents the constructor used for creating the date.
 48  
     */
 49  
    private final DateFactory<T> factory;
 50  
 
 51  
    /**
 52  
     * Constructor for the <code>DateTransform</code> object. This is
 53  
     * used to create a transform using a specified date format. The
 54  
     * format should typically contain enough information to create
 55  
     * the date using a different locale or time zone between read and
 56  
     * write operations.
 57  
     * 
 58  
     * @param type this is the type of date to be created by this
 59  
     */
 60  29
    public DateTransform(Class<T> type) throws Exception {
 61  29
       this.factory = new DateFactory<T>(type);
 62  29
    }
 63  
    
 64  
    /**
 65  
     * This method is used to convert the string value given to an
 66  
     * appropriate representation. This is used when an object is
 67  
     * being deserialized from the XML document and the value for
 68  
     * the string representation is required.
 69  
     * 
 70  
     * @param text the string representation of the date value 
 71  
     * 
 72  
     * @return this returns an appropriate instanced to be used
 73  
     */
 74  
    public synchronized T read(String text) throws Exception {      
 75  2040
       Date date = DateType.getDate(text);
 76  2040
       Long time = date.getTime();
 77  
       
 78  2040
       return factory.getInstance(time);
 79  
    }
 80  
    
 81  
    /**
 82  
     * This method is used to convert the provided value into an XML
 83  
     * usable format. This is used in the serialization process when
 84  
     * there is a need to convert a field value in to a string so 
 85  
     * that that value can be written as a valid XML entity.
 86  
     * 
 87  
     * @param date this is the value to be converted to a string
 88  
     * 
 89  
     * @return this is the string representation of the given date
 90  
     */
 91  
    public synchronized String write(T date) throws Exception {
 92  2084
       return DateType.getText(date);     
 93  
    }
 94  
 }