KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > 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.rt;
30
31 import com.caucho.jsp.PageContextImpl;
32 import com.caucho.jstl.ParamContainerTag;
33
34 import javax.servlet.jsp.JspException JavaDoc;
35 import javax.servlet.jsp.JspWriter JavaDoc;
36 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
37 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41 /**
42  * Looks up an i18n message from a bundle and prints it.
43  */

44 public class MessageTag extends BodyTagSupport JavaDoc implements ParamContainerTag {
45   private String JavaDoc _key;
46   private Object JavaDoc _bundle;
47
48   private ArrayList JavaDoc<Object JavaDoc> _params;
49
50   private String JavaDoc _var;
51   private String JavaDoc _scope;
52
53   /**
54    * Sets the message key.
55    *
56    * @param key the key value.
57    */

58   public void setKey(String JavaDoc key)
59   {
60     _key = key;
61   }
62
63   /**
64    * Sets the bundle.
65    *
66    * @param bundle the localication context
67    */

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

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

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

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

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