001 // Copyright 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.form.validator;
016
017 import org.apache.hivemind.util.PropertyUtils;
018 import org.apache.tapestry.IMarkupWriter;
019 import org.apache.tapestry.IRequestCycle;
020 import org.apache.tapestry.form.FormComponentContributorContext;
021 import org.apache.tapestry.form.IFormComponent;
022 import org.apache.tapestry.form.TranslatedField;
023 import org.apache.tapestry.form.translator.Translator;
024 import org.apache.tapestry.json.JSONObject;
025
026 /**
027 * Abstract implementation of {@link org.apache.tapestry.form.validator.Validator}.
028 *
029 * @author Howard Lewis Ship
030 * @since 4.0
031 */
032
033 public abstract class BaseValidator implements Validator
034 {
035 private String _message;
036
037 public BaseValidator()
038 {
039 }
040
041 public BaseValidator(String initializer)
042 {
043 PropertyUtils.configureProperties(this, initializer);
044 }
045
046 public String getMessage()
047 {
048 return _message;
049 }
050
051 public void setMessage(String message)
052 {
053 _message = message;
054 }
055
056 /**
057 * Returns false.
058 */
059
060 public boolean getAcceptsNull()
061 {
062 return false;
063 }
064
065 /**
066 * Does nothing.
067 */
068
069 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
070 FormComponentContributorContext context, IFormComponent field)
071 {
072 }
073
074 /**
075 * Returns false. Subclasses may override.
076 */
077
078 public boolean isRequired()
079 {
080 return false;
081 }
082
083 /**
084 * Utility method to store a field specific profile property which can later
085 * be used by client side validation.
086 *
087 * @param field
088 * The field to store the property for, will key off of {@link IFormComponent#getClientId()}.
089 * @param profile
090 * The profile for the form.
091 * @param key
092 * The property key to store.
093 * @param property
094 * The property to store.
095 */
096 public void accumulateProfileProperty(IFormComponent field, JSONObject profile,
097 String key, Object property)
098 {
099 if (!profile.has(field.getClientId()))
100 profile.put(field.getClientId(), new JSONObject());
101
102 JSONObject fieldProps = profile.getJSONObject(field.getClientId());
103
104 accumulateProperty(fieldProps, key, property);
105 }
106
107 /**
108 * Utility used to append onto an existing property represented as an
109 * object array.
110 * @param profile
111 * @param key
112 * @param value
113 */
114 public void accumulateProperty(JSONObject profile, String key, Object value)
115 {
116 profile.accumulate(key, value);
117 }
118
119 /**
120 * Used to grab the corresponding {@link Translator} for
121 * the field, if one exists.
122 * @param field
123 * @return The translator, or null if the required translator type
124 * doesn't exist.
125 */
126 public Translator getFieldTranslator(IFormComponent field, Class clazz)
127 {
128 if (TranslatedField.class.isAssignableFrom(field.getClass())) {
129 Translator trans = ((TranslatedField)field).getTranslator();
130 if (clazz.isInstance(trans)) {
131 return trans;
132 }
133 }
134
135 return null;
136 }
137 }