1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control;
16
17 import org.apache.struts.Globals;
18 import org.apache.struts.config.ModuleConfig;
19 import org.apache.struts.util.MessageResources;
20
21 import org.displaytag.properties.TableProperties;
22 import org.displaytag.localization.I18nStrutsAdapter;
23
24 import javax.servlet.jsp.tagext.Tag;
25 import javax.servlet.jsp.PageContext;
26 import javax.servlet.http.HttpServletRequest;
27
28 import java.util.Locale;
29 import java.util.ArrayList;
30 import java.util.ResourceBundle;
31 import java.util.StringTokenizer;
32 import java.util.MissingResourceException;
33 /**
34 * This is an extension to the DisplayTag I18nStrutsAdapter. This original
35 * implementation is not able to retrieved i18n messages that are not located
36 * into the Struts default resource bundle (messages.properties). This new
37 * extension allows you to specify the bundles to search for i18n messages.
38 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
39 * @version $Revision: 1.2 $
40 */
41 public class I18nStrutsBundleAdapter extends I18nStrutsAdapter{
42
43
44
45 /** The <code>locale.provider.bundles</code> property. */
46 public static final String PROPERTY_STRING_LOCALE_BUNDLES = "locale.provider.bundles";
47
48
49
50
51 /** Initialized flag. */
52 private boolean init = false;
53 /** The bundle names. */
54 private ArrayList i18nBundles = new ArrayList();
55
56
57
58
59 /**
60 * Lookup an internationalized resource that may not be packaged
61 * into the Struts default resource bundle. Use the value of "locale.provider.bundles"
62 * property from the displaytag.properties config file to know the bundles to search
63 * for resource.
64 * @see org.displaytag.localization.I18nResourceProvider#getResource(String, String, Tag, PageContext)
65 */
66 public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext){
67
68 if (!init) initialize(pageContext);
69
70
71 String key = (resourceKey != null) ? resourceKey : defaultValue;
72
73
74 for (int i=0; i<i18nBundles.size(); i++){
75 String name = (String)i18nBundles.get(i);
76 MessageResources resources = (MessageResources)pageContext.getAttribute(name, pageContext.APPLICATION_SCOPE);
77
78 if (resources == null){
79
80 ModuleConfig moduleConfig = (ModuleConfig)pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
81
82 if (moduleConfig == null){
83 moduleConfig = (ModuleConfig)pageContext.getServletContext().getAttribute(Globals.MODULE_KEY);
84 pageContext.getRequest().setAttribute(Globals.MODULE_KEY, moduleConfig);
85 }
86
87 resources = (MessageResources)pageContext.getAttribute(name + moduleConfig.getPrefix(),
88 PageContext.APPLICATION_SCOPE);
89 }
90
91 if (resources != null){
92
93 String resource = resources.getMessage(key);
94 if (resource != null) return resource;
95 }
96 }
97
98
99 return super.getResource(resourceKey, defaultValue, tag, pageContext);
100 }
101
102
103
104
105 /**
106 * Initialize bundles list from user property. Bundles list should
107 * be provided as a coma separated names of bundles to search for
108 * resource keys.
109 */
110 private void initialize(PageContext pageContext){
111
112 ResourceBundle bundle = null;
113 Locale locale = resolveLocale((HttpServletRequest)pageContext.getRequest());
114 try{
115
116 bundle = ResourceBundle.getBundle(TableProperties.LOCAL_PROPERTIES, locale);
117 }
118 catch (MissingResourceException e){
119 try{
120
121 bundle = ResourceBundle.getBundle(TableProperties.LOCAL_PROPERTIES, locale,
122 Thread.currentThread().getContextClassLoader());
123 }
124 catch (MissingResourceException mre) {
125 }
126
127 if (bundle != null){
128 String i18nBundlesList = null;
129 try{
130
131 i18nBundlesList = bundle.getString(PROPERTY_STRING_LOCALE_BUNDLES);
132 }
133 catch (MissingResourceException mre) {
134
135 if (i18nBundlesList != null && i18nBundlesList.length() > 0){
136
137 StringTokenizer tokenizer = new StringTokenizer(i18nBundlesList, ",");
138 while (tokenizer.hasMoreTokens()){
139 String token = tokenizer.nextToken();
140 String name = token.trim();
141 i18nBundles.add(name);
142 }
143 }
144 }
145 init = true;
146 }
147 }