Coverage Report - org.dozer.converters.XMLGregorianCalendarConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLGregorianCalendarConverter
81%
27/33
92%
13/14
5.667
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.dozer.converters;
 17  
 
 18  
 import org.apache.commons.beanutils.Converter;
 19  
 import org.dozer.MappingException;
 20  
 
 21  
 import javax.xml.datatype.DatatypeConfigurationException;
 22  
 import javax.xml.datatype.DatatypeFactory;
 23  
 import javax.xml.datatype.XMLGregorianCalendar;
 24  
 import java.text.DateFormat;
 25  
 import java.text.ParseException;
 26  
 import java.util.Calendar;
 27  
 import java.util.Date;
 28  
 import java.util.GregorianCalendar;
 29  
 
 30  
 /**
 31  
  * Internal class for converting Supported Data Types --> XMLGregorianCalendar.
 32  
  * <p>
 33  
  * Supported source data types include
 34  
  * <ul>
 35  
  * <li>java.util.Date</li>
 36  
  * <li>java.sql.Date</li>
 37  
  * <li>java.sql.Time</li>
 38  
  * <li>java.sql.Timestamp</li>
 39  
  * <li>java.util.Calendar</li>
 40  
  * <li>java.lang.String</li>
 41  
  * <li>any objects that return a number of milliseconds applicable to java.lang.Long format in their toString() form</li>
 42  
  * </ul>
 43  
  * </p>
 44  
  * <p/>
 45  
  * Only intended for internal use.
 46  
  * <p/>
 47  
  *
 48  
  * @author pnyheim
 49  
  * @author dmitry.buzdin
 50  
  */
 51  
 public class XMLGregorianCalendarConverter implements Converter {
 52  
 
 53  
   private DateFormat dateFormat;
 54  
 
 55  19
   public XMLGregorianCalendarConverter(DateFormat dateFormat) {
 56  19
     this.dateFormat = dateFormat;
 57  19
   }
 58  
 
 59  
   /**
 60  
    * Cache the DatatypeFactory because newInstance is very expensive.
 61  
    */
 62  
   private static DatatypeFactory dataTypeFactory;
 63  
 
 64  
   /**
 65  
    * Returns a new instance of DatatypeFactory, or the cached one if previously created.
 66  
    *
 67  
    * @return instance of DatatypeFactory
 68  
    */
 69  
   private static DatatypeFactory dataTypeFactory() {
 70  18
     if (dataTypeFactory == null) {
 71  
       try {
 72  1
         dataTypeFactory = DatatypeFactory.newInstance();
 73  0
       } catch (DatatypeConfigurationException e) {
 74  0
         throw new MappingException(e);
 75  1
       }
 76  
     }
 77  18
     return dataTypeFactory;
 78  
   }
 79  
 
 80  
   /**
 81  
    * {@inheritDoc}
 82  
    */
 83  
   public Object convert(Class destClass, Object srcObj) {
 84  19
     Class sourceClass = srcObj.getClass();
 85  19
     Calendar result = new GregorianCalendar();
 86  
 
 87  19
     if (java.util.Date.class.isAssignableFrom(sourceClass)) {
 88  
       // Date --> XMLGregorianCalendar
 89  7
       result.setTime((java.util.Date) srcObj);
 90  12
     } else if (Calendar.class.isAssignableFrom(sourceClass)) {
 91  
       // Calendar --> XMLGregorianCalendar
 92  3
       Calendar c = (Calendar) srcObj;
 93  3
       result.setTime(c.getTime());
 94  3
       result.setTimeZone(c.getTimeZone());
 95  3
     } else if (XMLGregorianCalendar.class.isAssignableFrom(sourceClass)) {
 96  1
       result = ((XMLGregorianCalendar) srcObj).toGregorianCalendar();
 97  8
     } else if (dateFormat != null && String.class.isAssignableFrom(sourceClass)) {
 98  7
       if ("".equals(srcObj)) {
 99  1
         return null;
 100  
       }
 101  
       try {
 102  6
         long time = dateFormat.parse((String) srcObj).getTime();
 103  6
         result.setTime(new Date(time));
 104  0
       } catch (ParseException e) {
 105  0
         throw new ConversionException("Unable to parse source object using specified date format", e);
 106  6
       }
 107  
     } else {
 108  
       try {
 109  1
         long time = Long.parseLong(srcObj.toString());
 110  1
         result.setTime(new Date(time));
 111  0
       } catch (NumberFormatException e) {
 112  0
         throw new ConversionException("Unable to determine time in millis of source object", e);
 113  1
       }
 114  
     }
 115  
 
 116  18
     return dataTypeFactory().newXMLGregorianCalendar((GregorianCalendar) result);
 117  
   }
 118  
 }