View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.util;
24  
25  import java.util.Enumeration;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Properties;
30  import java.util.ResourceBundle;
31  
32  import org.apache.commons.beanutils.BeanUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import com.gisgraphy.helper.PropertiesHelper;
37  import com.gisgraphy.model.LabelValue;
38  
39  /**
40   * Utility class to convert one object to another.
41   */
42  public final class ConvertUtil {
43      private static final Log log = LogFactory.getLog(ConvertUtil.class);
44  
45      /**
46       * Checkstyle rule: utility classes should not have public constructor
47       */
48      private ConvertUtil() {
49      }
50  
51      
52  
53      /**
54       * Convert a java.util.List of LabelValue objects to a LinkedHashMap.
55       * 
56       * @param list
57       *                the list to convert
58       * @return the populated map with the label as the key
59       */
60      public static Map<String, String> convertListToMap(List<LabelValue> list) {
61  	Map<String, String> map = new LinkedHashMap<String, String>();
62  
63  	for (LabelValue option : list) {
64  	    map.put(option.getLabel(), option.getValue());
65  	}
66  
67  	return map;
68      }
69  
70      /**
71       * Method to convert a ResourceBundle to a Properties object.
72       * 
73       * @param rb
74       *                a given resource bundle
75       * @return Properties a populated properties object
76       */
77      public static Properties convertBundleToProperties(ResourceBundle rb) {
78  	Properties props = new Properties();
79  
80  	for (Enumeration<String> keys = rb.getKeys(); keys.hasMoreElements();) {
81  	    String key = keys.nextElement();
82  	    props.put(key, rb.getString(key));
83  	}
84  
85  	return props;
86      }
87  
88      /**
89       * Convenience method used by tests to populate an object from a
90       * ResourceBundle
91       * 
92       * @param obj
93       *                an initialized object
94       * @param rb
95       *                a resource bundle
96       * @return a populated object
97       */
98      public static Object populateObject(Object obj, ResourceBundle rb) {
99  	try {
100 	    Map<String, String> map = PropertiesHelper.convertBundleToMap(rb);
101 	    BeanUtils.copyProperties(obj, map);
102 	} catch (Exception e) {
103 	    log
104 		    .error("Exception occurred populating object: "
105 			    + e.getMessage());
106 	}
107 
108 	return obj;
109     }
110 }