Clover coverage report - brownies library - 1.0-beta-1
Coverage timestamp: 月 8 16 2004 17:14:42 GMT+09:00
file stats: LOC: 137   Methods: 9
NCLOC: 93   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
PropertySVBeanMapper.java 87.5% 86.8% 77.8% 85.5%
coverage coverage
 1   
 /*
 2   
  * brownies and its relative products are published under the terms
 3   
  * of the Apache Software License.
 4   
  * 
 5   
  * Created on 2004/05/20 17:32:09
 6   
  */
 7   
 package org.asyrinx.brownie.core.csv;
 8   
 
 9   
 import java.io.IOException;
 10   
 import java.io.InputStream;
 11   
 import java.lang.reflect.InvocationTargetException;
 12   
 import java.text.DateFormat;
 13   
 import java.text.ParseException;
 14   
 import java.util.Date;
 15   
 import java.util.HashMap;
 16   
 import java.util.Iterator;
 17   
 import java.util.List;
 18   
 import java.util.Map;
 19   
 import java.util.Properties;
 20   
 import java.util.Set;
 21   
 
 22   
 import org.apache.commons.beanutils.BeanUtils;
 23   
 import org.apache.commons.beanutils.PropertyUtils;
 24   
 import org.apache.commons.lang.exception.NestableRuntimeException;
 25   
 import org.asyrinx.brownie.core.io.sf.StreamFactory;
 26   
 import org.asyrinx.brownie.core.io.sf.StreamFactoryFacade;
 27   
 import org.asyrinx.brownie.core.lang.ClassUtils;
 28   
 import org.asyrinx.brownie.core.lang.NumberUtils;
 29   
 import org.asyrinx.brownie.core.lang.enum.ValuedEnum;
 30   
 
 31   
 /**
 32   
  * @author akima
 33   
  */
 34   
 public class PropertySVBeanMapper implements SVBeanMapper {
 35   
 
 36   
     /**
 37   
      *  
 38   
      */
 39  2
     public PropertySVBeanMapper(String mappedClassName, Properties properties) {
 40  2
         super();
 41  2
         this.properties = properties;
 42  2
         this.mappedClassName = mappedClassName;
 43   
     }
 44   
 
 45   
     /**
 46   
      *  
 47   
      */
 48  2
     public PropertySVBeanMapper(String mappedClassName, String propertyContext)
 49   
             throws IOException {
 50  2
         this(mappedClassName, loadProperties(propertyContext));
 51   
     }
 52   
 
 53  2
     private static Properties loadProperties(String propertyContext)
 54   
             throws IOException {
 55  2
         final StreamFactory streamFactory = StreamFactoryFacade.newFacade();
 56  2
         final InputStream inputStream = streamFactory.newInput(propertyContext);
 57  2
         final Properties properties = new Properties();
 58  2
         properties.load(inputStream);
 59  2
         return properties;
 60   
     }
 61   
 
 62   
     protected final Properties properties;
 63   
 
 64   
     protected final String mappedClassName;
 65   
 
 66   
     private DateFormat dateFormat = DateFormat.getDateInstance();
 67   
 
 68   
     /*
 69   
      * (non-Javadoc)
 70   
      * 
 71   
      * @see org.asyrinx.brownie.core.text.SVBeanMapper#loadBean(java.util.List)
 72   
      */
 73  3
     public Object loadBean(List values) {
 74  3
         final Object bean = getBean();
 75  3
         loadPropertyValues(values, bean);
 76  3
         return bean;
 77   
     }
 78   
 
 79  3
     protected void loadPropertyValues(List values, final Object bean) {
 80  3
         final Set keys = properties.keySet();
 81  3
         final Map valueMap = new HashMap();
 82  3
         try {
 83  3
             for (Iterator iter = keys.iterator(); iter.hasNext();) {
 84  18
                 final Object key = iter.next();
 85  18
                 final String propName = String.valueOf(properties.get(key));
 86  18
                 final int keyIndex = NumberUtils.toInt(key, -1);
 87  18
                 if ((keyIndex < 0) || (keyIndex > values.size() - 1))
 88  0
                     throw new NestableRuntimeException("不正なkeyです: " + key);
 89  18
                 Object value = values.get(keyIndex);
 90  18
                 value = loadPropertyObject(bean, propName, value);
 91  18
                 valueMap.put(propName, value);
 92   
             }
 93  3
             BeanUtils.populate(bean, valueMap);
 94   
         } catch (Exception e) {
 95  0
             throw new NestableRuntimeException("bean: '" + bean
 96   
                     + "', valueMap: " + valueMap, e);
 97   
         }
 98   
     }
 99   
 
 100  18
     protected Object loadPropertyObject(final Object bean,
 101   
             final String propName, Object value) throws IllegalAccessException,
 102   
             InvocationTargetException, NoSuchMethodException, ParseException,
 103   
             ClassNotFoundException {
 104  18
         final Class propType = PropertyUtils.getPropertyType(bean, propName);
 105  18
         if (Date.class.isAssignableFrom(propType)) {
 106  3
             value = dateFormat.parse(String.valueOf(value));
 107  15
         } else if (ValuedEnum.class.isAssignableFrom(propType)) {
 108   
             //ここでforNameしておかないとクラスがロードされないことがある
 109  3
             Class.forName(propType.getName());
 110  3
             value = ValuedEnum.getEnumByName(propType, String.valueOf(value));
 111   
         }
 112  18
         return value;
 113   
     }
 114   
 
 115  3
     protected Object getBean() {
 116  3
         try {
 117  3
             return ClassUtils.newObject(mappedClassName, null);
 118   
         } catch (InstantiationException e) {
 119  0
             throw new NestableRuntimeException(e);
 120   
         }
 121   
     }
 122   
 
 123   
     /**
 124   
      * @return
 125   
      */
 126  0
     public DateFormat getDateFormat() {
 127  0
         return dateFormat;
 128   
     }
 129   
 
 130   
     /**
 131   
      * @param format
 132   
      */
 133  0
     public void setDateFormat(DateFormat format) {
 134  0
         dateFormat = format;
 135   
     }
 136   
 
 137   
 }