KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > validator > validwhen > ValidWhen


1 /*
2  * $Id: ValidWhen.java 164530 2005-04-25 03:11:07Z niallp $
3  *
4  * Copyright 2003-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 package org.apache.struts.validator.validwhen;
20
21 import java.io.StringReader JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.commons.validator.Field;
26 import org.apache.commons.validator.Validator;
27 import org.apache.commons.validator.ValidatorAction;
28 import org.apache.commons.validator.util.ValidatorUtils;
29 import org.apache.struts.action.ActionMessages;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.validator.Resources;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /**
36  * This class contains the validwhen validation that is used in the
37  * validator-rules.xml file.
38  *
39  * @since Struts 1.2
40  */

41 public class ValidWhen {
42
43     /**
44      * Commons Logging instance.
45      */

46     private static final Log log = LogFactory.getLog(ValidWhen.class);
47
48     /**
49      * Returns true if <code>obj</code> is null or a String.
50      */

51     private static boolean isString(Object JavaDoc obj) {
52         return (obj == null) ? true : String JavaDoc.class.isInstance(obj);
53     }
54
55     /**
56      * Checks if the field matches the boolean expression specified in
57      * <code>test</code> parameter.
58      *
59      * @param bean The bean validation is being performed on.
60      *
61      * @param va The <code>ValidatorAction</code> that is currently being
62      * performed.
63      *
64      * @param field The <code>Field</code> object associated with the current
65      * field being validated.
66      *
67      * @param errors The <code>ActionMessages</code> object to add errors to if any
68      * validation errors occur.
69      *
70      * @param request Current request object.
71      *
72      * @return <code>true</code> if meets stated requirements,
73      * <code>false</code> otherwise.
74      */

75     public static boolean validateValidWhen(
76         Object JavaDoc bean,
77         ValidatorAction va,
78         Field field,
79         ActionMessages errors,
80         Validator validator,
81         HttpServletRequest JavaDoc request) {
82             
83         Object JavaDoc form = validator.getParameterValue(Validator.BEAN_PARAM);
84         String JavaDoc value = null;
85         boolean valid = false;
86         int index = -1;
87         
88         if (field.isIndexed()) {
89             String JavaDoc key = field.getKey();
90
91             final int leftBracket = key.indexOf("[");
92             final int rightBracket = key.indexOf("]");
93
94             if ((leftBracket > -1) && (rightBracket > -1)) {
95                 index =
96                     Integer.parseInt(key.substring(leftBracket + 1, rightBracket));
97             }
98         }
99         
100         if (isString(bean)) {
101             value = (String JavaDoc) bean;
102         } else {
103             value = ValidatorUtils.getValueAsString(bean, field.getProperty());
104         }
105         
106         String JavaDoc test = field.getVarValue("test");
107         if (test == null) {
108             String JavaDoc msg = "ValidWhen Error 'test' parameter is missing for field ' " + field.getKey() + "'";
109             errors.add(field.getKey(), new ActionMessage(msg, false));
110             log.error(msg);
111             return false;
112         }
113         
114         // Create the Lexer
115
ValidWhenLexer lexer= null;
116         try {
117             lexer = new ValidWhenLexer(new StringReader JavaDoc(test));
118         } catch (Exception JavaDoc ex) {
119             String JavaDoc msg = "ValidWhenLexer Error for field ' " + field.getKey() + "' - " + ex;
120             errors.add(field.getKey(), new ActionMessage(msg + " - " + ex, false));
121             log.error(msg);
122             log.debug(msg, ex);
123             return false;
124         }
125
126         // Create the Parser
127
ValidWhenParser parser = null;
128         try {
129             parser = new ValidWhenParser(lexer);
130         } catch (Exception JavaDoc ex) {
131             String JavaDoc msg = "ValidWhenParser Error for field ' " + field.getKey() + "' - " + ex;
132             errors.add(field.getKey(), new ActionMessage(msg, false));
133             log.error(msg);
134             log.debug(msg, ex);
135             return false;
136         }
137
138
139         parser.setForm(form);
140         parser.setIndex(index);
141         parser.setValue(value);
142
143         try {
144             parser.expression();
145             valid = parser.getResult();
146             
147         } catch (Exception JavaDoc ex) {
148
149             // errors.add(
150
// field.getKey(),
151
// Resources.getActionMessage(validator, request, va, field));
152

153             String JavaDoc msg = "ValidWhen Error for field ' " + field.getKey() + "' - " + ex;
154             errors.add(field.getKey(), new ActionMessage(msg, false));
155             log.error(msg);
156             log.debug(msg, ex);
157                 
158             return false;
159         }
160         
161         if (!valid) {
162             errors.add(
163                 field.getKey(),
164                 Resources.getActionMessage(validator, request, va, field));
165                 
166             return false;
167         }
168         
169         return true;
170     }
171     
172 }
173
Popular Tags