001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.validator.util; 018 019import java.util.Collection; 020import java.util.HashMap; 021import java.util.Iterator; 022import java.util.Map; 023import java.util.Map.Entry; 024 025import org.apache.commons.beanutils.PropertyUtils; 026import org.apache.commons.collections.FastHashMap; // DEPRECATED 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.apache.commons.validator.Arg; 030import org.apache.commons.validator.Msg; 031import org.apache.commons.validator.Var; 032 033/** 034 * Basic utility methods. 035 * <p> 036 * The use of FastHashMap is deprecated and will be replaced in a future 037 * release. 038 * </p> 039 */ 040public class ValidatorUtils { 041 042 private static final Log LOG = LogFactory.getLog(ValidatorUtils.class); 043 044 /** 045 * Makes a deep copy of a <code>FastHashMap</code> if the values 046 * are <code>Msg</code>, <code>Arg</code>, 047 * or <code>Var</code>. Otherwise it is a shallow copy. 048 * 049 * @param fastHashMap <code>FastHashMap</code> to copy. 050 * @return FastHashMap A copy of the <code>FastHashMap</code> that was 051 * passed in. 052 * @deprecated This method is not part of Validator's public API. Validator 053 * will use it internally until FastHashMap references are removed. Use 054 * copyMap() instead. 055 */ 056 @Deprecated 057 public static FastHashMap copyFastHashMap(final FastHashMap fastHashMap) { 058 final FastHashMap results = new FastHashMap(); 059 @SuppressWarnings("unchecked") // FastHashMap is not generic 060 final Iterator<Entry<String, ?>> iterator = fastHashMap.entrySet().iterator(); 061 while (iterator.hasNext()) { 062 final Entry<String, ?> entry = iterator.next(); 063 final String key = entry.getKey(); 064 final Object value = entry.getValue(); 065 if (value instanceof Msg) { 066 results.put(key, ((Msg) value).clone()); 067 } else if (value instanceof Arg) { 068 results.put(key, ((Arg) value).clone()); 069 } else if (value instanceof Var) { 070 results.put(key, ((Var) value).clone()); 071 } else { 072 results.put(key, value); 073 } 074 } 075 results.setFast(true); 076 return results; 077 } 078 079 /** 080 * Makes a deep copy of a <code>Map</code> if the values are 081 * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>. Otherwise, 082 * it is a shallow copy. 083 * 084 * @param map The source Map to copy. 085 * 086 * @return A copy of the <code>Map</code> that was passed in. 087 */ 088 public static Map<String, Object> copyMap(final Map<String, Object> map) { 089 final Map<String, Object> results = new HashMap<>(map.size()); 090 map.forEach((key, value) -> { 091 if (value instanceof Msg) { 092 results.put(key, ((Msg) value).clone()); 093 } else if (value instanceof Arg) { 094 results.put(key, ((Arg) value).clone()); 095 } else if (value instanceof Var) { 096 results.put(key, ((Var) value).clone()); 097 } else { 098 results.put(key, value); 099 } 100 }); 101 return results; 102 } 103 104 /** 105 * Convenience method for getting a value from a bean property as a 106 * <code>String</code>. If the property is a <code>String[]</code> or 107 * <code>Collection</code> and it is empty, an empty <code>String</code> 108 * "" is returned. Otherwise, property.toString() is returned. This method 109 * may return {@code null} if there was an error retrieving the 110 * property. 111 * 112 * @param bean The bean object. 113 * @param property The name of the property to access. 114 * 115 * @return The value of the property. 116 */ 117 public static String getValueAsString(final Object bean, final String property) { 118 Object value = null; 119 120 try { 121 value = PropertyUtils.getProperty(bean, property); 122 123 } catch (final ReflectiveOperationException e) { 124 LOG.error(e.getMessage(), e); 125 } 126 127 if (value == null) { 128 return null; 129 } 130 131 if (value instanceof String[]) { 132 return ((String[]) value).length > 0 ? value.toString() : ""; 133 134 } 135 if (value instanceof Collection) { 136 return ((Collection<?>) value).isEmpty() ? "" : value.toString(); 137 138 } 139 return value.toString(); 140 141 } 142 143 /** 144 * <p>Replace part of a <code>String</code> with another value.</p> 145 * 146 * @param value <code>String</code> to perform the replacement on. 147 * @param key The name of the constant. 148 * @param replaceValue The value of the constant. 149 * 150 * @return The modified value. 151 */ 152 public static String replace(final String value, final String key, final String replaceValue) { 153 if (value == null || key == null || replaceValue == null) { 154 return value; 155 } 156 return value.replace(key, replaceValue); 157 } 158 159}