You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ma...@apache.org on 2005/08/31 06:49:09 UTC

svn commit: r264942 - in /jakarta/taglibs/sandbox/unstandard/trunk: src/org/apache/taglibs/unstandard/ClassUtils.java src/org/apache/taglibs/unstandard/TagUtils.java src/org/apache/taglibs/unstandard/UseConstantsTag.java xml/intro.xml xml/unstandard.xml

Author: martinc
Date: Tue Aug 30 21:49:04 2005
New Revision: 264942

URL: http://svn.apache.org/viewcvs?rev=264942&view=rev
Log:
Add a new 'useConstants' tag to the unstandard taglib. There is some inconsistency between this tag and the 'bind' tag, in terms of naming and usage, but there is so much that needs to be done to this taglib before it's ready for a real release that I decided not to worry about that right now.

Added:
    jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/ClassUtils.java   (with props)
    jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/TagUtils.java   (with props)
    jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/UseConstantsTag.java   (with props)
Modified:
    jakarta/taglibs/sandbox/unstandard/trunk/xml/intro.xml
    jakarta/taglibs/sandbox/unstandard/trunk/xml/unstandard.xml

Added: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/ClassUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/ClassUtils.java?rev=264942&view=auto
==============================================================================
--- jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/ClassUtils.java (added)
+++ jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/ClassUtils.java Tue Aug 30 21:49:04 2005
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2005 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.taglibs.unstandard;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Utility class for working with Class instances.
+ */
+public final class ClassUtils {
+
+    /**
+     * Private constructor to prevent instantiation of this class.
+     */
+    private ClassUtils() {
+    }
+
+    /**
+     * Return the <code>Class</code> object for the specified fully qualified
+     * class name, from the appropriate class loader.
+     *
+     * @param className Fully qualified name of the class to be loaded.
+     *
+     * @return The Class object for the requested class.
+     *
+     * @throws ClassNotFoundException If the class cannot be found.
+     */
+    public static Class loadClass(String className)
+            throws ClassNotFoundException {
+        // Look up the class loader to be used
+        ClassLoader classLoader =
+                Thread.currentThread().getContextClassLoader();
+        if (classLoader == null) {
+            classLoader = ClassUtils.class.getClassLoader();
+        }
+
+        // Attempt to load the specified class
+        return (classLoader.loadClass(className));
+    }
+
+    /**
+     * Return a new instance of the specified fully qualified class name,
+     * after loading the class from the appropriate class loader.
+     * The specified class <strong>MUST</strong> have a public zero-argument
+     * constructor.
+     *
+     * @param className Fully qualified name of the class to use.
+     *
+     * @return A new instance of the requested class.
+     *
+     * @throws ClassNotFoundException if the class cannot be found
+     * @throws IllegalAccessException if the class or its constructor
+     *  is not accessible
+     * @throws InstantiationException if this class represents an
+     *  abstract class, an interface, an array class, a primitive type,
+     *  or void
+     * @throws InstantiationException if this class has no
+     *  zero-arguments constructor
+     */
+    public static Object createInstance(String className)
+            throws ClassNotFoundException, IllegalAccessException,
+                    InstantiationException {
+        return (loadClass(className).newInstance());
+    }
+
+    /**
+     * Creates and returns a map of the names of public static final constants
+     * to their values, for the specified class.
+     *
+     * @param className The fully qualified name of the class for which the
+     *                  constants should be determined
+     *
+     * @return A <code>Map</code> from constant names to values.
+     * @throws ClassNotFoundException if TODOC error occurs.
+     * @throws IllegalAccessException if TODOC error occurs.
+     * @throws IllegalArgumentException if TODOC error occurs.
+     */
+    public static Map /* String, Object */ getClassConstants(String className)
+            throws ClassNotFoundException, IllegalArgumentException,
+                    IllegalAccessException {
+        Map /* String, Object */ constants = new HashMap();
+        Class clazz = ClassUtils.loadClass(className);
+
+        Field[] fields = clazz.getFields();
+        for (int i = 0; i < fields.length; i++) {
+            Field field = fields[i];
+            int modifiers = field.getModifiers();
+            if ((modifiers & Modifier.STATIC) != 0
+                    && (modifiers & Modifier.FINAL) != 0) {
+                Object value = field.get(null);
+                if (value != null) {
+                    constants.put(field.getName(), value);
+                }
+            }
+        }
+        return constants;
+    }
+}

Propchange: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/ClassUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/ClassUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/TagUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/TagUtils.java?rev=264942&view=auto
==============================================================================
--- jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/TagUtils.java (added)
+++ jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/TagUtils.java Tue Aug 30 21:49:04 2005
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2005 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.taglibs.unstandard;
+
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.JspTagException;
+import javax.servlet.jsp.PageContext;
+
+/**
+ * Utility methods for use in the implementation of JSP tags. These methods
+ * are separated from the tags themselves for two reasons. First, it enables
+ * code reuse across tag implementations. Second, and perhaps more important,
+ * it allows for testing these methods independently from a JSP container.
+ */
+public final class TagUtils {
+
+    /**
+     * Private constructor to prevent instantiation of this class.
+     */
+    private TagUtils() {
+    }
+
+    /**
+     * Convert the string representation of a scope into the corresponding
+     * constant from the <code>PageContext</code> class. If the string value
+     * is null, the supplied default value is returned instead.
+     *
+     * @param scope        The string value to convert.
+     * @param defaultScope The scope to return if the string value supplied is
+     *                     <code>null</code>.
+     *
+     * @return The constant corresponding to the string value supplied.
+     *
+     * @throws JspException if the string value is not a valid scope name, or
+     *                      if the string value is <code>null</code> and the
+     *                      default value supplied is not a valid scope.
+     */
+    public static int getScope(String scope, int defaultScope)
+            throws JspException {
+        int pcscope;
+
+        if (scope == null) {
+            switch (defaultScope) {
+            case PageContext.PAGE_SCOPE:
+            case PageContext.REQUEST_SCOPE:
+            case PageContext.SESSION_SCOPE:
+            case PageContext.APPLICATION_SCOPE:
+                pcscope = defaultScope;
+                break;
+            default:
+                throw new JspTagException(
+                        "Invalid default scope: " + defaultScope);
+            }
+        } else if ("page".equals(scope)) {
+            pcscope = PageContext.PAGE_SCOPE;
+        } else if ("request".equals(scope)) {
+            pcscope = PageContext.REQUEST_SCOPE;
+        } else if ("session".equals(scope)) {
+            pcscope = PageContext.SESSION_SCOPE;
+        } else if ("application".equals(scope)) {
+            pcscope = PageContext.APPLICATION_SCOPE;
+        } else {
+            throw new JspTagException("Invalid scope name: " + scope);
+        }
+
+        return pcscope;
+    }
+}

Propchange: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/TagUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/TagUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/UseConstantsTag.java
URL: http://svn.apache.org/viewcvs/jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/UseConstantsTag.java?rev=264942&view=auto
==============================================================================
--- jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/UseConstantsTag.java (added)
+++ jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/UseConstantsTag.java Tue Aug 30 21:49:04 2005
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2005 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.taglibs.unstandard;
+
+import java.io.IOException;
+import java.util.Map;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.JspTagException;
+import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.TagSupport;
+
+/**
+ * Tag that exposes all of the public constants in a class as a map stored in
+ * a scoped attribute. The scope may be specified, but defaults to page scope.
+ */
+public class UseConstantsTag extends TagSupport {
+
+    /**
+     * The fully qualified name of the Java class for which constants are to
+     * be exposed.
+     */
+    private String className;
+
+    /**
+     * The scope in which the exposed map will be stored.
+     */
+    private String scope;
+
+    /**
+     * The name of the scoped attribute in which the constants will be stored.
+     */
+    private String var;
+
+    /**
+     * Construct an instance of this class.
+     */
+    public UseConstantsTag() {
+    }
+
+    /**
+     * Retrieve the name of the class for which constants are to be exposed.
+     *
+     * @return The fully qualified class name.
+     */
+    public String getClassName() {
+        return className;
+    }
+
+    /**
+     * Set the name of the class for which constants are to be exposed.
+     *
+     * @param className The fully qualified class name.
+     */
+    public void setClassName(String className) {
+        this.className = className;
+    }
+
+    /**
+     * Retrieve the scope in which the exposed map will be stored.
+     *
+     * @return The name of the scope.
+     */
+    public String getScope() {
+        return scope;
+    }
+
+    /**
+     * Set the scope in which the exposed map will be stored.
+     *
+     * @param scope The name of the scope.
+     */
+    public void setScope(String scope) {
+        this.scope = scope;
+    }
+
+    /**
+     * Retrieve the variable name in which the exposed map will be stored.
+     *
+     * @return The name of the variable.
+     */
+    public String getVar() {
+        return var;
+    }
+
+    /**
+     * Set the variable name in which the exposed map will be stored.
+     *
+     * @param var The name of the variable.
+     */
+    public void setVar(String var) {
+        this.var = var;
+    }
+
+    /**
+     * Expose the constants for a class as a scoped attribute.
+     *
+     * @return A constant that identifies what the container should do next.
+     *
+     * @throws JspException if a fatal error occurs.
+     */
+    public int doStartTag() throws JspException {
+        int scope = TagUtils.getScope(this.scope, PageContext.PAGE_SCOPE);
+        if (className != null && var != null) {
+            Map constants = null;
+            try {
+                constants = ClassUtils.getClassConstants(className);
+            } catch (ClassNotFoundException e) {
+                throw new JspTagException("Class not found: " + className);
+            } catch (IllegalArgumentException e) {
+                throw new JspTagException("Illegal argument: " + className);
+            } catch (IllegalAccessException e) {
+                throw new JspTagException("Illegal access: " + className);
+            }
+            if (constants != null && !constants.isEmpty()) {
+                pageContext.setAttribute(var, constants, scope);
+            }
+        }
+
+        return SKIP_BODY;
+    }
+
+    /**
+     * Free up any resources being used by this tag handler.
+     */
+    public void release() {
+        super.release();
+        className = null;
+        scope = null;
+        var = null;
+    }
+}

Propchange: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/UseConstantsTag.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/taglibs/sandbox/unstandard/trunk/src/org/apache/taglibs/unstandard/UseConstantsTag.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/taglibs/sandbox/unstandard/trunk/xml/intro.xml
URL: http://svn.apache.org/viewcvs/jakarta/taglibs/sandbox/unstandard/trunk/xml/intro.xml?rev=264942&r1=264941&r2=264942&view=diff
==============================================================================
--- jakarta/taglibs/sandbox/unstandard/trunk/xml/intro.xml (original)
+++ jakarta/taglibs/sandbox/unstandard/trunk/xml/intro.xml Tue Aug 30 21:49:04 2005
@@ -108,6 +108,7 @@
     <ul>
       <li>Tim O'Brien</li>
       <li>Henri Yandell</li>
+      <li>Martin Cooper</li>
     </ul>
 
   </section>

Modified: jakarta/taglibs/sandbox/unstandard/trunk/xml/unstandard.xml
URL: http://svn.apache.org/viewcvs/jakarta/taglibs/sandbox/unstandard/trunk/xml/unstandard.xml?rev=264942&r1=264941&r2=264942&view=diff
==============================================================================
--- jakarta/taglibs/sandbox/unstandard/trunk/xml/unstandard.xml (original)
+++ jakarta/taglibs/sandbox/unstandard/trunk/xml/unstandard.xml Tue Aug 30 21:49:04 2005
@@ -1,6 +1,6 @@
 <?xml version="1.0" ?>
 <!--
-  Copyright 1999-2004 The Apache Software Foundation
+  Copyright 1999-2005 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.
@@ -26,6 +26,7 @@
        -->
   <!-- The name here is used in the HTML <meta name="author"...> tag -->
   <author>Henri Yandell</author>
+  <author>Martin Cooper</author>
 </properties>
 
 <!-- The following defines elements uses both to create the taglib
@@ -626,6 +627,115 @@
       </attribute>
 
     </tag>
+
+    <!-- useConstants tag -->    
+    <tag>
+      <name>useConstants</name> 
+      <tag-class>org.apache.taglibs.unstandard.UseConstantsTag</tag-class>
+      <body-content>empty</body-content>
+
+      <!-- JSP 1.2, display name of tag -->
+      <display-name>useConstants</display-name>
+
+      <!-- JSP 1.2 tag library DTD only, not used for generating
+           documentation.
+
+      <small-icon></small-icon>
+      <large-icon></large-icon>
+          -->
+      <!-- Complete description of this tag.  Used for JSP 1.2 TLD
+           and for generating HTML documentation. -->
+      <description>
+	      Exposes all of the public constants in a class as a map stored in
+          a scoped attribute. The scope may be specified, but defaults to page
+		  scope.
+      </description>
+
+      <!-- The next three non-standard elements are used to inform the relevant
+           sections of the HTML doc.  See, for example, the Regexp
+           taglib docs for examples of the output. -->
+      <!-- One line summary of what tag does for Tag Summary section -->
+      <summary>Expose Java class constants as a map of values</summary>
+      <!-- Version of taglib when this tag became available,
+           deprecated is a possible value. -->
+      <availability>1.0</availability>
+      <!-- Any restrictions on use of the tag -->
+      <restrictions>None</restrictions>
+
+
+      <variable>
+        <!-- JSP 1.2 elemenets -->
+        <!-- name-given or name-from-attribute must be declared
+        <name-given>var</name-given>
+             -->
+        <name-from-attribute>var</name-from-attribute>
+        <!-- Optional variable declarations -->
+        <!-- Package and class name of variable, default java.lang.String -->
+        <variable-class>java.lang.Object</variable-class>
+        <!-- Usage for declare is not clear in JSP1.2PFD, default true -->
+        <declare>true</declare>
+        <!-- scope of scripting variable, default NESTED -->
+        <scope>AT_BEGIN</scope>
+        <!-- description of scripting variable -->
+        <description>The name of the attribute into which the map will be stored.</description>
+
+        <!-- Used for the HTML documentation only.
+             Version of taglib when this script variable became available,
+             deprecated is a possible value. -->
+        <availability>1.0</availability>
+      </variable>
+
+      <attribute>
+        <name>var</name>
+        <required>yes</required>
+        <rtexprvalue>no</rtexprvalue>
+        <description>Name of the scoped attribute into which the map will be stored.</description>
+      </attribute>
+
+      <attribute>
+        <name>className</name>
+        <required>yes</required>
+        <rtexprvalue>no</rtexprvalue>
+        <description>Fully qualified name of the class from which constants will be extracted.</description>
+      </attribute>
+
+      <attribute>
+        <name>scope</name>
+        <required>no</required>
+        <rtexprvalue>no</rtexprvalue>
+        <description>Scope into which to store the map. Default is page scope.</description>
+      </attribute>
+
+      <!-- The entire example section is for usage examples that you want to
+           have added to the HTML doc under "Example" for each tag.  The
+           example section is also included with a JSP 1.2 TLD.  The
+           contents of these elements are rendered into a simple form of
+           syntax highlighting by the XSL sheet. Multiple usage sections
+           are allowed, and each will be display as a separate example
+           in the resultant HTML. -->
+      <example>
+        <usage>
+          <!-- Enter a JSP comment without the opening and closing tags.
+               Those will be added by the XSL.  Put no HTML mark-up in here.
+               Currently, only one comment per usage section is supported. -->
+          <comment>
+            To expose all of the constants in the Integer class:
+          </comment>
+  
+          <!-- The code element is for an example of using the tag.  I
+               normally put these in CDATA sections so that the XSL
+               transform to HTML will turn the angle brackets into the
+               appropriate &lt; and &gt; entities automatically. Only
+               one code element per usage element is supported.
+               The resultant HTML is formatted and colorized. -->
+          <code>
+            <![CDATA[<un:useConstants var="const" className="java.lang.Integer" />]]> 
+          </code>
+        </usage>
+      </example>
+
+    </tag>
+
   </tagtoc>
 
 </taglib>



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