KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > MessageTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl.el;
30
31 import com.caucho.el.Expr;
32 import com.caucho.jsp.PageContextImpl;
33 import com.caucho.jstl.ParamContainerTag;
34
35 import javax.el.ELContext;
36 import javax.servlet.jsp.JspException JavaDoc;
37 import javax.servlet.jsp.JspWriter JavaDoc;
38 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
39 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * Looks up an i18n message from a bundle and prints it.
44  */

45 public class MessageTag extends BodyTagSupport JavaDoc implements ParamContainerTag {
46   private Expr _keyExpr;
47   private Expr _bundleExpr;
48
49   private ArrayList JavaDoc<Object JavaDoc> _params;
50
51   private String JavaDoc _var;
52   private String JavaDoc _scope;
53
54   /**
55    * Sets the message key.
56    *
57    * @param key the JSP-EL expression for the key.
58    */

59   public void setKey(Expr key)
60   {
61     _keyExpr = key;
62   }
63
64   /**
65    * Sets the bundle.
66    *
67    * @param bundle the JSP-EL expression for the bundle.
68    */

69   public void setBundle(Expr bundle)
70   {
71     _bundleExpr = bundle;
72   }
73
74   /**
75    * Sets the variable.
76    *
77    * @param var the variable
78    */

79   public void setVar(String JavaDoc var)
80   {
81     _var = var;
82   }
83
84   /**
85    * Sets the scope.
86    *
87    * @param scope the scope
88    */

89   public void setScope(String JavaDoc scope)
90   {
91     _scope = scope;
92   }
93
94   /**
95    * Add a parameter value to the message.
96    */

97   public void addParam(Object JavaDoc value)
98   {
99     if (_params == null)
100       _params = new ArrayList JavaDoc<Object JavaDoc>();
101
102     _params.add(value);
103   }
104
105   /**
106    * Process the tag.
107    */

108   public int doEndTag()
109     throws JspException JavaDoc
110   {
111     Object JavaDoc []args = null;
112
113     if (_params != null) {
114       args = _params.toArray(new Object JavaDoc[_params.size()]);
115       _params = null;
116     }
117     
118     try {
119       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
120       ELContext env = pageContext.getELContext();
121       
122       JspWriter JavaDoc out = pageContext.getOut();
123
124       String JavaDoc key;
125
126       if (_keyExpr != null)
127         key = _keyExpr.evalString(env);
128       else
129         key = getBodyContent().getString().trim();
130
131       String JavaDoc msg;
132       
133       if (_bundleExpr != null) {
134         Object JavaDoc bundleObject = _bundleExpr.evalObject(env);
135
136         msg = pageContext.getLocalizedMessage(bundleObject, key, args, null);
137       }
138       else {
139         LocalizationContext lc;
140         lc = (LocalizationContext) pageContext.getAttribute("caucho.bundle");
141
142         if (lc == null)
143           msg = pageContext.getLocalizedMessage(key, args, null);
144         else
145           msg = pageContext.getLocalizedMessage(lc, key, args, null);
146       }
147
148       if (_var != null)
149         CoreSetTag.setValue(pageContext, _var, _scope, msg);
150       else
151         out.print(msg);
152     } catch (Exception JavaDoc e) {
153       throw new JspException JavaDoc(e);
154     }
155
156     return EVAL_PAGE;
157   }
158 }
159
Popular Tags