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.util.Collection;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Map.Entry;
24
25 import org.apache.commons.beanutils.PropertyUtils;
26 import org.apache.commons.collections.FastHashMap;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.commons.validator.Arg;
30 import org.apache.commons.validator.Msg;
31 import org.apache.commons.validator.Var;
32
33
34
35
36
37
38
39
40 public class ValidatorUtils {
41
42 private static final Log LOG = LogFactory.getLog(ValidatorUtils.class);
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @Deprecated
57 public static FastHashMap copyFastHashMap(final FastHashMap fastHashMap) {
58 final FastHashMap results = new FastHashMap();
59 @SuppressWarnings("unchecked")
60 final Iterator<Entry<String, ?>> iterator = fastHashMap.entrySet().iterator();
61 while (iterator.hasNext()) {
62 final Entry<String, ?> entry = iterator.next();
63 final String key = entry.getKey();
64 final Object value = entry.getValue();
65 if (value instanceof Msg) {
66 results.put(key, ((Msg) value).clone());
67 } else if (value instanceof Arg) {
68 results.put(key, ((Arg) value).clone());
69 } else if (value instanceof Var) {
70 results.put(key, ((Var) value).clone());
71 } else {
72 results.put(key, value);
73 }
74 }
75 results.setFast(true);
76 return results;
77 }
78
79
80
81
82
83
84
85
86
87 public static Map<String, Object> copyMap(final Map<String, Object> map) {
88 final Map<String, Object> results = new HashMap<>(map.size());
89 map.forEach((key, value) -> {
90 if (value instanceof Msg) {
91 results.put(key, ((Msg) value).clone());
92 } else if (value instanceof Arg) {
93 results.put(key, ((Arg) value).clone());
94 } else if (value instanceof Var) {
95 results.put(key, ((Var) value).clone());
96 } else {
97 results.put(key, value);
98 }
99 });
100 return results;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public static String getValueAsString(final Object bean, final String property) {
116 Object value = null;
117
118 try {
119 value = PropertyUtils.getProperty(bean, property);
120
121 } catch (final ReflectiveOperationException e) {
122 LOG.error(e.getMessage(), e);
123 }
124
125 if (value == null) {
126 return null;
127 }
128
129 if (value instanceof String[]) {
130 return ((String[]) value).length > 0 ? value.toString() : "";
131
132 }
133 if (value instanceof Collection) {
134 return ((Collection<?>) value).isEmpty() ? "" : value.toString();
135
136 }
137 return value.toString();
138
139 }
140
141
142
143
144
145
146
147
148
149 public static String replace(final String value, final String key, final String replaceValue) {
150 if (value == null || key == null || replaceValue == null) {
151 return value;
152 }
153 return value.replace(key, replaceValue);
154 }
155
156
157
158
159
160
161 @Deprecated
162 public ValidatorUtils() {
163
164 }
165
166 }