KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > BeanUtil


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util;
37
38 import java.util.*;
39 import java.io.*;
40 import java.beans.*;
41 import java.lang.reflect.*;
42
43 /** bean utilities */
44
45 public class BeanUtil
46 {
47     public static String JavaDoc encode(Object JavaDoc object) throws Exception JavaDoc
48     {
49         if (object != null)
50         {
51             ByteArrayOutputStream bOut = new ByteArrayOutputStream();
52                 
53             XMLEncoder encoder = new XMLEncoder(bOut);
54                 
55             encoder.writeObject(object);
56             
57             encoder.close();
58
59             return bOut.toString();
60         }
61
62         return null;
63     }
64
65     public static Object JavaDoc decode(String JavaDoc string) throws Exception JavaDoc
66     {
67         Object JavaDoc object = null;
68
69         if (string != null)
70         {
71             ByteArrayInputStream bIn = new ByteArrayInputStream(string.getBytes("utf-8"));
72             XMLDecoder decoder = new XMLDecoder(bIn);
73                 
74             object = decoder.readObject();
75
76             decoder.close();
77
78             return object;
79         }
80
81         return null;
82     }
83
84     /** get a bean property (strictly without map/list support) */
85
86     public static final Object JavaDoc getBeanProperty(Object JavaDoc object, String JavaDoc name) throws Exception JavaDoc
87     {
88         PropertyDescriptor pd[] = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors();
89                     
90         for (int i = pd.length; --i >= 0;)
91         {
92             String JavaDoc propertyName = pd[i].getName();
93             
94             if (name.equals(propertyName))
95             {
96                 Method m = pd[i].getReadMethod();
97                 
98                 return m.invoke(object, (Object JavaDoc[])null);
99             }
100         }
101         
102         throw new Exception JavaDoc("bean property " + name + " not found");
103     }
104
105     /** get a bean property */
106
107     public static final Object JavaDoc getProperty(Object JavaDoc object, String JavaDoc name) throws Exception JavaDoc
108     {
109         Class JavaDoc objectClass = object.getClass();
110
111         if (object instanceof Map)
112         {
113             return ((Map)object).get(name);
114         }
115         else if (object instanceof List)
116         {
117             List list = (List)object;
118
119             int index = Integer.parseInt(name);
120
121             if (index < 0)
122             {
123                 index = list.size() + index;
124             }
125             
126             return list.get(index);
127         }
128         else if (objectClass.isArray())
129         {
130             int index = Integer.parseInt(name);
131
132             if (index < 0)
133             {
134                 index = Array.getLength(object) + index;
135             }
136
137             return Array.get(object, index);
138         }
139
140         return getBeanProperty(object, name);
141     }
142
143     /** sets a bean property (strictly without map/list/array support) */
144
145     public static final Object JavaDoc setBeanProperty(Object JavaDoc object, String JavaDoc name, Object JavaDoc value) throws Exception JavaDoc
146     {
147         PropertyDescriptor pd[] = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors();
148                     
149         for (int i = pd.length; --i >= 0;)
150         {
151             String JavaDoc propertyName = pd[i].getName();
152             
153             if (name.equals(propertyName))
154             {
155                 Method m = pd[i].getWriteMethod();
156                 
157                 Class JavaDoc[] parameterTypes = m.getParameterTypes();
158                 
159                 Object JavaDoc convertedValue = TypeUtil.isClass(value, parameterTypes[0]);
160                 
161                 return m.invoke(object, new Object JavaDoc[]{convertedValue});
162             }
163         }
164
165         throw new Exception JavaDoc("bean property " + name + " not found");
166     }
167
168
169     /** sets a bean property */
170
171     public static final Object JavaDoc setProperty(Object JavaDoc object, String JavaDoc name, Object JavaDoc value) throws Exception JavaDoc
172     {
173         Class JavaDoc objectClass = object.getClass();
174
175         if (object instanceof Map)
176         {
177             return ((Map)object).put(name, value);
178         }
179         else if (object instanceof List)
180         {
181             List list = (List)object;
182
183             Object JavaDoc returnObject = null;
184
185             int listSize = list.size();
186
187             int index = -1;
188
189             boolean isInsertMode = false;
190             boolean isIgnoreNullMode = false;
191
192             if ("".equals(name))
193             {
194                 name = "*";
195             }
196
197             if (name.startsWith("*"))
198             {
199                 isInsertMode = true;
200                 name = name.substring(1);
201
202                 if (name.startsWith("*"))
203                 {
204                     isIgnoreNullMode = true;
205                     name = name.substring(1);
206                 }
207             }
208
209             if (name.length() == 0)
210             {
211                 index = listSize;
212             }
213             else
214             {
215                 index = Integer.parseInt(name);
216
217                 if (index < 0)
218                 {
219                     index = listSize + index;
220                 }
221             }
222
223             if (isInsertMode)
224             {
225                 if (value != null)
226                 {
227                     if (index == listSize)
228                     {
229                         list.add(value);
230                     }
231                     else
232                     {
233                         list.add(index, value);
234                     }
235                 }
236                 else if ((listSize > 0) && (!isIgnoreNullMode))
237                 {
238                     if (index == listSize)
239                     {
240                         returnObject = list.remove(listSize - 1);
241                     }
242                     else
243                     {
244                         returnObject = list.remove(index);
245                     }
246                 }
247             }
248             else
249             {
250                 returnObject = list.set(index, value);
251             }
252
253             return returnObject;
254         }
255         else if (objectClass.isArray())
256         {
257             Class JavaDoc componentClass = objectClass.getComponentType();
258                 
259             int index = Integer.parseInt(name);
260             
261             if (index < 0)
262             {
263                 index = Array.getLength(object) + index;
264             }
265             
266             Object JavaDoc currentValue = Array.get(object, index);
267
268             Array.set(object, index, TypeUtil.isClass(value, componentClass));
269
270             return currentValue;
271         }
272
273         return setBeanProperty(object, name, value);
274     }
275
276     /** fill Object with property map */
277
278     public static Object JavaDoc fill(Object JavaDoc object, Map map) throws Exception JavaDoc
279     {
280         if (object != null)
281         {
282             PropertyDescriptor pd[] = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors();
283                     
284             Object JavaDoc[] arg = new Object JavaDoc[1];
285
286             for (int i = 0; i < pd.length; i++)
287             {
288                 String JavaDoc name = pd[i].getName();
289
290                 Object JavaDoc propertyValue = map.get(name);
291
292                 if (propertyValue != null)
293                 {
294                     Method m = pd[i].getWriteMethod();
295
296                     Class JavaDoc[] parameterTypes = m.getParameterTypes();
297
298                     Object JavaDoc convertedValue = TypeUtil.isClass(propertyValue, parameterTypes[0]);
299                     arg[0] = convertedValue;
300
301                     m.invoke(object, arg);
302                 }
303             }
304         }
305
306         return object;
307     }
308
309     /** creates a map with all defined bean properties */
310
311     public static NestedMap getMap(Object JavaDoc object) throws Exception JavaDoc
312     {
313         PropertyDescriptor pd[] = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors();
314
315         NestedMap map = new NestedMap();
316         
317         for (int i = 0; i < pd.length; i++)
318         {
319             String JavaDoc name = pd[i].getName();
320
321             Object JavaDoc propertyValue = map.get(name);
322             
323             if (propertyValue == null)
324             {
325                 Method m = pd[i].getReadMethod();
326
327                 if (m != null)
328                 {
329                     propertyValue = m.invoke(object, (Object JavaDoc[])null);
330
331                     map.put(name, propertyValue);
332                 }
333             }
334         }
335
336         return map;
337     }
338 }
339
Popular Tags