Coverage report

  %line %branch
org.apache.commons.validator.util.ValidatorUtils
30% 
57% 

 1  
 /*
 2  
  * $Id: ValidatorUtils.java 384656 2006-03-10 00:36:37Z niallp $
 3  
  * $Rev: 384656 $
 4  
  * $Date: 2006-03-10 00:36:37 +0000 (Fri, 10 Mar 2006) $
 5  
  *
 6  
  * ====================================================================
 7  
  * Copyright 2001-2006 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  0
 public class ValidatorUtils {
 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 == class="keyword">null || replaceValue == class="keyword">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  175
         Object value = null;
 103  
 
 104  
         try {
 105  175
             value = PropertyUtils.getProperty(bean, property);
 106  
 
 107  175
         } catch(IllegalAccessException e) {
 108  0
             Log log = LogFactory.getLog(ValidatorUtils.class);
 109  0
             log.error(e.getMessage(), e);
 110  0
         } catch(InvocationTargetException e) {
 111  0
             Log log = LogFactory.getLog(ValidatorUtils.class);
 112  0
             log.error(e.getMessage(), e);
 113  0
         } catch(NoSuchMethodException e) {
 114  0
             Log log = LogFactory.getLog(ValidatorUtils.class);
 115  0
             log.error(e.getMessage(), e);
 116  
         }
 117  
 
 118  175
         if (value == null) {
 119  34
             return null;
 120  
         }
 121  
 
 122  141
         if (value instanceof String[]) {
 123  0
             return ((String[]) value).length > 0 ? value.toString() : "";
 124  
 
 125  141
         } else if (value instanceof Collection) {
 126  0
             return ((Collection) value).isEmpty() ? "" : value.toString();
 127  
 
 128  
         } else {
 129  141
             return value.toString();
 130  
         }
 131  
 
 132  
     }
 133  
 
 134  
     /**
 135  
      * Makes a deep copy of a <code>FastHashMap</code> if the values
 136  
      * are <code>Msg</code>, <code>Arg</code>,
 137  
      * or <code>Var</code>.  Otherwise it is a shallow copy.
 138  
      *
 139  
      * @param map <code>FastHashMap</code> to copy.
 140  
      * @return FastHashMap A copy of the <code>FastHashMap</code> that was
 141  
      * passed in.
 142  
      * @deprecated This method is not part of Validator's public API.  Validator
 143  
      * will use it internally until FastHashMap references are removed.  Use
 144  
      * copyMap() instead.
 145  
      */
 146  
     public static FastHashMap copyFastHashMap(FastHashMap map) {
 147  0
         FastHashMap results = new FastHashMap();
 148  
 
 149  0
         Iterator i = map.keySet().iterator();
 150  0
         while (i.hasNext()) {
 151  0
             String key = (String) i.next();
 152  0
             Object value = map.get(key);
 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  
         }
 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 copyMap(Map map) {
 179  0
         Map results = new HashMap();
 180  
 
 181  0
         Iterator iter = map.keySet().iterator();
 182  0
         while (iter.hasNext()) {
 183  0
             String key = (String) iter.next();
 184  0
             Object value = map.get(key);
 185  
 
 186  0
             if (value instanceof Msg) {
 187  0
                 results.put(key, ((Msg) value).clone());
 188  0
             } else if (value instanceof Arg) {
 189  0
                 results.put(key, ((Arg) value).clone());
 190  0
             } else if (value instanceof Var) {
 191  0
                 results.put(key, ((Var) value).clone());
 192  
             } else {
 193  0
                 results.put(key, value);
 194  
             }
 195  
         }
 196  0
         return results;
 197  
     }
 198  
 
 199  
 }

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