KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > taglib > AbstractTag


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.taglib;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.servlet.jsp.JspException JavaDoc;
30 import javax.servlet.jsp.JspTagException JavaDoc;
31 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
32
33 import org.apache.taglibs.standard.tag.el.core.ExpressionUtil;
34
35 /**
36  * Base class for all tags in the infoglue platform.
37  */

38 public abstract class AbstractTag extends TagSupport JavaDoc
39 {
40     /**
41      * TODO: remove, use var instead.
42      *
43      * Indicates (if present) the name of the page context variable to store the result in.
44      */

45     private String JavaDoc id;
46     
47     /**
48      * Default constructor.
49      */

50     protected AbstractTag()
51     {
52         super();
53     }
54
55     /**
56      * TODO: remove, use var instead.
57      *
58      * Sets the name of the page context variable to store the result in.
59      *
60      * @param id the id to use.
61      */

62     public void setId(String JavaDoc id)
63     {
64         this.id = id;
65     }
66
67     /**
68      * Stores the <code>value</code> in the page context variable indicated by the <code>id</code> attribute.
69      * If the <code>value</code> is null; the page context variable is removed instead.
70      *
71      * @param value the value to store.
72      */

73     protected void setResultAttribute(Object JavaDoc value)
74     {
75         if(value == null)
76         {
77             pageContext.removeAttribute(id);
78         }
79         else
80         {
81             pageContext.setAttribute(id, value);
82         }
83     }
84     
85     /**
86      * Depending on wheter the <code>id</code> attribute is set, does one of the following:
87      *
88      * - stores the value in a page context variable (if the <code>id</code> attribute is set).
89      * - writes the value to the output stream.
90      *
91      * @param value the value.
92      * @throws JspTagException if an exception occurs when storing/writing the value.
93      */

94     protected void produceResult(Object JavaDoc value) throws JspTagException JavaDoc
95     {
96         if(id == null)
97         {
98             write((value == null) ? "" : value.toString());
99         }
100         else
101         {
102             setResultAttribute(value);
103         }
104     }
105
106     /**
107      * Writes the specified text to the response stream.
108      *
109      * @param text the text to write.
110      * @throws JspException if an I/O error occurs.
111      */

112     protected void write(final String JavaDoc text) throws JspTagException JavaDoc
113     {
114         try
115         {
116             pageContext.getOut().write(text);
117         }
118         catch(IOException JavaDoc e)
119         {
120             e.printStackTrace();
121             throw new JspTagException JavaDoc("IO error: " + e.getMessage());
122         }
123     }
124
125     /**
126      * Evaluates an expression if present, but does not allow the expression to evaluate to <code>null</null>.
127      *
128      * @param tagName the name of the tag.
129      * @param attributeName the name of the attribute to evaluate.
130      * @param expression the expression to evaluate.
131      * @param expectedType the expected type of the evaluated expression.
132      * @return the evaluated expression.
133      * @throws JspException if an error occurs while evaluating the expression.
134      */

135     protected Object JavaDoc evaluate(String JavaDoc tagName, String JavaDoc attributeName, String JavaDoc expression, Class JavaDoc expectedType) throws JspException JavaDoc
136     {
137         return ExpressionUtil.evalNotNull(tagName, attributeName, expression, expectedType, this, pageContext);
138     }
139
140     /**
141      * Evaluates the expression which must evaluate to an Integer.
142      *
143      * @param tagName the name of the tag.
144      * @param attributeName the name of the attribute to evaluate.
145      * @param expression the expression to evaluate.
146      * @return the evaluated expression.
147      * @throws JspException if an error occurs while evaluating the expression.
148      */

149     protected Integer JavaDoc evaluateInteger(String JavaDoc tagName, String JavaDoc attributeName, String JavaDoc expression) throws JspException JavaDoc
150     {
151         return (Integer JavaDoc) evaluate(tagName, attributeName, expression, Integer JavaDoc.class);
152     }
153
154     /**
155      * Evaluates the expression which must evaluate to a String.
156      *
157      * @param tagName the name of the tag.
158      * @param attributeName the name of the attribute to evaluate.
159      * @param expression the expression to evaluate.
160      * @return the evaluated expression.
161      * @throws JspException if an error occurs while evaluating the expression.
162      */

163     protected String JavaDoc evaluateString(String JavaDoc tagName, String JavaDoc attributeName, String JavaDoc expression) throws JspException JavaDoc
164     {
165         return (String JavaDoc) evaluate(tagName, attributeName, expression, String JavaDoc.class);
166     }
167
168     /**
169      * Evaluates the expression which must evaluate to a Collection.
170      *
171      * @param tagName the name of the tag.
172      * @param attributeName the name of the attribute to evaluate.
173      * @param expression the expression to evaluate.
174      * @return the evaluated expression.
175      * @throws JspException if an error occurs while evaluating the expression.
176      */

177     protected Collection JavaDoc evaluateCollection(String JavaDoc tagName, String JavaDoc attributeName, String JavaDoc expression) throws JspException JavaDoc
178     {
179         return (Collection JavaDoc) evaluate(tagName, attributeName, expression, Collection JavaDoc.class);
180     }
181
182     /**
183      * Evaluates the expression which must evaluate to a List.
184      *
185      * @param tagName the name of the tag.
186      * @param attributeName the name of the attribute to evaluate.
187      * @param expression the expression to evaluate.
188      * @return the evaluated expression.
189      * @throws JspException if an error occurs while evaluating the expression.
190      */

191     protected List JavaDoc evaluateList(String JavaDoc tagName, String JavaDoc attributeName, String JavaDoc expression) throws JspException JavaDoc
192     {
193         return (List JavaDoc) evaluate(tagName, attributeName, expression, List JavaDoc.class);
194     }
195 }
196
Popular Tags