Source code: com/eireneh/util/Convert.java
1
2 package com.eireneh.util;
3
4 import java.awt.Font;
5 import java.awt.Color;
6 import java.util.Hashtable;
7 import java.util.Enumeration;
8
9 import com.eireneh.util.*;
10
11 /**
12 * Conversions between various types and Strings.
13 *
14 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
15 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
16 * Distribution Licence:<br />
17 * Project B is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General Public License,
19 * version 2 as published by the Free Software Foundation.<br />
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.<br />
24 * The License is available on the internet
25 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
26 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
28 * The copyright to this program is held by it's authors.
29 * </font></td></tr></table>
30 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
31 * @see docs.Licence
32 * @author Joe Walker
33 */
34 public class Convert
35 {
36 /**
37 * We don't want anyone doing this ...
38 */
39 private Convert()
40 {
41 }
42
43 /**
44 * Convert a String to a boolean
45 * @param data the thing to convert
46 * @return the converted data
47 */
48 public static boolean string2Boolean(String data)
49 {
50 if (data.equalsIgnoreCase("true")) return true;
51 if (data.equalsIgnoreCase("yes")) return true;
52 if (data.equalsIgnoreCase("ok")) return true;
53 if (data.equalsIgnoreCase("okay")) return true;
54 if (data.equalsIgnoreCase("on")) return true;
55 if (data.equalsIgnoreCase("1")) return true;
56
57 return false;
58 }
59
60 /**
61 * Convert a boolean to a String
62 * @param data the thing to convert
63 * @return the converted data
64 */
65 public static String boolean2String(boolean data)
66 {
67 return data ? "True" : "False";
68 }
69
70 /**
71 * Convert a String to an int
72 * @param data the thing to convert
73 * @return the converted data
74 */
75 public static int string2Int(String data)
76 {
77 try
78 {
79 return Integer.parseInt(data);
80 }
81 catch (NumberFormatException ex)
82 {
83 return 0;
84 }
85 }
86
87 /**
88 * Convert an int to a String
89 * @param data the thing to convert
90 * @return the converted data
91 */
92 public static String int2String(int data)
93 {
94 return Integer.toString(data);
95 }
96
97 /**
98 * Convert a String to an Object
99 * @param data the thing to convert
100 * @return the converted data
101 */
102 public static Object string2Object(String data) throws InstantiationException, ClassNotFoundException, IllegalAccessException
103 {
104 return Class.forName(data).newInstance();
105 }
106
107 /**
108 * Convert an Object to a String
109 * @param data the thing to convert
110 * @return the converted data
111 */
112 public static String object2String(Object data)
113 {
114 return data.getClass().getName();
115 }
116
117 /**
118 * Convert a String to a Class
119 * @param data the thing to convert
120 * @return the converted data
121 */
122 public static Class string2Class(String data) throws ClassNotFoundException
123 {
124 return Class.forName(data);
125 }
126
127 /**
128 * Convert a Class to a String
129 * @param data the thing to convert
130 * @return the converted data
131 */
132 public static String class2String(Class data)
133 {
134 return data.getName();
135 }
136
137 /**
138 * Convert a String to a Hashtable, with type checking
139 * @param data the thing to convert
140 * @return the converted data
141 */
142 public static Hashtable string2Hashtable(String data, Class superclass)
143 {
144 Hashtable commands = new Hashtable();
145
146 String[] data_arr = StringUtil.tokenize(data, " ");
147
148 for (int i=0; i<data_arr.length; i++)
149 {
150 try
151 {
152 int equ_pos = data_arr[i].indexOf('=');
153 String key = data_arr[i].substring(0, equ_pos);
154 String value = data_arr[i].substring(equ_pos + 1);
155 Class clazz = Class.forName(value);
156
157 if (clazz.isAssignableFrom(superclass))
158 throw new ClassCastException("Type Error");
159
160 commands.put(key, value);
161 }
162 catch (Exception ex)
163 {
164 log.warning("Invalid config file entry: "+data_arr[i]+" System message: "+ex.getMessage());
165 Reporter.informUser(Convert.class, ex);
166 }
167 }
168
169 return commands;
170 }
171
172 /**
173 * Convert a String to a Hashtable, without type checking
174 * @param data the thing to convert
175 * @return the converted data
176 */
177 public static Hashtable string2Hashtable(String data)
178 {
179 return string2Hashtable(data, Object.class);
180 }
181
182 /**
183 * Convert a Hashtable to a Sting
184 * @param data the thing to convert
185 * @return the converted data
186 */
187 public static String hashtable2String(Hashtable commands)
188 {
189 Enumeration en = commands.keys();
190 StringBuffer retcode = new StringBuffer();
191
192 while (en.hasMoreElements())
193 {
194 String key = "";
195 String value = "";
196
197 try
198 {
199 key = (String) en.nextElement();
200 value = (String) commands.get(key);
201
202 retcode.append(key);
203 retcode.append("=");
204 retcode.append(value);
205 retcode.append(" ");
206 }
207 catch (ClassCastException ex)
208 {
209 log.warning("non-String member found: key="+key+" value="+value);
210 Reporter.informUser(Convert.class, ex);
211 }
212 }
213
214 return retcode.toString().trim();
215 }
216
217 /**
218 * Convert a String to a StringArray
219 * @param data the thing to convert
220 * @return the converted data
221 */
222 public static String[] string2StringArray(String value, String separator)
223 {
224 return StringUtil.tokenize(value, separator);
225 }
226
227 /**
228 * Convert a StringArray to a String
229 * @param data the thing to convert
230 * @return the converted data
231 */
232 public static String stringArray2String(String[] value, String separator)
233 {
234 return StringUtil.cat(value, separator);
235 }
236
237 /** The log stream */
238 protected static Logger log = Logger.getLogger("util.util");
239 }
240