1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.validator.util; |
18 | |
|
19 | |
import java.lang.reflect.InvocationTargetException; |
20 | |
import java.util.Collection; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.Map; |
24 | |
import java.util.Map.Entry; |
25 | |
|
26 | |
import org.apache.commons.beanutils.PropertyUtils; |
27 | |
import org.apache.commons.collections.FastHashMap; |
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
import org.apache.commons.validator.Arg; |
31 | |
import org.apache.commons.validator.Msg; |
32 | |
import org.apache.commons.validator.Var; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class ValidatorUtils { |
44 | |
|
45 | 1 | private static final Log LOG = LogFactory.getLog(ValidatorUtils.class); |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public static String replace(String value, String key, String replaceValue) { |
57 | |
|
58 | 180 | if (value == null || key == null || replaceValue == null) { |
59 | 0 | return value; |
60 | |
} |
61 | |
|
62 | 180 | int pos = value.indexOf(key); |
63 | |
|
64 | 180 | if (pos < 0) { |
65 | 140 | return value; |
66 | |
} |
67 | |
|
68 | 40 | int length = value.length(); |
69 | 40 | int start = pos; |
70 | 40 | int end = pos + key.length(); |
71 | |
|
72 | 40 | if (length == key.length()) { |
73 | 40 | value = replaceValue; |
74 | |
|
75 | 0 | } else if (end == length) { |
76 | 0 | value = value.substring(0, start) + replaceValue; |
77 | |
|
78 | |
} else { |
79 | 0 | value = |
80 | |
value.substring(0, start) |
81 | |
+ replaceValue |
82 | |
+ replace(value.substring(end), key, replaceValue); |
83 | |
} |
84 | |
|
85 | 40 | return value; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public static String getValueAsString(Object bean, String property) { |
102 | 210 | Object value = null; |
103 | |
|
104 | |
try { |
105 | 210 | value = PropertyUtils.getProperty(bean, property); |
106 | |
|
107 | 0 | } catch(IllegalAccessException e) { |
108 | 0 | LOG.error(e.getMessage(), e); |
109 | 0 | } catch(InvocationTargetException e) { |
110 | 0 | LOG.error(e.getMessage(), e); |
111 | 0 | } catch(NoSuchMethodException e) { |
112 | 0 | LOG.error(e.getMessage(), e); |
113 | 210 | } |
114 | |
|
115 | 210 | if (value == null) { |
116 | 47 | return null; |
117 | |
} |
118 | |
|
119 | 163 | if (value instanceof String[]) { |
120 | 0 | return ((String[]) value).length > 0 ? value.toString() : ""; |
121 | |
|
122 | 163 | } else if (value instanceof Collection) { |
123 | 0 | return ((Collection<?>) value).isEmpty() ? "" : value.toString(); |
124 | |
|
125 | |
} else { |
126 | 163 | return value.toString(); |
127 | |
} |
128 | |
|
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
@Deprecated |
144 | |
public static FastHashMap copyFastHashMap(FastHashMap map) { |
145 | 0 | FastHashMap results = new FastHashMap(); |
146 | |
|
147 | |
@SuppressWarnings("unchecked") |
148 | 0 | Iterator<Entry<String, ?>> i = map.entrySet().iterator(); |
149 | 0 | while (i.hasNext()) { |
150 | 0 | Entry<String, ?> entry = i.next(); |
151 | 0 | String key = entry.getKey(); |
152 | 0 | Object value = entry.getValue(); |
153 | |
|
154 | 0 | if (value instanceof Msg) { |
155 | 0 | results.put(key, ((Msg) value).clone()); |
156 | 0 | } else if (value instanceof Arg) { |
157 | 0 | results.put(key, ((Arg) value).clone()); |
158 | 0 | } else if (value instanceof Var) { |
159 | 0 | results.put(key, ((Var) value).clone()); |
160 | |
} else { |
161 | 0 | results.put(key, value); |
162 | |
} |
163 | 0 | } |
164 | |
|
165 | 0 | results.setFast(true); |
166 | 0 | return results; |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public static Map<String, Object> copyMap(Map<String, Object> map) { |
179 | 0 | Map<String, Object> results = new HashMap<String, Object>(); |
180 | |
|
181 | 0 | Iterator<Entry<String, Object>> i = map.entrySet().iterator(); |
182 | 0 | while (i.hasNext()) { |
183 | 0 | Entry<String, Object> entry = i.next(); |
184 | 0 | String key = entry.getKey(); |
185 | 0 | Object value = entry.getValue(); |
186 | |
|
187 | 0 | if (value instanceof Msg) { |
188 | 0 | results.put(key, ((Msg) value).clone()); |
189 | 0 | } else if (value instanceof Arg) { |
190 | 0 | results.put(key, ((Arg) value).clone()); |
191 | 0 | } else if (value instanceof Var) { |
192 | 0 | results.put(key, ((Var) value).clone()); |
193 | |
} else { |
194 | 0 | results.put(key, value); |
195 | |
} |
196 | 0 | } |
197 | 0 | return results; |
198 | |
} |
199 | |
|
200 | |
} |