KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > example > antbook > xdoclet > FormTagsHandler


1 package org.example.antbook.xdoclet;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Properties JavaDoc;
10 import java.util.LinkedHashMap JavaDoc;
11
12 import xdoclet.XDocletException;
13 import xdoclet.tagshandler.AbstractProgramElementTagsHandler;
14 import xdoclet.tagshandler.MethodTagsHandler;
15 import xjavadoc.XClass;
16 import xjavadoc.XMethod;
17 import xjavadoc.XParameter;
18
19 public class FormTagsHandler extends AbstractProgramElementTagsHandler {
20
21     private final static List JavaDoc supportedTypes = new ArrayList JavaDoc();
22     private String JavaDoc curFieldName;
23     private String JavaDoc curType;
24     private boolean curFieldIsIdorVersion = false;
25     private boolean curFieldIsBoolean = false;
26     private boolean curFieldIsDate = false;
27     private boolean lastField = false;
28     
29     static {
30         supportedTypes.add("java.lang.String");
31         supportedTypes.add("java.lang.Integer");
32         supportedTypes.add("int");
33         supportedTypes.add("java.lang.Float");
34         supportedTypes.add("float");
35         supportedTypes.add("java.lang.Long");
36         supportedTypes.add("long");
37         supportedTypes.add("java.lang.Double");
38         supportedTypes.add("double");
39         supportedTypes.add("java.lang.Boolean");
40         supportedTypes.add("boolean");
41         supportedTypes.add("java.util.Date");
42         supportedTypes.add("java.sql.Timestamp");
43     }
44
45     /**
46      * Gets the package name for the parent of this Package.
47      * @author Lance Lavandowska
48      */

49     public String JavaDoc parentPackageName() {
50         String JavaDoc packageName = getCurrentPackage().getName();
51         return packageName.substring(0, packageName.lastIndexOf("."));
52     }
53
54     /**
55      * Gets the package name for the parent of this Package in directory format.
56      * @return Parent package path.
57      */

58     public String JavaDoc parentPackageDir() {
59         return parentPackageName().replace('.', '/');
60     }
61
62     /**
63      * Iterates the body for each field of the current form requiring validation.
64      *
65      * @param template
66      * @param attributes
67      * @throws XDocletException
68      */

69     public void forAllFields(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
70         XClass clazz = getCurrentClass();
71         Map JavaDoc setters = new LinkedHashMap JavaDoc(getFields(clazz));
72
73         for (Iterator JavaDoc iterator = setters.keySet().iterator(); iterator.hasNext();) {
74             curFieldName = (String JavaDoc) iterator.next();
75
76             XMethod field = (XMethod) setters.get(curFieldName);
77             
78             XMethod getter = field.getAccessor();
79             setCurrentMethod(getter);
80             curFieldIsIdorVersion = false;
81             Properties JavaDoc prop = new Properties JavaDoc();
82             prop.setProperty("tagName", "hibernate.id");
83
84             if (hasTag(prop, FOR_METHOD)) {
85                 prop.setProperty("paramName", "generator-class");
86                 String JavaDoc generatorClass = methodTagValue(prop);
87                 System.out.println("generatorClass: " + generatorClass);
88                 if (generatorClass != null && generatorClass.equals("assigned")) {
89                     curFieldIsIdorVersion = false;
90                 } else {
91                     curFieldIsIdorVersion = true;
92                 }
93             } else {
94                 curFieldIsIdorVersion = false;
95             }
96
97             prop.setProperty("tagName", "hibernate.version");
98
99             if (hasTag(prop, FOR_METHOD)) {
100                 curFieldIsIdorVersion = true;
101             }
102
103             String JavaDoc typename = field.getPropertyType().getType().getQualifiedName();
104             curFieldIsBoolean = typename.equals("boolean") || typename.equals("java.lang.Boolean");
105             curFieldIsDate = typename.equals("java.util.Date") || typename.equals("java.sql.Timestamp");
106
107             curType = typename;
108             setCurrentMethod(field);
109
110             if (!iterator.hasNext()) {
111                 lastField = true;
112             } else {
113                 lastField = false;
114             }
115
116             generate(template);
117         }
118     }
119
120     /**
121      * This method is added so that I can pick up a boolean field. When
122      * generating a form page, checkbox is used for boolean fields.
123      *
124      * @author hzhang(mb4henry@yahoo.com.au)
125      * @param template
126      * @param attributes
127      * @throws XDocletException
128      */

129     public void ifIsBooleanField(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
130         if (curFieldIsBoolean)
131             generate(template);
132     }
133
134   /**
135    * Method ifIsNotBooleanField
136    *
137    * @param template
138    * @param attributes
139    *
140    * @throws XDocletException
141    *
142    */

143     public void ifIsNotBooleanField(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
144         if (!curFieldIsBoolean)
145             generate(template);
146     }
147
148     /**
149      * This method is used so Date fields can be detected when building view templates.
150      *
151      * @param template
152      * @param attributes
153      * @throws XDocletException
154      */

155     public void ifIsDateField(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
156         if (curFieldIsDate)
157             generate(template);
158     }
159
160   /**
161    * Method ifIsDateField
162    *
163    * @param template
164    * @param attributes
165    *
166    * @throws XDocletException
167    *
168    */

169     public void ifIsNotDateField(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
170         if (!curFieldIsDate)
171             generate(template);
172     }
173
174
175     /**
176      * This method is used to determine id fields - this is used in the view
177      * pages to set the ids as hidden fields.
178      *
179      * @param template
180      * @param attributes
181      * @throws XDocletException
182      */

183     public void ifIsIdOrVersionField(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
184         if (curFieldIsIdorVersion) {
185             generate(template);
186         }
187     }
188
189     public void ifIsNotLastField(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
190         if (lastField == false) {
191            generate(template);
192         }
193     }
194
195     /**
196      * Method ifIsNotIdField
197      *
198      * @param template
199      * @param attributes
200      *
201      * @throws XDocletException
202     */

203     public void ifIsNotIdOrVersionField(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
204         if (!curFieldIsIdorVersion) {
205             generate(template);
206         }
207     }
208
209     /**
210      * Method ifFieldNameEquals
211      *
212      * @param template
213      * @param attributes
214      *
215      * @throws XDocletException
216      */

217     public void ifFieldNameEquals(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException{
218         String JavaDoc name = attributes.getProperty("name");
219   
220         if ((name != null) && name.equals(curFieldName)) {
221             generate(template);
222         }
223     }
224
225     /**
226      * Method ifFieldNameNotEquals
227      *
228      * @param template
229      * @param attributes
230      *
231      * @throws XDocletException
232      */

233     public void ifFieldNameNotEquals(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException {
234         String JavaDoc name = attributes.getProperty("name");
235     
236         if ((name != null) && !name.equals(curFieldName)) {
237             generate(template);
238         }
239     }
240
241     /**
242      * Method methodTagValue
243      * @param attributes
244      * @return
245      * @throws XDocletException
246      */

247     public String JavaDoc methodTagValue(Properties JavaDoc attributes) throws XDocletException {
248         XMethod method = getCurrentMethod();
249         setCurrentMethod(method.getAccessor());
250         String JavaDoc value = getTagValue(attributes, FOR_METHOD);
251         setCurrentMethod(method);
252         return value;
253     }
254
255     /**
256      * Method columnName
257      * @param attributes
258      * @return
259      * @throws XDocletException
260      */

261     public String JavaDoc columnName(Properties JavaDoc attributes) throws XDocletException {
262         Properties JavaDoc prop = new Properties JavaDoc();
263       
264         prop.setProperty("tagName", "hibernate.property");
265         prop.setProperty("paramName", "column");
266       
267         String JavaDoc column = methodTagValue(prop);
268       
269         if ((column == null) || (column.trim().length() < 1)) {
270             prop.setProperty("tagName", "hibernate.id");
271             column = methodTagValue(prop);
272         }
273       
274         return column;
275     }
276     
277     /**
278      * Returns the current fields name.
279      *
280      * @param props
281      * @return
282      */

283     public String JavaDoc fieldName(Properties JavaDoc props) {
284         return curFieldName;
285     }
286     
287     /**
288      * Returns the current field's java type.
289      * @param props
290      * @return
291      */

292     public String JavaDoc javaType(Properties JavaDoc props) {
293       return curType;
294     }
295     
296     /**
297      * Returns the current field's jdbc type
298      * @param props
299      * @return
300      */

301     public String JavaDoc jdbcType(Properties JavaDoc props) {
302         String JavaDoc jdbcType = "VARCHAR";
303       
304         if (curType != null) {
305             String JavaDoc javaType = curType.toLowerCase();
306         
307             if (javaType.indexOf("date") > 0) {
308                 jdbcType = "TIMESTAMP";
309             } else if (javaType.indexOf("timestamp") > 0) {
310                 jdbcType = "TIMESTAMP";
311             } else if ((javaType.indexOf("int") > 0) || (javaType.indexOf("long") > 0) || (javaType.indexOf("short") > 0)) {
312                 jdbcType = "INTEGER";
313             } else if (javaType.indexOf("double") > 0) {
314                 jdbcType = "DOUBLE";
315             } else if (javaType.indexOf("float") > 0) {
316                 jdbcType = "FLOAT";
317             }
318         }
319       
320         return jdbcType;
321     }
322     
323     /**
324      * @return Classname of the POJO with first letter in lowercase
325      */

326     public String JavaDoc classNameLower() {
327         String JavaDoc name = getCurrentClass().getName();
328         return Character.toLowerCase(name.charAt(0)) + name.substring(1);
329     }
330     
331     public String JavaDoc className() {
332         return getCurrentClass().getName();
333     }
334
335     /**
336      * Name of the POJO in UPPERCASE, for usage in Constants.java.
337      * @return
338      */

339     public String JavaDoc classNameUpper() {
340         String JavaDoc name = getCurrentClass().getName();
341         return name.toUpperCase();
342     }
343
344     public String JavaDoc fieldDescription(Properties JavaDoc props) {
345         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
346         boolean nextUpper = false;
347         for (int i = 0; i < curFieldName.length(); i++) {
348             char c = curFieldName.charAt(i);
349
350             if (i == 0) {
351                 buffer.append(Character.toUpperCase(c));
352                 continue;
353             }
354
355             if (Character.isUpperCase(c)) {
356                 buffer.append(' ');
357                 buffer.append(c);
358                 continue;
359             }
360
361             if (c == '.') {
362                 buffer.delete(0, buffer.length());
363                 nextUpper = true;
364                 continue;
365             }
366
367             char x = nextUpper ? Character.toUpperCase(c) : c;
368             buffer.append(x);
369             nextUpper = false;
370         }
371
372         return buffer.toString();
373     }
374
375     private Map JavaDoc getFields(XClass clazz) throws XDocletException {
376         return getFields(clazz, "");
377     }
378
379     private Map JavaDoc getFields(XClass clazz, String JavaDoc prefix) throws XDocletException {
380         Map JavaDoc fields = new LinkedHashMap JavaDoc();
381
382         Collection JavaDoc curFields = clazz.getMethods(true);
383
384         for (Iterator JavaDoc iterator = curFields.iterator(); iterator.hasNext();) {
385             XMethod setter = (XMethod) iterator.next();
386
387             if (MethodTagsHandler.isSetterMethod(setter)) {
388                 String JavaDoc name = MethodTagsHandler.getPropertyNameFor(setter);
389                 XParameter param = (XParameter) setter.getParameters().iterator().next();
390                 String JavaDoc type = param.getType().getQualifiedName();
391                 XMethod getter = setter.getAccessor();
392
393                 setCurrentClass(setter.getContainingClass());
394                 super.setCurrentMethod(getter);
395                 Properties JavaDoc pro = new Properties JavaDoc();
396                 pro.setProperty("tagName", "hibernate.component");
397                 
398                 if (super.hasTag(pro, FOR_METHOD)) {
399                     name += "Form";
400                     fields.putAll(getFields(param.getType(), prefix + name + "."));
401                 } else {
402                     fields.put(prefix + name, setter);
403                 }
404             }
405         }
406
407         return fields;
408     }
409 }
410
Popular Tags