KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > FormatTag


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.tags.AbstractSimpleTag;
21 import org.apache.beehive.netui.util.Bundle;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import java.util.Locale JavaDoc;
25
26 /**
27  * Abstract base class for formatting tags. Provides the basic formatting properties,
28  * as well as the base for the internal FormatTag.Formatter class.
29  */

30 public abstract class FormatTag extends AbstractSimpleTag
31 {
32     private String JavaDoc _language = null;
33     private String JavaDoc _country = null;
34     protected String JavaDoc _pattern = null; // The pattern used by a FormatTag to do its formatting.
35

36     /**
37      * Sets the language code for the locale.
38      * @param language the language code
39      * @jsptagref.attributedescription Sets the language code for the locale.
40      * @jsptagref.databindable false
41      * @jsptagref.attributesyntaxvalue <i>string_language</i>
42      * @netui:attribute required="false" rtexprvalue="true"
43      * description="Sets the language code for the locale."
44      */

45     public void setLanguage(String JavaDoc language)
46     {
47         _language = setNonEmptyValueAttribute(language);
48     }
49
50     /**
51      * Sets the country code for the locale.
52      * @param country the country code
53      * @jsptagref.attributedescription Sets the country code for the locale.
54      * @jsptagref.databindable false
55      * @jsptagref.attributesyntaxvalue <i>string_country</i>
56      * @netui:attribute required="false" rtexprvalue="true"
57      * description="Sets the country code for the locale."
58      */

59     public void setCountry(String JavaDoc country)
60     {
61         _country = setNonEmptyValueAttribute(country);
62     }
63
64     /**
65      * Returns the locale based on the country and language.
66      * @return the locale
67      */

68     public Locale JavaDoc getLocale()
69             throws JspException JavaDoc
70     {
71         Locale JavaDoc loc = null;
72         if (_language != null || _country != null) {
73             // language is required
74
if (_language == null) {
75                 String JavaDoc s = Bundle.getString("Tags_LocaleRequiresLanguage", new Object JavaDoc[]{_country});
76                 registerTagError(s, null);
77                 return super.getUserLocale();
78             }
79
80             if (_country == null)
81                 loc = new Locale JavaDoc(_language);
82             else
83                 loc = new Locale JavaDoc(_language, _country);
84         }
85         else
86             loc = super.getUserLocale();
87
88         return loc;
89     }
90
91     /**
92      * Sets the pattern to be used by this FormatTag.
93      * @param pattern the pattern to be used
94      * @jsptagref.attributedescription Sets the pattern to be used by this format tag.
95      * (See the tag description)
96      * @jsptagref.databindable false
97      * @jsptagref.attributesyntaxvalue <i>string_pattern</i>
98      * @netui:attribute required="false" rtexprvalue="true"
99      * description="Sets the pattern to be used by this FormatTag."
100      */

101     public void setPattern(String JavaDoc pattern)
102             throws JspException JavaDoc
103     {
104         _pattern = setRequiredValueAttribute(pattern, "patttern");
105     }
106
107     /**
108      * Internal FormatTag.Formatter which performs the actual formatting.
109      */

110     public abstract static class Formatter
111     {
112         private String JavaDoc pattern;
113
114         public String JavaDoc getPattern()
115         {
116             return pattern;
117         }
118
119         public void setPattern(String JavaDoc pattern)
120         {
121             this.pattern = pattern;
122         }
123
124         public String JavaDoc format(Object JavaDoc dataToFormat) throws JspException JavaDoc
125         {
126             //Default implementation
127
if (dataToFormat == null)
128                 return null;
129             else
130                 return dataToFormat.toString();
131         }
132
133         public boolean hasError()
134         {
135             return false;
136         }
137
138         public String JavaDoc getErrorMessage()
139         {
140             return null;
141         }
142     }
143 }
144
Popular Tags