001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: import org.apache.beehive.netui.tags.AbstractSimpleTag;
022: import org.apache.beehive.netui.util.Bundle;
023:
024: import javax.servlet.jsp.JspException;
025: import java.util.Locale;
026:
027: /**
028: * Abstract base class for formatting tags. Provides the basic formatting properties,
029: * as well as the base for the internal FormatTag.Formatter class.
030: */
031: public abstract class FormatTag extends AbstractSimpleTag {
032: private String _language = null;
033: private String _country = null;
034: protected String _pattern = null; // The pattern used by a FormatTag to do its formatting.
035:
036: /**
037: * Sets the language code for the locale.
038: * @param language the language code
039: * @jsptagref.attributedescription Sets the language code for the locale.
040: * @jsptagref.databindable false
041: * @jsptagref.attributesyntaxvalue <i>string_language</i>
042: * @netui:attribute required="false" rtexprvalue="true"
043: * description="Sets the language code for the locale."
044: */
045: public void setLanguage(String language) {
046: _language = setNonEmptyValueAttribute(language);
047: }
048:
049: /**
050: * Sets the country code for the locale.
051: * @param country the country code
052: * @jsptagref.attributedescription Sets the country code for the locale.
053: * @jsptagref.databindable false
054: * @jsptagref.attributesyntaxvalue <i>string_country</i>
055: * @netui:attribute required="false" rtexprvalue="true"
056: * description="Sets the country code for the locale."
057: */
058: public void setCountry(String country) {
059: _country = setNonEmptyValueAttribute(country);
060: }
061:
062: /**
063: * Returns the locale based on the country and language.
064: * @return the locale
065: */
066: public Locale getLocale() throws JspException {
067: Locale loc = null;
068: if (_language != null || _country != null) {
069: // language is required
070: if (_language == null) {
071: String s = Bundle.getString(
072: "Tags_LocaleRequiresLanguage",
073: new Object[] { _country });
074: registerTagError(s, null);
075: return super .getUserLocale();
076: }
077:
078: if (_country == null)
079: loc = new Locale(_language);
080: else
081: loc = new Locale(_language, _country);
082: } else
083: loc = super .getUserLocale();
084:
085: return loc;
086: }
087:
088: /**
089: * Sets the pattern to be used by this FormatTag.
090: * @param pattern the pattern to be used
091: * @jsptagref.attributedescription Sets the pattern to be used by this format tag.
092: * (See the tag description)
093: * @jsptagref.databindable false
094: * @jsptagref.attributesyntaxvalue <i>string_pattern</i>
095: * @netui:attribute required="false" rtexprvalue="true"
096: * description="Sets the pattern to be used by this FormatTag."
097: */
098: public void setPattern(String pattern) throws JspException {
099: _pattern = setRequiredValueAttribute(pattern, "patttern");
100: }
101:
102: /**
103: * Internal FormatTag.Formatter which performs the actual formatting.
104: */
105: public abstract static class Formatter {
106: private String pattern;
107:
108: public String getPattern() {
109: return pattern;
110: }
111:
112: public void setPattern(String pattern) {
113: this .pattern = pattern;
114: }
115:
116: public String format(Object dataToFormat) throws JspException {
117: //Default implementation
118: if (dataToFormat == null)
119: return null;
120: else
121: return dataToFormat.toString();
122: }
123:
124: public boolean hasError() {
125: return false;
126: }
127:
128: public String getErrorMessage() {
129: return null;
130: }
131: }
132: }
|