1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.talika.commons.util;
24
25 import java.text.NumberFormat;
26 import java.text.DateFormat;
27 import java.text.ParseException;
28 import java.util.Collections;
29 import java.util.Date;
30 import java.util.HashSet;
31 import java.util.Set;
32 import java.io.File;
33
34 /**
35 *
36 * @author Jose M. Palomar
37 * @version $Revision: 113 $
38 */
39 public final class ParseHelper {
40
41
42 /**
43 * Constant string value for <code>true</code>.
44 */
45 public static final String TRUE_VALUE = Boolean.toString(true);
46
47 /**
48 * Constant string value for <code>true</code>.
49 */
50 public static final String FALSE_VALUE = Boolean.toString(false);
51
52
53 /**
54 * Singleton instance.
55 */
56 private static final ParseHelper INSTANCE = new ParseHelper();
57
58 /**
59 * Set of classes supported by this helper.
60 */
61 private Set supportedClasses;
62
63 /**
64 * Integer parser.
65 */
66 private NumberFormat integerFormat;
67
68 /**
69 * Decimal parser.
70 */
71 private NumberFormat decimalFormat;
72
73 /**
74 * Date and time parser.
75 */
76 private DateFormat dateTimeFormat;
77
78 /**
79 * Time parser.
80 */
81 private DateFormat timeFormat;
82
83 /**
84 * Date parser.
85 */
86 private DateFormat dateFormat;
87
88
89 /**
90 * Constructs a new <code>ParseHelper</code> object.
91 */
92 private ParseHelper() {
93
94 this.integerFormat = NumberFormat.getNumberInstance();
95 this.integerFormat.setParseIntegerOnly(true);
96 this.decimalFormat = NumberFormat.getNumberInstance();
97 this.dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
98 this.dateTimeFormat.setLenient(false);
99 this.dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
100 this.dateFormat.setLenient(false);
101 this.timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
102 this.timeFormat.setLenient(false);
103
104
105 this.supportedClasses = new HashSet();
106 this.supportedClasses.add(java.lang.String.class);
107 this.supportedClasses.add(java.lang.Integer.class);
108 this.supportedClasses.add(java.lang.Long.class);
109 this.supportedClasses.add(java.lang.Float.class);
110 this.supportedClasses.add(java.lang.Double.class);
111 this.supportedClasses.add(java.lang.Boolean.class);
112 this.supportedClasses.add(java.util.Date.class);
113 this.supportedClasses.add(java.sql.Date.class);
114 this.supportedClasses.add(java.sql.Time.class);
115 this.supportedClasses.add(java.sql.Timestamp.class);
116 this.supportedClasses.add(java.io.File.class);
117 this.supportedClasses = Collections.unmodifiableSet(this.supportedClasses);
118
119 }
120
121
122 /**
123 * Obtains a <code>ParseHelper</code> instance.
124 *
125 * @return ParseHelper instance.
126 */
127 public static ParseHelper getInstance() {
128 return INSTANCE;
129 }
130
131 /**
132 * Parses a string text to produce an object of a target type.
133 *
134 * @param type Class target type.
135 * @param text String text to parse.
136 * @return Object an <code>Object</code> of target type parsed from string.
137 * @throws ParseException if the specified string cannot be parsed.
138 */
139 public Object parseObject(Class type, String text) throws ParseException {
140
141 if (type.getName().equals("java.lang.String")) {
142 return text;
143 }
144 if (type.getName().equals("java.lang.Integer")) {
145 return parseInteger(text);
146 }
147 else if (type.getName().equals("java.lang.Long")) {
148 return parseLong(text);
149 }
150 else if (type.getName().equals("java.lang.Float")) {
151 return parseFloat(text);
152 }
153 else if (type.getName().equals("java.lang.Double")) {
154 return parseDouble(text);
155 }
156 else if (type.getName().equals("java.lang.Boolean")) {
157 return parseBoolean(text);
158 }
159 else if (type.getName().equals("java.util.Date")) {
160 return parseDateTime(text);
161 }
162 else if (type.getName().equals("java.sql.Date")) {
163 return parseDate(text);
164 }
165 else if (type.getName().equals("java.sql.Time")) {
166 return parseTime(text);
167 }
168 else if (type.getName().equals("java.sql.Timestamp")) {
169 return parseDateTime(text);
170 }
171 else if (type.getName().equals("java.io.File")) {
172 return parseFile(text);
173 }
174 else {
175 throw new ParseException("Unsuported field type " + type.getName(), 0);
176 }
177
178 }
179
180 /**
181 * Parses a string text to produce an <code>Integer</code> object.
182 *
183 * @param text String text to parse.
184 * @return Integer an <code>Integer</code> object parsed from string.
185 * @throws ParseException if the specified string cannot be parsed.
186 */
187 public Integer parseInteger(String text) throws ParseException {
188 return new Integer(this.integerFormat.parse(text).intValue());
189 }
190
191 /**
192 * Parses a string text to produce a <code>Long</code> object.
193 *
194 * @param text String text to parse.
195 * @return Long a <code>Long</code> object parsed from string.
196 * @throws ParseException if the specified string cannot be parsed.
197 */
198 public Long parseLong(String text) throws ParseException {
199 return new Long(this.integerFormat.parse(text).longValue());
200 }
201
202 /**
203 * Parses a string text to produce a <code>Float</code> object.
204 *
205 * @param text String text to parse.
206 * @return Float a <code>Float</code> object parsed from string.
207 * @throws ParseException if the specified string cannot be parsed.
208 */
209 public Float parseFloat(String text) throws ParseException {
210 return new Float(this.decimalFormat.parse(text).floatValue());
211 }
212
213 /**
214 * Parses a string text to produce an <code>Double</code> object.
215 *
216 * @param text String text to parse.
217 * @return Double a <code>Double</code> object parsed from string.
218 * @throws ParseException if the specified string cannot be parsed.
219 */
220 public Double parseDouble(String text) throws ParseException {
221 return new Double(this.decimalFormat.parse(text).doubleValue());
222 }
223
224 /**
225 * Parses a string text to produce an <code>Boolean</code> object.
226 *
227 * @param text String text to parse.
228 * @return Boolean a <code>Boolean</code> object parsed from string.
229 * @throws ParseException if the specified string cannot be parsed.
230 */
231 public Boolean parseBoolean(String text) throws ParseException {
232
233 if (text.equalsIgnoreCase(TRUE_VALUE)) {
234 return Boolean.TRUE;
235 }
236 else if (text.equalsIgnoreCase(FALSE_VALUE)) {
237 return Boolean.FALSE;
238 }
239 else {
240 throw new ParseException("Invalid boolean value " + text, 0);
241 }
242
243 }
244
245 /**
246 * Parses a string text to produce a <code>Date</code> object.
247 *
248 * @param text String text to parse.
249 * @return Date a <code>Date</code> object parsed from string.
250 * @throws ParseException if the specified string cannot be parsed.
251 */
252 public Date parseDateTime(String text) throws ParseException {
253 return this.dateTimeFormat.parse(text);
254 }
255
256 /**
257 * Parses a string text to produce a <code>Date</code> object.
258 *
259 * @param text String text to parse.
260 * @return Date a <code>Date</code> object parsed from string.
261 * @throws ParseException if the specified string cannot be parsed.
262 */
263 public Date parseDate(String text) throws ParseException {
264 return this.dateFormat.parse(text);
265 }
266
267 /**
268 * Parses a string text to produce a <code>Date</code> object.
269 *
270 * @param text String text to parse.
271 * @return Date a <code>Date</code> object parsed from string.
272 * @throws ParseException if the specified string cannot be parsed.
273 */
274 public Date parseTime(String text) throws ParseException {
275 return this.timeFormat.parse(text);
276 }
277
278 /**
279 * Parses a string text to produce a <code>File</code> object.
280 *
281 * @param text String text to parse.
282 * @return File a <code>File</code> object parsed from string.
283 * @throws ParseException if the specified string cannot be parsed.
284 */
285 public File parseFile(String text) throws ParseException {
286
287 File file = new File(text);
288 if (!file.exists()) {
289 file = null;
290 }
291
292 return file;
293
294 }
295
296 /**
297 * Returns a <code>Set</code> of classes supported by this helper.
298 *
299 * @return Set of classes supported.
300 */
301 public Set getSupportedClasses() {
302 return this.supportedClasses;
303 }
304
305 }