source: trunk/gli/src/org/greenstone/gatherer/Dictionary.java@ 13596

Last change on this file since 13596 was 12295, checked in by kjdon, 18 years ago

changed string concatenation to use a StringBuffer

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer;
38
39import javax.swing.plaf.FontUIResource;
40import java.util.HashMap;
41import java.util.Locale;
42import java.util.ResourceBundle;
43
44
45/** Extends the ResourceBundle class to allow for the automatic insertion of arguments.<BR>
46 * <BR>
47 * Property files usable by this class have the Backus-Naur form: <BR>
48 * <BR>
49 * FileLine ::= Comment | Mapping <BR>
50 * Comment ::= '#' SString <BR>
51 * Mapping ::= NZString ':' SString ( Argument SString )* <BR>
52 * NZString ::= ( Char | Int ) SString <BR>
53 * Argument ::= '{' Int '}' <BR>
54 * SString ::= String . ['"','#',...] -> ['\"','\#',...] <BR>
55 * <BR>
56 * In order to add a new dictionary Locale, simply copy the existing dictionary.properties files, replace the values (Strings after the ':') with the new language specific ones being careful to maintain formatting and Gatherer placed arguments, then save the new dictionary as: <br>
57 * <BR>
58 * dictionary_<I>locale</I>.properties<BR>
59 * <BR>
60 * where locale is made of two two-letter codes seperated by an underscore. The first code is in lower-case and defines the language. The second is in upper-case and defines the country. For example the default dictionary could also correctly be called:
61 * <BR>
62 * dictionary_en_NZ.properties<BR>
63 * @author John Thompson, Greenstone Digital Library, University of Waikato
64 * @version 2.3
65 */
66public class Dictionary
67 extends HashMap {
68
69 /** The font used when displaying various html text. */
70 static private FontUIResource font = null;
71 /** A reference to remind us of the current locale. */
72 static private Locale locale = null;
73 /** The ResourceBundle which contains the raw key-value mappings.
74 Loaded from a file named "dictionary<I>locale</I>.properties. */
75 static private ResourceBundle dictionary = null;
76
77
78 public Dictionary(Locale locale, FontUIResource font)
79 {
80 super();
81
82 // Initialize
83 this.font = font;
84 this.locale = ((locale == null) ? Locale.getDefault() : locale);
85 this.dictionary = ResourceBundle.getBundle("dictionary", this.locale);
86 }
87
88
89 /** Retrieve the two letter code of the current language we are using, according to the stored locale.
90 * @return A <strong>String</strong> containing the two letter ISO639 language code.
91 */
92 static public String getLanguage()
93 {
94 return locale.getLanguage();
95 }
96
97
98 static public String get(String key)
99 {
100 return get(key, (String[]) null);
101 }
102
103
104 static public String get(String key, String arg)
105 {
106 String[] args = new String[1];
107 args[0] = arg;
108 return get(key, args);
109 }
110
111
112 /** 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>
113 * 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.
114 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
115 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
116 * @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 automatiically populated with formatting Strings of with argument String provided in the get call.
117 */
118 static public String get(String key, String[] args)
119 {
120 try {
121 String initial_raw = dictionary.getString(key);
122
123 // Convert into Unicode
124 String initial = "";
125 try {
126 // This "ISO-8859-1" looks out of place, but it is very important.
127 // It is essential to call getBytes with an 8-bit encoding, otherwise
128 // Java kindly deems some characters "undisplayable", and replaces
129 // them with question marks. This is NOT good.
130 initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
131 }
132 catch (Exception exception) {
133 DebugStream.printStackTrace(exception);
134 return initial_raw;
135 }
136
137 // Remove any comments from the string
138 if (initial.indexOf("#") != -1) {
139 initial = initial.substring(0, initial.indexOf("#"));
140 }
141
142 // If the string contains arguments we have to insert them.
143 StringBuffer complete = new StringBuffer();
144
145 // While we still have initial string left.
146 while (initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
147 // Remove preamble
148 int opening = initial.indexOf('{');
149 int closing = initial.indexOf('}');
150 int comment_mark = initial.indexOf('-', opening); // May not exist
151 if (comment_mark > closing) { // May also be detecting a later comment
152 comment_mark = -1;
153 }
154 complete.append(initial.substring(0, opening));
155
156 // Parse arg_num
157 String arg_str = null;
158 if (comment_mark != -1) {
159 arg_str = initial.substring(opening + 1, comment_mark);
160 }
161 else {
162 arg_str = initial.substring(opening + 1, closing);
163 }
164
165 if (closing + 1 < initial.length()) {
166 initial = initial.substring(closing + 1);
167 }
168 else {
169 initial = "";
170 }
171
172 // Insert argument
173 if (arg_str.equals("FONT")) {
174 complete.append((font != null ? font.getFontName() : "Arial"));
175 }
176 else {
177 int arg_num = Integer.parseInt(arg_str);
178 if (args != null && 0 <= arg_num && arg_num < args.length) {
179 complete.append(args[arg_num]);
180 }
181 }
182 }
183 complete.append(initial);
184
185 return complete.toString();
186 }
187 catch (Exception exception) {
188 System.err.println("Missing value for key: " + key);
189 // DebugStream.printStackTrace(e);
190 return key;
191 }
192 }
193
194}
195
Note: See TracBrowser for help on using the repository browser.