View Javadoc

    1   /*
    2    * Copyright 2002,2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    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   * &lt;j:getStatic var="closeOperation" className="javax.swing.JFrame"
    38   *              field="EXIT_ON_CLOSE"/&gt;
    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      // Tag interface
    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 /* Emacs configuration
    124  * Local variables:        **
    125  * mode:             java  **
    126  * c-basic-offset:   4     **
    127  * indent-tabs-mode: nil   **
    128  * End:                    **
    129  */