KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package xdoclet.modules.apache.struts;
6
7 import java.beans.Introspector JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import xjavadoc.XClass;
15 import xjavadoc.XMethod;
16 import xjavadoc.XTag;
17
18 import xdoclet.DocletContext;
19 import xdoclet.DocletTask;
20 import xdoclet.XDocletException;
21 import xdoclet.XDocletTagSupport;
22 import xdoclet.tagshandler.MethodTagsHandler;
23 import xdoclet.tagshandler.PackageTagsHandler;
24 import xdoclet.util.LogUtil;
25
26 /**
27  * @author Matt Raible (matt@raibledesigns.com)
28  * @created April 26, 2004
29  * @xdoclet.taghandler namespace="ActionForm"
30  * @version $Revision: 1.2 $
31  */

32 public class ActionFormTagsHandler extends XDocletTagSupport
33 {
34     /**
35      * Gets the ActionFormClassFor attribute of the ActionFormTagsHandler class.
36      *
37      * @param clazz Describe what the parameter does
38      * @return The ActionFormClassFor value
39      * @exception XDocletException Describe the exception
40      */

41     public static String JavaDoc getActionFormClassFor(XClass clazz) throws XDocletException
42     {
43         String JavaDoc packageName = clazz.getContainingPackage().getName();
44
45         packageName = PackageTagsHandler.getPackageNameFor(packageName, true);
46         return packageName + '.' + getActionFormClassName(clazz);
47     }
48
49     /**
50      * Gets the ActionFormClassName attribute of the ActionFormTagsHandler class
51      *
52      * @param clazz Describe what the parameter does
53      * @return The ActionFormClassName value
54      * @exception XDocletException Describe the exception
55      */

56     public static String JavaDoc getActionFormClassName(XClass clazz) throws XDocletException
57     {
58         XTag currentTag = ((ActionFormSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(ActionFormSubTask.class))).getCurrentFormTag();
59
60         // check if there is a name parameter
61
String JavaDoc name = currentTag.getAttributeValue("name");
62
63         if (name == null) {
64             return clazz.getTransformedName() + "Form";
65         }
66         else {
67             if (Character.isLowerCase(name.charAt(0))) {
68                 char chars[] = name.toCharArray();
69
70                 chars[0] = Character.toUpperCase(chars[0]);
71                 return new String JavaDoc(chars);
72             }
73             else {
74                 return name;
75             }
76         }
77     }
78
79     /**
80      * Return true if at least one struts:form tag is defined.
81      *
82      * @param clazz Class to check
83      * @return whether class has struts:form tag defined
84      * @exception XDocletException Description of Exception
85      */

86     public static boolean hasFormDefinition(XClass clazz) throws XDocletException
87     {
88         return clazz.getDoc().hasTag("struts.form", false);
89     }
90
91     /**
92      * Gets the ActionFormClassPattern attribute of the ActionFormTagsHandler class
93      *
94      * @return The ActionFormClassPattern value
95      */

96     protected static String JavaDoc getActionFormClassPattern()
97     {
98         return ((ActionFormSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(ActionFormSubTask.class))).getActionFormClassPattern();
99     }
100
101     /**
102      * Return the class name for the current class.
103      *
104      * @return Description of the Returned Value
105      * @exception XDocletException Description of Exception
106      * @doc.tag type="content"
107      */

108     public String JavaDoc actionFormClass() throws XDocletException
109     {
110         return getActionFormClassFor(getCurrentClass());
111     }
112
113     /**
114      * @return Description of the Returned Value
115      * @exception XDocletException Description of Exception
116      * @doc.tag type="content"
117      */

118     public String JavaDoc actionFormName() throws XDocletException
119     {
120         XTag currentTag = ((ActionFormSubTask) getDocletContext().getSubTaskBy(DocletTask.getSubTaskName(ActionFormSubTask.class))).getCurrentFormTag();
121         String JavaDoc formName = currentTag.getAttributeValue("name");
122
123         if (formName == null || formName.trim().length() == 0) {
124             return Introspector.decapitalize(getCurrentClass().getTransformedName() + "Form");
125         }
126         else {
127             return Introspector.decapitalize(formName);
128         }
129     }
130
131     /**
132      * Evaluates body for all fields included in form generation
133      *
134      * @param template The body of the block tag
135      * @exception XDocletException Description of Exception
136      * @doc.tag type="block"
137      */

138     public void forAllFormFields(String JavaDoc template) throws XDocletException
139     {
140         // all fields carrying @struts:form-field form-name="<bla>" where <bla> is current
141
// form name, or all persistent fields if include-all="true" is set
142

143         Log log = LogUtil.getLog(ActionFormTagsHandler.class, "forAllFormFields");
144         XClass currentClass = getCurrentClass();
145         Map JavaDoc foundFields = new HashMap JavaDoc();
146
147         if (log.isDebugEnabled()) {
148             log.debug("BEGIN-----------------------------------------");
149         }
150
151         do {
152             pushCurrentClass(currentClass);
153
154             if (log.isDebugEnabled()) {
155                 log.debug("-----CLASS=" + getCurrentClass().getName() + "----------------");
156             }
157
158             Collection JavaDoc methods = getCurrentClass().getMethods();
159
160             for (Iterator JavaDoc j = methods.iterator(); j.hasNext(); ) {
161                 setCurrentMethod((XMethod) j.next());
162                 // We are interested in persistent fields and fields marked as a form-field.
163
if (MethodTagsHandler.isGetter(getCurrentMethod().getName()) &&
164                     !foundFields.containsKey(getCurrentMethod().getName()) &&
165                     useMethodInForm(getCurrentMethod())) {
166                     if (useMethodInForm(getCurrentMethod())) {
167                         if (log.isDebugEnabled()) {
168                             log.debug("METHOD(I=" + getCurrentMethod().getName());
169                         }
170                         // Store that we found this field so we don't add it twice
171
foundFields.put(getCurrentMethod().getName(), getCurrentMethod().getName());
172
173                         generate(template);
174                     }
175                 }
176             }
177
178             // Add super class info
179
if (getCurrentClass().getSuperclass().getQualifiedName().equals("java.lang.Object")) {
180                 popCurrentClass();
181                 break;
182             }
183
184             popCurrentClass();
185             currentClass = currentClass.getSuperclass();
186         } while (true);
187
188         if (log.isDebugEnabled()) {
189             log.debug("END-------------------------------------------");
190         }
191     }
192
193     /**
194      * Evaluates the body if the method belongs in a given form.
195      *
196      * @param template The body of the block tag
197      * @exception XDocletException
198      * @doc.tag type="block"
199      */

200     public void ifUseMethodInForm(String JavaDoc template) throws XDocletException
201     {
202         if (useMethodInForm(getCurrentMethod()))
203             generate(template);
204     }
205
206     /**
207      * Check that method has struts:form-field tag with valid name, or is pk field (and pk fields are included) or
208      * include-all="true".
209      *
210      * @param method Description of Parameter
211      * @return Description of the Returned Value
212      * @exception XDocletException Description of Exception
213      */

214     protected boolean useMethodInForm(XMethod method) throws XDocletException
215     {
216         // check for include-all
217
XTag currentTag = ((ActionFormSubTask) getDocletContext().getSubTaskBy(DocletTask.getSubTaskName(ActionFormSubTask.class))).getCurrentFormTag();
218         String JavaDoc value = currentTag.getAttributeValue("include-all");
219
220         // by default, include all is false
221
if (value != null && value.equals("true")) {
222             return true;
223         }
224
225         // check for explicit inclusion
226
Collection JavaDoc mTags = method.getDoc().getTags("struts:form-field");
227
228         // if there's a name on the form, then user must specify a form-name attribute
229
// on the method. This is so multiple forms can be generated from the same POJO.
230
String JavaDoc fname = currentTag.getAttributeValue("name");
231
232         if (fname == null && !mTags.isEmpty()) {
233             return true;
234         }
235         else {
236             for (Iterator JavaDoc i = mTags.iterator(); i.hasNext(); ) {
237                 XTag mTag = (XTag) i.next();
238                 String JavaDoc pname = mTag.getAttributeValue("form-name");
239
240                 if (pname != null && fname != null && fname.equals(pname)) {
241                     return true;
242                 }
243             }
244         }
245
246         // no need in such field...
247
return false;
248     }
249 }
250
Popular Tags