KickJava   Java API By Example, From Geeks To Geeks.

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


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.JspTagException JavaDoc;
28 import javax.servlet.jsp.JspWriter JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import javax.servlet.jsp.tagext.Tag JavaDoc;
31 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
32
33 /** An abstract base class for the formatting tags to provide
34   * implementation inheritence.
35   *
36   * @author James Strachan
37   * @version $Revision: 1.7 $
38   */

39 public abstract class FormatTagSupport extends TagSupport JavaDoc {
40     
41     protected static String JavaDoc _tagname = "i18n:";
42
43     /** the value to be formatted */
44     private Object JavaDoc value;
45     /** the locale used to format the value */
46     private Locale JavaDoc locale;
47     /** the text output if the value is null */
48     private String JavaDoc defaultText = "";
49
50
51     // Tag interface
52
//-------------------------------------------------------------------------
53
public int doStartTag() throws JspException JavaDoc {
54         return EVAL_BODY_INCLUDE;
55     }
56     
57     public int doEndTag() throws JspException JavaDoc {
58         try {
59             Object JavaDoc value = getValue();
60             JspWriter JavaDoc out = pageContext.getOut();
61             String JavaDoc text = null;
62             if ( value != null ) {
63                 Format JavaDoc formatter = getFormat();
64                 if ( formatter == null ) {
65                     throw new JspTagException JavaDoc(
66                         this._tagname +
67                         " tag, could not find valid Format instance");
68                 }
69                 text = formatter.format( value );
70             }
71             else {
72                 text = getDefaultText();
73             }
74             if ( text != null ) {
75                 out.print( text );
76             }
77         }
78         catch ( IOException JavaDoc e ) {
79             handleIOException( e );
80         }
81         return EVAL_PAGE;
82     }
83     
84     public void release() {
85         super.release();
86         value = null;
87         locale = null;
88         defaultText = "";
89     }
90     
91     // Properties
92
//-------------------------------------------------------------------------
93
public Object JavaDoc getValue() {
94         return value;
95     }
96     
97     public void setValue( Object JavaDoc value ) {
98         this.value = value;
99     }
100
101     /** If no {@link java.util.Locale} has been explicitly configured then use the
102       * parent LocaleTag if present else the Locale from the ServletRequest
103       * else use the default JVM {@link java.util.Locale}.
104       */

105     public Locale JavaDoc getLocale() {
106         if ( locale == null ) {
107             return findLocale();
108         }
109         return locale;
110     }
111     
112     public void setLocale( Locale JavaDoc locale ) {
113         this.locale = locale;
114     }
115
116     public String JavaDoc getDefaultText() {
117         return defaultText;
118     }
119     
120     public void setDefaultText( String JavaDoc defaultText ) {
121         this.defaultText = defaultText;
122     }
123
124     
125     // Implementation methods
126
//-------------------------------------------------------------------------
127

128     /** Abstract class to return the value formatter
129       */

130     protected abstract Format JavaDoc getFormat();
131     
132     protected void handleIOException( IOException JavaDoc e ) throws JspException JavaDoc {
133         pageContext.getServletContext().log( "Caught: IOException: " + e );
134         throw new JspTagException JavaDoc(
135             this._tagname + " tag, IOException: " + e );
136     }
137
138     /** finds the current locale from either an outer LocaleTag or the
139       * current SerlvetRequest or the current JVM.
140       *
141       * @return a Locale instance
142       */

143     private Locale JavaDoc findLocale() {
144         // lets try find a LocaleTag first
145
LocaleTag localeTag = (LocaleTag) findAncestorWithClass( this, LocaleTag.class );
146         if ( localeTag != null ) {
147             return localeTag.getLocale();
148         }
149         else {
150             return pageContext.getResponse().getLocale();
151         }
152     }
153     
154     
155     /** A helper method for date, datetime & time based formatting tags.
156       * This method converts a string into a DateFormat style code.
157       */

158     protected int getStyleCode( String JavaDoc style ) {
159         if ( "short".equalsIgnoreCase( style ) ) {
160             return DateFormat.SHORT;
161         }
162         else if ( "medium".equalsIgnoreCase( style ) ) {
163             return DateFormat.MEDIUM;
164         }
165         else if ( "long".equalsIgnoreCase( style ) ) {
166             return DateFormat.LONG;
167         }
168         else if ( "full".equalsIgnoreCase( style ) ) {
169             return DateFormat.FULL;
170         }
171         else {
172             return DateFormat.SHORT;
173         }
174     }
175 }
176
177
178
Popular Tags