Coverage Report - org.apache.commons.validator.util.ValidatorUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidatorUtils
30%
19/63
26%
10/38
7.75
 
 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  
 import java.util.Map.Entry;
 25  
 
 26  
 import org.apache.commons.beanutils.PropertyUtils;
 27  
 import org.apache.commons.collections.FastHashMap; // DEPRECATED
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 import org.apache.commons.validator.Arg;
 31  
 import org.apache.commons.validator.Msg;
 32  
 import org.apache.commons.validator.Var;
 33  
 
 34  
 /**
 35  
  * Basic utility methods.
 36  
  * <p>
 37  
  * The use of FastHashMap is deprecated and will be replaced in a future
 38  
  * release.
 39  
  * </p>
 40  
  *
 41  
  * @version $Revision: 1739361 $
 42  
  */
 43  0
 public class ValidatorUtils {
 44  
 
 45  1
     private static final Log LOG = LogFactory.getLog(ValidatorUtils.class);
 46  
 
 47  
     /**
 48  
      * <p>Replace part of a <code>String</code> with another value.</p>
 49  
      *
 50  
      * @param value <code>String</code> to perform the replacement on.
 51  
      * @param key The name of the constant.
 52  
      * @param replaceValue The value of the constant.
 53  
      *
 54  
      * @return The modified value.
 55  
      */
 56  
     public static String replace(String value, String key, String replaceValue) {
 57  
 
 58  180
         if (value == null || key == null || replaceValue == null) {
 59  0
             return value;
 60  
         }
 61  
 
 62  180
         int pos = value.indexOf(key);
 63  
 
 64  180
         if (pos < 0) {
 65  140
             return value;
 66  
         }
 67  
 
 68  40
         int length = value.length();
 69  40
         int start = pos;
 70  40
         int end = pos + key.length();
 71  
 
 72  40
         if (length == key.length()) {
 73  40
             value = replaceValue;
 74  
 
 75  0
         } else if (end == length) {
 76  0
             value = value.substring(0, start) + replaceValue;
 77  
 
 78  
         } else {
 79  0
             value =
 80  
                     value.substring(0, start)
 81  
                     + replaceValue
 82  
                     + replace(value.substring(end), key, replaceValue);
 83  
         }
 84  
 
 85  40
         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  
      * @param bean The bean object.
 97  
      * @param property The name of the property to access.
 98  
      *
 99  
      * @return The value of the property.
 100  
      */
 101  
     public static String getValueAsString(Object bean, String property) {
 102  210
         Object value = null;
 103  
 
 104  
         try {
 105  210
             value = PropertyUtils.getProperty(bean, property);
 106  
 
 107  0
         } catch(IllegalAccessException e) {
 108  0
             LOG.error(e.getMessage(), e);
 109  0
         } catch(InvocationTargetException e) {
 110  0
             LOG.error(e.getMessage(), e);
 111  0
         } catch(NoSuchMethodException e) {
 112  0
             LOG.error(e.getMessage(), e);
 113  210
         }
 114  
 
 115  210
         if (value == null) {
 116  47
             return null;
 117  
         }
 118  
 
 119  163
         if (value instanceof String[]) {
 120  0
             return ((String[]) value).length > 0 ? value.toString() : "";
 121  
 
 122  163
         } else if (value instanceof Collection) {
 123  0
             return ((Collection<?>) value).isEmpty() ? "" : value.toString();
 124  
 
 125  
         } else {
 126  163
             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  
     @Deprecated
 144  
     public static FastHashMap copyFastHashMap(FastHashMap map) {
 145  0
         FastHashMap results = new FastHashMap();
 146  
 
 147  
         @SuppressWarnings("unchecked") // FastHashMap is not generic
 148  0
         Iterator<Entry<String, ?>> i = map.entrySet().iterator();
 149  0
         while (i.hasNext()) {
 150  0
             Entry<String, ?> entry = i.next();
 151  0
             String key = entry.getKey();
 152  0
             Object value = entry.getValue();
 153  
 
 154  0
             if (value instanceof Msg) {
 155  0
                 results.put(key, ((Msg) value).clone());
 156  0
             } else if (value instanceof Arg) {
 157  0
                 results.put(key, ((Arg) value).clone());
 158  0
             } else if (value instanceof Var) {
 159  0
                 results.put(key, ((Var) value).clone());
 160  
             } else {
 161  0
                 results.put(key, value);
 162  
             }
 163  0
         }
 164  
 
 165  0
         results.setFast(true);
 166  0
         return results;
 167  
     }
 168  
 
 169  
     /**
 170  
      * Makes a deep copy of a <code>Map</code> if the values are
 171  
      * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>.  Otherwise,
 172  
      * it is a shallow copy.
 173  
      *
 174  
      * @param map The source Map to copy.
 175  
      *
 176  
      * @return A copy of the <code>Map</code> that was passed in.
 177  
      */
 178  
     public static Map<String, Object> copyMap(Map<String, Object> map) {
 179  0
         Map<String, Object> results = new HashMap<String, Object>();
 180  
 
 181  0
         Iterator<Entry<String, Object>> i = map.entrySet().iterator();
 182  0
         while (i.hasNext()) {
 183  0
             Entry<String, Object> entry = i.next();
 184  0
             String key = entry.getKey();
 185  0
             Object value = entry.getValue();
 186  
 
 187  0
             if (value instanceof Msg) {
 188  0
                 results.put(key, ((Msg) value).clone());
 189  0
             } else if (value instanceof Arg) {
 190  0
                 results.put(key, ((Arg) value).clone());
 191  0
             } else if (value instanceof Var) {
 192  0
                 results.put(key, ((Var) value).clone());
 193  
             } else {
 194  0
                 results.put(key, value);
 195  
             }
 196  0
         }
 197  0
         return results;
 198  
     }
 199  
 
 200  
 }