KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > 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.rt;
30
31 import com.caucho.jsp.PageContextImpl;
32 import com.caucho.util.L10N;
33
34 import javax.servlet.jsp.JspException JavaDoc;
35 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
36 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
37 import javax.servlet.jsp.tagext.TryCatchFinally JavaDoc;
38
39 /**
40  * Sets the i18n localization bundle for a context.
41  */

42 public class BundleTag extends TagSupport JavaDoc implements TryCatchFinally JavaDoc {
43   private static L10N L = new L10N(BundleTag.class);
44   
45   private String JavaDoc _basename;
46   private String JavaDoc _prefix;
47
48   private Object JavaDoc _oldBundle;
49   private Object JavaDoc _oldPrefix;
50
51   /**
52    * Sets the basename.
53    */

54   public void setBasename(String JavaDoc basename)
55   {
56     _basename = basename;
57   }
58
59   /**
60    * Sets a prefix for message keys.
61    */

62   public void setPrefix(String JavaDoc prefix)
63   {
64     _prefix = prefix;
65   }
66
67   /**
68    * Process the tag.
69    */

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

93   public void doCatch(Throwable JavaDoc t)
94     throws Throwable JavaDoc
95   {
96     throw t;
97   }
98   
99   /**
100    * Handle the finaly
101    */

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