KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > FormSet


1 /*
2  * $Id: FormSet.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 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 JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
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 JavaDoc {
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 JavaDoc language = null;
47
48     /**
49      * Country component of <code>Locale</code> (optional).
50      */

51     private String JavaDoc country = null;
52
53     /**
54      * Variant component of <code>Locale</code> (optional).
55      */

56     private String JavaDoc 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 JavaDoc forms = new HashMap JavaDoc();
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 JavaDoc constants = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc language) {
89         this.language = language;
90     }
91
92     /**
93      * Gets the equivalent of the country component of <code>Locale</code>.
94      */

95     public String JavaDoc 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 JavaDoc country) {
103         this.country = country;
104     }
105
106     /**
107      * Gets the equivalent of the variant component of <code>Locale</code>.
108      */

109     public String JavaDoc 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 JavaDoc variant) {
117         this.variant = variant;
118     }
119
120     /**
121      * Add a <code>Constant</code> to the locale level.
122      */

123     public void addConstant(String JavaDoc name, String JavaDoc value) {
124         this.constants.put(name, value);
125     }
126
127     /**
128      * Add a <code>Form</code> to the <code>FormSet</code>.
129      */

130     public void addForm(Form f) {
131         forms.put(f.getName(), f);
132     }
133
134     /**
135      * Retrieve a <code>Form</code> based on the form name.
136      */

137     public Form getForm(String JavaDoc formName) {
138         return (Form) this.forms.get(formName);
139     }
140
141     /**
142      * A <code>Map</code> of <code>Form</code>s is returned as an
143      * unmodifiable <code>Map</code> with the key based on the form name.
144      */

145     public Map JavaDoc getForms() {
146         return Collections.unmodifiableMap(forms);
147     }
148
149     /**
150      * Processes all of the <code>Form</code>s.
151      */

152     synchronized void process(Map JavaDoc globalConstants) {
153         for (Iterator JavaDoc i = forms.values().iterator(); i.hasNext();) {
154             Form f = (Form) i.next();
155             f.process(globalConstants, constants, forms);
156         }
157
158         processed = true;
159     }
160
161     /**
162      * Returns a string representation of the object.
163      */

164     public String JavaDoc toString() {
165         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
166
167         results.append("FormSet: language=");
168         results.append(language);
169         results.append(" country=");
170         results.append(country);
171         results.append(" variant=");
172         results.append(variant);
173         results.append("\n");
174
175         for (Iterator JavaDoc i = getForms().values().iterator(); i.hasNext();) {
176             results.append(" ");
177             results.append(i.next());
178             results.append("\n");
179         }
180
181         return results.toString();
182     }
183
184 }
Popular Tags