KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > string > StringTagSupport


1 /*
2  * Copyright 1999,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.taglibs.string;
17
18 import java.io.IOException JavaDoc;
19 import java.io.StringWriter JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspWriter JavaDoc;
23 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
24
25 /**
26  * Abstract support class for the String Taglib.
27  * It handles the JSP taglib side of things and calls abstract
28  * protected methods to delegate the String functionality.
29  * <dl>
30  * <dt>var</dt><dd>
31  * PageContext variable to put the
32  * return result in instead of pushing
33  * out to the html page.
34  * </dd>
35  * </dl>
36  *
37  * @author bayard@generationjava.com
38  */

39 abstract public class StringTagSupport extends BodyTagSupport JavaDoc {
40
41
42
43     /**
44      * PageContext attribute to store the result in.
45      */

46     private String JavaDoc var;
47
48     /**
49      * Empty constructor. Initialises the attributes.
50      */

51     public StringTagSupport() {
52         initAttributes();
53     }
54
55     /**
56      * Get the PageContext attribute to store the result in.
57      */

58     public String JavaDoc getVar() {
59         return this.var;
60     }
61
62     /**
63      * Set the PageContext attribute to store the result in.
64      */

65     public void setVar(String JavaDoc var) {
66         this.var = var;
67     }
68        
69     /**
70      * Handles the manipulation of the String tag,
71      * evaluating the body of the tag. The evaluation
72      * is delegated to the changeString(String) method
73      */

74     public int doEndTag() throws JspException JavaDoc {
75
76     /*
77      * Although most of the tags that extends must have a body, some don't, like RandomStringTag
78      * So I'm removing the code below...
79      */

80          
81 // if( (bodyContent == null) && (!canBeEmpty()) ) {
82
// return EVAL_PAGE;
83
// }
84

85         String JavaDoc text = "";
86         if(bodyContent != null) {
87             StringWriter JavaDoc body = new StringWriter JavaDoc();
88             try {
89                 bodyContent.writeOut(body);
90                 text = body.toString();
91             } catch(IOException JavaDoc ioe) {
92                 ioe.printStackTrace();
93             }
94         }
95
96         // first, try to evaluate the string and associated the result on var
97
Object JavaDoc result = evaluateString( text );
98         if ( result != null && this.var != null ) {
99             pageContext.setAttribute(this.var, result);
100         } else {
101           // then, try to transform it
102
text = changeString(text);
103         
104           // TODO: RandomString is not working if body is set...
105
/*
106             System.err.println("...."+text+"....");
107             if ( text != null ) {
108             System.out.println( "length = " + text.length());
109             }
110           */

111         
112           if(this.var == null) {
113             JspWriter JavaDoc writer = pageContext.getOut();
114             try {
115               writer.print(text);
116             } catch (IOException JavaDoc e) {
117               throw new JspException JavaDoc(e.toString());
118             }
119           } else {
120             pageContext.setAttribute(this.var, text);
121           }
122
123         }
124
125         return (EVAL_PAGE);
126     }
127
128     /**
129      * Perform an operation on the passed String.
130      * The object returned by this operation (if not null) will be
131      * associated to PageContext attribute represented by this.var.
132      *
133      * @param str String to be manipulated
134      *
135      * @return Object result of operation upon passed String
136      */

137   public Object JavaDoc evaluateString(String JavaDoc str) throws JspException JavaDoc {
138     return null;
139   }
140   
141     /**
142      * Perform a transformation on the passed in String.
143      *
144      * @param str String to be manipulated
145      *
146      * @return String result of operation upon passed in String
147      */

148     abstract public String JavaDoc changeString(String JavaDoc str) throws JspException JavaDoc;
149
150     /**
151      * Initialise any properties to default values.
152      * This method is called upon construction, and
153      * after changeString(String) is called.
154      * This is a default empty implementation.
155      */

156     public void initAttributes() {
157         this.var = null;
158     }
159
160 }
161
Popular Tags