KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > tags > CodecTag


1 /*
2  * $$Id$$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on Dec 9, 2003
27  *
28  */

29 package org.jresearch.gossip.tags;
30
31 import java.io.IOException JavaDoc;
32
33 import javax.servlet.jsp.JspException JavaDoc;
34 import javax.servlet.jsp.JspWriter JavaDoc;
35 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
36 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
37
38 import org.apache.log.Logger;
39 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
40 import org.jresearch.gossip.exception.SystemException;
41 import org.jresearch.gossip.log.avalon.JGossipLog;
42 import org.jresearch.gossip.util.HtmlCodec;
43
44 /**
45  * DOCUMENT ME!
46  *
47  * @author Bel
48  */

49 public class CodecTag extends BodyTagSupport JavaDoc {
50
51     /**
52      * Logger instance.
53      */

54     private Logger log;
55
56     private String JavaDoc value = "";
57
58     /**
59      * Default c'tor.
60      */

61     public CodecTag() {
62         super();
63         try {
64             log = JGossipLog.getInstance().getAppLogger();
65         } catch (SystemException e) { /* Ignore Exception! */
66         }
67     }
68
69     /**
70      * DOCUMENT ME!
71      *
72      * @param value
73      * DOCUMENT ME!
74      */

75     public void setValue(String JavaDoc value) {
76         this.value = value;
77     }
78
79     private void eval() throws JspException JavaDoc {
80         value = (String JavaDoc) ExpressionEvaluatorManager.evaluate("value", value,
81                 String JavaDoc.class, this, pageContext);
82     }
83
84     public int doStartTag() throws JspException JavaDoc {
85         try {
86             if (value != null) {
87                 eval();
88                 JspWriter JavaDoc out = pageContext.getOut();
89                 out.print(HtmlCodec.encode(value.trim()));
90             }
91         } catch (IOException JavaDoc ex) {
92             if (log.isErrorEnabled()) {
93                 log.error("CodecTag::", ex);
94             }
95             throw new JspException JavaDoc("error in CodecTag tag:" + ex.getMessage());
96         }
97         return (EVAL_BODY_BUFFERED);
98     }
99
100     public int doAfterBody() throws JspException JavaDoc {
101         try {
102             BodyContent JavaDoc body = getBodyContent();
103             if (body != null) {
104                 JspWriter JavaDoc out = body.getEnclosingWriter();
105                 String JavaDoc encodedBody = HtmlCodec.encode(body.getString());
106                 out.print(encodedBody.equalsIgnoreCase("null") ? ""
107                         : encodedBody);
108             }
109
110         } catch (IOException JavaDoc ex) {
111             if (log.isErrorEnabled()) {
112                 log.error("CodecTag::", ex);
113             }
114             throw new JspException JavaDoc("error in CodecTag tag:" + ex.getMessage());
115         }
116         return (SKIP_BODY);
117     }
118 }
119
Popular Tags