View Javadoc

1   package com.panogenesis.webapp.taglib;
2   
3   import java.lang.reflect.AccessibleObject;
4   import java.lang.reflect.Field;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import javax.servlet.jsp.tagext.TagData;
9   import javax.servlet.jsp.tagext.TagExtraInfo;
10  import javax.servlet.jsp.tagext.VariableInfo;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import com.panogenesis.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.4 $ $Date: 2004/08/19 00:13:58 $
23   */
24  public class ConstantsTei extends TagExtraInfo {
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[] getVariableInfo(TagData data) {
31          // loop through and expose all attributes
32          List vars = new ArrayList();
33  
34          try {
35              String clazz = data.getAttributeString("className");
36  
37              if (clazz == null) {
38                  clazz = Constants.class.getName();
39              }
40  
41              Class c = Class.forName(clazz);
42  
43              // if no var specified, get all
44              if (data.getAttributeString("var") == null) {
45                  Field[] fields = c.getDeclaredFields();
46  
47                  AccessibleObject.setAccessible(fields, true);
48  
49                  for (int i = 0; i < fields.length; i++) {
50                      vars.add(new VariableInfo(fields[i].getName(),
51                                                "java.lang.String", true,
52                                                VariableInfo.AT_END));
53                  }
54              } else {
55                  String var = data.getAttributeString("var");
56                  vars.add(new VariableInfo(c.getField(var).getName(),
57                                            "java.lang.String", true,
58                                            VariableInfo.AT_END));
59              }
60          } catch (Exception cnf) {
61              log.error(cnf.getMessage());
62              cnf.printStackTrace();
63          }
64  
65          return (VariableInfo[]) vars.toArray(new VariableInfo[] {  });
66      }
67  }