1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.validator.validwhen;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
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.ActionMessage;
30 import org.apache.struts.action.ActionMessages;
31 import org.apache.struts.util.MessageResources;
32 import org.apache.struts.validator.Resources;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import java.io.StringReader;
37
38
39
40
41
42
43
44 public class ValidWhen {
45
46
47
48 private static final Log log = LogFactory.getLog(ValidWhen.class);
49
50
51
52
53 private static MessageResources sysmsgs =
54 MessageResources.getMessageResources(
55 "org.apache.struts.validator.LocalStrings");
56
57
58
59
60 private static boolean isString(Object obj) {
61 return (obj == null) ? true : String.class.isInstance(obj);
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public static boolean validateValidWhen(Object bean, ValidatorAction va,
80 Field field, ActionMessages errors, Validator validator,
81 HttpServletRequest request) {
82 Object form = validator.getParameterValue(Validator.BEAN_PARAM);
83 String value = null;
84 boolean valid = false;
85 int index = -1;
86
87 if (field.isIndexed()) {
88 String key = field.getKey();
89
90 final int leftBracket = key.indexOf("[");
91 final int rightBracket = key.indexOf("]");
92
93 if ((leftBracket > -1) && (rightBracket > -1)) {
94 index =
95 Integer.parseInt(key.substring(leftBracket + 1,
96 rightBracket));
97 }
98 }
99
100 if (isString(bean)) {
101 value = (String) bean;
102 } else {
103 value = ValidatorUtils.getValueAsString(bean, field.getProperty());
104 }
105
106 String test = null;
107
108 try {
109 test =
110 Resources.getVarValue("test", field, validator, request, true);
111 } catch (IllegalArgumentException ex) {
112 String logErrorMsg =
113 sysmsgs.getMessage("validation.failed", "validwhen",
114 field.getProperty(), ex.toString());
115
116 log.error(logErrorMsg);
117
118 String userErrorMsg = sysmsgs.getMessage("system.error");
119
120 errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
121
122 return false;
123 }
124
125
126 ValidWhenLexer lexer = null;
127
128 try {
129 lexer = new ValidWhenLexer(new StringReader(test));
130 } catch (Exception ex) {
131 String logErrorMsg =
132 "ValidWhenLexer Error for field ' " + field.getKey() + "' - "
133 + ex;
134
135 log.error(logErrorMsg);
136
137 String userErrorMsg = sysmsgs.getMessage("system.error");
138
139 errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
140
141 return false;
142 }
143
144
145 ValidWhenParser parser = null;
146
147 try {
148 parser = new ValidWhenParser(lexer);
149 } catch (Exception ex) {
150 String logErrorMsg =
151 "ValidWhenParser Error for field ' " + field.getKey() + "' - "
152 + ex;
153
154 log.error(logErrorMsg);
155
156 String userErrorMsg = sysmsgs.getMessage("system.error");
157
158 errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
159
160 return false;
161 }
162
163 parser.setForm(form);
164 parser.setIndex(index);
165 parser.setValue(value);
166
167 try {
168 parser.expression();
169 valid = parser.getResult();
170 } catch (Exception ex) {
171 String logErrorMsg =
172 "ValidWhen Error for field ' " + field.getKey() + "' - " + ex;
173
174 log.error(logErrorMsg);
175
176 String userErrorMsg = sysmsgs.getMessage("system.error");
177
178 errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
179
180 return false;
181 }
182
183 if (!valid) {
184 errors.add(field.getKey(),
185 Resources.getActionMessage(validator, request, va, field));
186
187 return false;
188 }
189
190 return true;
191 }
192 }