KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > util > ValidatorUtils


1 /*
2  * $Id: ValidatorUtils.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 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 JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
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     public static String JavaDoc replace(String JavaDoc value, String JavaDoc key, String JavaDoc replaceValue) {
57
58         if (value == null || key == null || replaceValue == null) {
59             return value;
60         }
61
62         int pos = value.indexOf(key);
63
64         if (pos < 0) {
65             return value;
66         }
67
68         int length = value.length();
69         int start = pos;
70         int end = pos + key.length();
71
72         if (length == key.length()) {
73             value = replaceValue;
74
75         } else if (end == length) {
76             value = value.substring(0, start) + replaceValue;
77
78         } else {
79             value =
80                     value.substring(0, start)
81                     + replaceValue
82                     + replace(value.substring(end), key, replaceValue);
83         }
84
85         return value;
86     }
87
88     /**
89      * Convenience method for getting a value from a bean property as a
90      * <code>String</code>. If the property is a <code>String[]</code> or
91      * <code>Collection</code> and it is empty, an empty <code>String</code>
92      * "" is returned. Otherwise, property.toString() is returned. This method
93      * may return <code>null</code> if there was an error retrieving the
94      * property.
95      */

96     public static String JavaDoc getValueAsString(Object JavaDoc bean, String JavaDoc property) {
97         Object JavaDoc value = null;
98
99         try {
100             value = PropertyUtils.getProperty(bean, property);
101
102         } catch(IllegalAccessException JavaDoc e) {
103             log.error(e.getMessage(), e);
104         } catch(InvocationTargetException JavaDoc e) {
105             log.error(e.getMessage(), e);
106         } catch(NoSuchMethodException JavaDoc e) {
107             log.error(e.getMessage(), e);
108         }
109
110         if (value == null) {
111             return null;
112         }
113
114         if (value instanceof String JavaDoc[]) {
115             return ((String JavaDoc[]) value).length > 0 ? value.toString() : "";
116
117         } else if (value instanceof Collection JavaDoc) {
118             return ((Collection JavaDoc) value).isEmpty() ? "" : value.toString();
119
120         } else {
121             return value.toString();
122         }
123
124     }
125
126     /**
127      * Makes a deep copy of a <code>FastHashMap</code> if the values
128      * are <code>Msg</code>, <code>Arg</code>,
129      * or <code>Var</code>. Otherwise it is a shallow copy.
130      *
131      * @param map <code>FastHashMap</code> to copy.
132      * @return FastHashMap A copy of the <code>FastHashMap</code> that was
133      * passed in.
134      * @deprecated This method is not part of Validator's public API. Validator
135      * will use it internally until FastHashMap references are removed. Use
136      * copyMap() instead.
137      */

138     public static FastHashMap copyFastHashMap(FastHashMap map) {
139         FastHashMap results = new FastHashMap();
140
141         Iterator JavaDoc i = map.keySet().iterator();
142         while (i.hasNext()) {
143             String JavaDoc key = (String JavaDoc) i.next();
144             Object JavaDoc value = map.get(key);
145
146             if (value instanceof Msg) {
147                 results.put(key, ((Msg) value).clone());
148             } else if (value instanceof Arg) {
149                 results.put(key, ((Arg) value).clone());
150             } else if (value instanceof Var) {
151                 results.put(key, ((Var) value).clone());
152             } else {
153                 results.put(key, value);
154             }
155         }
156
157         results.setFast(true);
158         return results;
159     }
160     
161     /**
162      * Makes a deep copy of a <code>Map</code> if the values are
163      * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>. Otherwise,
164      * it is a shallow copy.
165      *
166      * @return A copy of the <code>Map</code> that was passed in.
167      */

168     public static Map JavaDoc copyMap(Map JavaDoc map) {
169         Map JavaDoc results = new HashMap JavaDoc();
170
171         Iterator JavaDoc iter = map.keySet().iterator();
172         while (iter.hasNext()) {
173             String JavaDoc key = (String JavaDoc) iter.next();
174             Object JavaDoc value = map.get(key);
175
176             if (value instanceof Msg) {
177                 results.put(key, ((Msg) value).clone());
178             } else if (value instanceof Arg) {
179                 results.put(key, ((Arg) value).clone());
180             } else if (value instanceof Var) {
181                 results.put(key, ((Var) value).clone());
182             } else {
183                 results.put(key, value);
184             }
185         }
186         return results;
187     }
188
189 }
190
Popular Tags