KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > apache > struts > ejb > StrutsFormSubTask


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

5 package xdoclet.modules.apache.struts.ejb;
6
7 import java.util.*;
8 import xjavadoc.*;
9
10 import xdoclet.XDocletException;
11 import xdoclet.XDocletMessages;
12 import xdoclet.modules.apache.struts.StrutsFormTagsHandler;
13 import xdoclet.modules.ejb.AbstractEjbCodeGeneratorSubTask;
14 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
15 import xdoclet.tagshandler.PackageTagsHandler;
16
17 import xdoclet.util.Translator;
18
19 /**
20  * Generates a Struts ActionForm, based on an entity EJB. More information on Struts is available on the <a
21  * HREF="http://struts.apache.org/">Struts website</a> , or the <a
22  * HREF="http://struts.apache.org/api/org/apache/struts/action/ActionForm.html">ActionForm API</a> .
23  *
24  * @author Dmitri Colebatch (dim@bigpond.net.au)
25  * @created September 3, 2001
26  * @ant.element display-name="Struts Form" name="strutsform" parent="xdoclet.modules.ejb.EjbDocletTask"
27  * @version $Revision: 1.12 $
28  */

29 public class StrutsFormSubTask extends AbstractEjbCodeGeneratorSubTask
30 {
31     /**
32      * The default template file - struts_form.xdt.
33      */

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

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

44     protected XTag currentFormTag;
45
46     /**
47      * Describe what the StrutsFormSubTask constructor does
48      */

49     public StrutsFormSubTask()
50     {
51         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
52         setDestinationFile(getStrutsFormClassPattern() + ".java");
53         addOfType("javax.ejb.EntityBean");
54         addOfType("java.lang.Object");
55     }
56
57     /**
58      * Gets the CurrentFormTag attribute of the StrutsFormSubTask 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 StrutsFormClassPattern value
71      */

72     public String JavaDoc getStrutsFormClassPattern()
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 StrutsFormSubTask 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 StrutsFormSubTask 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 (getStrutsFormClassPattern() == null || getStrutsFormClassPattern().trim().equals("")) {
112             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"pattern"}));
113         }
114
115         if (getStrutsFormClassPattern().indexOf("{0}") == -1) {
116             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.PATTERN_HAS_NO_PLACEHOLDER));
117         }
118         if (getStrutsFormClassPattern().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 StrutsFormSubTask 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(StrutsFormTagsHandler.getStrutsFormClassFor(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 (StrutsFormTagsHandler.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 formTags = clazz.getDoc().getTags("struts.form");
165
166         for (Iterator 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[]{StrutsFormTagsHandler.getStrutsFormClassName(getCurrentClass())}));
183     }
184 }
185
Popular Tags