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

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 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
25public class Dictionary {
26
27 /** The locale of this dictionary. */
28 protected Locale locale = null;
29
30 /** The resource that has been loaded */
31 protected String resource = null;
32
33 /** The ResourceBundle which contains the raw key-value mappings. Loaded from a file named "resource_locale.properties*/
34 private ResourceBundle raw = null;
35
36 /** 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.
37 */
38 public Dictionary(String resource, String lang) {
39 // Initialize.
40
41 this.locale = new Locale(lang);
42 this.resource = resource;
43 if (this.locale == null) {
44 this.locale = Locale.getDefault();
45 }
46 try {
47 this.raw = ResourceBundle.getBundle(this.resource, this.locale);
48 } catch (Exception e) {
49 //System.err.println("Dictionary: couldn't locate a resource bundle for "+resource);
50 }
51 }
52
53 /** 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.
54 */
55 public Dictionary(String resource, String lang, ClassLoader loader) {
56 // Initialize.
57
58 this.locale = new Locale(lang);
59 this.resource = resource;
60 if (this.locale == null) {
61 this.locale = Locale.getDefault();
62 }
63 // try the specified class loader
64 try {
65 this.raw = ResourceBundle.getBundle(this.resource, this.locale, loader);
66 return;
67 } catch (Exception e) {};
68
69 try {
70 this.raw = ResourceBundle.getBundle(this.resource, this.locale);
71 } catch (Exception ex) {
72 //System.err.println("Dictionary: couldn't locate a resource bundle for "+resource);
73 }
74
75 }
76
77
78 public Enumeration getKeys() {
79 if (this.raw != null) {
80 return this.raw.getKeys();
81 }
82 return null;
83 }
84 /** Overloaded to call get with both a key and an empty argument array.
85 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
86 * @return A <strong>String</strong> which has been referenced by the key String and that contains no argument fields.
87 */
88 public String get(String key) {
89 return get(key, null);
90 }
91
92 /** 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>
93 * 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.
94 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
95 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
96 * @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.
97 */
98 public String get(String key, String args[]) {
99 if (this.raw == null) {
100 return null;
101 }
102 try {
103 String initial_raw = this.raw.getString(key);
104 // convert to unicode, copied from gatherer dictionary
105 String initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
106 // If the string contains arguments we have to insert them.
107 String complete = "";
108 // While we still have initial string left.
109 while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
110 // Remove preamble
111 int opening = initial.indexOf('{');
112 int closing = initial.indexOf('}');
113 complete = complete + initial.substring(0, opening);
114 // Parse arg_num
115 String arg_str = initial.substring(opening + 1, closing);
116 int arg_num = Integer.parseInt(arg_str);
117 if(closing + 1 < initial.length()) {
118 initial = initial.substring(closing + 1);
119 }
120 else {
121 initial = "";
122 }
123 // Insert argument
124 if(args != null && 0 <= arg_num && arg_num < args.length) {
125 complete = complete + args[arg_num];
126 }
127 }
128 return complete + initial;
129 }
130 catch (Exception e) {
131 //System.err.println("Dictionary Error: couldn't find string for key:" + key +" in resource "+this.resource);
132 return null;
133 }
134 }
135}
136
Note: See TracBrowser for help on using the repository browser.