1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.core;
17
18 import org.apache.commons.jelly.JellyContext;
19 import org.apache.commons.jelly.JellyTagException;
20 import org.apache.commons.jelly.MissingAttributeException;
21 import org.apache.commons.jelly.TagSupport;
22 import org.apache.commons.jelly.XMLOutput;
23 import org.apache.commons.jelly.util.ClassLoaderUtils;
24
25
26 /***
27 * A tag which can retrieve the value of a static field of a given class.
28 * The following attributes are required:<br />
29 * <ul>
30 * <li>var - The variable to which to assign the resulting value.</li>
31 * <li>field - The name of the static field to retrieve.</li>
32 * <li>className - The name of the class containing the static field.</li>
33 * </ul>
34 *
35 * Example usage:
36 * <pre>
37 * <j:getStatic var="closeOperation" className="javax.swing.JFrame"
38 * field="EXIT_ON_CLOSE"/>
39 * </pre>
40 *
41 * @version $Revision: 155420 $
42 */
43
44 public class GetStaticTag extends TagSupport {
45
46 /*** The variable to which to assign the resulting value. */
47 private String var;
48
49 /*** The name of the static field to retrieve. */
50 private String field;
51
52 /*** The name of the class containing the static field. */
53 private String className;
54
55
56 /***
57 * Sets the name of the variable exported by this tag.
58 *
59 * @param var The variable name.
60 */
61
62 public void setVar(String var) {
63 this.var = var;
64 }
65
66
67 /***
68 * Sets the name of the field to retrieve.
69 *
70 * @param method The method name
71 */
72
73 public void setField(String field) {
74 this.field = field;
75 }
76
77
78 /***
79 * Sets the fully qualified name of the class containing the static field.
80 *
81 * @param className The name of the class.
82 */
83
84 public void setClassName(String className) {
85 this.className = className;
86 }
87
88
89
90
91
92 public void doTag(XMLOutput output) throws JellyTagException {
93 String message = null;
94
95 if(var == null)
96 message = "var";
97 else if(field == null)
98 message = "field";
99 else if(className == null)
100 message = "className";
101
102 if(message != null)
103 throw new MissingAttributeException(message);
104
105 try {
106 Class type = ClassLoaderUtils.getClassLoader(getClass()).loadClass(className);
107 Object result = type.getField(field).get(null);
108 JellyContext context = getContext();
109
110 context.setVariable(var, result);
111
112 } catch(Throwable t) {
113 throw
114 new JellyTagException("Could not access " + className + "." +
115 var + ". Original exception message: " +
116 t.getMessage(), t);
117 }
118 }
119
120 }
121
122
123
124
125
126
127
128
129