KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.MessageFormat JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import xjavadoc.XClass;
16 import xjavadoc.XMethod;
17 import xjavadoc.XTag;
18 import xdoclet.DocletContext;
19 import xdoclet.DocletTask;
20 import xdoclet.XDocletException;
21 import xdoclet.modules.apache.struts.ejb.StrutsFormSubTask;
22 import xdoclet.modules.ejb.EjbTagsHandler;
23 import xdoclet.modules.ejb.entity.PersistentTagsHandler;
24 import xdoclet.tagshandler.MethodTagsHandler;
25 import xdoclet.util.LogUtil;
26
27 /**
28  * @author Dmitri Colebatch (dim@bigpond.net.au)
29  * @created Oct 19, 2001
30  * @xdoclet.taghandler namespace="StrutsForm"
31  * @version $Revision: 1.13 $
32  */

33 public class StrutsFormTagsHandler extends EjbTagsHandler
34 {
35     /**
36      * Gets the StrutsFormClassFor attribute of the StrutsFormTagsHandler class.
37      *
38      * @param clazz Describe what the parameter does
39      * @return The StrutsFormClassFor value
40      * @exception XDocletException Describe the exception
41      */

42     public static String JavaDoc getStrutsFormClassFor(XClass clazz) throws XDocletException
43     {
44         String JavaDoc packageName = clazz.getContainingPackage().getName();
45
46         packageName = choosePackage(packageName, null, DocletTask.getSubTaskName(StrutsFormSubTask.class));
47         return packageName + '.' + getStrutsFormClassName(clazz);
48     }
49
50     /**
51      * Gets the StrutsFormClassName attribute of the StrutsFormTagsHandler class
52      *
53      * @param clazz Describe what the parameter does
54      * @return The StrutsFormClassName value
55      * @exception XDocletException Describe the exception
56      */

57     public static String JavaDoc getStrutsFormClassName(XClass clazz) throws XDocletException
58     {
59         XTag currentTag = ((StrutsFormSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(StrutsFormSubTask.class))).getCurrentFormTag();
60
61         // check if there is a name parameter
62
String JavaDoc name = currentTag.getAttributeValue("name");
63
64         if (name == null) {
65             return getShortEjbNameFor(clazz) + "Form";
66         }
67         else {
68             return MessageFormat.format(getStrutsFormClassPattern(), new Object JavaDoc[]{getShortEjbNameFor(clazz), name});
69         }
70     }
71
72     /**
73      * Return true if at least one struts:form tag is defined.
74      *
75      * @param clazz Class to check
76      * @return whether class has struts:form tag defined
77      * @exception XDocletException Description of Exception
78      */

79     public static boolean hasFormDefinition(XClass clazz) throws XDocletException
80     {
81         return clazz.getDoc().hasTag("struts:form", false);
82     }
83
84     /**
85      * Gets the StrutsFormClassPattern attribute of the StrutsFormTagsHandler class
86      *
87      * @return The StrutsFormClassPattern value
88      */

89     protected static String JavaDoc getStrutsFormClassPattern()
90     {
91         return ((StrutsFormSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(StrutsFormSubTask.class))).getStrutsFormClassPattern();
92     }
93
94     /**
95      * Return the class name for the current class.
96      *
97      * @return Description of the Returned Value
98      * @exception XDocletException Description of Exception
99      * @doc.tag type="content"
100      */

101     public String JavaDoc strutsFormClass() throws XDocletException
102     {
103         return getStrutsFormClassFor(getCurrentClass());
104     }
105
106     /**
107      * @return Description of the Returned Value
108      * @exception XDocletException Description of Exception
109      * @doc.tag type="content"
110      */

111     public String JavaDoc strutsFormName() throws XDocletException
112     {
113         XTag currentTag = ((StrutsFormSubTask) getDocletContext().getSubTaskBy(DocletTask.getSubTaskName(StrutsFormSubTask.class))).getCurrentFormTag();
114         String JavaDoc formName = currentTag.getAttributeValue("name");
115
116         if (formName == null || formName.trim().length() == 0) {
117             return Introspector.decapitalize(getEjbNameFor(getCurrentClass()) + "Form");
118         }
119         else {
120             return Introspector.decapitalize(getEjbNameFor(getCurrentClass()) + '.' + formName);
121         }
122     }
123
124     /**
125      * Evaluates body for all fields included in form generation
126      *
127      * @param template The body of the block tag
128      * @exception XDocletException Description of Exception
129      * @doc.tag type="block"
130      */

131     public void forAllFormFields(String JavaDoc template) throws XDocletException
132     {
133         // all fields carrying @struts:form-field form-name="<bla>" where <bla> is current
134
// form name, or all persistent fields if include-all="true" is set
135
// pk fields are included implicitly, unless include-pk="false" is specified.
136

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

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

208     protected boolean useMethodInForm(XMethod method) throws XDocletException
209     {
210         // check for include-all
211
XTag currentTag = ((StrutsFormSubTask) getDocletContext().getSubTaskBy(DocletTask.getSubTaskName(StrutsFormSubTask.class))).getCurrentFormTag();
212         String JavaDoc value = currentTag.getAttributeValue("include-all");
213
214         // by default, include all is false
215
if (value != null && value.equals("true")) {
216             return true;
217         }
218
219         // include all pk fields unless include-pk="false"
220
value = currentTag.getAttributeValue("include-pk");
221         if (PersistentTagsHandler.isPkField(method) && (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