1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/FormSet.java,v 1.15 2004/02/21 17:10:29 rleland Exp $
  3. * $Revision: 1.15 $
  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. /**
  28. * Holds a set of <code>Form</code>s stored associated with a
  29. * <code>Locale</code> based on the country, language, and variant specified.
  30. * Instances of this class are configured with a <formset> xml element.
  31. */
  32. public class FormSet implements Serializable {
  33. /**
  34. * Whether or not the this <code>FormSet</code> was processed
  35. * for replacing variables in strings with their values.
  36. */
  37. private boolean processed = false;
  38. /**
  39. * Language component of <code>Locale</code> (required).
  40. */
  41. private String language = null;
  42. /**
  43. * Country component of <code>Locale</code> (optional).
  44. */
  45. private String country = null;
  46. /**
  47. * Variant component of <code>Locale</code> (optional).
  48. */
  49. private String variant = null;
  50. /**
  51. * A <code>Map</code> of <code>Form</code>s
  52. * using the name field of the <code>Form</code> as the key.
  53. */
  54. private Map forms = new HashMap();
  55. /**
  56. * A <code>Map</code> of <code>Constant</code>s
  57. * using the name field of the <code>Constant</code> as the key.
  58. */
  59. private Map constants = new HashMap();
  60. /**
  61. * Whether or not the this <code>FormSet</code> was processed
  62. * for replacing variables in strings with their values.
  63. */
  64. public boolean isProcessed() {
  65. return processed;
  66. }
  67. /**
  68. * Gets the equivalent of the language component of <code>Locale</code>.
  69. */
  70. public String getLanguage() {
  71. return language;
  72. }
  73. /**
  74. * Sets the equivalent of the language component of <code>Locale</code>.
  75. */
  76. public void setLanguage(String language) {
  77. this.language = language;
  78. }
  79. /**
  80. * Gets the equivalent of the country component of <code>Locale</code>.
  81. */
  82. public String getCountry() {
  83. return country;
  84. }
  85. /**
  86. * Sets the equivalent of the country component of <code>Locale</code>.
  87. */
  88. public void setCountry(String country) {
  89. this.country = country;
  90. }
  91. /**
  92. * Gets the equivalent of the variant component of <code>Locale</code>.
  93. */
  94. public String getVariant() {
  95. return variant;
  96. }
  97. /**
  98. * Sets the equivalent of the variant component of <code>Locale</code>.
  99. */
  100. public void setVariant(String variant) {
  101. this.variant = variant;
  102. }
  103. /**
  104. * Add a <code>Constant</code> (locale level).
  105. * @deprecated Use addConstant(String, String) instead.
  106. */
  107. public void addConstant(Constant c) {
  108. if (c.getName() != null && c.getName().length() > 0 &&
  109. c.getValue() != null && c.getValue().length() > 0) {
  110. constants.put(c.getName(), c.getValue());
  111. }
  112. }
  113. /**
  114. * Add a <code>Constant</code> to the locale level.
  115. * @deprecated Use addConstant(String, String) instead.
  116. */
  117. public void addConstantParam(String name, String value) {
  118. if (name != null && name.length() > 0 &&
  119. value != null && value.length() > 0) {
  120. constants.put(name, value);
  121. }
  122. }
  123. /**
  124. * Add a <code>Constant</code> to the locale level.
  125. */
  126. public void addConstant(String name, String value) {
  127. this.constants.put(name, value);
  128. }
  129. /**
  130. * Add a <code>Form</code> to the <code>FormSet</code>.
  131. */
  132. public void addForm(Form f) {
  133. forms.put(f.getName(), f);
  134. }
  135. /**
  136. * Retrieve a <code>Form</code> based on the form name.
  137. * @deprecated Use getForm(String) instead.
  138. */
  139. public Form getForm(Object key) {
  140. return (Form) this.forms.get(key);
  141. }
  142. /**
  143. * Retrieve a <code>Form</code> based on the form name.
  144. */
  145. public Form getForm(String formName) {
  146. return (Form) this.forms.get(formName);
  147. }
  148. /**
  149. * A <code>Map</code> of <code>Form</code>s is returned as an
  150. * unmodifiable <code>Map</code> with the key based on the form name.
  151. */
  152. public Map getForms() {
  153. return Collections.unmodifiableMap(forms);
  154. }
  155. /**
  156. * Processes all of the <code>Form</code>s.
  157. * @deprecated This method is called by the framework. It will be made protected
  158. * in a future release. TODO
  159. */
  160. public synchronized void process(Map globalConstants) {
  161. for (Iterator i = forms.values().iterator(); i.hasNext();) {
  162. Form f = (Form) i.next();
  163. f.process(globalConstants, constants);
  164. }
  165. processed = true;
  166. }
  167. /**
  168. * Returns a string representation of the object.
  169. */
  170. public String toString() {
  171. StringBuffer results = new StringBuffer();
  172. results.append("FormSet: language=");
  173. results.append(language);
  174. results.append(" country=");
  175. results.append(country);
  176. results.append(" variant=");
  177. results.append(variant);
  178. results.append("\n");
  179. for (Iterator i = getForms().values().iterator(); i.hasNext();) {
  180. results.append(" ");
  181. results.append(i.next());
  182. results.append("\n");
  183. }
  184. return results.toString();
  185. }
  186. }