KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > validator > BeanValidatorForm


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.struts.validator;
17
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.lang.reflect.Array JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import org.apache.struts.action.ActionMapping;
24 import org.apache.commons.beanutils.DynaClass;
25 import org.apache.commons.beanutils.DynaBean;
26 import org.apache.commons.beanutils.WrapDynaBean;
27 import org.apache.commons.beanutils.ConvertUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * <p>Struts <i>validator</i> <code>ActionForm</code> backed by either a <code>DynaBean</code> or POJO JavaBean.</p>
33  *
34  * <p>Passing a POJO JavaBean to the constructor will automatically create an associated
35  * <code>WrapDynaBean</code>. One use for this would be to migrate <i>view</i>
36  * objects from an existing system which, for the usual reasons, can't be changed to extend
37  * <ActionForm</code>.</p>
38  *
39  * <p>This form is based on the standard struts <code>ValidatorForm</code> for use with the
40  * <i>Validator</i> framework and validates either using the <i>name</i> from the Struts
41  * <code>ActionMapping</code> or the <code>ActionMapping</code>'s path depending on
42  * whether <code>pathValidation</code> is <code>true</code> or <code>false</code>.</p>
43  */

44 public class BeanValidatorForm extends ValidatorForm implements DynaBean, Serializable JavaDoc {
45
46    /**
47     * Commons Logging
48     */

49     protected static Log logger = LogFactory.getLog(BeanValidatorForm.class);
50
51     /**
52      * The <code>DynaBean</code> that this ActionForm is backed by.
53      */

54     protected DynaBean dynaBean;
55
56     /**
57      * Indicates whether the ActionMapping's path should be used for the
58      * validation key.
59      */

60     protected boolean pathValidation = false;
61
62     // ------------------- Constructor ----------------------------------
63

64     /**
65      * Construct a new <code>BeanValidatorForm</code> with the specified bean.
66      */

67     public BeanValidatorForm(Object JavaDoc bean) {
68
69         if (bean instanceof DynaBean) {
70
71             dynaBean = (DynaBean)bean;
72
73         } else {
74
75             dynaBean = new WrapDynaBean(bean);
76
77         }
78     }
79
80     // ------------------- Protected Methods ----------------------------------
81

82    /**
83     * <p>Set whether this form should validate based on the <code>ActionMapping</code>'s path.</p>
84     */

85     protected void setPathValidation(boolean pathValidation) {
86         this.pathValidation = pathValidation;
87     }
88
89    /**
90     * <p>Indicates whether this form should validate based on the <code>ActionMapping</code>'s path.</p>
91     */

92     protected boolean isPathValidation() {
93         return pathValidation;
94     }
95
96
97     // ------------------- Public Methods ----------------------------------
98

99    /**
100     * <p>Return the <code>DynaBean</code> that this <code>ActionForm</code> is backed by.</p>
101     */

102     public DynaBean getDynaBean() {
103         return dynaBean;
104     }
105
106    /**
107     * <p>Return the <code>Bean</code> that this <code>ActionForm</code> is backed by.</p>
108     *
109     * <p>If the <code>DynaBean</code> is a <code>WrapDynaBean</code> type then this method
110     * returns the 'Wrapped' POJO bean associated with it. If you require the actual <code>WrapDynaBean</code>
111     * then use the <code>getDynaBean()</code> method.</p>
112     */

113     public Object JavaDoc getInstance() {
114
115         if (dynaBean instanceof WrapDynaBean) {
116             return ((WrapDynaBean)dynaBean).getInstance();
117         }
118
119         return dynaBean;
120
121     }
122
123    /**
124     * <p>Return the size of an indexed or mapped property.</p>
125     */

126     public int size(String JavaDoc name) {
127
128         Object JavaDoc value = dynaBean.get(name);
129         if (value == null) {
130             return 0;
131         }
132
133         if (value instanceof Map JavaDoc) {
134             return ((Map JavaDoc)value).size();
135         }
136
137         if (value instanceof List JavaDoc) {
138             return ((List JavaDoc)value).size();
139         }
140
141         if ((value.getClass().isArray())) {
142             return Array.getLength(value);
143         }
144
145         return 0;
146
147     }
148
149     // ------------------- ValidatorForm Methods ----------------------------------
150

151     /**
152      * Returns the Validation key
153      *
154      * @param mapping The mapping used to select this instance
155      * @param request The servlet request we are processing
156      * @return validation key to use
157      */

158     public String JavaDoc getValidationKey(ActionMapping mapping,
159                                    HttpServletRequest JavaDoc request) {
160
161         String JavaDoc validationKey = null;
162
163         if (isPathValidation()) {
164
165             // Get the path replacing any slashes by underscore
166
validationKey = mapping.getPath();
167
168             // Remove any leading slash
169
if (validationKey.charAt(0) == '/') {
170                 validationKey = validationKey.substring(1);
171             }
172
173             // Replace any slashes by underscore
174
if (validationKey.indexOf("/") > 0) {
175                 validationKey = validationKey.replace('/', '_');
176             }
177
178         } else {
179
180             validationKey = mapping.getAttribute();
181
182         }
183
184         if (logger.isDebugEnabled()) {
185             logger.debug("Validating ActionForm '" + mapping.getName() +
186                          "' using key '" + validationKey +
187                          "' for mapping '" + mapping.getPath() + "'");
188         }
189
190
191         return validationKey;
192
193     }
194
195     // ------------------- DynaBean Methods ----------------------------------
196

197    /**
198     * Return the <code>DynaClass</code> instance that describes the set of
199     * properties available for this DynaBean.
200     */

201     public DynaClass getDynaClass() {
202         return dynaBean.getDynaClass();
203     }
204
205    /**
206      * Return the value of a simple property with the specified name.
207      *
208      * @param name Name of the property whose value is to be retrieved
209     */

210     public Object JavaDoc get(String JavaDoc name) {
211         return dynaBean.get(name);
212     }
213
214    /**
215      * Return the value of an indexed property with the specified name.
216      *
217      * @param name Name of the property whose value is to be retrieved
218      * @param index Index of the value to be retrieved
219     */

220     public Object JavaDoc get(String JavaDoc name, int index) {
221         return dynaBean.get(name, index);
222     }
223
224    /**
225      * Return the value of a mapped property with the specified name,
226      * or <code>null</code> if there is no value for the specified key.
227      *
228      * @param name Name of the property whose value is to be retrieved
229      * @param key Key of the value to be retrieved
230     */

231     public Object JavaDoc get(String JavaDoc name, String JavaDoc key) {
232         return dynaBean.get(name, key);
233     }
234
235    /**
236      * Set the value of a simple property with the specified name.
237      *
238      * @param name Name of the property whose value is to be set
239      * @param value Value to which this property is to be set
240     */

241     public void set(String JavaDoc name, Object JavaDoc value) {
242
243         // Set the page number (for validator)
244
if ("page".equals(name)) {
245
246             if (value == null) {
247                 page = 0;
248             } else if (value instanceof Integer JavaDoc) {
249                 page = ((Integer JavaDoc)value).intValue();
250             } else {
251                 try {
252                     page = ((Integer JavaDoc)ConvertUtils.convert(value.toString(), Integer JavaDoc.class)).intValue();
253                 }
254                 catch (Exception JavaDoc ignore) {
255                     page = 0;
256                 }
257             }
258         }
259
260         dynaBean.set(name, value);
261
262     }
263
264    /**
265      * Set the value of an indexed property with the specified name.
266      *
267      * @param name Name of the property whose value is to be set
268      * @param index Index of the property to be set
269      * @param value Value to which this property is to be set
270     */

271     public void set(String JavaDoc name, int index, Object JavaDoc value) {
272         dynaBean.set(name, index, value);
273     }
274
275    /**
276      * Set the value of a mapped property with the specified name.
277      *
278      * @param name Name of the property whose value is to be set
279      * @param key Key of the property to be set
280      * @param value Value to which this property is to be set
281     */

282     public void set(String JavaDoc name, String JavaDoc key, Object JavaDoc value) {
283         dynaBean.set(name, key, value);
284     }
285
286    /**
287      * Does the specified mapped property contain a value for the specified
288      * key value?
289      *
290      * @param name Name of the property to check
291      * @param key Name of the key to check
292     */

293     public boolean contains(String JavaDoc name, String JavaDoc key) {
294         return dynaBean.contains(name, key);
295     }
296
297    /**
298      * Remove any existing value for the specified key on the
299      * specified mapped property.
300      *
301      * @param name Name of the property for which a value is to
302      * be removed
303      * @param key Key of the value to be removed
304     */

305     public void remove(String JavaDoc name, String JavaDoc key) {
306         dynaBean.remove(name, key);
307     }
308 }
Popular Tags