1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResults.java,v 1.10 2004/02/21 17:10:29 rleland Exp $
  3. * $Revision: 1.10 $
  4. * $Date: 2004/02/21 17:10:29 $
  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;
  22. import java.io.Serializable;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.Map;
  27. import java.util.Set;
  28. /**
  29. * This contains the results of a set of validation rules processed
  30. * on a JavaBean.
  31. */
  32. public class ValidatorResults implements Serializable {
  33. /**
  34. * Map of validation results.
  35. */
  36. protected Map hResults = new HashMap();
  37. /**
  38. * Merge another ValidatorResults into mine.
  39. */
  40. public void merge(ValidatorResults results) {
  41. this.hResults.putAll(results.hResults);
  42. }
  43. /**
  44. * Add a the result of a validator action.
  45. */
  46. public void add(Field field, String validatorName, boolean result) {
  47. this.add(field, validatorName, result, null);
  48. }
  49. /**
  50. * Add a the result of a validator action.
  51. */
  52. public void add(
  53. Field field,
  54. String validatorName,
  55. boolean result,
  56. Object value) {
  57. ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
  58. if (validatorResult == null) {
  59. validatorResult = new ValidatorResult(field);
  60. this.hResults.put(field.getKey(), validatorResult);
  61. }
  62. validatorResult.add(validatorName, result, value);
  63. }
  64. /**
  65. * Clear all results recorded by this object.
  66. */
  67. public void clear() {
  68. this.hResults.clear();
  69. }
  70. /**
  71. * Return <code>true</code> if there are no messages recorded
  72. * in this collection, or <code>false</code> otherwise.
  73. * @deprecated Use isEmpty() instead.
  74. */
  75. public boolean empty() {
  76. return this.isEmpty();
  77. }
  78. /**
  79. * Return <code>true</code> if there are no messages recorded
  80. * in this collection, or <code>false</code> otherwise.
  81. */
  82. public boolean isEmpty() {
  83. return this.hResults.isEmpty();
  84. }
  85. /**
  86. * Gets the <code>ValidatorResult</code> associated
  87. * with the key passed in. The key the <code>ValidatorResult</code>
  88. * is stored under is the <code>Field</code>'s getKey method.
  89. *
  90. * @param key The key generated from <code>Field</code> (this is often just
  91. * the field name).
  92. */
  93. public ValidatorResult getValidatorResult(String key) {
  94. return (ValidatorResult) this.hResults.get(key);
  95. }
  96. /**
  97. * Return the set of all recorded messages, without distinction
  98. * by which property the messages are associated with. If there are
  99. * no messages recorded, an empty enumeration is returned.
  100. * @deprecated Use getPropertyNames() instead.
  101. */
  102. public Iterator get() {
  103. if (hResults.isEmpty()) {
  104. return Collections.EMPTY_LIST.iterator();
  105. }
  106. return hResults.keySet().iterator();
  107. }
  108. /**
  109. * Return the set of property names for which at least one message has
  110. * been recorded. If there are no messages, an empty Iterator is returned.
  111. * If you have recorded global messages, the String value of
  112. * <code>ActionMessages.GLOBAL_MESSAGE</code> will be one of the returned
  113. * property names.
  114. * @deprecated Use getPropertyNames() instead.
  115. */
  116. public Iterator properties() {
  117. return hResults.keySet().iterator();
  118. }
  119. /**
  120. * Return the set of property names for which at least one message has
  121. * been recorded.
  122. * @return An unmodifiable Set of the property names.
  123. */
  124. public Set getPropertyNames() {
  125. return Collections.unmodifiableSet(this.hResults.keySet());
  126. }
  127. /**
  128. * Get a <code>Map</code> of any <code>Object</code>s returned from
  129. * validation routines.
  130. */
  131. public Map getResultValueMap() {
  132. Map results = new HashMap();
  133. for (Iterator i = hResults.keySet().iterator(); i.hasNext();) {
  134. String propertyKey = (String) i.next();
  135. ValidatorResult vr = this.getValidatorResult(propertyKey);
  136. Map actions = vr.getActionMap();
  137. for (Iterator x = actions.keySet().iterator(); x.hasNext();) {
  138. String actionKey = (String) x.next();
  139. ValidatorResult.ResultStatus rs =
  140. (ValidatorResult.ResultStatus) actions.get(actionKey);
  141. if (rs != null) {
  142. Object result = rs.getResult();
  143. if (result != null && !(result instanceof Boolean)) {
  144. results.put(propertyKey, result);
  145. }
  146. }
  147. }
  148. }
  149. return results;
  150. }
  151. }