View Javadoc

1   package org.apache.turbine.util;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Hashtable;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Vector;
23  
24  /***
25   * Used for adding and accessing messages that relate to a specific
26   * form and field.  Allows to query for messages by form name and
27   * field name.  Used together with FormMessage class.
28   *
29   * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
30   * @version $Id: FormMessages.java,v 1.3.2.2 2004/05/20 03:16:38 seade Exp $
31   */
32  public class FormMessages
33  {
34      private Hashtable forms_messages;
35      private Hashtable fields_messages;
36      private Hashtable messages_fields;
37      private Hashtable forms_fields;
38  
39      /***
40       * Constructor.
41       */
42      public FormMessages()
43      {
44          forms_messages = new Hashtable();
45          fields_messages = new Hashtable();
46          messages_fields = new Hashtable();
47          forms_fields = new Hashtable();
48      }
49  
50      /***
51       * Sets a message for a field of a form.  The message is given as
52       * a long representing a return code.
53       *
54       * @param formName A String with the form name.
55       * @param fieldName A String with the field name.
56       * @param returnCode A long with the return code.
57       */
58      public void setMessage(String formName,
59                             String fieldName,
60                             long returnCode)
61      {
62          setMessage(formName, fieldName, String.valueOf(returnCode));
63      }
64  
65      /***
66       * Sets a message for a field of a form.  The message is given as
67       * a String.
68       *
69       * @param formName A String with the form name.
70       * @param fieldName A String with the field name.
71       * @param messageName A String with the message.
72       */
73      public void setMessage(String formName,
74                             String fieldName,
75                             String messageName)
76      {
77          fieldName = formName + "-" + fieldName;
78          addValue(forms_messages, formName, messageName);
79          addValue(fields_messages, fieldName, messageName);
80          addValue(messages_fields, messageName, fieldName);
81          addValue(forms_fields, formName, fieldName);
82      }
83  
84      /***
85       * Adds a pair key/value to a table, making sure not to add
86       * duplicate keys.
87       *
88       * @param table A Hastable.
89       * @param key A String with the key.
90       * @param value A String with value.
91       */
92      private void addValue(Hashtable table,
93                            String key,
94                            String value)
95      {
96          Vector values;
97  
98          if (!table.containsKey(key))
99          {
100             values = new Vector();
101             values.addElement(value);
102             table.put(key, values);
103         }
104         else
105         {
106             values = ((Vector) table.get(key));
107             if (!values.contains(value))
108                 values.addElement(value);
109         }
110     }
111 
112     /***
113      * Gets a pair key/value from a table.
114      *
115      * @param table A Hastable.
116      * @param key A String with the key.
117      * @return A Vector with the pair key/value, or null.
118      */
119     private final Vector getValues(Hashtable table, String key)
120     {
121         return (Vector) table.get(key);
122     }
123 
124     /***
125      * Gets all form messages for a given form.
126      *
127      * @param formName A String with the form name.
128      * @return A FormMessage[].
129      */
130     public FormMessage[] getFormMessages(String formName)
131     {
132         Vector messages, fields;
133         String messageName, fieldName;
134         messages = getValues(forms_messages, formName);
135         if (messages != null)
136         {
137             FormMessage[] result = new FormMessage[messages.size()];
138             for (int i = 0; i < messages.size(); i++)
139             {
140                 result[i] = new FormMessage(formName);
141                 messageName = (String) messages.elementAt(i);
142                 result[i].setMessage(messageName);
143                 fields = getValues(messages_fields, messageName);
144                 for (int j = 0; j < fields.size(); j++)
145                 {
146                     fieldName = (String) fields.elementAt(j);
147                     if (formHasField(formName, fieldName))
148                     {
149                         result[i].setFieldName(fieldName);
150                     }
151                 }
152             }
153             return result;
154         }
155         return null;
156     }
157 
158     /***
159      * Get form messages for a given form and field.
160      *
161      * @param formName A String with the form name.
162      * @param fieldName A String with the field name.
163      * @return A FormMessage[].
164      */
165     public FormMessage[] getFormMessages(String formName, String fieldName)
166     {
167         String key = formName + "-" + fieldName;
168 
169         Vector messages = getValues(fields_messages, key);
170         String messageName;
171 
172         if (messages != null)
173         {
174             FormMessage[] result = new FormMessage[messages.size()];
175             for (int i = 0; i < messages.size(); i++)
176             {
177                 result[i] = new FormMessage(formName, fieldName);
178                 messageName = (String) messages.elementAt(i);
179                 result[i].setMessage(messageName);
180             }
181             return result;
182         }
183         return null;
184     }
185 
186     /***
187      * Check whether a form as a field.
188      *
189      * @param formName A String with the form name.
190      * @param fieldName A String with the field name.
191      * @return True if form has the field.
192      */
193     private boolean formHasField(String formName,
194                                  String fieldName)
195     {
196         List fields = getValues(forms_fields, formName);
197         for (Iterator iter = fields.iterator(); iter.hasNext();)
198         {
199             if (fieldName.equals(iter.next().toString()))
200             {
201                 return true;
202             }
203         }
204         return false;
205     }
206 }