001: /*
002: * MeshCMS - A simple CMS based on SiteMesh
003: * Copyright (C) 2004-2007 Luciano Vernaschi
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * You can contact the author at http://www.cromoteca.com
020: * and at info@cromoteca.com
021: */
022:
023: package org.meshcms.webui;
024:
025: import java.io.*;
026: import org.meshcms.util.*;
027:
028: /**
029: * Encapsulates the functionalities of a form field.
030: */
031: public class FormField implements Serializable {
032: /**
033: * Denotes a text field.
034: */
035: public static final int TEXT = 0;
036:
037: /**
038: * Denotes a text field that is supposed to accept e-mail addresses.
039: */
040: public static final int EMAIL = 1;
041:
042: /**
043: * Denotes a submit button.
044: */
045: public static final int SUBMIT = 2;
046:
047: /**
048: * Denotes a reset button.
049: */
050: public static final int RESET = 3;
051:
052: /**
053: * Denotes a text field that is supposed to accept numbers.
054: */
055: public static final int NUMBER = 4;
056:
057: /**
058: * Denotes a hidden field. Hidden fields are stored in memory and not in the
059: * form.
060: */
061: public static final int HIDDEN = 5;
062:
063: /**
064: * Denotes a select field.
065: */
066: public static final int SELECT_OPTION = 6;
067:
068: private String name;
069: private String code;
070: private int type;
071: private String value;
072: private String[] options;
073: private boolean required;
074: private boolean sender;
075: private boolean senderName;
076: private boolean recipient;
077: private boolean subject;
078: private boolean messageBody;
079: private int rows = 1;
080:
081: /**
082: * Sets the friendly name of the field.
083: */
084: public void setName(String name) {
085: this .name = Utils.trim(name);
086: }
087:
088: /**
089: * Returns the friendly name of the field.
090: */
091: public String getName() {
092: return name;
093: }
094:
095: /**
096: * Sets the element name of the field.
097: */
098: public void setCode(String code) {
099: this .code = Utils.trim(code);
100: }
101:
102: /**
103: * Returns the element name of the field.
104: */
105: public String 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 value) {
127: this .value = Utils.trim(value);
128: }
129:
130: /**
131: * Returns the value of the field.
132: */
133: public String 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[] 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[] 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 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 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 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 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 && val
306: .indexOf("mime-version:") > -1);
307:
308: }
309:
310: /**
311: * Returns a modified version of the passed string, such as it can be used as
312: * a field name.
313: */
314: public static String createCode(String s) {
315: StringBuffer sb = new StringBuffer(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: * Returns a descriptive name of the field. This is generally the field name.
332: */
333: public String getDescription() {
334: String 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 toString() {
357: return "Form Field: " + getDescription();
358: }
359:
360: /**
361: * Returns 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: * Returns 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: * Returns 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: }
|