001: /*
002: * Copyright 2006-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 java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.Locale;
022: import java.util.Set;
023:
024: import org.iscreen.ConfigurationException;
025: import org.iscreen.Validator;
026: import org.iscreen.impl.BaseConfiguredValidator;
027:
028: /**
029: * A special validator wrapper that wraps a Validator and the
030: * configuration associated with it.
031: *
032: * @author Shellman, Dan
033: */
034: public class OgnlConfiguredValidator extends BaseConfiguredValidator {
035: public static final String DEFAULT_TO = "value";
036: public static final String DEFAULT_FROM = "#root";
037:
038: /**
039: * Default constructor.
040: */
041: public OgnlConfiguredValidator() {
042: } //end OgnlConfiguredValidator()
043:
044: /**
045: * Maps the appropriate properties from one object to another.
046: *
047: * @param from The object to map from.
048: * @param to The object to map to.
049: */
050: protected void executeMappings(Object from, Object to) {
051: Iterator it;
052: Set theMappings;
053:
054: theMappings = getMappings();
055: if (theMappings.isEmpty()) {
056: theMappings.add(getDefaultMapping());
057: }
058:
059: it = theMappings.iterator();
060: while (it.hasNext()) {
061: OgnlObjectMapping mapping;
062:
063: mapping = (OgnlObjectMapping) it.next();
064: mapping.map(from, to);
065: }
066: } //end executeMappings()
067:
068: /**
069: * Adds a mapping for mapping the object being validated to the
070: * beanToValidate object (that the Validator created).
071: *
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 OgnlObjectMapping(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 OGNL 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 OgnlMessage. 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 OGNL 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 OgnlPropertyMapping(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: *
111: * @return Returns a default configured mapping.
112: */
113: protected static OgnlObjectMapping getDefaultMapping() {
114: return new OgnlObjectMapping(DEFAULT_FROM, DEFAULT_TO);
115: } //end getDefaultMapping()
116:
117: /**
118: * Creates the Validator and configures it. Multiple calls will NOT
119: * construct a new Validator each call, but will return the same
120: * instance created the first time.
121: *
122: *
123: * @return Returns a created and configured Validator.
124: */
125: public Validator getConfiguredValidator() {
126: Iterator it;
127:
128: if (validator != null) {
129: return validator;
130: }
131:
132: try {
133: validator = (Validator) getValidatorClass().newInstance();
134: } catch (InstantiationException e) {
135: throw new ConfigurationException(
136: "Unable to construct validator using class named "
137: + getValidatorClass().getName(), e);
138: } catch (IllegalAccessException e) {
139: throw new ConfigurationException(
140: "Illegal access constructing validator using class named "
141: + getValidatorClass().getName(), e);
142: }
143:
144: it = getStaticProperties().iterator();
145: while (it.hasNext()) {
146: OgnlPropertyMapping mapping;
147:
148: mapping = (OgnlPropertyMapping) it.next();
149: mapping.map(validator);
150: }
151:
152: return validator;
153: } //end getConfiguredValidator()
154:
155: /**
156: * Retrieves the Collection of fields (the 'getter' OGNL expressions)
157: * that are used in mapping properties from the JavaBean/Object being
158: * validated to the validation bean.
159: *
160: * @return Returns the Collection of fields (the 'getter' OGNL expressions).
161: */
162: protected Collection getFields() {
163: Collection col = new ArrayList();
164: Iterator it;
165:
166: it = getMappings().iterator();
167: while (it.hasNext()) {
168: OgnlObjectMapping mapping;
169:
170: mapping = (OgnlObjectMapping) it.next();
171: col.add(mapping.getGetter());
172: }
173:
174: return col;
175: } //end getFields()
176:
177: protected String convertDoc(Object obj, String unconvertedDoc) {
178: OgnlMessage message = new OgnlMessage(unconvertedDoc);
179:
180: return message.getMessage(obj, Locale.getDefault());
181: } //end convertDoc()
182: } //end OgnlConfiguredValidator
|