001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package clime.messadmin.taglib.fmt;
018:
019: //package org.apache.taglibs.standard.tag.rt.fmt;
020:
021: import java.io.IOException;
022: import java.text.MessageFormat;
023: import java.util.ArrayList;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.MissingResourceException;
027: import java.util.ResourceBundle;
028:
029: import javax.servlet.jsp.JspException;
030: import javax.servlet.jsp.JspTagException;
031: import javax.servlet.jsp.PageContext;
032: import javax.servlet.jsp.tagext.BodyTagSupport;
033: import javax.servlet.jsp.tagext.Tag;
034:
035: import clime.messadmin.taglib.core.Util;
036: import clime.messadmin.taglib.jstl.fmt.LocalizationContext;
037:
038: /**
039: * <p>A handler for <message> that supports rtexprvalue-based
040: * attributes.</p>
041: *
042: * @author Jan Luehe
043: */
044:
045: public class MessageTag extends BodyTagSupport {
046:
047: //*********************************************************************
048: // Accessor methods
049:
050: // for tag attribute
051: public void setKey(String key) throws JspTagException {
052: this .keyAttrValue = key;
053: this .keySpecified = true;
054: }
055:
056: // for tag attribute
057: public void setBundle(LocalizationContext locCtxt)
058: throws JspTagException {
059: this .bundleAttrValue = locCtxt;
060: this .bundleSpecified = true;
061: }
062:
063: //*********************************************************************
064: // Public constants
065:
066: public static final String UNDEFINED_KEY = "???";//$NON-NLS-1$
067:
068: //*********************************************************************
069: // Protected state
070:
071: protected String keyAttrValue; // 'key' attribute value
072: protected boolean keySpecified; // 'key' attribute specified
073: protected LocalizationContext bundleAttrValue; // 'bundle' attribute value
074: protected boolean bundleSpecified; // 'bundle' attribute specified?
075:
076: //*********************************************************************
077: // Private state
078:
079: private String var; // 'var' attribute
080: private int scope; // 'scope' attribute
081: private List params;
082:
083: //*********************************************************************
084: // Constructor and initialization
085:
086: public MessageTag() {
087: super ();
088: params = new ArrayList();
089: init();
090: }
091:
092: private void init() {
093: var = null;
094: scope = PageContext.PAGE_SCOPE;
095: keyAttrValue = null;
096: keySpecified = false;
097: bundleAttrValue = null;
098: bundleSpecified = false;
099: }
100:
101: //*********************************************************************
102: // Tag attributes known at translation time
103:
104: public void setVar(String var) {
105: this .var = var;
106: }
107:
108: public void setScope(String scope) {
109: this .scope = Util.getScope(scope);
110: }
111:
112: //*********************************************************************
113: // Collaboration with subtags
114:
115: /**
116: * Adds an argument (for parametric replacement) to this tag's message.
117: *
118: * @see ParamSupport
119: */
120: public void addParam(Object arg) {
121: params.add(arg);
122: }
123:
124: //*********************************************************************
125: // Tag logic
126:
127: public int doStartTag() throws JspException {
128: params.clear();
129: return EVAL_BODY_BUFFERED;
130: }
131:
132: public int doEndTag() throws JspException {
133:
134: String key = null;
135: LocalizationContext locCtxt = null;
136:
137: // determine the message key by...
138: if (keySpecified) {
139: // ... reading 'key' attribute
140: key = keyAttrValue;
141: } else {
142: // ... retrieving and trimming our body
143: if (bodyContent != null && bodyContent.getString() != null)
144: key = bodyContent.getString().trim();
145: }
146:
147: if ((key == null) || key.equals("")) {//$NON-NLS-1$
148: try {
149: pageContext.getOut().print("??????");//$NON-NLS-1$
150: } catch (IOException ioe) {
151: throw new JspTagException(ioe.toString());//, ioe);
152: }
153: return EVAL_PAGE;
154: }
155:
156: String prefix = null;
157: if (!bundleSpecified) {
158: Tag t = findAncestorWithClass(this , BundleTag.class);
159: if (t != null) {
160: // use resource bundle from parent <bundle> tag
161: BundleTag parent = (BundleTag) t;
162: locCtxt = parent.getLocalizationContext();
163: prefix = parent.getPrefix();
164: } else {
165: locCtxt = BundleTag.getLocalizationContext(pageContext);
166: }
167: } else {
168: // localization context taken from 'bundle' attribute
169: locCtxt = bundleAttrValue;
170: if (locCtxt.getLocale() != null) {
171: SetLocaleTag.setResponseLocale(pageContext, locCtxt
172: .getLocale());
173: }
174: }
175:
176: String message = UNDEFINED_KEY + key + UNDEFINED_KEY;
177: if (locCtxt != null) {
178: ResourceBundle bundle = locCtxt.getResourceBundle();
179: if (bundle != null) {
180: try {
181: // prepend 'prefix' attribute from parent bundle
182: if (prefix != null)
183: key = prefix + key;
184: message = bundle.getString(key);
185: // Perform parametric replacement if required
186: if (!params.isEmpty()) {
187: Object[] messageArgs = params.toArray();
188: MessageFormat formatter = new MessageFormat(""); // empty pattern, default Locale //$NON-NLS-1$
189: if (locCtxt.getLocale() != null) {
190: formatter.setLocale(locCtxt.getLocale());
191: } else {
192: // For consistency with the <fmt:formatXXX> actions,
193: // we try to get a locale that matches the user's preferences
194: // as well as the locales supported by 'date' and 'number'.
195: //System.out.println("LOCALE-LESS LOCCTXT: GETTING FORMATTING LOCALE");
196: Locale locale = SetLocaleTag
197: .getFormattingLocale(pageContext);
198: //System.out.println("LOCALE: " + locale);
199: if (locale != null) {
200: formatter.setLocale(locale);
201: }
202: }
203: formatter.applyPattern(message);
204: message = formatter.format(messageArgs);
205: }
206: } catch (MissingResourceException mre) {
207: message = UNDEFINED_KEY + key + UNDEFINED_KEY;
208: }
209: }
210: }
211:
212: if (var != null) {
213: pageContext.setAttribute(var, message, scope);
214: } else {
215: try {
216: pageContext.getOut().print(message);
217: } catch (IOException ioe) {
218: throw new JspTagException(ioe.toString());//, ioe);
219: }
220: }
221:
222: return EVAL_PAGE;
223: }
224:
225: // Releases any resources we may have (or inherit)
226: public void release() {
227: init();
228: }
229: }
|