KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > webui > FormField


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.webui;
24
25 import java.io.*;
26 import org.meshcms.util.*;
27
28 /**
29  * Encapsulates the functionalities of a form field.
30  */

31 public class FormField implements Serializable {
32   /**
33    * Denotes a text field.
34    */

35   public static final int TEXT = 0;
36
37   /**
38    * Denotes a text field that is supposed to accept e-mail addresses.
39    */

40   public static final int EMAIL = 1;
41
42   /**
43    * Denotes a submit button.
44    */

45   public static final int SUBMIT = 2;
46
47   /**
48    * Denotes a reset button.
49    */

50   public static final int RESET = 3;
51
52   /**
53    * Denotes a text field that is supposed to accept numbers.
54    */

55   public static final int NUMBER = 4;
56
57   /**
58    * Denotes a hidden field. Hidden fields are stored in memory and not in the
59    * form.
60    */

61   public static final int HIDDEN = 5;
62
63   /**
64    * Denotes a select field.
65    */

66   public static final int SELECT_OPTION = 6;
67
68   private String JavaDoc name;
69   private String JavaDoc code;
70   private int type;
71   private String JavaDoc value;
72   private String JavaDoc[] options;
73   private boolean required;
74   private boolean sender;
75   private boolean senderName;
76   private boolean recipient;
77   private boolean subject;
78   private boolean messageBody;
79   private int rows = 1;
80
81   /**
82    * Sets the friendly name of the field.
83    */

84   public void setName(String JavaDoc name) {
85     this.name = Utils.trim(name);
86   }
87
88   /**
89    * Returns the friendly name of the field.
90    */

91   public String JavaDoc getName() {
92     return name;
93   }
94
95   /**
96    * Sets the element name of the field.
97    */

98   public void setCode(String JavaDoc code) {
99     this.code = Utils.trim(code);
100   }
101
102   /**
103    * Returns the element name of the field.
104    */

105   public String JavaDoc getCode() {
106     return code;
107   }
108
109   /**
110    * Sets the type of the field.
111    */

112   public void setType(int type) {
113     this.type = type;
114   }
115
116   /**
117    * Returns the type of the field.
118    */

119   public int getType() {
120     return type;
121   }
122
123   /**
124    * Sets the value of the field.
125    */

126   public void setValue(String JavaDoc value) {
127     this.value = Utils.trim(value);
128   }
129
130   /**
131    * Returns the value of the field.
132    */

133   public String JavaDoc getValue() {
134     return value;
135   }
136
137   /**
138    * Sets the options of the field. To be used only for fields whose type
139    * is {@link #SELECT_OPTION}.
140    */

141   public void setOptions(String JavaDoc[] options) {
142     this.options = options;
143
144     for (int i = 0; i < options.length; i++) {
145       this.options[i] = Utils.trim(this.options[i]);
146     }
147   }
148
149   /**
150    * Returns the options of the field.
151    */

152   public String JavaDoc[] getOptions() {
153     return options;
154   }
155
156   /**
157    * Sets both the friendly name and the element name of the field. The value
158    * passed as argument is used as friendly name, while the element name will
159    * be created using {@link #createCode}.
160    */

161   public void setNameAndCode(String JavaDoc name) {
162     setName(name);
163     setCode(createCode(name));
164   }
165
166   /**
167    * Sets the required flag. A required field is supposed to have a value.
168    */

169   public void setRequired(boolean required) {
170     this.required = required;
171   }
172
173   /**
174    * Returns the value of the required flag.
175    */

176   public boolean isRequired() {
177     return required;
178   }
179
180   /**
181    * Sets the sender flag. This should be set to true for a field that is
182    * supposed to contain the e-mail address of the sender.
183    */

184   public void setSender(boolean sender) {
185     this.sender = sender;
186
187     if (sender) {
188       setRequired(true);
189     }
190   }
191
192   /**
193    * Returns the value of the sender flag.
194    */

195   public boolean isSender() {
196     return sender;
197   }
198
199   /**
200    * Sets the recipient flag. This should be set to true for a field that is
201    * supposed to contain the e-mail address of the recipient.
202    */

203   public void setRecipient(boolean recipient) {
204     this.recipient = recipient;
205   }
206
207   /**
208    * Returns the value of the recipient flag.
209    */

210   public boolean isRecipient() {
211     return recipient;
212   }
213
214   /**
215    * Sets the number of rows for the editable field. In general, an
216    * <code>input</code> field is replaced with a <code>textarea</code> when
217    * this number is greater than 1.
218    */

219   public void setRows(int rows) {
220     this.rows = rows;
221   }
222
223   /**
224    * Returns the number of rows for the editable field.
225    */

226   public int getRows() {
227     return rows;
228   }
229
230   /**
231    * Sets the value of a parameter. The parameter set depends on the passed
232    * argument.
233    */

234   public void setParameter(String JavaDoc value) {
235     if (value.equals("text")) {
236       setType(TEXT);
237     } else if (value.equals("email")) {
238       setType(EMAIL);
239     } else if (value.startsWith("text:")) {
240       setType(TEXT);
241       setRows(Utils.parseInt(Utils.trim(value.substring(5)), 12));
242     } else if (value.equals("textarea")) {
243       setType(TEXT);
244       setRows(12);
245     } else if (value.equals("submit")) {
246       setType(SUBMIT);
247     } else if (value.equals("reset")) {
248       setType(RESET);
249     } else if (value.equals("number")) {
250       setType(NUMBER);
251     } else if (value.equals("hidden")) {
252       setType(HIDDEN);
253     } else if (value.startsWith("options:")) {
254       setOptions(Utils.tokenize(value.substring(8), ","));
255       setType(SELECT_OPTION);
256     } else if (value.equals("required")) {
257       setRequired(true);
258     } else if (value.equals("sender")) {
259       setSender(true); // implies setRequired(true);
260
} else if (value.equals("sendername")) {
261       setSenderName(true);
262     } else if (value.equals("subject")) {
263       setSubject(true);
264     } else if (value.equals("messagebody")) {
265       setMessageBody(true);
266     } else if (value.startsWith("recipient:")) {
267       setRecipient(true);
268       setValue(value.substring(10));
269     } else if (value.startsWith("value:")) {
270       setValue(value.substring(6));
271     } else {
272       setNameAndCode(value);
273     }
274   }
275
276   /**
277    * Tries to determine if the value of the field is acceptable. The check
278    * covers required values, e-mail fields and numeric fields.
279    */

280   public boolean checkValue() {
281     String JavaDoc val = Utils.noNull(getValue()).trim();
282
283     if (isRequired() && val.equals("")) {
284       return false;
285     }
286
287     if (getType() == EMAIL || isSender() || isRecipient()) {
288       if (!Utils.checkAddress(val)) {
289         return false;
290       }
291     }
292
293     if (getType() == NUMBER && !val.equals("")) {
294       try {
295         Double.parseDouble(val);
296       } catch (Exception JavaDoc ex) {
297         return false;
298       }
299     }
300
301     val = val.toLowerCase();
302
303     /* The purpose of this if block is to reject some attempts to spam the mail
304        form. All identified attempts contained these pieces of text somewhere */

305       return !(val.indexOf("content-type:") > -1 &&
306           val.indexOf("mime-version:") > -1);
307
308   }
309
310   /**
311    * @return a modified version of the passed string, such as it can be used as
312    * a field name.
313    */

314   public static String JavaDoc createCode(String JavaDoc s) {
315     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s.length());
316
317     for (int i = 0; i < s.length(); i++) {
318       char c = Character.toLowerCase(s.charAt(i));
319
320       if ("abcdefghijklmnopqrstuvwxyz_-0123456789".indexOf(c) < 0) {
321         sb.append('_');
322       } else {
323         sb.append(c);
324       }
325     }
326
327     return sb.toString();
328   }
329
330   /**
331    * @return a descriptive name of the field. This is generally the field name.
332    */

333   public String JavaDoc getDescription() {
334     String JavaDoc desc = getValue();
335
336     if (Utils.isNullOrEmpty(desc)) {
337       desc = getName();
338
339       if (Utils.isNullOrEmpty(desc)) {
340         if (isRecipient()) {
341           desc = "(recipient)";
342         } else if (isSender()) {
343           desc = "(sender)";
344         } else {
345           desc = "(unknown)";
346         }
347       }
348     }
349
350     return desc;
351   }
352
353   /**
354    * Returns a description of the field.
355    */

356   public String JavaDoc toString() {
357     return "Form Field: " + getDescription();
358   }
359
360   /**
361    * @return the value of the sender name flag.
362    */

363   public boolean isSenderName() {
364     return senderName;
365   }
366
367   /**
368    * Sets the sender name flag. This should be set to true for a field that is
369    * supposed to contain the (complete) name of the sender.
370    */

371   public void setSenderName(boolean senderName) {
372     this.senderName = senderName;
373   }
374
375   /**
376    * @return the value of the subject flag.
377    */

378   public boolean isSubject() {
379     return subject;
380   }
381
382   /**
383    * Sets the subject flag. This should be set to true for a field that is
384    * supposed to contain the subject of the message.
385    */

386   public void setSubject(boolean subject) {
387     this.subject = subject;
388   }
389
390   /**
391    * @return the value of the message body flag.
392    */

393   public boolean isMessageBody() {
394     return messageBody;
395   }
396
397   /**
398    * Sets the message body flag. If true, this filed is considered to be the
399    * body of the message and will be written without caption
400    */

401   public void setMessageBody(boolean messageBody) {
402     this.messageBody = messageBody;
403   }
404
405   /*public static List getFields(ModuleDescriptor md, PageContext pageContext) {
406     List fields = new ArrayList();
407
408     if (Utils.checkAddress(md.getArgument())) {
409       FormField tempField = new FormField();
410       String recipient = md.getArgument();
411       tempField.setParameter("recipient:" + recipient);
412       fields.add(tempField);
413
414       ResourceBundle pageBundle = ResourceBundle.getBundle
415           ("org/meshcms/webui/Locales", WebUtils.getPageLocale(pageContext));
416
417       tempField = new FormField();
418       tempField.setParameter(pageBundle.getString("mailName"));
419       tempField.setParameter("sendername");
420       fields.add(tempField);
421
422       tempField = new FormField();
423       tempField.setParameter(pageBundle.getString("mailAddress"));
424       tempField.setParameter("email");
425       tempField.setParameter("sender");
426       fields.add(tempField);
427
428       tempField = new FormField();
429       tempField.setParameter(pageBundle.getString("mailMessage"));
430       tempField.setParameter("textarea");
431       tempField.setParameter("messagebody");
432       fields.add(tempField);
433
434       tempField = new FormField();
435       tempField.setParameter(pageBundle.getString("mailSend"));
436       tempField.setParameter("submit");
437       fields.add(tempField);
438     } else {
439       WebSite webSite = (WebSite) pageContext.getRequest().getAttribute("webSite");
440       File[] files = md.getModuleFiles(webSite, false);
441       WebUtils.updateLastModifiedTime((HttpServletRequest) pageContext.getRequest(), files[0]);
442
443       try {
444         String[] lines = Utils.readAllLines(files[0]);
445         FormField field = null;
446
447         for (int i = 0; i < lines.length; i++) {
448           String line = lines[i].trim();
449
450           if (line.equals("")) {
451             if (field != null) {
452               fields.add(field);
453               field = null;
454             }
455           } else {
456             if (field == null) {
457               field = new FormField();
458             }
459
460             field.setParameter(line);
461           }
462         }
463
464         if (field != null) {
465           fields.add(field);
466         }
467       } catch (IOException ex) {
468         webSite.log("Can't read mail form description", ex);
469       }
470     }
471
472     return fields;
473   }*/

474 }
475
Popular Tags