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

Last change on this file since 31127 was 30477, checked in by davidb, 8 years ago

Changes in the Java code to support the new approach taken to client-side XSLT (using Saxon-CE JS library in the browser -- see next commit). Also some better error reporting when processing XSLT files

  • Property svn:keywords set to Author Date Id Revision
File size: 7.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 locale of this dictionary. */
33 protected Locale locale = null;
34
35 /** The resource that has been loaded */
36 protected String resource = null;
37
38 /**
39 * The ResourceBundle which contains the raw key-value mappings. Loaded from
40 * a file named "resource_locale.properties
41 */
42 private ResourceBundle raw = null;
43
44 /**
45 * Constructs the Dictionary class by first creating a locale from the
46 * specified language, (or using the default locale if this didn't work),
47 * then loading a resource bundle based on this locale.
48 */
49 public Dictionary(String resource, String lang)
50 {
51 // Initialize.
52
53 this.locale = new Locale(lang);
54 this.resource = resource;
55 if (this.locale == null)
56 {
57 this.locale = Locale.getDefault();
58 }
59 try
60 {
61 this.raw = ResourceBundle.getBundle(this.resource, this.locale);
62 }
63 catch (Exception e)
64 {
65 //logger.debug("Dictionary: couldn't locate a resource bundle for "+resource);
66 }
67 }
68
69 public Dictionary(String resource, Locale locale)
70 {
71 this.locale = locale;
72 this.resource = resource;
73 try
74 {
75 this.raw = ResourceBundle.getBundle(this.resource, this.locale);
76 }
77 catch (Exception e)
78 {
79 //logger.debug("Dictionary: couldn't locate a resource bundle for "+resource);
80 }
81 }
82
83 /**
84 * Constructs the Dictionary class by first creating a locale from the
85 * specified language, (or using the default locale if this didn't work),
86 * then loading a resource bundle based on this locale. A classloader is
87 * specified which can be used to find the resource.
88 */
89 public Dictionary(String resource, String lang, ClassLoader loader)
90 {
91 // Initialize.
92 this.locale = new Locale(lang);
93 this.resource = resource;
94 if (this.locale == null)
95 {
96 this.locale = Locale.getDefault();
97 }
98 // try the specified class loader
99 try
100 {
101 this.raw = ResourceBundle.getBundle(this.resource, this.locale, loader);
102 return;
103 }
104 catch (Exception e)
105 {
106 }
107 ;
108
109 try
110 {
111 this.raw = ResourceBundle.getBundle(this.resource, this.locale);
112 }
113 catch (Exception ex)
114 {
115 //logger.debug("Dictionary: couldn't locate a resource bundle for "+resource);
116 }
117 }
118
119 public Enumeration getKeys()
120 {
121 if (this.raw != null)
122 {
123 return this.raw.getKeys();
124 }
125 return null;
126 }
127
128 /**
129 * Overloaded to call get with both a key and an empty argument array.
130 *
131 * @param key
132 * A <strong>String</strong> which is mapped to a initial String
133 * within the ResourceBundle.
134 * @return A <strong>String</strong> which has been referenced by the key
135 * String and that contains no argument fields.
136 */
137 public String get(String key)
138 {
139 return get(key, null);
140 }
141
142 public static String processArgs(String initial, String args[])
143 {
144 // If the string contains arguments we have to insert them.
145 StringBuffer complete = new StringBuffer();
146 // While we still have initial string left.
147 while (initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1)
148 {
149 // Remove preamble
150 int opening = initial.indexOf('{');
151 int closing = initial.indexOf('}');
152 int comment_mark = initial.indexOf('-', opening); // May not exist
153 if (comment_mark > closing)
154 { // May also be detecting a later comment
155 comment_mark = -1;
156 }
157 complete.append(initial.substring(0, opening));
158
159 // Parse arg_num
160 String arg_str = null;
161 if (comment_mark != -1)
162 {
163 arg_str = initial.substring(opening + 1, comment_mark);
164 }
165 else
166 {
167 arg_str = initial.substring(opening + 1, closing);
168 }
169 if (closing + 1 < initial.length())
170 {
171 initial = initial.substring(closing + 1);
172 }
173 else
174 {
175 initial = "";
176 }
177 int arg_num = Integer.parseInt(arg_str);
178 // Insert argument
179 if (args != null && 0 <= arg_num && arg_num < args.length)
180 {
181 complete.append(args[arg_num]);
182 }
183 }
184 complete.append(initial);
185 return complete.toString();
186 }
187
188 /**
189 * Used to retrieve a property value from the Locale specific
190 * ResourceBundle, based upon the key and arguments supplied. If the key
191 * cannot be found or if some other part of the call fails a default
192 * (English) error message is returned. <BR>
193 * Here the get recieves a second argument which is an array of Strings used
194 * to populate argument fields, denoted {<I>n</I>}, within the value String
195 * returned.
196 *
197 * @param key
198 * A <strong>String</strong> which is mapped to a initial String
199 * within the ResourceBundle.
200 * @param args
201 * A <strong>String[]</strong> used to populate argument fields
202 * within the complete String.
203 * @return A <strong>String</strong> which has been referenced by the key
204 * String and that either contains no argument fields, or has had
205 * the argument fields populated with argument Strings provided in
206 * the get call.
207 */
208 public String get(String key, String args[])
209 {
210 // The following 'argsStr' doesn't appear to be used in rest of method, so
211 // commenting out
212 /*
213 String argsStr = "";
214 if (args != null)
215 {
216 for (String arg : args)
217 {
218 argsStr += arg + " ";
219 }
220 }
221 */
222
223 if (this.raw == null)
224 {
225 return null;
226 }
227 try
228 {
229 String initial_raw = this.raw.getString(key);
230 // convert to unicode, copied from gatherer dictionary
231 String initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
232
233 // if we haven't been given any args, don't bother looking for them
234 if (args == null)
235 {
236 return initial;
237 }
238
239 return processArgs(initial,args);
240 /*
241 // If the string contains arguments we have to insert them.
242 StringBuffer complete = new StringBuffer();
243 // While we still have initial string left.
244 while (initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1)
245 {
246 // Remove preamble
247 int opening = initial.indexOf('{');
248 int closing = initial.indexOf('}');
249 int comment_mark = initial.indexOf('-', opening); // May not exist
250 if (comment_mark > closing)
251 { // May also be detecting a later comment
252 comment_mark = -1;
253 }
254 complete.append(initial.substring(0, opening));
255
256 // Parse arg_num
257 String arg_str = null;
258 if (comment_mark != -1)
259 {
260 arg_str = initial.substring(opening + 1, comment_mark);
261 }
262 else
263 {
264 arg_str = initial.substring(opening + 1, closing);
265 }
266 if (closing + 1 < initial.length())
267 {
268 initial = initial.substring(closing + 1);
269 }
270 else
271 {
272 initial = "";
273 }
274 int arg_num = Integer.parseInt(arg_str);
275 // Insert argument
276 if (args != null && 0 <= arg_num && arg_num < args.length)
277 {
278 complete.append(args[arg_num]);
279 }
280 }
281 complete.append(initial);
282 return complete.toString();
283 */
284 }
285 catch (Exception e)
286 {
287 //logger.debug("Dictionary Error: couldn't find string for key:" + key +" in resource "+this.resource);
288 return null;
289 }
290 }
291}
Note: See TracBrowser for help on using the repository browser.