KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > apache > struts > ActionFormSubTask


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.apache.struts;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import xjavadoc.XClass;
10 import xjavadoc.XTag;
11
12 import xdoclet.TemplateSubTask;
13 import xdoclet.XDocletException;
14 import xdoclet.XDocletMessages;
15 import xdoclet.modules.apache.struts.ejb.XDocletModulesApacheStrutsEjbMessages;
16 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
17 import xdoclet.tagshandler.PackageTagsHandler;
18 import xdoclet.util.Translator;
19
20 /**
21  * Generates a Struts ActionForm, based on a POJO. More information on Struts is available on the <a
22  * HREF="http://struts.apache.org/">Struts website</a> , or the <a
23  * HREF="http://struts.apache.org/api/org/apache/struts/action/ActionForm.html">ActionForm API</a>.
24  *
25  * @author Matt Raible (matt@raibledesigns.com)
26  * @created April 26, 2004
27  * @ant.element display-name="Action Form" name="actionform" parent="xdoclet.DocletTask"
28  * @version $Revision: 1.2 $
29  */

30 public class ActionFormSubTask extends TemplateSubTask
31 {
32     /**
33      * The default template file - struts_form.xdt.
34      */

35     protected static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/struts_form.xdt";
36
37     /**
38      * The pattern for the form class. Defaults to {0}{1}Form if not present.
39      */

40     protected String JavaDoc formClassPattern;
41
42     /**
43      * Form tag being processed right now.
44      */

45     protected XTag currentFormTag;
46
47     /**
48      * Describe what the ActionFormActionFormSubTaskstructor does
49      */

50     public ActionFormSubTask()
51     {
52         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
53         setDestinationFile(getActionFormClassPattern() + ".java");
54         addOfType("java.lang.Object");
55     }
56
57     /**
58      * Gets the CurrentFormTag attribute of the ActionFormSubTask object
59      *
60      * @return The CurrentFormTag value
61      */

62     public XTag getCurrentFormTag()
63     {
64         return currentFormTag;
65     }
66
67     /**
68      * Return the class pattern.
69      *
70      * @return The ActionFormClassPattern value
71      */

72     public String JavaDoc getActionFormClassPattern()
73     {
74         if (formClassPattern != null) {
75             return formClassPattern;
76         }
77         else {
78             return "{0}{1}Form";
79         }
80     }
81
82     /**
83      * Sets the CurrentFormTag attribute of the ActionFormSubTask object
84      *
85      * @param t The new CurrentFormTag value
86      */

87     public void setCurrentFormTag(XTag t)
88     {
89         this.currentFormTag = t;
90     }
91
92     /**
93      * Sets the Pattern attribute of the ActionFormSubTask object
94      *
95      * @param newPattern The new Pattern value
96      */

97     public void setPattern(String JavaDoc newPattern)
98     {
99         formClassPattern = newPattern;
100     }
101
102     /**
103      * Called to validate configuration parameters.
104      *
105      * @exception XDocletException Description of Exception
106      */

107     public void validateOptions() throws XDocletException
108     {
109         super.validateOptions();
110
111         if (getActionFormClassPattern() == null || getActionFormClassPattern().trim().equals("")) {
112             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"pattern"}));
113         }
114
115         if (getActionFormClassPattern().indexOf("{0}") == -1) {
116             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.PATTERN_HAS_NO_PLACEHOLDER));
117         }
118         if (getActionFormClassPattern().indexOf("{1}") == -1) {
119             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesApacheStrutsEjbMessages.PATTERN_HAS_NO_FORM_PLACEHOLDER));
120         }
121     }
122
123     /**
124      * Gets the GeneratedFileName attribute of the ActionFormSubTask object
125      *
126      * @param clazz Describe what the parameter does
127      * @return The GeneratedFileName value
128      * @exception XDocletException Describe the exception
129      */

130     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
131     {
132         return PackageTagsHandler.packageNameAsPathFor(ActionFormTagsHandler.getActionFormClassFor(getCurrentClass())) + ".java";
133     }
134
135     /**
136      * Returns whether struts form[s] shall be generated for this class
137      *
138      * @param clazz Description of Parameter
139      * @return is form tag shall be generated
140      * @exception XDocletException Description of Exception
141      */

142     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
143     {
144         if (super.matchesGenerationRules(clazz) == false) {
145             return false;
146         }
147
148         if (ActionFormTagsHandler.hasFormDefinition(getCurrentClass())) {
149             return true;
150         }
151         else {
152             return false;
153         }
154     }
155
156     /**
157      * iterate through all struts:form tags,and produce separate classes
158      *
159      * @param clazz Description of Parameter
160      * @exception XDocletException Description of Exception
161      */

162     protected void generateForClass(XClass clazz) throws XDocletException
163     {
164         Collection JavaDoc formTags = clazz.getDoc().getTags("struts.form");
165
166         for (Iterator JavaDoc i = formTags.iterator(); i.hasNext(); ) {
167             XTag tag = (XTag) i.next();
168
169             setCurrentFormTag(tag);
170             super.generateForClass(clazz);
171         }
172     }
173
174     /**
175      * Describe what the method does
176      *
177      * @exception XDocletException Describe the exception
178      */

179     protected void engineStarted() throws XDocletException
180     {
181         System.out.println(Translator.getString(XDocletModulesApacheStrutsEjbMessages.class, XDocletModulesApacheStrutsEjbMessages.GENERATING_FILE,
182             new String JavaDoc[]{ActionFormTagsHandler.getActionFormClassName(getCurrentClass())}));
183     }
184 }
185
Popular Tags