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