1 /* 2 * #%L 3 * Nuiton Validator 4 * %% 5 * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit 6 * %% 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as 9 * published by the Free Software Foundation, either version 3 of the 10 * License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Lesser Public License for more details. 16 * 17 * You should have received a copy of the GNU General Lesser Public 18 * License along with this program. If not, see 19 * <http://www.gnu.org/licenses/lgpl-3.0.html>. 20 * #L% 21 */ 22 package org.nuiton.validator.xwork2.field; 23 24 import com.opensymphony.xwork2.validator.ValidationException; 25 import com.opensymphony.xwork2.validator.validators.FieldExpressionValidator; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 /** 30 * Nuiton default field validator. 31 * 32 * This validator offers a {@link #skip} property that can be used to skip or 33 * not the validator, this property is a OGNL expression. 34 * 35 * To use this new field validator support, just now implements the method 36 * {@link #validateWhenNotSkip(Object)}. This method will be invoked only if the skip 37 * parameter is evaluated to {@code false}. 38 * 39 * @author Tony Chemit - chemit@codelutin.com 40 * @since 2.3 41 */ 42 public class NuitonFieldExpressionValidator extends FieldExpressionValidator { 43 44 private static final Log log = LogFactory.getLog(NuitonFieldExpressionValidator.class); 45 46 /** 47 * extra parameter at the very beginning of the 48 * {@link #validate(Object)} method to be able to skip (or not) the 49 * validator execution. 50 * 51 * by default the value is {@code false} : it seems fair to want to 52 * validate if the validator is used :D... 53 */ 54 protected String skip = "false"; 55 56 /** 57 * Sets the value of the {@link #skip} parameter. 58 * 59 * @param skip the new value of the {@link #skip} parameter 60 */ 61 public void setSkip(String skip) { 62 this.skip = skip; 63 } 64 65 /** 66 * Method to be invoked when skip parameter was not evaludated to {@code true}. 67 * 68 * @param object the object to be validated. 69 * @throws ValidationException is thrown if there is validation error(s). 70 */ 71 72 protected void validateWhenNotSkip(Object object) throws ValidationException { 73 super.validate(object); 74 } 75 76 @Override 77 public void validate(Object object) throws ValidationException { 78 79 // evaluate the skip parameter 80 boolean mustSkip = evaluateSkipParameter(object); 81 82 if (mustSkip) { 83 84 // skip is set to true, so skip the validation 85 if (log.isDebugEnabled()) { 86 log.debug("Skip the validation from " + this + 87 ", due to skip parameter evaluated to true"); 88 } 89 return; 90 } 91 92 // must validate 93 validateWhenNotSkip(object); 94 } 95 96 /** 97 * Evaluate the skip parameter value against the object to validate. 98 * 99 * This parameter can be an OGNL expression. 100 * 101 * @param object the object to validate 102 * @return the evaluation of the skip parameter. 103 * @throws ValidationException if could not evaluate the parameter 104 */ 105 protected boolean evaluateSkipParameter(Object object) throws ValidationException { 106 107 skip = skip.trim(); 108 109 if ("false".equals(skip)) { 110 111 return false; 112 } 113 114 if ("true".equals(skip)) { 115 116 return true; 117 } 118 119 try { 120 Boolean answer = Boolean.FALSE; 121 Object obj; 122 obj = getFieldValue(skip, object); 123 if (obj != null && obj instanceof Boolean) { 124 answer = (Boolean) obj; 125 } 126 return answer; 127 } catch (ValidationException e) { 128 throw e; 129 } catch (Exception e) { 130 // let this pass, but it will be logged right below 131 throw new ValidationException( 132 "Can not evaluate boolean expression [" + skip + 133 "] for reason " + e.getMessage()); 134 } 135 136 } 137 138 }