KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > i18n > FormatStringTag


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
17 package org.apache.taglibs.i18n;
18
19 import java.io.IOException JavaDoc;
20 import java.text.Format JavaDoc;
21 import java.text.DateFormat JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.servlet.ServletRequest JavaDoc;
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.Tag JavaDoc;
30 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
31
32 /** A simple tag that allows a String to be output with null handling.
33   *
34   * @author James Strachan
35   * @version $Revision: 1.4 $
36   */

37 public class FormatStringTag extends TagSupport JavaDoc {
38     
39     protected static final String JavaDoc _tagname = "i18n:formatString";
40
41     /** the value to be formatted */
42     private String JavaDoc value;
43     /** the text output if the value is null */
44     private String JavaDoc defaultText = "";
45
46
47     // Tag interface
48
//-------------------------------------------------------------------------
49
public int doStartTag() throws JspException JavaDoc {
50         return EVAL_BODY_INCLUDE;
51     }
52     
53     public int doEndTag() throws JspException JavaDoc {
54         try {
55             JspWriter JavaDoc out = pageContext.getOut();
56             String JavaDoc text = getValue();
57             if ( text == null ) {
58                 text = getDefaultText();
59                 if ( text == null ) {
60                     text = "";
61                 }
62             }
63             out.print( text );
64         }
65         catch ( IOException JavaDoc e ) {
66             handleIOException( e );
67         }
68         return EVAL_PAGE;
69     }
70     
71     public void release() {
72         super.release();
73         value = null;
74         defaultText = "";
75     }
76     
77     // Properties
78
//-------------------------------------------------------------------------
79
public String JavaDoc getValue() {
80         return value;
81     }
82     
83     public void setValue( String JavaDoc value ) {
84         this.value = value;
85     }
86
87     public String JavaDoc getDefaultText() {
88         return defaultText;
89     }
90     
91     public void setDefaultText( String JavaDoc defaultText ) {
92         this.defaultText = defaultText;
93     }
94     
95     // Implementation methods
96
//-------------------------------------------------------------------------
97
protected void handleIOException( IOException JavaDoc e ) throws JspException JavaDoc {
98         pageContext.getServletContext().log( this._tagname + " tag, IOException: " + e );
99         throw new JspException JavaDoc( this._tagname + " tag, IOException: " + e );
100     }
101 }
102
103
104
Popular Tags