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.FieldValidatorSupport;
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 abstract class NuitonFieldValidatorSupport extends FieldValidatorSupport {
43
44 private static final Log log = LogFactory.getLog(NuitonFieldValidatorSupport.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 abstract void validateWhenNotSkip(Object object) throws ValidationException;
73
74 @Override
75 public void validate(Object object) throws ValidationException {
76
77 // evaluate the skip parameter
78 boolean mustSkip = evaluateSkipParameter(object);
79
80 if (mustSkip) {
81
82 // skip is set to true, so skip the validation
83 if (log.isDebugEnabled()) {
84 log.debug("Skip the validation from " + this +
85 ", due to skip parameter evaluated to true");
86 }
87 return;
88 }
89
90 // must validate
91 validateWhenNotSkip(object);
92 }
93
94 /**
95 * Evaluate the skip parameter value against the object to validate.
96 *
97 * This parameter can be an OGNL expression.
98 *
99 * @param object the object to validate
100 * @return the evaluation of the skip parameter.
101 * @throws ValidationException if could not evaluate the parameter
102 */
103 protected boolean evaluateSkipParameter(Object object) throws ValidationException {
104
105 skip = skip.trim();
106
107 if ("false".equals(skip)) {
108
109 return false;
110 }
111
112 if ("true".equals(skip)) {
113
114 return true;
115 }
116
117 try {
118 Boolean answer = Boolean.FALSE;
119 Object obj;
120 obj = getFieldValue(skip, object);
121 if (obj != null && obj instanceof Boolean) {
122 answer = (Boolean) obj;
123 }
124 return answer;
125 } catch (ValidationException e) {
126 throw e;
127 } catch (Exception e) {
128 // let this pass, but it will be logged right below
129 throw new ValidationException(
130 "Can not evaluate boolean expression [" + skip +
131 "] for reason " + e.getMessage());
132 }
133
134 }
135
136 @Override
137 public Object getFieldValue(String name,
138 Object object) throws ValidationException {
139 return super.getFieldValue(name, object);
140 }
141 }