You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by df...@apache.org on 2004/06/05 04:23:41 UTC

cvs commit: jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core GetStaticTag.java CoreTagLibrary.java

dfs         2004/06/04 19:23:41

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/core
                        CoreTagLibrary.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/core
                        GetStaticTag.java
  Log:
  In the thread on commons-dev starting at:
    http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg41386.html
  we discussed the addition to Jelly of the ability to retrieve the
  values of static fields.  The participants of the discussion agreed
  that the functionality was desirable as a core feature.  However, it
  was also pointed out that perhaps Jexl is the right place for this
  functionality to exist.  Given the immediate utility of the
  functionality and the general agreement that it would be okay to
  add a new core tag to fulfill the need, I have added a GetStaticTag
  class that implements a <getStatic/> tag to retrieve static fields
  in classes, modeled along the ines of <invokeStatic/>.
  
  Revision  Changes    Path
  1.30      +2 -1      jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java
  
  Index: CoreTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/CoreTagLibrary.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- CoreTagLibrary.java	24 Feb 2004 14:10:38 -0000	1.29
  +++ CoreTagLibrary.java	5 Jun 2004 02:23:41 -0000	1.30
  @@ -53,6 +53,7 @@
           registerTag("break", BreakTag.class);
           registerTag("expr", ExprTag.class);
           registerTag("file", FileTag.class);
  +        registerTag("getStatic", GetStaticTag.class);
           registerTag("invoke", InvokeTag.class);
           registerTag("invokeStatic", InvokeStaticTag.class);
           registerTag("new", NewTag.class);
  
  
  
  1.1                  jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/GetStaticTag.java
  
  Index: GetStaticTag.java
  ===================================================================
  /*
   * Copyright 2002,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.jelly.tags.core;
  
  import org.apache.commons.jelly.JellyContext;
  import org.apache.commons.jelly.JellyTagException;
  import org.apache.commons.jelly.MissingAttributeException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  
  /** 
   * A tag which can retrieve the value of a static field of a given class.
   * The following attributes are required:<br />
   * <ul>
   *   <li>var - The variable to which to assign the resulting value.</li>
   *   <li>field - The name of the static field to retrieve.</li>
   *   <li>className - The name of the class containing the static field.</li>
   * </ul>
   *
   * Example usage:
   * <pre>
   * &lt;j:getStatic var="closeOperation" className="javax.swing.JFrame"
   *              field="EXIT_ON_CLOSE"/&gt; 
   * </pre>
   *
   * @version $Revision: 1.1 $
   */
  
  public class GetStaticTag extends TagSupport {
  
      /** The variable to which to assign the resulting value. */
      private String var;
  
      /** The name of the static field to retrieve. */
      private String field;
  
      /** The name of the class containing the static field. */
      private String className;
  
  
      /** 
       * Sets the name of the variable exported by this tag.
       *
       * @param var The variable name.
       */
  
      public void setVar(String var) {
          this.var = var;
      }
  
  
      /**
       * Sets the name of the field to retrieve.
       *
       * @param method The method name
       */
  
      public void setField(String field) {
          this.field = field;
      }
  
  
      /**
       * Sets the fully qualified name of the class containing the static field.
       *
       * @param className The name of the class.
       */
  
      public void setClassName(String className) {
          this.className = className;
      }
  
  
      // Tag interface
      //------------------------------------------------------------------------ 
  
      public void doTag(XMLOutput output) throws JellyTagException {
          String message = null;
  
          if(var == null)
              message = "var";
          else if(field == null)
              message = "field";
          else if(className == null)
              message = "className";
  
          if(message != null)
              throw new MissingAttributeException(message);
  
          try {
              Class type     = getClass().getClassLoader().loadClass(className);
              Object result  = type.getField(field).get(null);
              JellyContext context = getContext();
  
              context.setVariable(var, result);
  
          } catch(Throwable t) {
              throw
                  new JellyTagException("Could not access " + className + "." +
                                        var + ".  Original exception message: " +
                                        t.getMessage(), t);
          }
      }
  
  }
  
  
  /* Emacs configuration
   * Local variables:        **
   * mode:             java  **
   * c-basic-offset:   4     **
   * indent-tabs-mode: nil   **
   * End:                    **
   */
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org