| Convert.java |
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 or later
5 * as published by the Free Software Foundation. This program is distributed
6 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7 * the 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 * © CrossWire Bible Society, 2005 - 2016
18 *
19 */
20 package org.crosswire.common.util;
21
22 import java.util.Map;
23
24 import org.slf4j.LoggerFactory;
25
26 /**
27 * Conversions between various types and Strings.
28 *
29 * @see gnu.lgpl.License The GNU Lesser General Public License for details.
30 * @author Joe Walker
31 */
32 public final class Convert {
33 /**
34 * We don't want anyone doing this ...
35 */
36 private Convert() {
37 }
38
39 /**
40 * Convert a String to a boolean
41 *
42 * @param data
43 * the thing to convert
44 * @return the converted data
45 */
46 public static boolean string2Boolean(String data) {
47 return Boolean.valueOf(data).booleanValue()
48 || "yes".equalsIgnoreCase(data)
49 || "ok".equalsIgnoreCase(data)
50 || "okay".equalsIgnoreCase(data)
51 || "on".equalsIgnoreCase(data)
52 || "1".equals(data);
53 }
54
55 /**
56 * Convert a boolean to a String
57 *
58 * @param data
59 * the thing to convert
60 * @return the converted data
61 */
62 public static String boolean2String(boolean data) {
63 return Boolean.toString(data);
64 }
65
66 /**
67 * Convert a String to an int
68 *
69 * @param data
70 * the thing to convert
71 * @return the converted data
72 */
73 public static int string2Int(String data) {
74 try {
75 return Integer.parseInt(data);
76 } catch (NumberFormatException ex) {
77 return 0;
78 }
79 }
80
81 /**
82 * Convert an int to a String
83 *
84 * @param data
85 * the thing to convert
86 * @return the converted data
87 */
88 public static String int2String(int data) {
89 return Integer.toString(data);
90 }
91
92 /**
93 * Convert a String to an Object
94 *
95 * @param data the thing to convert
96 * @return the converted data
97 * @throws InstantiationException
98 * if this {@code data} represents an abstract class,
99 * an interface, an array class, a primitive type, or void;
100 * or if the class has no nullary constructor;
101 * or if the instantiation fails for some other reason.
102 * @throws ClassNotFoundException if the class is not found
103 * @throws IllegalAccessException if the class or its nullary
104 * constructor is not accessible.
105 */
106 public static Object string2Object(String data) throws InstantiationException, ClassNotFoundException, IllegalAccessException {
107 return ClassUtil.forName(data).newInstance();
108 }
109
110 /**
111 * Convert an Object to a String
112 *
113 * @param data
114 * the thing to convert
115 * @return the converted data
116 */
117 public static String object2String(Object data) {
118 return data.getClass().getName();
119 }
120
121 /**
122 * Convert a String to a Map, without type checking
123 *
124 * @param data
125 * the thing to convert
126 * @return the converted data
127 */
128 public static PropertyMap string2Map(String data) {
129 PropertyMap commands = new PropertyMap();
130
131 String[] parts = StringUtil.split(data, " ");
132 String entry = "";
133 for (int i = 0; i < parts.length; i++) {
134 try {
135 entry = parts[i];
136 int pos = entry.indexOf('=');
137 String key = entry.substring(0, pos);
138 String value = entry.substring(pos + 1);
139 Class<?> clazz = ClassUtil.forName(value);
140
141 if (clazz.isAssignableFrom(Object.class)) {
142 assert false;
143 } else {
144 commands.put(key, value);
145 }
146 } catch (ClassNotFoundException ex) {
147 log.warn("Invalid config file entry: {} System message: {}", entry, ex.getMessage());
148 Reporter.informUser(Convert.class, ex);
149 }
150 }
151
152 return commands;
153 }
154
155 /**
156 * Convert a Map to a Sting
157 *
158 * @param commands
159 * the thing to convert
160 * @return the converted data
161 */
162 public static String map2String(Map<? extends Object, ? extends Object> commands) {
163 StringBuilder retcode = new StringBuilder();
164 for (Map.Entry<? extends Object, ? extends Object> entry : commands.entrySet()) {
165 retcode.append(entry.getKey());
166 retcode.append('=');
167 retcode.append(entry.getValue());
168 retcode.append(' ');
169 }
170
171 return retcode.toString().trim();
172 }
173
174 /**
175 * The log stream
176 */
177 private static final org.slf4j.Logger log = LoggerFactory.getLogger(Convert.class);
178 }
179