1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Form.java,v 1.14.2.1 2004/06/22 02:24:38 husted Exp $
  3. * $Revision: 1.14.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;
  22. import java.io.Serializable;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;
  28. import org.apache.commons.collections.FastHashMap; // DEPRECATED
  29. /**
  30. * <p>
  31. * This contains a set of validation rules for a form/JavaBean. The information is
  32. * contained in a list of <code>Field</code> objects. Instances of this class are
  33. * configured with a <form> xml element.
  34. * </p>
  35. * <p>
  36. * The use of FastHashMap is deprecated and will be replaced in a future
  37. * release.
  38. * </p>
  39. */
  40. public class Form implements Serializable {
  41. /**
  42. * The name/key the set of validation rules is
  43. * stored under.
  44. */
  45. protected String name = null;
  46. /**
  47. * List of <code>Field</code>s. Used to maintain
  48. * the order they were added in although individual
  49. * <code>Field</code>s can be retrieved using
  50. * <code>Map</code> of <code>Field</code>s.
  51. */
  52. protected List lFields = new ArrayList();
  53. /**
  54. * Map of <code>Field</code>s keyed on their property value.
  55. */
  56. protected FastHashMap hFields = new FastHashMap();
  57. /**
  58. * Gets the name/key of the set of validation rules.
  59. */
  60. public String getName() {
  61. return name;
  62. }
  63. /**
  64. * Sets the name/key of the set of validation rules.
  65. */
  66. public void setName(String name) {
  67. this.name = name;
  68. }
  69. /**
  70. * Add a <code>Field</code> to the <code>Form</code>.
  71. */
  72. public void addField(Field f) {
  73. this.lFields.add(f);
  74. this.hFields.put(f.getKey(), f);
  75. }
  76. /**
  77. * A <code>List</code> of <code>Field</code>s is returned as an
  78. * unmodifiable <code>List</code>.
  79. */
  80. public List getFields() {
  81. return Collections.unmodifiableList(lFields);
  82. }
  83. /**
  84. * The <code>Field</code>s are returned as an unmodifiable <code>Map</code>.
  85. * @deprecated Use containsField(String) and getField(String) instead.
  86. */
  87. public Map getFieldMap() {
  88. return Collections.unmodifiableMap(hFields);
  89. }
  90. /**
  91. * Returns the Field with the given name or null if this Form has no such
  92. * field.
  93. * @since Validator 1.1
  94. */
  95. public Field getField(String fieldName) {
  96. return (Field) this.hFields.get(fieldName);
  97. }
  98. /**
  99. * Returns true if this Form contains a Field with the given name.
  100. * @since Validator 1.1
  101. */
  102. public boolean containsField(String fieldName) {
  103. return this.hFields.containsKey(fieldName);
  104. }
  105. /**
  106. * Processes all of the <code>Form</code>'s <code>Field</code>s.
  107. * @deprecated This method is called by the framework. It will be made protected
  108. * in a future release. TODO
  109. */
  110. public void process(Map globalConstants, Map constants) {
  111. hFields.setFast(true);
  112. for (Iterator i = lFields.iterator(); i.hasNext();) {
  113. Field f = (Field) i.next();
  114. f.process(globalConstants, constants);
  115. }
  116. }
  117. /**
  118. * Returns a string representation of the object.
  119. */
  120. public String toString() {
  121. StringBuffer results = new StringBuffer();
  122. results.append("Form: ");
  123. results.append(name);
  124. results.append("\n");
  125. for (Iterator i = lFields.iterator(); i.hasNext();) {
  126. results.append("\tField: \n");
  127. results.append(i.next());
  128. results.append("\n");
  129. }
  130. return results.toString();
  131. }
  132. /**
  133. * Validate all Fields in this Form on the given page and below.
  134. * @param params A Map of parameter class names to parameter values to pass
  135. * into validation methods.
  136. * @param actions A Map of validator names to ValidatorAction objects.
  137. * @param page Fields on pages higher than this will not be validated.
  138. * @return A ValidatorResults object containing all validation messages.
  139. * @throws ValidatorException
  140. */
  141. ValidatorResults validate(Map params, Map actions, int page)
  142. throws ValidatorException {
  143. ValidatorResults results = new ValidatorResults();
  144. Iterator fields = this.lFields.iterator();
  145. while (fields.hasNext()) {
  146. Field field = (Field) fields.next();
  147. params.put(Validator.FIELD_PARAM, field);
  148. if (field.getPage() <= page) {
  149. results.merge(field.validate(params, actions));
  150. }
  151. }
  152. return results;
  153. }
  154. }