001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator.util;
018:
019: import java.lang.reflect.InvocationTargetException;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.commons.beanutils.PropertyUtils;
026: import org.apache.commons.collections.FastHashMap;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.commons.validator.Arg;
030: import org.apache.commons.validator.Msg;
031: import org.apache.commons.validator.Var;
032:
033: /**
034: * Basic utility methods.
035: * <p>
036: * The use of FastHashMap is deprecated and will be replaced in a future
037: * release.
038: * </p>
039: *
040: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
041: */
042: public class ValidatorUtils {
043:
044: /**
045: * <p>Replace part of a <code>String</code> with another value.</p>
046: *
047: * @param value <code>String</code> to perform the replacement on.
048: * @param key The name of the constant.
049: * @param replaceValue The value of the constant.
050: *
051: * @return The modified value.
052: */
053: public static String replace(String value, String key,
054: String replaceValue) {
055:
056: if (value == null || key == null || replaceValue == null) {
057: return value;
058: }
059:
060: int pos = value.indexOf(key);
061:
062: if (pos < 0) {
063: return value;
064: }
065:
066: int length = value.length();
067: int start = pos;
068: int end = pos + key.length();
069:
070: if (length == key.length()) {
071: value = replaceValue;
072:
073: } else if (end == length) {
074: value = value.substring(0, start) + replaceValue;
075:
076: } else {
077: value = value.substring(0, start) + replaceValue
078: + replace(value.substring(end), key, replaceValue);
079: }
080:
081: return value;
082: }
083:
084: /**
085: * Convenience method for getting a value from a bean property as a
086: * <code>String</code>. If the property is a <code>String[]</code> or
087: * <code>Collection</code> and it is empty, an empty <code>String</code>
088: * "" is returned. Otherwise, property.toString() is returned. This method
089: * may return <code>null</code> if there was an error retrieving the
090: * property.
091: *
092: * @param bean The bean object.
093: * @param property The name of the property to access.
094: *
095: * @return The value of the property.
096: */
097: public static String getValueAsString(Object bean, String property) {
098: Object value = null;
099:
100: try {
101: value = PropertyUtils.getProperty(bean, property);
102:
103: } catch (IllegalAccessException e) {
104: Log log = LogFactory.getLog(ValidatorUtils.class);
105: log.error(e.getMessage(), e);
106: } catch (InvocationTargetException e) {
107: Log log = LogFactory.getLog(ValidatorUtils.class);
108: log.error(e.getMessage(), e);
109: } catch (NoSuchMethodException e) {
110: Log log = LogFactory.getLog(ValidatorUtils.class);
111: log.error(e.getMessage(), e);
112: }
113:
114: if (value == null) {
115: return null;
116: }
117:
118: if (value instanceof String[]) {
119: return ((String[]) value).length > 0 ? value.toString()
120: : "";
121:
122: } else if (value instanceof Collection) {
123: return ((Collection) value).isEmpty() ? "" : value
124: .toString();
125:
126: } else {
127: return value.toString();
128: }
129:
130: }
131:
132: /**
133: * Makes a deep copy of a <code>FastHashMap</code> if the values
134: * are <code>Msg</code>, <code>Arg</code>,
135: * or <code>Var</code>. Otherwise it is a shallow copy.
136: *
137: * @param map <code>FastHashMap</code> to copy.
138: * @return FastHashMap A copy of the <code>FastHashMap</code> that was
139: * passed in.
140: * @deprecated This method is not part of Validator's public API. Validator
141: * will use it internally until FastHashMap references are removed. Use
142: * copyMap() instead.
143: */
144: public static FastHashMap copyFastHashMap(FastHashMap map) {
145: FastHashMap results = new FastHashMap();
146:
147: Iterator i = map.keySet().iterator();
148: while (i.hasNext()) {
149: String key = (String) i.next();
150: Object value = map.get(key);
151:
152: if (value instanceof Msg) {
153: results.put(key, ((Msg) value).clone());
154: } else if (value instanceof Arg) {
155: results.put(key, ((Arg) value).clone());
156: } else if (value instanceof Var) {
157: results.put(key, ((Var) value).clone());
158: } else {
159: results.put(key, value);
160: }
161: }
162:
163: results.setFast(true);
164: return results;
165: }
166:
167: /**
168: * Makes a deep copy of a <code>Map</code> if the values are
169: * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>. Otherwise,
170: * it is a shallow copy.
171: *
172: * @param map The source Map to copy.
173: *
174: * @return A copy of the <code>Map</code> that was passed in.
175: */
176: public static Map copyMap(Map map) {
177: Map results = new HashMap();
178:
179: Iterator iter = map.keySet().iterator();
180: while (iter.hasNext()) {
181: String key = (String) iter.next();
182: Object value = map.get(key);
183:
184: if (value instanceof Msg) {
185: results.put(key, ((Msg) value).clone());
186: } else if (value instanceof Arg) {
187: results.put(key, ((Arg) value).clone());
188: } else if (value instanceof Var) {
189: results.put(key, ((Var) value).clone());
190: } else {
191: results.put(key, value);
192: }
193: }
194: return results;
195: }
196:
197: }
|