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

Last change on this file since 18405 was 18405, checked in by kjdon, 15 years ago

modifications to make this in line with gs2 version. Allows comments in arg indicators in dictionary text (eg {0-collection name}). The - needs to come immediately after the number

  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 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
25import org.apache.log4j.*;
26
27public 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 the string contains arguments we have to insert them.
126 StringBuffer complete = new StringBuffer();
127 // While we still have initial string left.
128 while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
129 // Remove preamble
130 int opening = initial.indexOf('{');
131 int closing = initial.indexOf('}');
132 int comment_mark = initial.indexOf('-', opening); // May not exist
133 if (comment_mark > closing) { // May also be detecting a later comment
134 comment_mark = -1;
135 }
136 complete.append(initial.substring(0, opening));
137
138 // Parse arg_num
139 String arg_str = null;
140 if (comment_mark != -1) {
141 arg_str = initial.substring(opening + 1, comment_mark);
142 } else {
143 arg_str = initial.substring(opening + 1, closing);
144 }
145 if(closing + 1 < initial.length()) {
146 initial = initial.substring(closing + 1);
147 }
148 else {
149 initial = "";
150 }
151 int arg_num = Integer.parseInt(arg_str);
152 // Insert argument
153 if(args != null && 0 <= arg_num && arg_num < args.length) {
154 complete.append(args[arg_num]);
155 }
156 }
157 complete.append(initial);
158 return complete.toString();
159 }
160 catch (Exception e) {
161 //logger.debug("Dictionary Error: couldn't find string for key:" + key +" in resource "+this.resource);
162 return null;
163 }
164 }
165}
166
Note: See TracBrowser for help on using the repository browser.