01: /*
02: Mdarad-Toolobox is a collection of tools for Architected RAD
03: (Rapid Application Development) based on an MDA approach.
04: The toolbox contains frameworks and generators for many environments
05: (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
06: applications from a design Model
07: Copyright (C) 2004-2005 Elapse Technologies Inc.
08:
09: 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,
44: Class type, boolean canBeNull) {
45: if (type == null) {
46: throw new IllegalArgumentException(
47: "The type passed should not be null");
48: }
49:
50: // the form is valid
51: if (canBeNull && form == null) {
52: return;
53: }
54:
55: // check if the object is null
56: if (!canBeNull && form == null) {
57: throw new FormException(
58: "The form object should not be null");
59: }
60:
61: Class formClass = form.getClass();
62:
63: // check if the object has the right instance
64: if (!canBeNull && !type.isAssignableFrom(formClass)) {
65: throw new FormException(
66: "The form object should be an instance of "
67: + type.getName() + "but is an instance of "
68: + form.getClass().getName());
69: }
70: }
71: }
|