KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > localization > I18nWebworkAdapter


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.localization;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.Locale JavaDoc;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.jsp.PageContext JavaDoc;
19 import javax.servlet.jsp.tagext.Tag JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.displaytag.Messages;
24
25 import com.opensymphony.webwork.views.jsp.TagUtils;
26 import com.opensymphony.xwork.ActionContext;
27 import com.opensymphony.xwork.LocaleProvider;
28 import com.opensymphony.xwork.TextProvider;
29 import com.opensymphony.xwork.util.OgnlValueStack;
30
31
32 /**
33  * Webwork implementation of a resource provider and locale resolver.
34  * @author Richard HALLIER
35  * @author Fabrizio Giustina
36  * @version $Revision: 737 $ ($Author: fgiust $)
37  */

38 public class I18nWebworkAdapter implements LocaleResolver, I18nResourceProvider
39 {
40
41     /**
42      * prefix/suffix for missing entries.
43      */

44     public static final String JavaDoc UNDEFINED_KEY = "???"; //$NON-NLS-1$
45

46     /**
47      * logger.
48      */

49     private static Log log = LogFactory.getLog(I18nWebworkAdapter.class);
50
51     /**
52      * @see LocaleResolver#resolveLocale(HttpServletRequest)
53      */

54     public Locale JavaDoc resolveLocale(HttpServletRequest JavaDoc request)
55     {
56
57         Locale JavaDoc result = null;
58         OgnlValueStack stack = ActionContext.getContext().getValueStack();
59
60         Iterator JavaDoc iterator = stack.getRoot().iterator();
61         while (iterator.hasNext())
62         {
63             Object JavaDoc o = iterator.next();
64
65             if (o instanceof LocaleProvider)
66             {
67                 LocaleProvider lp = (LocaleProvider) o;
68                 result = lp.getLocale();
69
70                 break;
71             }
72         }
73
74         if (result == null)
75         {
76             log.debug("Missing LocalProvider actions, init locale to default");
77             result = Locale.getDefault();
78         }
79
80         return result;
81     }
82
83     /**
84      * @see I18nResourceProvider#getResource(String, String, Tag, PageContext)
85      */

86     public String JavaDoc getResource(String JavaDoc resourceKey, String JavaDoc defaultValue, Tag JavaDoc tag, PageContext JavaDoc pageContext)
87     {
88
89         // if resourceKey isn't defined either, use defaultValue
90
String JavaDoc key = (resourceKey != null) ? resourceKey : defaultValue;
91
92         String JavaDoc message = null;
93         OgnlValueStack stack = TagUtils.getStack(pageContext);
94         Iterator JavaDoc iterator = stack.getRoot().iterator();
95
96         while (iterator.hasNext())
97         {
98             Object JavaDoc o = iterator.next();
99
100             if (o instanceof TextProvider)
101             {
102                 TextProvider tp = (TextProvider) o;
103                 message = tp.getText(key, null, null);
104
105                 break;
106             }
107         }
108
109         // if user explicitely added a titleKey we guess this is an error
110
if (message == null && resourceKey != null)
111         {
112             log.debug(Messages.getString("Localization.missingkey", resourceKey)); //$NON-NLS-1$
113
message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY;
114         }
115
116         return message;
117     }
118 }
119
Popular Tags