1 /*
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 $
3 * $Revision: 1.7.2.1 $
4 * $Date: 2004/06/22 02:24:38 $
5 *
6 * ====================================================================
7 * Copyright 2001-2004 The Apache Software Foundation
8 *
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
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
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.
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.Iterator;
27
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;
35
36 /***
37 * Basic utility methods.
38 *
39 * The use of FastHashMap is deprecated and will be replaced in a future
40 * release.
41 */
42 public class ValidatorUtils {
43
44 /***
45 * Logger.
46 */
47 private static Log log = LogFactory.getLog(ValidatorUtils.class);
48
49 /***
50 * <p>Replace part of a <code>String</code> with another value.</p>
51 *
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.
55 */
56 public static String replace(
57 String value,
58 String key,
59 String replaceValue) {
60
61 if (value == null || key == null || replaceValue == null) {
62 return value;
63 }
64
65 int pos = value.indexOf(key);
66
67 if (pos < 0) {
68 return value;
69 }
70
71 int length = value.length();
72 int start = pos;
73 int end = pos + key.length();
74
75 if (length == key.length()) {
76 value = replaceValue;
77
78 } else if (end == length) {
79 value = value.substring(0, start) + replaceValue;
80
81 } else {
82 value =
83 value.substring(0, start)
84 + replaceValue
85 + replace(value.substring(end), key, replaceValue);
86 }
87
88 return value;
89 }
90
91 /***
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
97 * property.
98 * @param bean
99 * @param 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.error(e.getMessage(), e);
109 } catch(InvocationTargetException e) {
110 log.error(e.getMessage(), e);
111 } catch(NoSuchMethodException e) {
112 log.error(e.getMessage(), e);
113 }
114
115 if (value == null) {
116 return null;
117 }
118
119 if (value instanceof String[]) {
120 return ((String[]) value).length > 0 ? value.toString() : "";
121
122 } else if (value instanceof Collection) {
123 return ((Collection) value).isEmpty() ? "" : value.toString();
124
125 } else {
126 return value.toString();
127 }
128
129 }
130
131 /***
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.
135 *
136 * @param map <code>FastHashMap</code> to copy.
137 * @return FastHashMap A copy of the <code>FastHashMap</code> that was
138 * passed in.
139 */
140 public static FastHashMap copyFastHashMap(FastHashMap map) {
141 FastHashMap results = new FastHashMap();
142
143 Iterator i = map.keySet().iterator();
144 while (i.hasNext()) {
145 String key = (String) i.next();
146 Object value = map.get(key);
147
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());
154 } else {
155 results.put(key, value);
156 }
157 }
158
159 results.setFast(true);
160
161 return results;
162 }
163
164 }
This page was automatically generated by Maven