KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > I18nTag


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.xwork.ActionContext;
8 import com.opensymphony.xwork.LocaleProvider;
9 import com.opensymphony.xwork.TextProviderSupport;
10 import com.opensymphony.xwork.util.LocalizedTextUtil;
11 import org.apache.commons.logging.LogFactory;
12
13 import javax.servlet.jsp.JspException JavaDoc;
14 import java.util.Locale JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16
17
18 /**
19  * Gets a resource bundle and place it on the value stack. This allows
20  * the text tag to access messages from any bundle, and not just the bundle
21  * associated with the current action.
22  *
23  * @author Rickard Öberg (rickard@dreambean.com)
24  * @version $Revision: 1.5 $
25  */

26 public class I18nTag extends WebWorkTagSupport {
27     //~ Instance fields ////////////////////////////////////////////////////////
28

29     // Attributes ----------------------------------------------------
30
protected String JavaDoc nameAttr;
31
32     //~ Methods ////////////////////////////////////////////////////////////////
33

34     // Public --------------------------------------------------------
35
public void setName(String JavaDoc aName) {
36         nameAttr = aName;
37     }
38
39     public int doEndTag() throws JspException JavaDoc {
40         getStack().pop();
41
42         return EVAL_PAGE;
43     }
44
45     // BodyTag implementation ----------------------------------------
46
public int doStartTag() throws JspException JavaDoc {
47         // Get bundle
48
try {
49             String JavaDoc name = this.findString(nameAttr);
50             ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc) findValue("texts('" + name + "')");
51
52             if (bundle == null) {
53                 bundle = LocalizedTextUtil.findResourceBundle(name, (Locale JavaDoc) getStack().getContext().get(ActionContext.LOCALE));
54             }
55
56             if (bundle != null) {
57                 final Locale JavaDoc locale = (Locale JavaDoc) getStack().getContext().get(ActionContext.LOCALE);
58                 getStack().push(new TextProviderSupport(bundle, new LocaleProvider() {
59                     public Locale JavaDoc getLocale() {
60                         return locale;
61                     }
62                 }));
63             }
64         } catch (Exception JavaDoc e) {
65             LogFactory.getLog(getClass()).error("Could not find the bundle " + nameAttr, e);
66             throw new JspException JavaDoc("Could not find the bundle " + nameAttr);
67         }
68
69         return EVAL_BODY_INCLUDE;
70     }
71 }
72
Popular Tags