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