View Javadoc

1   /*
2    * $Id: ValidatorUtils.java 280974 2005-09-15 00:06:59Z niallp $
3    * $Rev: 280974 $
4    * $Date: 2005-09-15 01:06:59 +0100 (Thu, 15 Sep 2005) $
5    *
6    * ====================================================================
7    * Copyright 2001-2005 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.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  import org.apache.commons.beanutils.PropertyUtils;
31  import org.apache.commons.collections.FastHashMap;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.commons.validator.Arg;
35  import org.apache.commons.validator.Msg;
36  import org.apache.commons.validator.Var;
37  
38  /***
39   * Basic utility methods.
40   * <p>
41   * The use of FastHashMap is deprecated and will be replaced in a future
42   * release.
43   * </p>
44   */
45  public class ValidatorUtils {
46  
47      private static final 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       * @return The modified value.
57       */
58      public static String replace(String value, String key, String replaceValue) {
59  
60          if (value == null || key == null || replaceValue == null) {
61              return value;
62          }
63  
64          int pos = value.indexOf(key);
65  
66          if (pos < 0) {
67              return value;
68          }
69  
70          int length = value.length();
71          int start = pos;
72          int end = pos + key.length();
73  
74          if (length == key.length()) {
75              value = replaceValue;
76  
77          } else if (end == length) {
78              value = value.substring(0, start) + replaceValue;
79  
80          } else {
81              value =
82                      value.substring(0, start)
83                      + replaceValue
84                      + replace(value.substring(end), key, replaceValue);
85          }
86  
87          return value;
88      }
89  
90      /***
91       * Convenience method for getting a value from a bean property as a
92       * <code>String</code>.  If the property is a <code>String[]</code> or
93       * <code>Collection</code> and it is empty, an empty <code>String</code>
94       * "" is returned.  Otherwise, property.toString() is returned.  This method
95       * may return <code>null</code> if there was an error retrieving the
96       * property.
97       *
98       * @param bean The bean object.
99       * @param property The name of the property to access.
100      *
101      * @return The value of the property.
102      */
103     public static String getValueAsString(Object bean, String property) {
104         Object value = null;
105 
106         try {
107             value = PropertyUtils.getProperty(bean, property);
108 
109         } catch(IllegalAccessException e) {
110             log.error(e.getMessage(), e);
111         } catch(InvocationTargetException e) {
112             log.error(e.getMessage(), e);
113         } catch(NoSuchMethodException e) {
114             log.error(e.getMessage(), e);
115         }
116 
117         if (value == null) {
118             return null;
119         }
120 
121         if (value instanceof String[]) {
122             return ((String[]) value).length > 0 ? value.toString() : "";
123 
124         } else if (value instanceof Collection) {
125             return ((Collection) value).isEmpty() ? "" : value.toString();
126 
127         } else {
128             return value.toString();
129         }
130 
131     }
132 
133     /***
134      * Makes a deep copy of a <code>FastHashMap</code> if the values
135      * are <code>Msg</code>, <code>Arg</code>,
136      * or <code>Var</code>.  Otherwise it is a shallow copy.
137      *
138      * @param map <code>FastHashMap</code> to copy.
139      * @return FastHashMap A copy of the <code>FastHashMap</code> that was
140      * passed in.
141      * @deprecated This method is not part of Validator's public API.  Validator
142      * will use it internally until FastHashMap references are removed.  Use
143      * copyMap() instead.
144      */
145     public static FastHashMap copyFastHashMap(FastHashMap map) {
146         FastHashMap results = new FastHashMap();
147 
148         Iterator i = map.keySet().iterator();
149         while (i.hasNext()) {
150             String key = (String) i.next();
151             Object value = map.get(key);
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 iter = map.keySet().iterator();
181         while (iter.hasNext()) {
182             String key = (String) iter.next();
183             Object value = map.get(key);
184 
185             if (value instanceof Msg) {
186                 results.put(key, ((Msg) value).clone());
187             } else if (value instanceof Arg) {
188                 results.put(key, ((Arg) value).clone());
189             } else if (value instanceof Var) {
190                 results.put(key, ((Var) value).clone());
191             } else {
192                 results.put(key, value);
193             }
194         }
195         return results;
196     }
197 
198 }