1 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 45 public class ValidatorUtils { 46 47 private static final Log log = LogFactory.getLog(ValidatorUtils.class); 48 49 56 public static String replace(String value, String key, String 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 96 public static String getValueAsString(Object bean, String property) { 97 Object value = null; 98 99 try { 100 value = PropertyUtils.getProperty(bean, property); 101 102 } catch(IllegalAccessException e) { 103 log.error(e.getMessage(), e); 104 } catch(InvocationTargetException e) { 105 log.error(e.getMessage(), e); 106 } catch(NoSuchMethodException e) { 107 log.error(e.getMessage(), e); 108 } 109 110 if (value == null) { 111 return null; 112 } 113 114 if (value instanceof String []) { 115 return ((String []) value).length > 0 ? value.toString() : ""; 116 117 } else if (value instanceof Collection ) { 118 return ((Collection ) value).isEmpty() ? "" : value.toString(); 119 120 } else { 121 return value.toString(); 122 } 123 124 } 125 126 138 public static FastHashMap copyFastHashMap(FastHashMap map) { 139 FastHashMap results = new FastHashMap(); 140 141 Iterator i = map.keySet().iterator(); 142 while (i.hasNext()) { 143 String key = (String ) i.next(); 144 Object 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 168 public static Map copyMap(Map map) { 169 Map results = new HashMap (); 170 171 Iterator iter = map.keySet().iterator(); 172 while (iter.hasNext()) { 173 String key = (String ) iter.next(); 174 Object 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 |