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: 658783 $ $Date: 2008-05-21 19:44:18 +0200 (Mi, 21. Mai 2008) $
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, String replaceValue) {
054    
055            if (value == null || key == null || replaceValue == null) {
056                return value;
057            }
058    
059            int pos = value.indexOf(key);
060    
061            if (pos < 0) {
062                return value;
063            }
064    
065            int length = value.length();
066            int start = pos;
067            int end = pos + key.length();
068    
069            if (length == key.length()) {
070                value = replaceValue;
071    
072            } else if (end == length) {
073                value = value.substring(0, start) + replaceValue;
074    
075            } else {
076                value =
077                        value.substring(0, start)
078                        + replaceValue
079                        + replace(value.substring(end), key, replaceValue);
080            }
081    
082            return value;
083        }
084    
085        /**
086         * Convenience method for getting a value from a bean property as a
087         * <code>String</code>.  If the property is a <code>String[]</code> or
088         * <code>Collection</code> and it is empty, an empty <code>String</code>
089         * "" is returned.  Otherwise, property.toString() is returned.  This method
090         * may return <code>null</code> if there was an error retrieving the
091         * property.
092         *
093         * @param bean The bean object.
094         * @param property The name of the property to access.
095         *
096         * @return The value of the property.
097         */
098        public static String getValueAsString(Object bean, String property) {
099            Object value = null;
100    
101            try {
102                value = PropertyUtils.getProperty(bean, property);
103    
104            } catch(IllegalAccessException e) {
105                Log log = LogFactory.getLog(ValidatorUtils.class);
106                log.error(e.getMessage(), e);
107            } catch(InvocationTargetException e) {
108                Log log = LogFactory.getLog(ValidatorUtils.class);
109                log.error(e.getMessage(), e);
110            } catch(NoSuchMethodException e) {
111                Log log = LogFactory.getLog(ValidatorUtils.class);
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         * @deprecated This method is not part of Validator's public API.  Validator
140         * will use it internally until FastHashMap references are removed.  Use
141         * copyMap() instead.
142         */
143        public static FastHashMap copyFastHashMap(FastHashMap map) {
144            FastHashMap results = new FastHashMap();
145    
146            Iterator i = map.keySet().iterator();
147            while (i.hasNext()) {
148                String key = (String) i.next();
149                Object value = map.get(key);
150    
151                if (value instanceof Msg) {
152                    results.put(key, ((Msg) value).clone());
153                } else if (value instanceof Arg) {
154                    results.put(key, ((Arg) value).clone());
155                } else if (value instanceof Var) {
156                    results.put(key, ((Var) value).clone());
157                } else {
158                    results.put(key, value);
159                }
160            }
161    
162            results.setFast(true);
163            return results;
164        }
165        
166        /**
167         * Makes a deep copy of a <code>Map</code> if the values are 
168         * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>.  Otherwise, 
169         * it is a shallow copy.
170         *
171         * @param map The source Map to copy.
172         *
173         * @return A copy of the <code>Map</code> that was passed in.
174         */
175        public static Map copyMap(Map map) {
176            Map results = new HashMap();
177    
178            Iterator iter = map.keySet().iterator();
179            while (iter.hasNext()) {
180                String key = (String) iter.next();
181                Object value = map.get(key);
182    
183                if (value instanceof Msg) {
184                    results.put(key, ((Msg) value).clone());
185                } else if (value instanceof Arg) {
186                    results.put(key, ((Arg) value).clone());
187                } else if (value instanceof Var) {
188                    results.put(key, ((Var) value).clone());
189                } else {
190                    results.put(key, value);
191                }
192            }
193            return results;
194        }
195    
196    }