View Javadoc

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