1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.commons.validator.util;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29
30 import org.apache.commons.beanutils.PropertyUtils;
31 import org.apache.commons.collections.FastHashMap;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.commons.validator.Arg;
35 import org.apache.commons.validator.Msg;
36 import org.apache.commons.validator.Var;
37
38 /***
39 * Basic utility methods.
40 * <p>
41 * The use of FastHashMap is deprecated and will be replaced in a future
42 * release.
43 * </p>
44 */
45 public class ValidatorUtils {
46
47 /***
48 * <p>Replace part of a <code>String</code> with another value.</p>
49 *
50 * @param value <code>String</code> to perform the replacement on.
51 * @param key The name of the constant.
52 * @param replaceValue The value of the constant.
53 *
54 * @return The modified value.
55 */
56 public static String replace(String value, String key, String replaceValue) {
57
58 if (value == null || key == null || replaceValue == null) {
59 return value;
60 }
61
62 int pos = value.indexOf(key);
63
64 if (pos < 0) {
65 return value;
66 }
67
68 int length = value.length();
69 int start = pos;
70 int end = pos + key.length();
71
72 if (length == key.length()) {
73 value = replaceValue;
74
75 } else if (end == length) {
76 value = value.substring(0, start) + replaceValue;
77
78 } else {
79 value =
80 value.substring(0, start)
81 + replaceValue
82 + replace(value.substring(end), key, replaceValue);
83 }
84
85 return value;
86 }
87
88 /***
89 * Convenience method for getting a value from a bean property as a
90 * <code>String</code>. If the property is a <code>String[]</code> or
91 * <code>Collection</code> and it is empty, an empty <code>String</code>
92 * "" is returned. Otherwise, property.toString() is returned. This method
93 * may return <code>null</code> if there was an error retrieving the
94 * property.
95 *
96 * @param bean The bean object.
97 * @param property The name of the property to access.
98 *
99 * @return The value of the property.
100 */
101 public static String getValueAsString(Object bean, String property) {
102 Object value = null;
103
104 try {
105 value = PropertyUtils.getProperty(bean, property);
106
107 } catch(IllegalAccessException e) {
108 Log log = LogFactory.getLog(ValidatorUtils.class);
109 log.error(e.getMessage(), e);
110 } catch(InvocationTargetException e) {
111 Log log = LogFactory.getLog(ValidatorUtils.class);
112 log.error(e.getMessage(), e);
113 } catch(NoSuchMethodException e) {
114 Log log = LogFactory.getLog(ValidatorUtils.class);
115 log.error(e.getMessage(), e);
116 }
117
118 if (value == null) {
119 return null;
120 }
121
122 if (value instanceof String[]) {
123 return ((String[]) value).length > 0 ? value.toString() : "";
124
125 } else if (value instanceof Collection) {
126 return ((Collection) value).isEmpty() ? "" : value.toString();
127
128 } else {
129 return value.toString();
130 }
131
132 }
133
134 /***
135 * Makes a deep copy of a <code>FastHashMap</code> if the values
136 * are <code>Msg</code>, <code>Arg</code>,
137 * or <code>Var</code>. Otherwise it is a shallow copy.
138 *
139 * @param map <code>FastHashMap</code> to copy.
140 * @return FastHashMap A copy of the <code>FastHashMap</code> that was
141 * passed in.
142 * @deprecated This method is not part of Validator's public API. Validator
143 * will use it internally until FastHashMap references are removed. Use
144 * copyMap() instead.
145 */
146 public static FastHashMap copyFastHashMap(FastHashMap map) {
147 FastHashMap results = new FastHashMap();
148
149 Iterator i = map.keySet().iterator();
150 while (i.hasNext()) {
151 String key = (String) i.next();
152 Object value = map.get(key);
153
154 if (value instanceof Msg) {
155 results.put(key, ((Msg) value).clone());
156 } else if (value instanceof Arg) {
157 results.put(key, ((Arg) value).clone());
158 } else if (value instanceof Var) {
159 results.put(key, ((Var) value).clone());
160 } else {
161 results.put(key, value);
162 }
163 }
164
165 results.setFast(true);
166 return results;
167 }
168
169 /***
170 * Makes a deep copy of a <code>Map</code> if the values are
171 * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>. Otherwise,
172 * it is a shallow copy.
173 *
174 * @param map The source Map to copy.
175 *
176 * @return A copy of the <code>Map</code> that was passed in.
177 */
178 public static Map copyMap(Map map) {
179 Map results = new HashMap();
180
181 Iterator iter = map.keySet().iterator();
182 while (iter.hasNext()) {
183 String key = (String) iter.next();
184 Object value = map.get(key);
185
186 if (value instanceof Msg) {
187 results.put(key, ((Msg) value).clone());
188 } else if (value instanceof Arg) {
189 results.put(key, ((Arg) value).clone());
190 } else if (value instanceof Var) {
191 results.put(key, ((Var) value).clone());
192 } else {
193 results.put(key, value);
194 }
195 }
196 return results;
197 }
198
199 }