KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.L10N;
34
35 import javax.servlet.jsp.JspException JavaDoc;
36 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
37 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
38 import javax.servlet.jsp.tagext.TryCatchFinally JavaDoc;
39
40 /**
41  * Sets the i18n localization bundle for a context.
42  */

43 public class BundleTag extends TagSupport JavaDoc implements TryCatchFinally JavaDoc {
44   private static L10N L = new L10N(BundleTag.class);
45   
46   private Expr _basenameExpr;
47   private Expr _prefixExpr;
48
49   private Object JavaDoc _oldBundle;
50   private Object JavaDoc _oldPrefix;
51
52   /**
53    * Sets the JSP-EL expression for the basename.
54    */

55   public void setBasename(Expr basename)
56   {
57     _basenameExpr = basename;
58   }
59
60   /**
61    * Sets a prefix for message keys.
62    */

63   public void setPrefix(Expr prefix)
64   {
65     _prefixExpr = prefix;
66   }
67
68   /**
69    * Process the tag.
70    */

71   public int doStartTag()
72     throws JspException JavaDoc
73   {
74     PageContextImpl pc = (PageContextImpl) pageContext;
75
76     String JavaDoc basename = _basenameExpr.evalString(pc.getELContext());
77
78     _oldBundle = pc.getAttribute("caucho.bundle");
79     _oldPrefix = pc.getAttribute("caucho.bundle.prefix");
80     
81     LocalizationContext bundle = pc.getBundle(basename);
82
83     pc.setAttribute("caucho.bundle", bundle);
84
85     if (_prefixExpr != null) {
86       String JavaDoc prefix = _prefixExpr.evalString(pc.getELContext());
87
88       pc.setAttribute("caucho.bundle.prefix", prefix);
89     }
90     else if (_oldPrefix != null)
91       pc.removeAttribute("caucho.bundle.prefix");
92
93     return EVAL_BODY_INCLUDE;
94   }
95
96   /**
97    * Handle the catch
98    */

99   public void doCatch(Throwable JavaDoc t)
100     throws Throwable JavaDoc
101   {
102     throw t;
103   }
104   
105   /**
106    * Handle the finaly
107    */

108   public void doFinally()
109   {
110     if (_oldBundle == null)
111       pageContext.removeAttribute("caucho.bundle");
112     else
113       pageContext.setAttribute("caucho.bundle", _oldBundle);
114     
115     if (_oldPrefix == null)
116       pageContext.removeAttribute("caucho.bundle.prefix");
117     else
118       pageContext.setAttribute("caucho.bundle.prefix", _oldPrefix);
119   }
120
121   public static Object JavaDoc setBundle(String JavaDoc baseName, PageContextImpl pc)
122   {
123     Object JavaDoc oldBundle = pc.getAttribute("caucho.bundle");
124     
125     LocalizationContext bundle = pc.getBundle(baseName);
126
127     pc.setAttribute("caucho.bundle", bundle);
128
129     return oldBundle;
130   }
131 }
132
Popular Tags