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.ognl;
017:
018: import ognl.Ognl;
019: import ognl.OgnlException;
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: * This is the OGNL-specific validator that handles forwarding from one
029: * validation set to another.
030: *
031: * @author Shellman, Dan
032: */
033: public class OgnlValidationServiceValidator extends
034: ValidationServiceValidator {
035: /**
036: * Default constructor.
037: */
038: public OgnlValidationServiceValidator(
039: DefaultValidationService service) {
040: super (service);
041: } //end OgnlValidationServiceValidator()
042:
043: // ***
044: // Protected methods
045: // ***
046:
047: /**
048: * Determines whether we're iterating over the mapped object.
049: *
050: * @param context The ValidatorContext
051: * @param root The ContextBean
052: * @param obj The object being validated.
053: *
054: * @return Returns true if we should iterate.
055: */
056: protected boolean shouldIterate(InternalValidatorContext context,
057: ContextBean root, Object obj) {
058: if (iterateExp != null
059: && !iterateExp.trim().equals("")
060: && (iterateExp.equalsIgnoreCase("true") || iterateExp
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: if (ifExp != null && !ifExp.trim().equals("")) {
081: Boolean ifFlag;
082:
083: try {
084: ifFlag = (Boolean) Ognl.getValue(ifExp, obj,
085: Boolean.class);
086: if (!ifFlag.booleanValue()) {
087: //This means the 'if' expression returned false.
088: return false;
089: }
090: } catch (OgnlException e) {
091: throw new ConfigurationException(
092: "Invalid OGNL expression in 'if' expression of "
093: + ifExp, e);
094: }
095: }
096:
097: return true;
098: } //end shouldExecute()
099:
100: /**
101: * Handle the mapping of the object being validated to the property
102: * that will be validated (may be the object, itself).
103: *
104: * @param context The ValidatorContext
105: * @param contextBean The ContextBean
106: * @param obj The object being validated.
107: *
108: * @return Returns the object that will be forwarded to the called
109: * validation set.
110: */
111: protected Object mapObject(InternalValidatorContext context,
112: ContextBean contextBean, Object obj) {
113: Object mappedObj;
114:
115: if (mapExp == null || mapExp.trim().equals("")) {
116: mappedObj = obj;
117: } else {
118: try {
119: mappedObj = Ognl.getValue(mapExp, obj);
120: } catch (OgnlException e) {
121: throw new ConfigurationException(
122: "Invalid OGNL mapping expression of " + mapExp,
123: e);
124: }
125: }
126:
127: return mappedObj;
128: } //end mapObject()
129: } //end OgnlValidationServiceValidator
|