KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > examples > madlib > MadLibValidator


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.examples.madlib;
8
9
10 import java.text.ParseException JavaDoc;
11 import java.text.SimpleDateFormat JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import com.inversoft.error.BasicError;
15 import com.inversoft.error.ErrorRegistry;
16 import com.inversoft.error.PropertyError;
17 import com.inversoft.verge.mvc.controller.Action;
18 import com.inversoft.verge.mvc.validator.Validator;
19 import com.inversoft.verge.util.RequestContext;
20 import com.inversoft.verge.util.WebBeanProperty;
21
22
23 /**
24  * <p>
25  * This is the validator for the MadLib system. This verifies
26  * that numbers are in fact numbers and also verifies that
27  * the user entered all the fillins.
28  * </p>
29  *
30  * @author Brian Pontarelli
31  */

32 public class MadLibValidator implements Validator {
33
34     /**
35      * The name of the error bundle
36      */

37     public static final String JavaDoc ERROR_BUNDLE =
38         "com.inversoft.verge.examples.madlib.madlib-errors";
39
40     /**
41      * Validates the MadLibBean to verify that all the information required was
42      * specified
43      *
44      * @param modelObjects All the model objects keyed by their scope keys
45      * @param action The Action that was taken
46      * @return True if the validation was successful, false otherwise
47      */

48     public boolean validate(Map JavaDoc modelObjects, Action action) {
49         MadLibBean mb = (MadLibBean) modelObjects.get("madLibBean");
50         RequestContext requestContext = action.getRequestContext();
51         MadLib madlib = mb.getMadLib();
52         boolean ret = true;
53         boolean oneNull = false;
54
55         // Verify that all the numbers were filled in
56
int numbers = madlib.getNumberOfNumbers();
57         for (int i = 0; i < numbers; i++) {
58             if (mb.getNumber(i) == null) {
59                 StringBuffer JavaDoc property = new StringBuffer JavaDoc();
60                 property.append("madLibBean.number[").append(i).append("]");
61                 requestContext.addError(new PropertyError(property.toString()));
62                 ret = false;
63                 oneNull = true;
64             }
65         }
66
67         // If there were no errors on the numbers, check the words
68
int words = madlib.getNumberOfWords();
69         for (int i = 0; i < words; i++) {
70             if (mb.getWord(i) == null) {
71                 StringBuffer JavaDoc property = new StringBuffer JavaDoc();
72                 property.append("madLibBean.word[").append(i).append("]");
73                 requestContext.addError(new PropertyError(property.toString()));
74                 ret = false;
75                 oneNull = true;
76             }
77         }
78
79         // If one was null, add another error
80
if (oneNull) {
81             BasicError error = ErrorRegistry.getBasicError(ERROR_BUNDLE, "input",
82                 null, this);
83             requestContext.addError(error);
84         }
85
86         // Validate the dates
87
MadLibFillin[] fillins = madlib.getDisplayMadLibFillins();
88         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc();
89         String JavaDoc format;
90         int index;
91         boolean formatInvalid = false;
92         for (int i = 0; i < fillins.length; i++) {
93             format = fillins[i].getFormat();
94             index = fillins[i].getIndex();
95
96             if (format != null) {
97                 sdf.applyPattern(format);
98
99                 try {
100                     sdf.parse(mb.getWord(index));
101                 } catch (ParseException JavaDoc pe) {
102                     // The property name has to be built because we can't dynamically
103
// determine which word is currently being validated
104
StringBuffer JavaDoc property = new StringBuffer JavaDoc();
105                     property.append("madLibBean.word[").append(index).append("]");
106
107                     PropertyError error = new PropertyError(property.toString());
108                     requestContext.addError(error);
109                     ret = false;
110                     formatInvalid = true;
111                 }
112             }
113         }
114
115         // Add a format error message
116
if (formatInvalid) {
117             BasicError error = ErrorRegistry.getBasicError(ERROR_BUNDLE, "format",
118                 null, this);
119             requestContext.addError(error);
120         }
121
122         // Add an overall error message
123
if (!ret) {
124             BasicError error = ErrorRegistry.getBasicError(ERROR_BUNDLE, "overall",
125                 null, this);
126             requestContext.addError(error);
127         }
128
129         return ret;
130     }
131
132     /**
133      * Handles the exception when a number fillin was not a valid number. In this
134      * case, an error is added to the context to tell the user that the number is
135      * invalid
136      *
137      * @param model Not used
138      * @param property Not used
139      * @param action The action taken
140      */

141     public void handleConversion(Object JavaDoc model, WebBeanProperty property,
142             Action action) {
143         PropertyError error = new PropertyError(property.getFullName());
144         RequestContext requestContext = action.getRequestContext();
145         requestContext.addError(error);
146     }
147 }
Free Books   Free Magazines  
Popular Tags