KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > www > TagUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: TagUtils.java,v 1.5 2007/01/07 06:14:09 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.www;
23
24 import java.io.IOException JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
30
31 /**
32  * This class is a collection of useful methods when working with custom JSP tags.
33  *
34  * Many methods in this class are inspired by Struts classes
35  * org.apache.struts.util.ResponseUtils
36  * org.apache.struts.taglib.TagUtils
37  *
38  * @version $Id: TagUtils.java,v 1.5 2007/01/07 06:14:09 bastafidli Exp $
39  * @author Miro Halas
40  * @code.reviewer Miro Halas
41  * @code.reviewed Initial revision
42  */

43 public final class TagUtils
44 {
45    // Constructors /////////////////////////////////////////////////////////////
46

47    /**
48     * Private constructor since this class cannot be instantiated
49     */

50    private TagUtils(
51    )
52    {
53       // Do nothing
54
}
55
56    // Public methods ///////////////////////////////////////////////////////////
57

58    /**
59     * Filter the specified string for characters that are senstive to
60     * HTML interpreters, returning the string with these characters replaced
61     * by the corresponding character entities.
62     *
63     * @param value - the string to be filtered and returned
64     * @return String - filtered value
65     */

66    public static String JavaDoc filter(
67        String JavaDoc value
68     )
69     {
70        if (value == null)
71        {
72            return (null);
73        }
74
75        char[] content = new char[value.length()];
76        
77        value.getChars(0, value.length(), content, 0);
78        StringBuffer JavaDoc result = new StringBuffer JavaDoc(content.length + 50);
79        
80        for (int i = 0; i < content.length; i++)
81        {
82            switch (content[i])
83            {
84               case ('<'):
85               {
86                  result.append("&lt;");
87                  break;
88               }
89               case ('>'):
90               {
91                  result.append("&gt;");
92                  break;
93               }
94               case ('&'):
95               {
96                  result.append("&amp;");
97                  break;
98               }
99               case ('"'):
100               {
101                  result.append("&quot;");
102                  break;
103               }
104               case ('\''):
105               {
106                  result.append("&#39;");
107                  break;
108               }
109               default:
110               {
111                  result.append(content[i]);
112                  break;
113               }
114            }
115        }
116
117        return result.toString();
118    }
119
120
121    /**
122     * Write the specified text as the response to the writer associated with
123     * this page.
124     *
125     * Note: If you are writing body content from the <code>doAfterBody()</code>
126     * method of a custom tag class that implements <code>BodyTag</code>, you
127     * should be calling <code>writePrevious()</code> instead.
128     *
129     * @param pageContext - the PageContext object for this page
130     * @param text - the text to be written
131     * @exception JspException - an error has occured
132     */

133    public static void write(
134       PageContext JavaDoc pageContext,
135       String JavaDoc text
136    )throws JspException JavaDoc
137    {
138       JspWriter JavaDoc writer = pageContext.getOut();
139
140       try
141       {
142          writer.print(text);
143       }
144       catch (IOException JavaDoc ioExc)
145       {
146          throw new JspException JavaDoc(ioExc);
147       }
148    }
149
150
151    /**
152     * Write the specified text as the response to the writer associated with
153     * the body content for the tag within which we are currently nested.
154     *
155     * @param pageContext - the PageContext object for this page
156     * @param text - the text to be written
157     * @exception JspException - an error has occured
158     */

159    public static void writePrevious(
160       PageContext JavaDoc pageContext,
161       String JavaDoc text
162    ) throws JspException JavaDoc
163    {
164        JspWriter JavaDoc writer = pageContext.getOut();
165        if (writer instanceof BodyContent JavaDoc)
166        {
167            writer = ((BodyContent JavaDoc) writer).getEnclosingWriter();
168        }
169        try
170        {
171            writer.print(text);
172        }
173        catch (IOException JavaDoc ioExc)
174        {
175            throw new JspException JavaDoc(ioExc);
176        }
177    }
178 }
179
Popular Tags