KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > struts > action > FormUtils


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.mdarad.framework.util.struts.action;
24
25 import org.apache.struts.action.ActionForm;
26
27 /**
28  * The class FormUtils contains many util methods used
29  * within the forms object and the form actions
30  *
31  * @author Philippe Brouillette
32  * @version 1.0
33  * @since 1.4
34  */

35 public class FormUtils {
36     /**
37      * Method that throws <code>FormException</code> if the instance of the form is of the type
38      * is not of the type passed as argument.
39      * @param form <code>ActionForm</code> to be checked
40      * @param type Class type needed
41      * @param canBeNull flag that indicates if the form object can be null
42      */

43     public static void validateFormInstance(ActionForm form, Class JavaDoc type, boolean canBeNull) {
44         if (type == null) {
45             throw new IllegalArgumentException JavaDoc("The type passed should not be null");
46         }
47         
48         // the form is valid
49
if (canBeNull && form == null) {
50             return;
51         }
52         
53         // check if the object is null
54
if (!canBeNull && form == null) {
55             throw new FormException("The form object should not be null");
56         }
57
58         Class JavaDoc formClass = form.getClass();
59         
60         // check if the object has the right instance
61
if (!canBeNull && !type.isAssignableFrom(formClass)) {
62                 throw new FormException("The form object should be an instance of " + type.getName() +
63                         "but is an instance of " + form.getClass().getName());
64         }
65     }
66 }
Popular Tags