%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.util.FormMessages |
|
|
1 | package org.apache.turbine.util; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 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 264152 2005-08-29 14:50:22Z henning $ |
|
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 | 0 | { |
44 | 0 | forms_messages = new Hashtable(); |
45 | 0 | fields_messages = new Hashtable(); |
46 | 0 | messages_fields = new Hashtable(); |
47 | 0 | forms_fields = new Hashtable(); |
48 | 0 | } |
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 | 0 | setMessage(formName, fieldName, String.valueOf(returnCode)); |
63 | 0 | } |
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 | 0 | fieldName = formName + "-" + fieldName; |
78 | 0 | addValue(forms_messages, formName, messageName); |
79 | 0 | addValue(fields_messages, fieldName, messageName); |
80 | 0 | addValue(messages_fields, messageName, fieldName); |
81 | 0 | addValue(forms_fields, formName, fieldName); |
82 | 0 | } |
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 | 0 | if (!table.containsKey(key)) |
99 | { |
|
100 | 0 | values = new Vector(); |
101 | 0 | values.addElement(value); |
102 | 0 | table.put(key, values); |
103 | } |
|
104 | else |
|
105 | { |
|
106 | 0 | values = ((Vector) table.get(key)); |
107 | 0 | if (!values.contains(value)) |
108 | { |
|
109 | 0 | values.addElement(value); |
110 | } |
|
111 | } |
|
112 | 0 | } |
113 | ||
114 | /** |
|
115 | * Gets a pair key/value from a table. |
|
116 | * |
|
117 | * @param table A Hastable. |
|
118 | * @param key A String with the key. |
|
119 | * @return A Vector with the pair key/value, or null. |
|
120 | */ |
|
121 | private Vector getValues(Hashtable table, String key) |
|
122 | { |
|
123 | 0 | return (Vector) table.get(key); |
124 | } |
|
125 | ||
126 | /** |
|
127 | * Gets all form messages for a given form. |
|
128 | * |
|
129 | * @param formName A String with the form name. |
|
130 | * @return A FormMessage[]. |
|
131 | */ |
|
132 | public FormMessage[] getFormMessages(String formName) |
|
133 | { |
|
134 | Vector messages, fields; |
|
135 | String messageName, fieldName; |
|
136 | 0 | messages = getValues(forms_messages, formName); |
137 | 0 | if (messages != null) |
138 | { |
|
139 | 0 | FormMessage[] result = new FormMessage[messages.size()]; |
140 | 0 | for (int i = 0; i < messages.size(); i++) |
141 | { |
|
142 | 0 | result[i] = new FormMessage(formName); |
143 | 0 | messageName = (String) messages.elementAt(i); |
144 | 0 | result[i].setMessage(messageName); |
145 | 0 | fields = getValues(messages_fields, messageName); |
146 | 0 | for (int j = 0; j < fields.size(); j++) |
147 | { |
|
148 | 0 | fieldName = (String) fields.elementAt(j); |
149 | 0 | if (formHasField(formName, fieldName)) |
150 | { |
|
151 | 0 | result[i].setFieldName(fieldName); |
152 | } |
|
153 | } |
|
154 | } |
|
155 | 0 | return result; |
156 | } |
|
157 | 0 | return new FormMessage[0]; |
158 | } |
|
159 | ||
160 | /** |
|
161 | * Get form messages for a given form and field. |
|
162 | * |
|
163 | * @param formName A String with the form name. |
|
164 | * @param fieldName A String with the field name. |
|
165 | * @return A FormMessage[]. |
|
166 | */ |
|
167 | public FormMessage[] getFormMessages(String formName, String fieldName) |
|
168 | { |
|
169 | 0 | String key = formName + "-" + fieldName; |
170 | ||
171 | 0 | Vector messages = getValues(fields_messages, key); |
172 | String messageName; |
|
173 | ||
174 | 0 | if (messages != null) |
175 | { |
|
176 | 0 | FormMessage[] result = new FormMessage[messages.size()]; |
177 | 0 | for (int i = 0; i < messages.size(); i++) |
178 | { |
|
179 | 0 | result[i] = new FormMessage(formName, fieldName); |
180 | 0 | messageName = (String) messages.elementAt(i); |
181 | 0 | result[i].setMessage(messageName); |
182 | } |
|
183 | 0 | return result; |
184 | } |
|
185 | 0 | return new FormMessage[0]; |
186 | } |
|
187 | ||
188 | /** |
|
189 | * Check whether a form as a field. |
|
190 | * |
|
191 | * @param formName A String with the form name. |
|
192 | * @param fieldName A String with the field name. |
|
193 | * @return True if form has the field. |
|
194 | */ |
|
195 | private boolean formHasField(String formName, |
|
196 | String fieldName) |
|
197 | { |
|
198 | 0 | List fields = getValues(forms_fields, formName); |
199 | 0 | for (Iterator iter = fields.iterator(); iter.hasNext();) |
200 | { |
|
201 | 0 | if (fieldName.equals(iter.next().toString())) |
202 | { |
|
203 | 0 | return true; |
204 | } |
|
205 | } |
|
206 | 0 | return false; |
207 | } |
|
208 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |