Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.validator.util.ValidatorUtils |
30% |
58% |

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 | 0 |
     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 | 180 |
             if (value == null || key == class="keyword">null || replaceValue == class="keyword">null) {
56 | 0 |
                 return value;
57 |  |
             }
58 |  |
59 | 180 |
             int pos = value.indexOf(key);
60 |  |
61 | 180 |
             if (pos < 0) {
62 | 140 |
                 return value;
63 |  |
             }
64 |  |
65 | 40 |
             int length = value.length();
66 | 40 |
             int start = pos;
67 | 40 |
             int end = pos + key.length();
68 |  |
69 | 40 |
             if (length == key.length()) {
70 | 40 |
                 value = replaceValue;
71 |  |
72 | 0 |
             } else if (end == length) {
73 | 0 |
                 value = value.substring(0, start) + replaceValue;
74 |  |
75 |  |
             } else {
76 | 0 |
                 value =
77 |  |
                         value.substring(0, start)
78 |  |
                         + replaceValue
79 |  |
                         + replace(value.substring(end), key, replaceValue);
80 |  |
             }
81 |  |
82 | 40 |
             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 | 175 |
             Object value = null;
100 |  |
101 |  |
             try {
102 | 175 |
                 value = PropertyUtils.getProperty(bean, property);
103 |  |
104 | 175 |
             } catch(IllegalAccessException e) {
105 | 0 |
                 Log log = LogFactory.getLog(ValidatorUtils.class);
106 | 0 |
                 log.error(e.getMessage(), e);
107 | 0 |
             } catch(InvocationTargetException e) {
108 | 0 |
                 Log log = LogFactory.getLog(ValidatorUtils.class);
109 | 0 |
                 log.error(e.getMessage(), e);
110 | 0 |
             } catch(NoSuchMethodException e) {
111 | 0 |
                 Log log = LogFactory.getLog(ValidatorUtils.class);
112 | 0 |
                 log.error(e.getMessage(), e);
113 |  |
             }
114 |  |
115 | 175 |
             if (value == null) {
116 | 34 |
                 return null;
117 |  |
             }
118 |  |
119 | 141 |
             if (value instanceof String[]) {
120 | 0 |
                 return ((String[]) value).length > 0 ? value.toString() : "";
121 |  |
122 | 141 |
             } else if (value instanceof Collection) {
123 | 0 |
                 return ((Collection) value).isEmpty() ? "" : value.toString();
124 |  |
125 |  |
             } else {
126 | 141 |
                 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 | 0 |
             FastHashMap results = new FastHashMap();
145 |  |
146 | 0 |
             Iterator i = map.keySet().iterator();
147 | 0 |
             while (i.hasNext()) {
148 | 0 |
                 String key = (String) i.next();
149 | 0 |
                 Object value = map.get(key);
150 |  |
151 | 0 |
                 if (value instanceof Msg) {
152 | 0 |
                     results.put(key, ((Msg) value).clone());
153 | 0 |
                 } else if (value instanceof Arg) {
154 | 0 |
                     results.put(key, ((Arg) value).clone());
155 | 0 |
                 } else if (value instanceof Var) {
156 | 0 |
                     results.put(key, ((Var) value).clone());
157 |  |
                 } else {
158 | 0 |
                     results.put(key, value);
159 |  |
                 }
160 |  |
             }
161 |  |
162 | 0 |
             results.setFast(true);
163 | 0 |
             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 | 0 |
             Map results = new HashMap();
177 |  |
178 | 0 |
             Iterator iter = map.keySet().iterator();
179 | 0 |
             while (iter.hasNext()) {
180 | 0 |
                 String key = (String) iter.next();
181 | 0 |
                 Object value = map.get(key);
182 |  |
183 | 0 |
                 if (value instanceof Msg) {
184 | 0 |
                     results.put(key, ((Msg) value).clone());
185 | 0 |
                 } else if (value instanceof Arg) {
186 | 0 |
                     results.put(key, ((Arg) value).clone());
187 | 0 |
                 } else if (value instanceof Var) {
188 | 0 |
                     results.put(key, ((Var) value).clone());
189 |  |
                 } else {
190 | 0 |
                     results.put(key, value);
191 |  |
                 }
192 |  |
             }
193 | 0 |
             return results;
194 |  |
         }
195 |  |
196 |  |
     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.