source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/Dictionary.java@ 3968

Last change on this file since 3968 was 3577, checked in by kjdon, 22 years ago

new dictionary class - wrapper around a resource bundle. used for display text strings

  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1package org.greenstone.gsdl3.util;
2
3import java.util.ResourceBundle;
4import java.util.Locale;
5import java.util.Enumeration;
6
7public class Dictionary {
8
9 /** The locale of this dictionary. */
10 protected Locale locale_ = null;
11
12 /** The resource that has been loaded */
13 protected String resource_ = null;
14
15 /** The ResourceBundle which contains the raw key-value mappings. Loaded from a file named "resource_locale.properties*/
16 private ResourceBundle raw_ = null;
17
18 /** Constructs the Dictionary class by first checking if a Locale has been set. If not the default locale is used, and a ResourceBundle is created. Finally a single important String, Language, is made available outside the class so a more read-able version of the Locale of this Dictionary is present.
19 * @param locale The <strong>Locale</strong> used to load the desired dictionary resource bundle.
20 */
21 public Dictionary(String resource, String lang) {
22 // Initialize.
23
24 locale_ = new Locale(lang);
25 resource_ = resource;
26 if (locale_ == null) {
27 locale_ = Locale.getDefault();
28 }
29 try {
30 raw_ = ResourceBundle.getBundle(resource_, locale_);
31 } catch (Exception e) {
32 System.err.println("Dictionary: couldn't locate a resource bundle. Error message: "+e.getMessage());
33 }
34 }
35
36 public Enumeration getKeys() {
37 if (raw_ != null) {
38 return raw_.getKeys();
39 }
40 return null;
41 }
42 /** Overloaded to call get with both a key and an empty argument array.
43 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
44 * @return A <strong>String</strong> which has been referenced by the key String and that contains no argument fields.
45 */
46 public String get(String key) {
47 return get(key, null);
48 }
49
50 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>
51 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned.
52 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
53 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
54 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields populated with argument Strings provided in the get call.
55 */
56 public String get(String key, String args[]) {
57 if (raw_ == null) {
58 return null;
59 }
60 try {
61 String initial = raw_.getString(key);
62 // If the string contains arguments we have to insert them.
63 String complete = "";
64 // While we still have initial string left.
65 while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
66 // Remove preamble
67 int opening = initial.indexOf('{');
68 int closing = initial.indexOf('}');
69 complete = complete + initial.substring(0, opening);
70 // Parse arg_num
71 String arg_str = initial.substring(opening + 1, closing);
72 int arg_num = Integer.parseInt(arg_str);
73 if(closing + 1 < initial.length()) {
74 initial = initial.substring(closing + 1);
75 }
76 else {
77 initial = "";
78 }
79 // Insert argument
80 if(args != null && 0 <= arg_num && arg_num < args.length) {
81 complete = complete + args[arg_num];
82 }
83 }
84 return complete + initial;
85 }
86 catch (Exception e) {
87 System.err.println("Exception: " + e.toString());
88 e.printStackTrace();
89 return null;
90 }
91 }
92}
93
Note: See TracBrowser for help on using the repository browser.