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 */
017package org.apache.commons.validator.util;
018
019import java.lang.reflect.InvocationTargetException;
020import java.util.Collection;
021import java.util.HashMap;
022import java.util.Iterator;
023import java.util.Map;
024import java.util.Map.Entry;
025
026import org.apache.commons.beanutils.PropertyUtils;
027import org.apache.commons.collections.FastHashMap;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.apache.commons.validator.Arg;
031import org.apache.commons.validator.Msg;
032import org.apache.commons.validator.Var;
033
034/**
035 * Basic utility methods.
036 * <p>
037 * The use of FastHashMap is deprecated and will be replaced in a future
038 * release.
039 * </p>
040 *
041 * @version $Revision: 1227703 $ $Date: 2012-01-05 18:15:19 +0100 (Do, 05 Jan 2012) $
042 */
043public class ValidatorUtils {
044
045    /**
046     * <p>Replace part of a <code>String</code> with another value.</p>
047     *
048     * @param value <code>String</code> to perform the replacement on.
049     * @param key The name of the constant.
050     * @param replaceValue The value of the constant.
051     *
052     * @return The modified value.
053     */
054    public static String replace(String value, String key, 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 =
078                    value.substring(0, start)
079                    + replaceValue
080                    + replace(value.substring(end), key, replaceValue);
081        }
082
083        return value;
084    }
085
086    /**
087     * Convenience method for getting a value from a bean property as a
088     * <code>String</code>.  If the property is a <code>String[]</code> or
089     * <code>Collection</code> and it is empty, an empty <code>String</code>
090     * "" is returned.  Otherwise, property.toString() is returned.  This method
091     * may return <code>null</code> if there was an error retrieving the
092     * property.
093     *
094     * @param bean The bean object.
095     * @param property The name of the property to access.
096     *
097     * @return The value of the property.
098     */
099    public static String getValueAsString(Object bean, String property) {
100        Object value = null;
101
102        try {
103            value = PropertyUtils.getProperty(bean, property);
104
105        } catch(IllegalAccessException e) {
106            Log log = LogFactory.getLog(ValidatorUtils.class);
107            log.error(e.getMessage(), e);
108        } catch(InvocationTargetException e) {
109            Log log = LogFactory.getLog(ValidatorUtils.class);
110            log.error(e.getMessage(), e);
111        } catch(NoSuchMethodException e) {
112            Log log = LogFactory.getLog(ValidatorUtils.class);
113            log.error(e.getMessage(), e);
114        }
115
116        if (value == null) {
117            return null;
118        }
119
120        if (value instanceof String[]) {
121            return ((String[]) value).length > 0 ? value.toString() : "";
122
123        } else if (value instanceof Collection) {
124            return ((Collection) value).isEmpty() ? "" : value.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.entrySet().iterator();
148        while (i.hasNext()) {
149            Entry entry = (Entry) i.next();
150            String key = (String) entry.getKey();
151            Object value = entry.getValue();
152
153            if (value instanceof Msg) {
154                results.put(key, ((Msg) value).clone());
155            } else if (value instanceof Arg) {
156                results.put(key, ((Arg) value).clone());
157            } else if (value instanceof Var) {
158                results.put(key, ((Var) value).clone());
159            } else {
160                results.put(key, value);
161            }
162        }
163
164        results.setFast(true);
165        return results;
166    }
167
168    /**
169     * Makes a deep copy of a <code>Map</code> if the values are
170     * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>.  Otherwise,
171     * it is a shallow copy.
172     *
173     * @param map The source Map to copy.
174     *
175     * @return A copy of the <code>Map</code> that was passed in.
176     */
177    public static Map copyMap(Map map) {
178        Map results = new HashMap();
179
180        Iterator i = map.entrySet().iterator();
181        while (i.hasNext()) {
182            Entry entry = (Entry) i.next();
183            String key = (String) entry.getKey();
184            Object value = entry.getValue();
185
186            if (value instanceof Msg) {
187                results.put(key, ((Msg) value).clone());
188            } else if (value instanceof Arg) {
189                results.put(key, ((Arg) value).clone());
190            } else if (value instanceof Var) {
191                results.put(key, ((Var) value).clone());
192            } else {
193                results.put(key, value);
194            }
195        }
196        return results;
197    }
198
199}