View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator.util;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import org.apache.commons.beanutils.PropertyUtils;
26  import org.apache.commons.collections.FastHashMap;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.commons.validator.Arg;
30  import org.apache.commons.validator.Msg;
31  import org.apache.commons.validator.Var;
32  
33  /***
34   * Basic utility methods.
35   * <p>
36   * The use of FastHashMap is deprecated and will be replaced in a future
37   * release.
38   * </p>
39   *
40   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
41   */
42  public class ValidatorUtils {
43  
44      /***
45       * <p>Replace part of a <code>String</code> with another value.</p>
46       *
47       * @param value <code>String</code> to perform the replacement on.
48       * @param key The name of the constant.
49       * @param replaceValue The value of the constant.
50       *
51       * @return The modified value.
52       */
53      public static String replace(String value, String key, String replaceValue) {
54  
55          if (value == null || key == null || replaceValue == null) {
56              return value;
57          }
58  
59          int pos = value.indexOf(key);
60  
61          if (pos < 0) {
62              return value;
63          }
64  
65          int length = value.length();
66          int start = pos;
67          int end = pos + key.length();
68  
69          if (length == key.length()) {
70              value = replaceValue;
71  
72          } else if (end == length) {
73              value = value.substring(0, start) + replaceValue;
74  
75          } else {
76              value =
77                      value.substring(0, start)
78                      + replaceValue
79                      + replace(value.substring(end), key, replaceValue);
80          }
81  
82          return value;
83      }
84  
85      /***
86       * Convenience method for getting a value from a bean property as a
87       * <code>String</code>.  If the property is a <code>String[]</code> or
88       * <code>Collection</code> and it is empty, an empty <code>String</code>
89       * "" is returned.  Otherwise, property.toString() is returned.  This method
90       * may return <code>null</code> if there was an error retrieving the
91       * property.
92       *
93       * @param bean The bean object.
94       * @param property The name of the property to access.
95       *
96       * @return The value of the property.
97       */
98      public static String getValueAsString(Object bean, String property) {
99          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 }