001: /*
002: * $Id: ValidWhen.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.validator.validwhen;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.commons.validator.Field;
026: import org.apache.commons.validator.Validator;
027: import org.apache.commons.validator.ValidatorAction;
028: import org.apache.commons.validator.util.ValidatorUtils;
029: import org.apache.struts.action.ActionMessage;
030: import org.apache.struts.action.ActionMessages;
031: import org.apache.struts.util.MessageResources;
032: import org.apache.struts.validator.Resources;
033:
034: import javax.servlet.http.HttpServletRequest;
035:
036: import java.io.StringReader;
037:
038: /**
039: * This class contains the validwhen validation that is used in the
040: * validator-rules.xml file.
041: *
042: * @since Struts 1.2
043: */
044: public class ValidWhen {
045: /**
046: * Commons Logging instance.
047: */
048: private static final Log log = LogFactory.getLog(ValidWhen.class);
049:
050: /**
051: * The message resources for this package.
052: */
053: private static MessageResources sysmsgs = MessageResources
054: .getMessageResources("org.apache.struts.validator.LocalStrings");
055:
056: /**
057: * Returns true if <code>obj</code> is null or a String.
058: */
059: private static boolean isString(Object obj) {
060: return (obj == null) ? true : String.class.isInstance(obj);
061: }
062:
063: /**
064: * Checks if the field matches the boolean expression specified in
065: * <code>test</code> parameter.
066: *
067: * @param bean The bean validation is being performed on.
068: * @param va The <code>ValidatorAction</code> that is currently being
069: * performed.
070: * @param field The <code>Field</code> object associated with the
071: * current field being validated.
072: * @param errors The <code>ActionMessages</code> object to add errors to
073: * if any validation errors occur.
074: * @param request Current request object.
075: * @return <code>true</code> if meets stated requirements,
076: * <code>false</code> otherwise.
077: */
078: public static boolean validateValidWhen(Object bean,
079: ValidatorAction va, Field field, ActionMessages errors,
080: Validator validator, HttpServletRequest request) {
081: Object form = validator.getParameterValue(Validator.BEAN_PARAM);
082: String value = null;
083: boolean valid = false;
084: int index = -1;
085:
086: if (field.isIndexed()) {
087: String key = field.getKey();
088:
089: final int leftBracket = key.indexOf("[");
090: final int rightBracket = key.indexOf("]");
091:
092: if ((leftBracket > -1) && (rightBracket > -1)) {
093: index = Integer.parseInt(key.substring(leftBracket + 1,
094: rightBracket));
095: }
096: }
097:
098: if (isString(bean)) {
099: value = (String) bean;
100: } else {
101: value = ValidatorUtils.getValueAsString(bean, field
102: .getProperty());
103: }
104:
105: String test = null;
106:
107: try {
108: test = Resources.getVarValue("test", field, validator,
109: request, true);
110: } catch (IllegalArgumentException ex) {
111: String logErrorMsg = sysmsgs.getMessage(
112: "validation.failed", "validwhen", field
113: .getProperty(), ex.toString());
114:
115: log.error(logErrorMsg);
116:
117: String userErrorMsg = sysmsgs.getMessage("system.error");
118:
119: errors.add(field.getKey(), new ActionMessage(userErrorMsg,
120: false));
121:
122: return false;
123: }
124:
125: // Create the Lexer
126: ValidWhenLexer lexer = null;
127:
128: try {
129: lexer = new ValidWhenLexer(new StringReader(test));
130: } catch (Exception ex) {
131: String logErrorMsg = "ValidWhenLexer Error for field ' "
132: + field.getKey() + "' - " + ex;
133:
134: log.error(logErrorMsg);
135:
136: String userErrorMsg = sysmsgs.getMessage("system.error");
137:
138: errors.add(field.getKey(), new ActionMessage(userErrorMsg,
139: false));
140:
141: return false;
142: }
143:
144: // Create the Parser
145: ValidWhenParser parser = null;
146:
147: try {
148: parser = new ValidWhenParser(lexer);
149: } catch (Exception ex) {
150: String logErrorMsg = "ValidWhenParser Error for field ' "
151: + field.getKey() + "' - " + ex;
152:
153: log.error(logErrorMsg);
154:
155: String userErrorMsg = sysmsgs.getMessage("system.error");
156:
157: errors.add(field.getKey(), new ActionMessage(userErrorMsg,
158: 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 = "ValidWhen Error for field ' "
172: + 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,
179: false));
180:
181: return false;
182: }
183:
184: if (!valid) {
185: errors.add(field.getKey(), Resources.getActionMessage(
186: validator, request, va, field));
187:
188: return false;
189: }
190:
191: return true;
192: }
193: }
|