1 18 19 package org.apache.struts.validator.validwhen; 20 21 import java.io.StringReader ; 22 23 import javax.servlet.http.HttpServletRequest ; 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 41 public class ValidWhen { 42 43 46 private static final Log log = LogFactory.getLog(ValidWhen.class); 47 48 51 private static boolean isString(Object obj) { 52 return (obj == null) ? true : String .class.isInstance(obj); 53 } 54 55 75 public static boolean validateValidWhen( 76 Object bean, 77 ValidatorAction va, 78 Field field, 79 ActionMessages errors, 80 Validator validator, 81 HttpServletRequest request) { 82 83 Object form = validator.getParameterValue(Validator.BEAN_PARAM); 84 String value = null; 85 boolean valid = false; 86 int index = -1; 87 88 if (field.isIndexed()) { 89 String 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 ) bean; 102 } else { 103 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 104 } 105 106 String test = field.getVarValue("test"); 107 if (test == null) { 108 String 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 ValidWhenLexer lexer= null; 116 try { 117 lexer = new ValidWhenLexer(new StringReader (test)); 118 } catch (Exception ex) { 119 String 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 ValidWhenParser parser = null; 128 try { 129 parser = new ValidWhenParser(lexer); 130 } catch (Exception ex) { 131 String 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 ex) { 148 149 153 String 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 |