001: /*
002: * Copyright 2007 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.mvel;
017:
018: import org.mvel.CompileException;
019: import org.mvel.ExpressionParser;
020:
021: import org.iscreen.ConfigurationException;
022: import org.iscreen.impl.ContextBean;
023: import org.iscreen.impl.DefaultValidationService;
024: import org.iscreen.impl.InternalValidatorContext;
025: import org.iscreen.impl.ValidationServiceValidator;
026:
027: /**
028: *
029: * @author Shellman, Dan
030: */
031: public class MvelValidationServiceValidator extends
032: ValidationServiceValidator {
033: /**
034: * Default constructor.
035: */
036: public MvelValidationServiceValidator(
037: DefaultValidationService service) {
038: super (service);
039: } //end MvelValidationServiceValidator()
040:
041: // ***
042: // Protected methods
043: // ***
044:
045: /**
046: * Determines whether we're iterating over the mapped object.
047: *
048: * @param context The ValidatorContext
049: * @param root The ContextBean
050: * @param obj The object being validated.
051: *
052: * @return Returns true if we should iterate.
053: */
054: protected boolean shouldIterate(InternalValidatorContext context,
055: ContextBean root, Object obj) {
056: String itExp = getIterateExpression();
057:
058: if (itExp != null
059: && !itExp.trim().equals("")
060: && (itExp.equalsIgnoreCase("true") || itExp
061: .equalsIgnoreCase("yes"))) {
062: return true;
063: }
064:
065: return false;
066: } //end shouldIterate()
067:
068: /**
069: * Determines whether the inclusion/call to the validation set
070: * should occur.
071: *
072: * @param context The ValidatorContext
073: * @param root The ContextBean
074: * @param obj The object being validated.
075: *
076: * @return Returns true if the validation set should be called/executed.
077: */
078: protected boolean shouldExecute(InternalValidatorContext context,
079: ContextBean root, Object obj) {
080: String exp = getIfExpression();
081:
082: if (exp != null && !exp.trim().equals("")) {
083: Boolean ifFlag;
084:
085: try {
086: ifFlag = (Boolean) ExpressionParser.evalToBoolean(exp,
087: obj);
088: if (!ifFlag.booleanValue()) {
089: //This means the 'if' expression returned false.
090: return false;
091: }
092: } catch (CompileException e) {
093: throw new ConfigurationException(
094: "Invalid MVEL expression in 'if' expression of "
095: + exp, e);
096: }
097: }
098:
099: return true;
100: } //end shouldExecute()
101:
102: /**
103: * Handle the mapping of the object being validated to the property
104: * that will be validated (may be the object, itself).
105: *
106: * @param context The ValidatorContext
107: * @param contextBean The ContextBean
108: * @param obj The object being validated.
109: *
110: * @return Returns the object that will be forwarded to the called
111: * validation set.
112: */
113: protected Object mapObject(InternalValidatorContext context,
114: ContextBean contextBean, Object obj) {
115: Object mappedObj;
116: String exp = getMapExpression();
117:
118: if (exp == null || exp.trim().equals("")) {
119: mappedObj = obj;
120: } else {
121: try {
122: mappedObj = ExpressionParser.eval(exp, obj);
123: } catch (CompileException e) {
124: throw new ConfigurationException(
125: "Invalid MVEL mapping expression of " + exp, e);
126: }
127: }
128:
129: return mappedObj;
130: } //end mapObject()
131: } //end MvelValidationServiceValidator
|