KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > JspTools


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp;
8
9
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16 import javax.servlet.jsp.JspException JavaDoc;
17 import javax.servlet.jsp.JspFactory JavaDoc;
18 import javax.servlet.jsp.PageContext JavaDoc;
19 import javax.servlet.jsp.tagext.Tag JavaDoc;
20 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
21
22 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
23
24 import com.inversoft.verge.mvc.MVCConstants;
25 import com.inversoft.verge.mvc.MVCRequest;
26 import com.inversoft.verge.mvc.view.HtmlConstants;
27 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
28
29
30 /**
31  * This class contains various methods that can be useful
32  * when creating JSP views and new JSP tags.
33  *
34  * @author Brian Pontarelli
35  */

36 public class JspTools {
37
38     /* The converter used for converting boolean tag parameters */
39     //static final TypeConverter boolConverter = TypeConverterRegistry.lookup(Boolean.class);
40

41     /**
42      * This constants stores whether or not the application currently running
43      * is running inside a JSP 2.0 compliant container or not. This can be used
44      * in Tag libraries to determine whether or not an attribute should be
45      * evaluated for EL expressions. This is true if the container is compliant
46      * with JSP 2.0 or greater.
47      */

48     public static final boolean JSP_20;
49
50     /**
51      * The key of the HashMap that is used to store radio button tag name mappings
52      */

53     public static final String JavaDoc RADIO_NAMES_KEY = "inversoftRadioNames";
54
55
56     /**
57      * Sets up the JSP_20 variable using the JspFactory
58      */

59     static {
60         String JavaDoc version =
61             JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion();
62         double versionDbl = Double.parseDouble(version.substring(0, 3));
63         JSP_20 = (versionDbl >= 2.0) ? true : false;
64     }
65
66
67     /**
68      * <p>
69      * Converts the given string to the full string by expanding all the variables
70      * in the String using the JSTL 1.0-1.0.1 EL. This method will execute, if
71      * and only if, the {@link #JSP_20 JSP_20} constant is true. Otherwise, it
72      * will simply return the string untouched.
73      * </p>
74      *
75      * <p>
76      * If the JSP_20 variable is true, the JSTL <em>MUST</em> be in the classpath
77      * of the web application.
78      * </p>
79      *
80      * <p>
81      * Since the EL (both versions) are complicated beasts, please see the spec
82      * for both, depending on what version your container is running.
83      * </p>
84      *
85      * @param attrName The string to convert
86      * @param expression The expression to evaluate
87      * @param expectedType The expected return type
88      * @param tag The Tag the value is from
89      * @param context Used to lookup values and for the web repository
90      * @return The converted string or null if the string is null or empty
91      * @throws JspException If a variable did not exist or was invalid
92      */

93     public static Object JavaDoc expand(String JavaDoc attrName, String JavaDoc expression,
94             Class JavaDoc expectedType, Tag JavaDoc tag, PageContext JavaDoc context) throws JspException JavaDoc {
95
96         Object JavaDoc ret = expression;
97         if (ret != null && !JSP_20) {
98             ret = ExpressionEvaluatorManager.evaluate(attrName, expression,
99                 expectedType, tag, context);
100         }
101
102         return ret;
103     }
104
105     /**
106      * Optionally appends the id of the tag, if it is not null
107      *
108      * @param buf The StringBuffer to append the id to
109      * @param tag THe tag that contains the id
110      */

111     public static void appendId(StringBuffer JavaDoc buf, TagSupport JavaDoc tag) {
112         String JavaDoc id = tag.getId();
113         if (id != null) {
114             HtmlViewToolkit.appendAttribute(buf, HtmlConstants.ID, id);
115         }
116     }
117
118     /**
119      * Retrieves the radio button name for the model definition, if one was added
120      * previously. Otherwise this returns null.
121      *
122      * @param definition The model definition
123      * @param context The PageContext used to store the names Map
124      * @return The radio button name or null
125      */

126     public static String JavaDoc getRadioName(String JavaDoc definition, PageContext JavaDoc context) {
127         Map JavaDoc map = (Map JavaDoc) context.getAttribute(RADIO_NAMES_KEY);
128         if (map == null) {
129             return null;
130         }
131
132         return (String JavaDoc) map.get(definition);
133     }
134
135     /**
136      * Retrieves the radio button name for the model definition, if one was added
137      * previously. Otherwise this returns null.
138      *
139      * @param definition The model definition
140      * @param name The name to store
141      * @param context The PageContext used to store the names Map
142      */

143     public static void setRadioName(String JavaDoc definition, String JavaDoc name,
144             PageContext JavaDoc context) {
145         Map JavaDoc map = (Map JavaDoc) context.getAttribute(RADIO_NAMES_KEY);
146         if (map == null) {
147             map = new HashMap JavaDoc();
148             context.setAttribute(RADIO_NAMES_KEY, map);
149         }
150
151         map.put(definition, name);
152     }
153
154     /**
155      * Retrieves the MVCRequest from the HttpServletRequest or builds a new one
156      * and stores it.
157      *
158      * @param pageContext Used to retrieve/build/store the MVCRequset
159      * @return The MVCRequest
160      */

161     public static MVCRequest getMVCRequest(PageContext JavaDoc pageContext) {
162         // Try to get the MVCRequest, or just create one
163
MVCRequest mvcRequest =
164             (MVCRequest) pageContext.getAttribute(MVCConstants.MVC_REQUEST_KEY,
165                 PageContext.REQUEST_SCOPE);
166         if (mvcRequest == null) {
167             mvcRequest = new MVCRequest((HttpServletRequest JavaDoc) pageContext.getRequest(),
168                 (HttpServletResponse JavaDoc) pageContext.getResponse());
169             pageContext.setAttribute(MVCConstants.MVC_REQUEST_KEY, mvcRequest,
170                 PageContext.REQUEST_SCOPE);
171         }
172
173         return mvcRequest;
174     }
175 }
Popular Tags