2
* $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/util/ValidatorUtils.java,v 1.7.2.1 2004/06/22 02:24:38 husted Exp $
4
* $Date: 2004/06/22 02:24:38 $
6
* ====================================================================
7
* Copyright 2001-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
22
package org.apache.commons.validator.util;
24
import java.lang.reflect.InvocationTargetException;
25
import java.util.Collection;
26
import java.util.Iterator;
28
import org.apache.commons.beanutils.PropertyUtils;
29
import org.apache.commons.collections.FastHashMap; // DEPRECATED
30
import org.apache.commons.logging.Log;
31
import org.apache.commons.logging.LogFactory;
32
import org.apache.commons.validator.Arg;
33
import org.apache.commons.validator.Msg;
34
import org.apache.commons.validator.Var;
37
* Basic utility methods.
39
* The use of FastHashMap is deprecated and will be replaced in a future
42
public class ValidatorUtils {
47
private static Log log = LogFactory.getLog(ValidatorUtils.class);
50
* <p>Replace part of a <code>String</code> with another value.</p>
52
* @param value <code>String</code> to perform the replacement on.
53
* @param key The name of the constant.
54
* @param replaceValue The value of the constant.
56
public static String replace(
59
String replaceValue) {
61
if (value == null || key == null || replaceValue == null) {
65
int pos = value.indexOf(key);
71
int length = value.length();
73
int end = pos + key.length();
75
if (length == key.length()) {
78
} else if (end == length) {
79
value = value.substring(0, start) + replaceValue;
83
value.substring(0, start)
85
+ replace(value.substring(end), key, replaceValue);
92
* Convenience method for getting a value from a bean property as a
93
* <code>String</code>. If the property is a <code>String[]</code> or
94
* <code>Collection</code> and it is empty, an empty <code>String</code>
95
* "" is returned. Otherwise, property.toString() is returned. This method
96
* may return <code>null</code> if there was an error retrieving the
101
public static String getValueAsString(Object bean, String property) {
105
value = PropertyUtils.getProperty(bean, property);
107
} catch(IllegalAccessException e) {
108
log.error(e.getMessage(), e);
109
} catch(InvocationTargetException e) {
110
log.error(e.getMessage(), e);
111
} catch(NoSuchMethodException e) {
112
log.error(e.getMessage(), e);
119
if (value instanceof String[]) {
120
return ((String[]) value).length > 0 ? value.toString() : "";
122
} else if (value instanceof Collection) {
123
return ((Collection) value).isEmpty() ? "" : value.toString();
126
return value.toString();
132
* Makes a deep copy of a <code>FastHashMap</code> if the values
133
* are <code>Msg</code>, <code>Arg</code>,
134
* or <code>Var</code>. Otherwise it is a shallow copy.
136
* @param map <code>FastHashMap</code> to copy.
137
* @return FastHashMap A copy of the <code>FastHashMap</code> that was
140
public static FastHashMap copyFastHashMap(FastHashMap map) {
141
FastHashMap results = new FastHashMap();
143
Iterator i = map.keySet().iterator();
144
while (i.hasNext()) {
145
String key = (String) i.next();
146
Object value = map.get(key);
148
if (value instanceof Msg) {
149
results.put(key, ((Msg) value).clone());
150
} else if (value instanceof Arg) {
151
results.put(key, ((Arg) value).clone());
152
} else if (value instanceof Var) {
153
results.put(key, ((Var) value).clone());
155
results.put(key, value);
159
results.setFast(true);