source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/Dictionary.java@ 33769

Last change on this file since 33769 was 33769, checked in by kjdon, 4 years ago

updated getTextSTring to contain all the functionality from ServiceRack's getTextString, so need to pass in class, and stop_at_class arguments

  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
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 */
19package org.greenstone.gsdl3.util;
20
21import java.util.ResourceBundle;
22import java.util.Locale;
23import java.util.Enumeration;
24
25import org.apache.log4j.*;
26
27public 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);
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);
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);
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);
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 String initial_raw = this.raw.getString(key);
223 // convert to unicode, copied from gatherer dictionary
224 String initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
225
226 // if we haven't been given any args, don't bother looking for them
227 if (args == null)
228 {
229 return initial;
230 }
231
232 return processArgs(initial,args);
233 }
234 catch (Exception e)
235 {
236 //logger.debug("Dictionary Error: couldn't find string for key:" + key +" in resource "+this.resource);
237 return null;
238 }
239 }
240
241
242 public static String getTextString(String key, String lang, String[] args, String dictionary_name, Class class_obj, String stop_at_class, ClassLoader class_loader)
243 {
244
245 if (dictionary_name == null && class_obj == null) {
246 // no dictionary or class was specified, just use the default
247 // dictionary
248 dictionary_name = core_servlet_dictionary_name;
249 }
250
251 if (dictionary_name != null)
252 {
253 // just try the one specified dictionary
254 Dictionary dict = new Dictionary(dictionary_name, lang, class_loader);
255 String result = dict.get(key, args);
256 if (result == null)
257 { // not found
258
259 logger.error("couldn't find key in dict, "+key);
260 //return "_" + key + "_"; // should we use this??
261 return null;
262 }
263 return result;
264 }
265
266 // try the class names first before the default dictionary
267 String original_class_name = class_obj.getName();
268 original_class_name = original_class_name.substring(original_class_name.lastIndexOf('.') + 1);
269 Dictionary dict = new Dictionary(original_class_name, lang, class_loader);
270 String result = dict.get(key, args);
271 if (result != null)
272 {
273 return result;
274 }
275
276 // we have to try super classes
277 String class_name;
278 Class c = class_obj.getSuperclass();
279 while (c != null)
280 {
281 class_name = c.getName();
282 class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
283 if (class_name.equals(stop_at_class))
284 {
285 // this is as far as we go
286 break;
287 }
288 dict = new Dictionary(class_name, lang, class_loader);
289 result = dict.get(key, args);
290 if (result != null) {
291 return result;
292 }
293 c = c.getSuperclass();
294 }
295
296 // if we got here, class names were no help, try the default dictionary
297 dict = new Dictionary(core_servlet_dictionary_name, lang, class_loader);
298
299 // First we try the key qualified with the original class name
300 // (eg have TextQuery.name and LuceneSearch.TextQuery.name in the
301 // default properties file)
302 String full_key = original_class_name+"."+key;
303 result = dict.get(full_key, args);
304 if (result == null) {
305 result = dict.get(key, args);
306 }
307 return result;
308
309 }
310
311}
Note: See TracBrowser for help on using the repository browser.