001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.tags.core;
017:
018: import org.apache.commons.jelly.JellyContext;
019: import org.apache.commons.jelly.JellyTagException;
020: import org.apache.commons.jelly.MissingAttributeException;
021: import org.apache.commons.jelly.TagSupport;
022: import org.apache.commons.jelly.XMLOutput;
023: import org.apache.commons.jelly.util.ClassLoaderUtils;
024:
025: /**
026: * A tag which can retrieve the value of a static field of a given class.
027: * The following attributes are required:<br />
028: * <ul>
029: * <li>var - The variable to which to assign the resulting value.</li>
030: * <li>field - The name of the static field to retrieve.</li>
031: * <li>className - The name of the class containing the static field.</li>
032: * </ul>
033: *
034: * Example usage:
035: * <pre>
036: * <j:getStatic var="closeOperation" className="javax.swing.JFrame"
037: * field="EXIT_ON_CLOSE"/>
038: * </pre>
039: *
040: * @version $Revision: 155420 $
041: */
042:
043: public class GetStaticTag extends TagSupport {
044:
045: /** The variable to which to assign the resulting value. */
046: private String var;
047:
048: /** The name of the static field to retrieve. */
049: private String field;
050:
051: /** The name of the class containing the static field. */
052: private String className;
053:
054: /**
055: * Sets the name of the variable exported by this tag.
056: *
057: * @param var The variable name.
058: */
059:
060: public void setVar(String var) {
061: this .var = var;
062: }
063:
064: /**
065: * Sets the name of the field to retrieve.
066: *
067: * @param method The method name
068: */
069:
070: public void setField(String field) {
071: this .field = field;
072: }
073:
074: /**
075: * Sets the fully qualified name of the class containing the static field.
076: *
077: * @param className The name of the class.
078: */
079:
080: public void setClassName(String className) {
081: this .className = className;
082: }
083:
084: // Tag interface
085: //------------------------------------------------------------------------
086:
087: public void doTag(XMLOutput output) throws JellyTagException {
088: String message = null;
089:
090: if (var == null)
091: message = "var";
092: else if (field == null)
093: message = "field";
094: else if (className == null)
095: message = "className";
096:
097: if (message != null)
098: throw new MissingAttributeException(message);
099:
100: try {
101: Class type = ClassLoaderUtils.getClassLoader(getClass())
102: .loadClass(className);
103: Object result = type.getField(field).get(null);
104: JellyContext context = getContext();
105:
106: context.setVariable(var, result);
107:
108: } catch (Throwable t) {
109: throw new JellyTagException("Could not access " + className
110: + "." + var + ". Original exception message: "
111: + t.getMessage(), t);
112: }
113: }
114:
115: }
116:
117: /* Emacs configuration
118: * Local variables: **
119: * mode: java **
120: * c-basic-offset: 4 **
121: * indent-tabs-mode: nil **
122: * End: **
123: */
|