KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > i18n > StrutsMessageFinder


1 /**
2  * Dec 30, 2004
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.tagutil.i18n;
19
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.http.HttpSession JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24
25
26 import org.apache.struts.Globals;
27 import org.apache.struts.util.MessageResources;
28
29 /**
30  * Struts message finder adapter.
31  *
32  * @author jonni
33  * @version $Id$
34  */

35 public final class StrutsMessageFinder implements MessageFinder {
36   private static final long serialVersionUID = 100L;
37
38   /**
39    * Struts resource bundle
40    */

41   private MessageResources resources;
42   /**
43    * Locale to use
44    */

45   private Locale JavaDoc locale;
46
47   /**
48    * Default constructor.
49    */

50   public StrutsMessageFinder() {
51     super();
52   }
53
54   /** {@inheritDoc} */
55   public void setPageContext(PageContext JavaDoc pageContext) {
56     this.resources = getMessageResources(pageContext);
57     this.locale = getLocale(pageContext);
58   }
59
60   /**
61    * Looks up and returns Struts resourde bundle
62    *
63    * @param pageContext allows scoped-attribute lookup
64    * @return Struts resource bundle
65    */

66   private MessageResources getMessageResources(PageContext JavaDoc pageContext) {
67     return ((MessageResources)
68         pageContext.getRequest().getAttribute(Globals.MESSAGES_KEY));
69   }
70
71   /**
72    * Returns the locale object created by Struts, or client browser's locale
73    * if the former's not available.
74    *
75    * @param pageContext allows scoped-attribute lookup
76    * @return client's locale
77    */

78   private Locale JavaDoc getLocale(PageContext JavaDoc pageContext) {
79     HttpSession JavaDoc session = pageContext.getSession();
80     Locale JavaDoc ret = null;
81     // See if a Locale has been set up for Struts
82
if (session != null) {
83       ret = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
84     }
85
86     // If we've found nothing so far, use client browser's Locale
87
if (ret == null) {
88       ret = pageContext.getRequest().getLocale();
89     }
90     return ret;
91   }
92
93   /** {@inheritDoc} */
94   public String JavaDoc get(String JavaDoc key) {
95     return this.resources.getMessage(this.locale, key);
96   }
97
98   /** {@inheritDoc} */
99   public String JavaDoc get(String JavaDoc key, Object JavaDoc arg0) {
100     return this.resources.getMessage(this.locale, key, arg0);
101   }
102
103   /** {@inheritDoc} */
104   public String JavaDoc get(String JavaDoc key, Object JavaDoc[] args) {
105     return this.resources.getMessage(this.locale, key, args);
106   }
107 }
108
Popular Tags