Source code: javatools/text/AppropriateFormat.java
1 /*
2 * AppropriateFormat.java
3 *
4 * Created on 9 aprile 2003, 9.31
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.text;
26
27 import java.util.HashMap;
28 import java.text.DateFormat;
29
30 /**
31 *
32 * @author Antonio Petrelli
33 * @version 0.2.0
34 */
35 public class AppropriateFormat {
36
37 /** Creates a new instance of AppropriateFormat */
38 public AppropriateFormat() {
39 type2builder = new HashMap();
40 intBuilder = new IntegerBuilder();
41 longBuilder = new LongBuilder();
42 booleanBuilder = new BooleanBuilder();
43 shortBuilder = new ShortBuilder();
44 type2builder.put("java.lang.Integer", intBuilder);
45 type2builder.put("java.lang.Long", longBuilder);
46 type2builder.put("java.lang.Boolean", booleanBuilder);
47 type2builder.put("java.lang.Short", shortBuilder);
48 }
49
50 public void setDateFormat(DateFormat format) {
51 dateBuilder = new DateBuilder(format);
52 type2builder.put("java.util.Date", dateBuilder);
53 }
54
55 public Object parseObject(String value, String type) {
56 Object tempObject;
57 TypeBuilder builder;
58
59 tempObject = value;
60 if (!type.equals("java.lang.String")) {
61 builder = (TypeBuilder) type2builder.get(type);
62 if (builder != null)
63 tempObject = builder.build(value);
64 }
65 return tempObject;
66 }
67
68 private HashMap type2builder;
69 private IntegerBuilder intBuilder;
70 private LongBuilder longBuilder;
71 private BooleanBuilder booleanBuilder;
72 private ShortBuilder shortBuilder;
73 private DateBuilder dateBuilder;
74 }