source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/Dictionary.java@ 36361

Last change on this file since 36361 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 7.5 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 java.awt.ComponentOrientation;
40import javax.swing.plaf.FontUIResource;
41import java.util.HashMap;
42import java.util.Locale;
43import java.util.ResourceBundle;
44
45
46/** Extends the ResourceBundle class to allow for the automatic insertion of arguments.<BR>
47 * <BR>
48 * Property files usable by this class have the Backus-Naur form: <BR>
49 * <BR>
50 * FileLine ::= Comment | Mapping <BR>
51 * Comment ::= '#' SString <BR>
52 * Mapping ::= NZString ':' SString ( Argument SString )* <BR>
53 * NZString ::= ( Char | Int ) SString <BR>
54 * Argument ::= '{' Int '}' <BR>
55 * SString ::= String . ['"','#',...] -> ['\"','\#',...] <BR>
56 * <BR>
57 * 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>
58 * <BR>
59 * dictionary_<I>locale</I>.properties<BR>
60 * <BR>
61 * 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:
62 * <BR>
63 * dictionary_en_NZ.properties<BR>
64 * @author John Thompson, Greenstone Digital Library, University of Waikato
65 * @version 2.3
66 */
67public class Dictionary
68 extends HashMap {
69
70 /** The font used when displaying various html text. */
71 static private FontUIResource font = null;
72 /** A reference to remind us of the current locale. */
73 static private Locale locale = null;
74 /** The ResourceBundle which contains the raw key-value mappings.
75 Loaded from a file named "dictionary<I>locale</I>.properties. */
76 static private ResourceBundle dictionary = null;
77 /** language orientation property */
78 static private ComponentOrientation orientation = null;
79
80 public Dictionary(Locale locale, FontUIResource font)
81 {
82 super();
83
84 // Initialize
85 this.font = font;
86 this.locale = ((locale == null) ? Locale.getDefault() : locale);
87 this.dictionary = ResourceBundle.getBundle("dictionary", this.locale);
88 if (this.get("Component.Orientation").equals("RTL")){
89 this.orientation = ComponentOrientation.RIGHT_TO_LEFT;
90 } else {
91 this.orientation = ComponentOrientation.LEFT_TO_RIGHT;
92 }
93
94 }
95
96 /** Retrieve the two letter code of the current language we are using, according to the stored locale.
97 * @return A <strong>String</strong> containing the two letter ISO639 language code.
98 */
99 static public String getLanguage()
100 {
101 return locale.getLanguage();
102 }
103 /** returns the Components Orientation for use with right to left languages
104 * @return A <strong>ComponentOrientation</strong> with the current
105 dictionary's language orientation.
106 * by Amin Hejazi
107 */
108 static public ComponentOrientation getOrientation()
109 {
110 return orientation;
111 }
112
113 static public String get(String key)
114 {
115 return get(key, (String[]) null);
116 }
117
118 static public String get(String key, String arg)
119 {
120 String[] args = new String[1];
121 args[0] = arg;
122 return get(key, args);
123 }
124
125
126 /** 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>
127 * 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.
128 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
129 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
130 * @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.
131 */
132 static public String get(String key, String[] args)
133 {
134 try {
135 String initial_raw = dictionary.getString(key).trim();
136
137 // Convert into Unicode
138 String initial = "";
139 try {
140 // This "ISO-8859-1" looks out of place, but it is very important.
141 // It is essential to call getBytes with an 8-bit encoding, otherwise
142 // Java kindly deems some characters "undisplayable", and replaces
143 // them with question marks. This is NOT good.
144 initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
145 }
146 catch (Exception exception) {
147 DebugStream.printStackTrace(exception);
148 return initial_raw;
149 }
150
151 // Remove any comments from the string
152 if (initial.indexOf("#") != -1) {
153 initial = initial.substring(0, initial.indexOf("#"));
154 initial = initial.trim();
155 }
156
157 // If the string contains arguments we have to insert them.
158 StringBuffer complete = new StringBuffer();
159
160 // While we still have initial string left.
161 while (initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
162 // Remove preamble
163 int opening = initial.indexOf('{');
164 int closing = initial.indexOf('}');
165 int comment_mark = initial.indexOf('-', opening); // May not exist
166 if (comment_mark > closing) { // May also be detecting a later comment
167 comment_mark = -1;
168 }
169 complete.append(initial.substring(0, opening));
170
171 // Parse arg_num
172 String arg_str = null;
173 if (comment_mark != -1) {
174 arg_str = initial.substring(opening + 1, comment_mark);
175 }
176 else {
177 arg_str = initial.substring(opening + 1, closing);
178 }
179
180 if (closing + 1 < initial.length()) {
181 initial = initial.substring(closing + 1);
182 }
183 else {
184 initial = "";
185 }
186
187 // Insert argument
188 if (arg_str.equals("FONT")) {
189 complete.append((font != null ? font.getFontName() : "Arial"));
190 }
191 else {
192 int arg_num = Integer.parseInt(arg_str);
193 if (args != null && 0 <= arg_num && arg_num < args.length) {
194 complete.append(args[arg_num]);
195 }
196 }
197 }
198 complete.append(initial);
199
200 return complete.toString();
201 }
202 catch (Exception exception) {
203 System.err.println("Missing value for key: " + key);
204 // DebugStream.printStackTrace(e);
205 return key;
206 }
207 }
208
209}
210
Note: See TracBrowser for help on using the repository browser.