1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/util/ValidatorUtils.java,v 1.7.2.1 2004/06/22 02:24:38 husted Exp $
  3. * $Revision: 1.7.2.1 $
  4. * $Date: 2004/06/22 02:24:38 $
  5. *
  6. * ====================================================================
  7. * Copyright 2001-2004 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. package org.apache.commons.validator.util;
  22. import java.lang.reflect.InvocationTargetException;
  23. import java.util.Collection;
  24. import java.util.Iterator;
  25. import org.apache.commons.beanutils.PropertyUtils;
  26. import org.apache.commons.collections.FastHashMap; // DEPRECATED
  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. * Basic utility methods.
  34. *
  35. * The use of FastHashMap is deprecated and will be replaced in a future
  36. * release.
  37. */
  38. public class ValidatorUtils {
  39. /**
  40. * Logger.
  41. */
  42. private static Log log = LogFactory.getLog(ValidatorUtils.class);
  43. /**
  44. * <p>Replace part of a <code>String</code> with another value.</p>
  45. *
  46. * @param value <code>String</code> to perform the replacement on.
  47. * @param key The name of the constant.
  48. * @param replaceValue The value of the constant.
  49. */
  50. public static String replace(
  51. String value,
  52. String key,
  53. String replaceValue) {
  54. if (value == null || key == null || replaceValue == null) {
  55. return value;
  56. }
  57. int pos = value.indexOf(key);
  58. if (pos < 0) {
  59. return value;
  60. }
  61. int length = value.length();
  62. int start = pos;
  63. int end = pos + key.length();
  64. if (length == key.length()) {
  65. value = replaceValue;
  66. } else if (end == length) {
  67. value = value.substring(0, start) + replaceValue;
  68. } else {
  69. value =
  70. value.substring(0, start)
  71. + replaceValue
  72. + replace(value.substring(end), key, replaceValue);
  73. }
  74. return value;
  75. }
  76. /**
  77. * Convenience method for getting a value from a bean property as a
  78. * <code>String</code>. If the property is a <code>String[]</code> or
  79. * <code>Collection</code> and it is empty, an empty <code>String</code>
  80. * "" is returned. Otherwise, property.toString() is returned. This method
  81. * may return <code>null</code> if there was an error retrieving the
  82. * property.
  83. * @param bean
  84. * @param property
  85. */
  86. public static String getValueAsString(Object bean, String property) {
  87. Object value = null;
  88. try {
  89. value = PropertyUtils.getProperty(bean, property);
  90. } catch(IllegalAccessException e) {
  91. log.error(e.getMessage(), e);
  92. } catch(InvocationTargetException e) {
  93. log.error(e.getMessage(), e);
  94. } catch(NoSuchMethodException e) {
  95. log.error(e.getMessage(), e);
  96. }
  97. if (value == null) {
  98. return null;
  99. }
  100. if (value instanceof String[]) {
  101. return ((String[]) value).length > 0 ? value.toString() : "";
  102. } else if (value instanceof Collection) {
  103. return ((Collection) value).isEmpty() ? "" : value.toString();
  104. } else {
  105. return value.toString();
  106. }
  107. }
  108. /**
  109. * Makes a deep copy of a <code>FastHashMap</code> if the values
  110. * are <code>Msg</code>, <code>Arg</code>,
  111. * or <code>Var</code>. Otherwise it is a shallow copy.
  112. *
  113. * @param map <code>FastHashMap</code> to copy.
  114. * @return FastHashMap A copy of the <code>FastHashMap</code> that was
  115. * passed in.
  116. */
  117. public static FastHashMap copyFastHashMap(FastHashMap map) {
  118. FastHashMap results = new FastHashMap();
  119. Iterator i = map.keySet().iterator();
  120. while (i.hasNext()) {
  121. String key = (String) i.next();
  122. Object value = map.get(key);
  123. if (value instanceof Msg) {
  124. results.put(key, ((Msg) value).clone());
  125. } else if (value instanceof Arg) {
  126. results.put(key, ((Arg) value).clone());
  127. } else if (value instanceof Var) {
  128. results.put(key, ((Var) value).clone());
  129. } else {
  130. results.put(key, value);
  131. }
  132. }
  133. results.setFast(true);
  134. return results;
  135. }
  136. }