KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > bean > modifiers > TrimStrings


1 package jodd.bean.modifiers;
2
3 import jodd.bean.BeanUtil;
4
5 /**
6  * Simple bean modifier that trims all String parameters of given java bean object.
7  */

8 public class TrimStrings implements jodd.bean.PropertyCallback {
9
10     public void onProperty(Object obj, String name) {
11         if (obj != null) {
12             Object value = BeanUtil.getProperty(obj, name);
13             if (value != null) {
14                 if (value instanceof String) { // trim String parameter
15
value = ((String)value).trim();
16                     BeanUtil.setProperty(obj, name, value);
17                 } else if (value.getClass().isArray() == true) {
18                     if (value instanceof String[]) { // trim String[] parameter
19
String[] valueArray = (String[]) value;
20                         for (int i = 0; i < valueArray.length; i++) {
21                             valueArray[i] = valueArray[i].trim();
22                         }
23                     } else {
24                         Object[] valueArray = (Object[]) value; // trim Strings in Object[] parameter
25
for (int i = 0; i < valueArray.length; i++) {
26                             if (valueArray[i] instanceof String) {
27                                 valueArray[i] = ((String)valueArray[i]).trim();
28                             }
29                         }
30                     }
31                 }
32             }
33         }
34         return;
35     }
36 }
37
Popular Tags