1 | /*
|
---|
2 | * Dictionary.java
|
---|
3 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
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. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import java.util.ResourceBundle;
|
---|
22 | import java.util.Locale;
|
---|
23 | import java.util.Enumeration;
|
---|
24 |
|
---|
25 | import org.apache.log4j.*;
|
---|
26 |
|
---|
27 | public class Dictionary
|
---|
28 | {
|
---|
29 |
|
---|
30 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.Dictionary.class.getName());
|
---|
31 |
|
---|
32 | /** The default servlet dictionary */
|
---|
33 | public static final String core_servlet_dictionary_name = "core_servlet_dictionary";
|
---|
34 |
|
---|
35 | /** The locale of this dictionary. */
|
---|
36 | protected Locale locale = null;
|
---|
37 |
|
---|
38 | /** The resource that has been loaded */
|
---|
39 | protected String resource = null;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * The ResourceBundle which contains the raw key-value mappings. Loaded from
|
---|
43 | * a file named "resource_locale.properties
|
---|
44 | */
|
---|
45 | private ResourceBundle raw = null;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Constructs the Dictionary class by first creating a locale from the
|
---|
49 | * specified language, (or using the default locale if this didn't work),
|
---|
50 | * then loading a resource bundle based on this locale.
|
---|
51 | */
|
---|
52 | public Dictionary(String resource, String lang)
|
---|
53 | {
|
---|
54 | // Initialize.
|
---|
55 |
|
---|
56 | this.locale = new Locale(lang);
|
---|
57 | this.resource = resource;
|
---|
58 | if (this.locale == null)
|
---|
59 | {
|
---|
60 | this.locale = Locale.getDefault();
|
---|
61 | }
|
---|
62 | try
|
---|
63 | {
|
---|
64 | this.raw = ResourceBundle.getBundle(this.resource, this.locale, new UTF8Control());
|
---|
65 | }
|
---|
66 | catch (Exception e)
|
---|
67 | {
|
---|
68 | //logger.debug("Dictionary: couldn't locate a resource bundle for "+resource);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public Dictionary(String resource, Locale locale)
|
---|
73 | {
|
---|
74 | this.locale = locale;
|
---|
75 | this.resource = resource;
|
---|
76 | try
|
---|
77 | {
|
---|
78 | this.raw = ResourceBundle.getBundle(this.resource, this.locale, new UTF8Control());
|
---|
79 | }
|
---|
80 | catch (Exception e)
|
---|
81 | {
|
---|
82 | //logger.debug("Dictionary: couldn't locate a resource bundle for "+resource);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Constructs the Dictionary class by first creating a locale from the
|
---|
88 | * specified language, (or using the default locale if this didn't work),
|
---|
89 | * then loading a resource bundle based on this locale. A classloader is
|
---|
90 | * specified which can be used to find the resource.
|
---|
91 | */
|
---|
92 | public Dictionary(String resource, String lang, ClassLoader loader)
|
---|
93 | {
|
---|
94 | // Initialize.
|
---|
95 | this.locale = new Locale(lang);
|
---|
96 | this.resource = resource;
|
---|
97 | if (this.locale == null)
|
---|
98 | {
|
---|
99 | this.locale = Locale.getDefault();
|
---|
100 | }
|
---|
101 | // try the specified class loader
|
---|
102 | if (loader != null) {
|
---|
103 | try
|
---|
104 | {
|
---|
105 | this.raw = ResourceBundle.getBundle(this.resource, this.locale, loader, new UTF8Control());
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | catch (Exception e)
|
---|
109 | {
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | // try with default class loader
|
---|
114 | try
|
---|
115 | {
|
---|
116 | this.raw = ResourceBundle.getBundle(this.resource, this.locale, new UTF8Control());
|
---|
117 | }
|
---|
118 | catch (Exception ex)
|
---|
119 | {
|
---|
120 | //logger.debug("Dictionary: couldn't locate a resource bundle for "+resource);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public Enumeration getKeys()
|
---|
125 | {
|
---|
126 | if (this.raw != null)
|
---|
127 | {
|
---|
128 | return this.raw.getKeys();
|
---|
129 | }
|
---|
130 | return null;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Overloaded to call get with both a key and an empty argument array.
|
---|
135 | *
|
---|
136 | * @param key
|
---|
137 | * A <strong>String</strong> which is mapped to a initial String
|
---|
138 | * within the ResourceBundle.
|
---|
139 | * @return A <strong>String</strong> which has been referenced by the key
|
---|
140 | * String and that contains no argument fields.
|
---|
141 | */
|
---|
142 | public String get(String key)
|
---|
143 | {
|
---|
144 | return get(key, null);
|
---|
145 | }
|
---|
146 |
|
---|
147 | public static String processArgs(String initial, String args[])
|
---|
148 | {
|
---|
149 | // If the string contains arguments we have to insert them.
|
---|
150 | StringBuffer complete = new StringBuffer();
|
---|
151 | // While we still have initial string left.
|
---|
152 | while (initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1)
|
---|
153 | {
|
---|
154 | // Remove preamble
|
---|
155 | int opening = initial.indexOf('{');
|
---|
156 | int closing = initial.indexOf('}');
|
---|
157 | int comment_mark = initial.indexOf('-', opening); // May not exist
|
---|
158 | if (comment_mark > closing)
|
---|
159 | { // May also be detecting a later comment
|
---|
160 | comment_mark = -1;
|
---|
161 | }
|
---|
162 | complete.append(initial.substring(0, opening));
|
---|
163 |
|
---|
164 | // Parse arg_num
|
---|
165 | String arg_str = null;
|
---|
166 | if (comment_mark != -1)
|
---|
167 | {
|
---|
168 | arg_str = initial.substring(opening + 1, comment_mark);
|
---|
169 | }
|
---|
170 | else
|
---|
171 | {
|
---|
172 | arg_str = initial.substring(opening + 1, closing);
|
---|
173 | }
|
---|
174 | if (closing + 1 < initial.length())
|
---|
175 | {
|
---|
176 | initial = initial.substring(closing + 1);
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | initial = "";
|
---|
181 | }
|
---|
182 | int arg_num = Integer.parseInt(arg_str);
|
---|
183 | // Insert argument
|
---|
184 | if (args != null && 0 <= arg_num && arg_num < args.length)
|
---|
185 | {
|
---|
186 | complete.append(args[arg_num]);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | complete.append(initial);
|
---|
190 | return complete.toString();
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Used to retrieve a property value from the Locale specific
|
---|
195 | * ResourceBundle, based upon the key and arguments supplied. If the key
|
---|
196 | * cannot be found or if some other part of the call fails a default
|
---|
197 | * (English) error message is returned. <BR>
|
---|
198 | * Here the get recieves a second argument which is an array of Strings used
|
---|
199 | * to populate argument fields, denoted {<I>n</I>}, within the value String
|
---|
200 | * returned.
|
---|
201 | *
|
---|
202 | * @param key
|
---|
203 | * A <strong>String</strong> which is mapped to a initial String
|
---|
204 | * within the ResourceBundle.
|
---|
205 | * @param args
|
---|
206 | * A <strong>String[]</strong> used to populate argument fields
|
---|
207 | * within the complete String.
|
---|
208 | * @return A <strong>String</strong> which has been referenced by the key
|
---|
209 | * String and that either contains no argument fields, or has had
|
---|
210 | * the argument fields populated with argument Strings provided in
|
---|
211 | * the get call.
|
---|
212 | */
|
---|
213 | public String get(String key, String args[])
|
---|
214 | {
|
---|
215 |
|
---|
216 | if (this.raw == null)
|
---|
217 | {
|
---|
218 | return null;
|
---|
219 | }
|
---|
220 | try
|
---|
221 | {
|
---|
222 | // In older versions of Java, the resource-bundle/properties file was assumed to be "ISO-8859-1", and so
|
---|
223 | // we used to perform a string conversion here so we could support UTF-8
|
---|
224 | // With the introduction of UTF8Control, based on a StackOverflow posting, we now directly work
|
---|
225 | // with the resource as UTF-8, and so not conversion is needed
|
---|
226 | String initial = this.raw.getString(key);
|
---|
227 |
|
---|
228 | // if we haven't been given any args, don't bother looking for them
|
---|
229 | if (args == null)
|
---|
230 | {
|
---|
231 | return initial;
|
---|
232 | }
|
---|
233 |
|
---|
234 | return processArgs(initial,args);
|
---|
235 | }
|
---|
236 | catch (Exception e)
|
---|
237 | {
|
---|
238 | //logger.debug("Dictionary Error: couldn't find string for key:" + key +" in resource "+this.resource);
|
---|
239 | return null;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | public static String getTextString(String key, String lang, String[] args, String dictionary_name, Class class_obj, String stop_at_class, ClassLoader class_loader)
|
---|
245 | {
|
---|
246 |
|
---|
247 | if (dictionary_name == null && class_obj == null) {
|
---|
248 | // no dictionary or class was specified, just use the default
|
---|
249 | // dictionary
|
---|
250 | dictionary_name = core_servlet_dictionary_name;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (dictionary_name != null)
|
---|
254 | {
|
---|
255 | // just try the one specified dictionary
|
---|
256 | Dictionary dict = new Dictionary(dictionary_name, lang, class_loader);
|
---|
257 | String result = dict.get(key, args);
|
---|
258 | if (result == null)
|
---|
259 | { // not found
|
---|
260 |
|
---|
261 | logger.error("couldn't find key in dict, "+key);
|
---|
262 | //return "_" + key + "_"; // should we use this??
|
---|
263 | return null;
|
---|
264 | }
|
---|
265 | return result;
|
---|
266 | }
|
---|
267 |
|
---|
268 | // try the class names first before the default dictionary
|
---|
269 | String original_class_name = class_obj.getName();
|
---|
270 | original_class_name = original_class_name.substring(original_class_name.lastIndexOf('.') + 1);
|
---|
271 | Dictionary dict = new Dictionary(original_class_name, lang, class_loader);
|
---|
272 | String result = dict.get(key, args);
|
---|
273 | if (result != null)
|
---|
274 | {
|
---|
275 | return result;
|
---|
276 | }
|
---|
277 |
|
---|
278 | // we have to try super classes
|
---|
279 | String class_name;
|
---|
280 | Class c = class_obj.getSuperclass();
|
---|
281 | while (c != null)
|
---|
282 | {
|
---|
283 | class_name = c.getName();
|
---|
284 | class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
|
---|
285 | if (class_name.equals(stop_at_class))
|
---|
286 | {
|
---|
287 | // this is as far as we go
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | dict = new Dictionary(class_name, lang, class_loader);
|
---|
291 | result = dict.get(key, args);
|
---|
292 | if (result != null) {
|
---|
293 | return result;
|
---|
294 | }
|
---|
295 | c = c.getSuperclass();
|
---|
296 | }
|
---|
297 |
|
---|
298 | // if we got here, class names were no help, try the default dictionary
|
---|
299 | dict = new Dictionary(core_servlet_dictionary_name, lang, class_loader);
|
---|
300 |
|
---|
301 | // First we try the key qualified with the original class name
|
---|
302 | // (eg have TextQuery.name and LuceneSearch.TextQuery.name in the
|
---|
303 | // default properties file)
|
---|
304 | String full_key = original_class_name+"."+key;
|
---|
305 | result = dict.get(full_key, args);
|
---|
306 | if (result == null) {
|
---|
307 | result = dict.get(key, args);
|
---|
308 | }
|
---|
309 | return result;
|
---|
310 |
|
---|
311 | }
|
---|
312 |
|
---|
313 | }
|
---|