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 java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.Set;
022:
023: import org.mvel.Interpreter;
024:
025: import org.iscreen.ConfigurationException;
026: import org.iscreen.Validator;
027: import org.iscreen.impl.BaseConfiguredValidator;
028:
029: /**
030: * This ValidatorWrapper represents validators for MVEL-based
031: * XML configurations.
032: *
033: * @author Shellman, Dan
034: */
035: public class MvelConfiguredValidator extends BaseConfiguredValidator {
036: public static final String DEFAULT_TO = "value";
037: public static final String DEFAULT_FROM = "this";
038:
039: /**
040: * Default constructor.
041: */
042: public MvelConfiguredValidator() {
043: } //end MvelConfiguredValidator()
044:
045: /**
046: * Maps the appropriate properties from one object to another.
047: *
048: * @param from The object to map from.
049: * @param to The object to map to.
050: */
051: protected void executeMappings(Object from, Object to) {
052: Iterator it;
053: Set theMappings;
054:
055: theMappings = getMappings();
056: if (theMappings.isEmpty()) {
057: theMappings.add(getDefaultMapping());
058: }
059:
060: it = theMappings.iterator();
061: while (it.hasNext()) {
062: MvelObjectMapping mapping;
063:
064: mapping = (MvelObjectMapping) it.next();
065: mapping.map(from, to);
066: }
067: } //end executeMappings()
068:
069: /**
070: * Adds a mapping for mapping the object being validated to the
071: * beanToValidate object (that the Validator created).
072: *
073: * @param from The OGNL from expression (getter)
074: * @param to The OGNL to expression (setter)
075: */
076: public void addMapping(String from, String to) {
077: if (from == null || from.trim().equals("")) {
078: from = DEFAULT_FROM;
079: }
080:
081: if (to == null || to.trim().equals("")) {
082: to = DEFAULT_TO;
083: }
084:
085: mappings.add(new MvelObjectMapping(from, to));
086: } //end addMapping()
087:
088: /**
089: * Adds a "static" property to set on the underlying Validator. A
090: * "static" property is really an MVEL expression that will set
091: * some value on the Validator once (such as a constraint, service,
092: * or failure message).<br />
093: * <br />
094: * For failures, the object type should be an MvelMessage. For
095: * a constraint or service, any object type is fine (as long as it
096: * maps to the underlying Validator property).
097: *
098: *
099: * @param property The MVEL expression to set a value
100: * @param obj The value to set, once, on the Validator to configure it.
101: */
102: public void addStaticProperty(String property, Object obj) {
103: staticProperties.add(new MvelPropertyMapping(property, obj));
104: }
105:
106: /**
107: * Create a default mapping so that if no mapping is defined in configuration,
108: * a default mapping can be used.
109: *
110: * @return Returns a default configured mapping.
111: */
112: protected static MvelObjectMapping getDefaultMapping() {
113: return new MvelObjectMapping(DEFAULT_FROM, DEFAULT_TO);
114: } //end getDefaultMapping()
115:
116: /**
117: * Creates the Validator and configures it. Multiple calls will NOT
118: * construct a new Validator each call, but will return the same
119: * instance created the first time.
120: *
121: *
122: * @return Returns a created and configured Validator.
123: */
124: public Validator getConfiguredValidator() {
125: Iterator it;
126:
127: if (validator != null) {
128: return validator;
129: }
130:
131: try {
132: validator = (Validator) getValidatorClass().newInstance();
133: } catch (InstantiationException e) {
134: throw new ConfigurationException(
135: "Unable to construct validator using class named "
136: + getValidatorClass().getName(), e);
137: } catch (IllegalAccessException e) {
138: throw new ConfigurationException(
139: "Illegal access constructing validator using class named "
140: + getValidatorClass().getName(), e);
141: }
142:
143: it = getStaticProperties().iterator();
144: while (it.hasNext()) {
145: MvelPropertyMapping mapping;
146:
147: mapping = (MvelPropertyMapping) it.next();
148: mapping.map(validator);
149: }
150:
151: return validator;
152: } //end getConfiguredValidator()
153:
154: /**
155: * Retrieves the Collection of fields (the 'getter' OGNL expressions)
156: * that are used in mapping properties from the JavaBean/Object being
157: * validated to the validation bean.
158: *
159: * @return Returns the Collection of fields (the 'getter' OGNL expressions).
160: */
161: protected Collection getFields() {
162: Collection col = new ArrayList();
163: Iterator it;
164:
165: it = getMappings().iterator();
166: while (it.hasNext()) {
167: MvelObjectMapping mapping;
168:
169: mapping = (MvelObjectMapping) it.next();
170: col.add(mapping.getGetter());
171: }
172:
173: return col;
174: } //end getFields()
175:
176: protected String convertDoc(Object obj, String unconvertedDoc) {
177: return Interpreter.evalToString(unconvertedDoc, obj);
178: } //end convertDoc()
179: } //end MvelConfiguredValidator
|