KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > util > PropertyConverter


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.setup.util;
10
11
12 import java.lang.reflect.Constructor JavaDoc;
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.beans.PropertyEditorManager JavaDoc;
15 import java.beans.PropertyEditor JavaDoc;
16
17
18 /**
19  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
20  * Date: Apr 15, 2005; Time: 11:50:47 AM
21  * @since JBoss portal 2.0
22  * Class org.jboss.portal.setup.util.PropertyConverter converts a property
23  * from a String value to an instance of a specified type.
24  */

25 public class PropertyConverter
26 {
27
28    static
29    {
30       PropertyEditor JavaDoc lEditor = PropertyEditorManager.findEditor(int.class);
31       if (lEditor != null)
32       {
33          PropertyEditorManager.registerEditor(Integer JavaDoc.class, lEditor.getClass());
34       }
35       lEditor = null;
36       lEditor = PropertyEditorManager.findEditor(boolean.class);
37       if (lEditor != null)
38       {
39          PropertyEditorManager.registerEditor(Boolean JavaDoc.class, lEditor.getClass());
40       }
41
42       PropertyEditorManager.registerEditor(Character JavaDoc.class, CharEditor.class);
43       PropertyEditorManager.registerEditor(Character.TYPE, CharEditor.class);
44
45
46    }
47
48    /**
49     * Convert a string to a value of the "desired" type. Throw an
50     * IllegalArgumentException if the conversion fails.
51     * Special case of string arrays (represented as a space-separated
52     * string)
53     *
54     * @param value to be converted to a String
55     * @return An string representaion of a converted object
56     * @throws IllegalArgumentException if the conversion fails
57     */

58    public static String JavaDoc convertToString
59       (Object JavaDoc
60       value)
61       throws IllegalArgumentException JavaDoc
62    {
63       String JavaDoc lStrValue = null;
64       if (value != null)
65       {
66          PropertyEditor JavaDoc lPe = PropertyEditorManager.findEditor(value.getClass());
67          if (lPe != null)
68          {
69             lPe.setValue(value);
70             lStrValue = lPe.getAsText();
71          }
72          else
73          {
74             throw new IllegalArgumentException JavaDoc("PropertyEdidor is not defined for a specified type:" +
75                value.getClass().getName());
76          }
77       }
78       return lStrValue;
79    }
80
81    /**
82     * Convert a string to a value of the "desired" type. Throw an
83     * IllegalArgumentException if the conversion fails.
84     * Special case of string arrays (represented as a space-separated
85     * string)
86     *
87     * @param strValue The String representation of the value
88     * @param propType Expected value type
89     * value type
90     * @return An object of the specified type converted from the string
91     * @throws IllegalArgumentException if the conversion fails
92     */

93    public static Object JavaDoc convertToValue
94       (String JavaDoc
95       strValue, Class JavaDoc propType)
96       throws IllegalArgumentException JavaDoc
97    {
98       Object JavaDoc lValue = null;
99       if (propType == null)
100       {
101          throw new IllegalStateException JavaDoc("Property class type cannot be null.");
102       }
103
104       PropertyEditor JavaDoc lPe = PropertyEditorManager.findEditor(propType);
105       if (lPe != null)
106       {
107          lPe.setAsText(strValue);
108          lValue = lPe.getValue();
109       }
110       if (lValue == null && strValue != null)
111       {
112          //try to find a constructor
113
lValue = genericConvert(strValue, propType);
114       }
115       return lValue;
116    }
117
118
119    private static Object JavaDoc genericConvert(String JavaDoc strValue, Class JavaDoc type)
120    {
121       Object JavaDoc lValue = null;
122       try
123       {
124          Constructor JavaDoc lConstructor = type.getDeclaredConstructor(new Class JavaDoc[]{String JavaDoc.class});
125          lValue = lConstructor.newInstance(new Object JavaDoc[]{strValue});
126
127       }
128       catch (NoSuchMethodException JavaDoc nsme)
129       {
130          IllegalArgumentException JavaDoc iex = new IllegalArgumentException JavaDoc("Don't know how to convert a String to property of type " +
131             type.getName());
132          iex.initCause(nsme);
133          throw iex;
134       }
135       catch (InstantiationException JavaDoc ie)
136       {
137          IllegalArgumentException JavaDoc iex = new IllegalArgumentException JavaDoc("Don't know how to convert a String to property of type " +
138             type.getName());
139          iex.initCause(ie);
140          throw iex;
141       }
142       catch (IllegalAccessException JavaDoc ia)
143       {
144          IllegalArgumentException JavaDoc iex = new IllegalArgumentException JavaDoc("Don't know how to convert a String to property of type " +
145             type.getName());
146          iex.initCause(ia);
147          throw iex;
148       }
149       catch (InvocationTargetException JavaDoc ite)
150       {
151          IllegalArgumentException JavaDoc iex = new IllegalArgumentException JavaDoc("Don't know how to convert a String to property of type " +
152             type.getName());
153          iex.initCause(ite.getTargetException());
154          throw iex;
155       }
156       if (lValue == null)
157       {
158          throw new IllegalArgumentException JavaDoc("Don't know how to convert a String to property of type " +
159             type.getName());
160       }
161       return lValue;
162    }
163
164 }
165
Popular Tags