KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > taglib > ConstantsTei


1 package org.appfuse.webapp.taglib;
2
3 import java.lang.reflect.AccessibleObject JavaDoc;
4 import java.lang.reflect.Field JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7
8 import javax.servlet.jsp.tagext.TagData JavaDoc;
9 import javax.servlet.jsp.tagext.TagExtraInfo JavaDoc;
10 import javax.servlet.jsp.tagext.VariableInfo JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.appfuse.Constants;
15
16
17 /**
18  * Implementation of <code>TagExtraInfo</code> for the <b>constants</b>
19  * tag, identifying the scripting object(s) to be made visible.
20  *
21  * @author Matt Raible
22  * @version $Revision: 1.5 $ $Date: 2006-01-29 22:00:13 -0700 (Sun, 29 Jan 2006) $
23  */

24 public class ConstantsTei extends TagExtraInfo JavaDoc {
25     private final Log log = LogFactory.getLog(ConstantsTei.class);
26
27     /**
28      * Return information about the scripting variables to be created.
29      */

30     public VariableInfo JavaDoc[] getVariableInfo(TagData JavaDoc data) {
31         // loop through and expose all attributes
32
List JavaDoc vars = new ArrayList JavaDoc();
33
34         try {
35             String JavaDoc clazz = data.getAttributeString("className");
36
37             if (clazz == null) {
38                 clazz = Constants.class.getName();
39             }
40
41             Class JavaDoc c = Class.forName(clazz);
42
43             // if no var specified, get all
44
if (data.getAttributeString("var") == null) {
45                 Field JavaDoc[] fields = c.getDeclaredFields();
46
47                 AccessibleObject.setAccessible(fields, true);
48
49                 for (int i = 0; i < fields.length; i++) {
50                     String JavaDoc type = fields[i].getType().getName();
51                     vars.add(new VariableInfo JavaDoc(fields[i].getName(),
52                              ((fields[i].getType().isArray()) ? type.substring(2, type.length()-1) + "[]" : type),
53                              true, VariableInfo.AT_END));
54                 }
55             } else {
56                 String JavaDoc var = data.getAttributeString("var");
57                 String JavaDoc type = c.getField(var).getType().getName();
58                 vars.add(new VariableInfo JavaDoc(c.getField(var).getName(),
59                          ((c.getField(var).getType().isArray()) ? type.substring(2, type.length()-1) + "[]" : type),
60                          true, VariableInfo.AT_END));
61             }
62         } catch (Exception JavaDoc cnf) {
63             log.error(cnf.getMessage());
64             cnf.printStackTrace();
65         }
66
67         return (VariableInfo JavaDoc[]) vars.toArray(new VariableInfo JavaDoc[] { });
68     }
69 }
70
Popular Tags