Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 137   Methods: 5
NCLOC: 93   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CategoryBeanMapper.java 0% 0% 0% 0%
coverage
 1    /*
 2    * CategoryBeanMapper.java
 3    *
 4    * Created on 28. September 2004, 12:32
 5    */
 6   
 7    package org.jconfig.utils;
 8   
 9    import java.util.Properties;
 10    import java.util.HashMap;
 11    import java.beans.*;
 12    import java.lang.reflect.*;
 13    import org.jconfig.*;
 14    import org.jconfig.error.ErrorReporter;
 15    import org.jconfig.utils.editors.*;
 16    /**
 17    * This class is a little helper utility that will configure a simple java bean with
 18    * the properties inside a category. The set methods of the java bean will used to
 19    * configure the bean and the names of the set methods must match the names of the
 20    * properties in the category.
 21    *
 22    * @author Andreas Mecky andreasmecky@yahoo.de
 23    * @author Terry Dye terrydye@yahoo.com
 24    */
 25    public class CategoryBeanMapper {
 26   
 27    // a list of primitives that we will take care of
 28    private static HashMap primitives = new HashMap();
 29    static {
 30  0 primitives.put("int", Integer.class);
 31  0 primitives.put("boolean", Boolean.class);
 32  0 primitives.put("double", Double.class);
 33  0 primitives.put("float", Float.class);
 34  0 primitives.put("long", Long.class);
 35    }
 36   
 37    // the char is special and we need a TypeConverter here
 38    private static HashMap converters = new HashMap();
 39    static {
 40  0 converters.put("char", new CharConverter());
 41    }
 42   
 43    // noone should instantiate this one
 44  0 private CategoryBeanMapper() {
 45    }
 46   
 47    /**
 48    * This method will configure a java bean with the properties inside
 49    * the defined category of the defined configuration (usually loaded
 50    * from the config.xml file)
 51    *
 52    * @param obj the java bean that will be configured
 53    * @param categoryName the name of the category
 54    * @param configName the name of the configuration
 55    */
 56  0 public static void mapBean(Object obj,String categoryName,String configName) {
 57  0 Configuration cfg = ConfigurationManager.getConfiguration(configName);
 58  0 Properties props = cfg.getProperties(categoryName);
 59  0 mapBean(obj,props);
 60    }
 61   
 62    /**
 63    * This method will configure a java bean with the properties inside
 64    * the defined category of the default configuration (usually loaded
 65    * from the config.xml file)
 66    *
 67    * @param obj the java bean that will be configured
 68    * @param categoryName the name of the category
 69    */
 70  0 public static void mapBean(Object obj,String categoryName) {
 71  0 Configuration cfg = ConfigurationManager.getConfiguration();
 72  0 Properties props = cfg.getProperties(categoryName);
 73  0 mapBean(obj,props);
 74    }
 75   
 76  0 private static void mapBean(Object obj,Properties fieldMappings) {
 77  0 try {
 78  0 BeanInfo binfo = Introspector.getBeanInfo (obj.getClass());
 79  0 PropertyDescriptor[] pds = binfo.getPropertyDescriptors();
 80  0 for (int i = 0; i < pds.length; i++) {
 81  0 PropertyDescriptor pd = pds[i];
 82  0 String propName = pd.getName();
 83  0 String dbValue = (String)fieldMappings.get(propName);
 84  0 if (dbValue != null ) {
 85  0 Class typeClass;
 86  0 String typeName = pd.getPropertyType().getName();
 87  0 Object curObject = null;
 88  0 if ( converters.containsKey(typeName) ) {
 89  0 TypeConverter tc = (TypeConverter)converters.get(typeName);
 90  0 curObject = tc.getObject(dbValue);
 91    }
 92  0 else if ( primitives.containsKey(typeName) ) {
 93  0 typeClass = (Class)primitives.get(typeName);
 94  0 curObject = getParameter(typeClass, dbValue);
 95    }
 96    else {
 97  0 typeClass = Class.forName(typeName);
 98  0 curObject = getParameter(typeClass, dbValue);
 99    }
 100   
 101  0 Method setter = pd.getWriteMethod();
 102  0 if (setter != null) {
 103  0 if (curObject == null && pd.getPropertyType().isPrimitive()) {
 104  0 throw new IllegalArgumentException ("Null property value for primitive property " + propName);
 105    }
 106  0 setter.invoke (obj, new Object[] { curObject });
 107    }
 108    else {
 109  0 ErrorReporter.getErrorHandler().reportError("CategoryBeanMapper: No setter method found for:"+propName);
 110    }
 111    }
 112    }
 113    }
 114    catch (Exception e) {
 115  0 ErrorReporter.getErrorHandler().reportError("CategoryBeanMapper: Exception while trying to configure bean:"+e.getMessage());
 116    }
 117    }
 118   
 119  0 private static Object getParameter(Class clazz,String value) {
 120  0 Object obj = null;
 121  0 try {
 122  0 PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
 123  0 if( editor != null ) {
 124  0 editor.setAsText(value);
 125  0 obj = editor.getValue();
 126    }
 127    else {
 128  0 Constructor constr = clazz.getDeclaredConstructor(new Class[]{String.class});
 129  0 obj = constr.newInstance(new Object[]{value});
 130    }
 131    }
 132    catch (Exception e) {
 133  0 ErrorReporter.getErrorHandler().reportError("CategoryBeanMapper: Exception while trying to define parameter:"+e.getMessage());
 134    }
 135  0 return obj;
 136    }
 137    }