1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: Convert.java 2223 2012-01-26 21:28:02Z dmsmith $
21   */
22  package org.crosswire.common.util;
23  
24  import java.util.Map;
25  
26  /**
27   * Conversions between various types and Strings.
28   * 
29   * @see gnu.lgpl.License for license details.<br>
30   *      The copyright to this program is held by it's authors.
31   * @author Joe Walker [joe at eireneh dot com]
32   */
33  public final class Convert {
34      /**
35       * We don't want anyone doing this ...
36       */
37      private Convert() {
38      }
39  
40      /**
41       * Convert a String to a boolean
42       * 
43       * @param data
44       *            the thing to convert
45       * @return the converted data
46       */
47      public static boolean string2Boolean(String data) {
48          return Boolean.valueOf(data).booleanValue()
49                  || "yes".equalsIgnoreCase(data)
50                  || "ok".equalsIgnoreCase(data)
51                  || "okay".equalsIgnoreCase(data)
52                  || "on".equalsIgnoreCase(data)
53                  || "1".equals(data);
54      }
55  
56      /**
57       * Convert a boolean to a String
58       * 
59       * @param data
60       *            the thing to convert
61       * @return the converted data
62       */
63      public static String boolean2String(boolean data) {
64          return Boolean.toString(data);
65      }
66  
67      /**
68       * Convert a String to an int
69       * 
70       * @param data
71       *            the thing to convert
72       * @return the converted data
73       */
74      public static int string2Int(String data) {
75          try {
76              return Integer.parseInt(data);
77          } catch (NumberFormatException ex) {
78              return 0;
79          }
80      }
81  
82      /**
83       * Convert an int to a String
84       * 
85       * @param data
86       *            the thing to convert
87       * @return the converted data
88       */
89      public static String int2String(int data) {
90          return Integer.toString(data);
91      }
92  
93      /**
94       * Convert a String to an Object
95       * 
96       * @param data
97       *            the thing to convert
98       * @return the converted data
99       */
100     public static Object string2Object(String data) throws InstantiationException, ClassNotFoundException, IllegalAccessException {
101         return ClassUtil.forName(data).newInstance();
102     }
103 
104     /**
105      * Convert an Object to a String
106      * 
107      * @param data
108      *            the thing to convert
109      * @return the converted data
110      */
111     public static String object2String(Object data) {
112         return data.getClass().getName();
113     }
114 
115     /**
116      * Convert a String to a Class
117      * 
118      * @param data
119      *            the thing to convert
120      * @return the converted data
121      */
122     public static Class<?> string2Class(String data) throws ClassNotFoundException {
123         return ClassUtil.forName(data);
124     }
125 
126     /**
127      * Convert a Class to a String
128      * 
129      * @param data
130      *            the thing to convert
131      * @return the converted data
132      */
133     public static String class2String(Class<?> data) {
134         return data.getName();
135     }
136 
137     /**
138      * Convert a String to a Map, with type checking
139      * 
140      * @param data
141      *            the thing to convert
142      * @return the converted data
143      */
144     public static PropertyMap string2Hashtable(String data, Class<?> superclass) {
145         PropertyMap commands = new PropertyMap();
146 
147         String[] data_arr = StringUtil.split(data, " ");
148         String entry = "";
149         for (int i = 0; i < data_arr.length; i++) {
150             try {
151                 entry = data_arr[i];
152                 int equ_pos = entry.indexOf('=');
153                 String key = entry.substring(0, equ_pos);
154                 String value = entry.substring(equ_pos + 1);
155                 Class<?> clazz = ClassUtil.forName(value);
156 
157                 if (clazz.isAssignableFrom(superclass)) {
158                     assert false;
159                 } else {
160                     commands.put(key, value);
161                 }
162             } catch (ClassNotFoundException ex) {
163                 log.warn("Invalid config file entry: " + entry + " System message: " + ex.getMessage());
164                 Reporter.informUser(Convert.class, ex);
165             }
166         }
167 
168         return commands;
169     }
170 
171     /**
172      * Convert a String to a Map, without type checking
173      * 
174      * @param data
175      *            the thing to convert
176      * @return the converted data
177      */
178     public static PropertyMap string2Map(String data) {
179         return string2Hashtable(data, Object.class);
180     }
181 
182     /**
183      * Convert a Map to a Sting
184      * 
185      * @param commands
186      *            the thing to convert
187      * @return the converted data
188      */
189     public static String map2String(Map<? extends Object, ? extends Object> commands) {
190         StringBuilder retcode = new StringBuilder();
191         for (Map.Entry<? extends Object, ? extends Object> entry : commands.entrySet()) {
192             retcode.append(entry.getKey());
193             retcode.append('=');
194             retcode.append(entry.getValue());
195             retcode.append(' ');
196         }
197 
198         return retcode.toString().trim();
199     }
200 
201     /**
202      * Convert a String to a StringArray
203      * 
204      * @param value
205      *            the thing to convert
206      * @return the converted data
207      */
208     public static String[] string2StringArray(String value, String separator) {
209         return StringUtil.split(value, separator);
210     }
211 
212     /**
213      * Convert a StringArray to a String
214      * 
215      * @param value
216      *            the thing to convert
217      * @return the converted data
218      */
219     public static String stringArray2String(String[] value, String separator) {
220         return StringUtil.join(value, separator);
221     }
222 
223     /**
224      * The log stream
225      */
226     private static final Logger log = Logger.getLogger(Convert.class);
227 }
228