1 |
|
---|
2 | import java.util.ResourceBundle;
|
---|
3 | import java.util.Locale;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.net.URL;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.Reader;
|
---|
10 | import java.io.FileReader;
|
---|
11 | import java.io.InputStreamReader;
|
---|
12 | import java.io.BufferedReader;
|
---|
13 | //import org.apache.xerces.parsers.DOMParser;
|
---|
14 | import org.xml.sax.InputSource;
|
---|
15 | //import org.xml.sax.XMLReader;
|
---|
16 | //import org.xml.sax.XMLReaderFactory;
|
---|
17 | import javax.xml.parsers.DocumentBuilder;
|
---|
18 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
19 | import org.w3c.dom.Document;
|
---|
20 | import org.w3c.dom.Element;
|
---|
21 | import org.w3c.dom.Node;
|
---|
22 | import org.w3c.dom.NodeList;
|
---|
23 |
|
---|
24 | public class Dictionary {
|
---|
25 |
|
---|
26 | private ResourceBundle glidict = null;
|
---|
27 | private ResourceBundle perldict = null;
|
---|
28 | private MacroFile core_macros = null;
|
---|
29 | private MacroFile aux_macros = null;
|
---|
30 | private HashMap metadata = null;
|
---|
31 | private Locale locale = null;
|
---|
32 | private String lang = null;
|
---|
33 | static final private int GLIDICT = 0;
|
---|
34 | static final private int GLIHELP = 1;
|
---|
35 | static final private int COREDM = 2;
|
---|
36 | static final private int AUXDM = 3;
|
---|
37 | static final private int TUTORIALS = 4;
|
---|
38 | static final private int PERLMODULES = 5;
|
---|
39 | static final private int METADATA = 6;
|
---|
40 |
|
---|
41 | protected String gsdl_home = null;
|
---|
42 |
|
---|
43 | private ClassLoader classloader = null;
|
---|
44 |
|
---|
45 |
|
---|
46 | public Dictionary(String lang) {
|
---|
47 | this.lang = lang;
|
---|
48 | this.locale = new Locale(lang);
|
---|
49 | if (this.locale==null) {
|
---|
50 | this.locale = Locale.getDefault();
|
---|
51 | }
|
---|
52 | gsdl_home = System.getProperty("GSDLHOME");
|
---|
53 | if (gsdl_home == null) {
|
---|
54 | System.err.println("Error: GSDLHOME is null!!");
|
---|
55 | System.exit(1);
|
---|
56 | }
|
---|
57 |
|
---|
58 | this.classloader = new MyClassLoader(this.getClass().getClassLoader());
|
---|
59 | }
|
---|
60 |
|
---|
61 | public String get(String key) {
|
---|
62 | return get(key, null);
|
---|
63 | }
|
---|
64 | public String get(String key, String args) {
|
---|
65 | int pos = key.indexOf("::");
|
---|
66 | if (pos==-1) {
|
---|
67 | System.err.println("invalid key: "+key);
|
---|
68 | return "";
|
---|
69 | }
|
---|
70 | String package_name = key.substring(0, pos);
|
---|
71 | String key_name = key.substring(pos+2);
|
---|
72 | //System.err.println("pack = "+package_name+", key="+key_name);
|
---|
73 |
|
---|
74 | if (package_name.equals("glidict")) {
|
---|
75 | loadDictionary(GLIDICT);
|
---|
76 | return getDictionaryText(glidict, key_name, args);
|
---|
77 | }
|
---|
78 | if (package_name.equals("coredm")) {
|
---|
79 | loadDictionary(COREDM);
|
---|
80 | return getMacroText(core_macros, key_name);
|
---|
81 | }
|
---|
82 | if (package_name.equals("perlmodules")) {
|
---|
83 | loadDictionary(PERLMODULES);
|
---|
84 | return getDictionaryText(perldict, key_name, args);
|
---|
85 | }
|
---|
86 | if (package_name.equals("auxdm")) {
|
---|
87 | loadDictionary(AUXDM);
|
---|
88 | return getMacroText(aux_macros, key_name);
|
---|
89 | }
|
---|
90 | if (package_name.equals("glihelp")) {
|
---|
91 | return "";
|
---|
92 | }
|
---|
93 | if (package_name.equals("tutorials")) {
|
---|
94 | return "";
|
---|
95 | }
|
---|
96 | if (package_name.equals("metadata")) {
|
---|
97 | pos = key_name.indexOf(".");
|
---|
98 | String namespace = key_name.substring(0,pos);
|
---|
99 | String element = key_name.substring(pos+1);
|
---|
100 | if (loadMetadata(namespace)) {
|
---|
101 | return getMetadataName(namespace, element);
|
---|
102 | }
|
---|
103 | return key_name;
|
---|
104 | }
|
---|
105 | System.err.println("invalid package name "+package_name);
|
---|
106 | return "";
|
---|
107 | }
|
---|
108 |
|
---|
109 | public String getDictionaryText(ResourceBundle dictionary, String key, String args) {
|
---|
110 |
|
---|
111 | try {
|
---|
112 | String initial_raw = dictionary.getString(key);
|
---|
113 | // convert to unicode, copied from gatherer dictionary
|
---|
114 | String initial = new String(initial_raw.getBytes("ISO-8859-1"), "UTF-8");
|
---|
115 | if (args == null || args.equals("")) {
|
---|
116 | return initial;
|
---|
117 | }
|
---|
118 | String [] args_array = args.split(";");
|
---|
119 | // If the string contains arguments we have to insert them.
|
---|
120 | String complete = "";
|
---|
121 | // While we still have initial string left.
|
---|
122 | while(initial.length() > 0 && initial.indexOf('{') != -1 && initial.indexOf('}') != -1) {
|
---|
123 | // Remove preamble
|
---|
124 | int opening = initial.indexOf('{');
|
---|
125 | int closing = initial.indexOf('}');
|
---|
126 | complete = complete + initial.substring(0, opening);
|
---|
127 | // Parse arg_num
|
---|
128 | String arg_str = initial.substring(opening + 1, closing);
|
---|
129 | int arg_num = Integer.parseInt(arg_str);
|
---|
130 | if(closing + 1 < initial.length()) {
|
---|
131 | initial = initial.substring(closing + 1);
|
---|
132 | }
|
---|
133 | else {
|
---|
134 | initial = "";
|
---|
135 | }
|
---|
136 | // Insert argument
|
---|
137 | if(0 <= arg_num && arg_num < args_array.length) {
|
---|
138 | complete = complete + args_array[arg_num];
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return complete + initial;
|
---|
142 |
|
---|
143 | //this.raw = ResourceBundle.getBundle(this.resource, this.locale);
|
---|
144 | } catch (Exception e) {
|
---|
145 | //System.err.println("Dictionary: couldn't locate a resource bundle for "+resource);
|
---|
146 | }
|
---|
147 | System.err.println("no value found for key "+key+" in dictionary "+dictionary.toString());
|
---|
148 | return "";
|
---|
149 | }
|
---|
150 |
|
---|
151 | private String getMacroText(MacroFile macros, String key) {
|
---|
152 | return macros.get(key);
|
---|
153 | }
|
---|
154 |
|
---|
155 | private String getMetadataName(String namespace, String element_name) {
|
---|
156 | Element meta_set = (Element)metadata.get(namespace);
|
---|
157 | String default_value = namespace+"."+element_name;
|
---|
158 | if (meta_set == null) {
|
---|
159 | return default_value;
|
---|
160 | }
|
---|
161 | Element this_elem = getNamedChild(meta_set, "Element", "name", element_name);
|
---|
162 | if (this_elem == null) {
|
---|
163 | return default_value;
|
---|
164 | }
|
---|
165 | Element this_lang = getNamedChild(this_elem, "Language", "code", this.lang);
|
---|
166 | if (this_lang == null) {
|
---|
167 | this_lang = this_elem; // sometimes have attributes as children of element
|
---|
168 | }
|
---|
169 |
|
---|
170 | // need to modify this if we get multiple languages at the same level
|
---|
171 | Element label_att = getNamedChild(this_lang, "Attribute", "name", "label");
|
---|
172 | if (label_att == null) {
|
---|
173 | return default_value;
|
---|
174 | }
|
---|
175 | return namespace+"."+getTextValue(label_att);
|
---|
176 | }
|
---|
177 |
|
---|
178 | private Element getNamedChild(Element parent, String child_element, String att_name, String att_value) {
|
---|
179 | NodeList elements = parent.getElementsByTagName(child_element);
|
---|
180 | for (int i=0; i<elements.getLength(); i++) {
|
---|
181 | Element e = (Element)elements.item(i);
|
---|
182 | if (e.getAttribute(att_name).equals(att_value)) {
|
---|
183 | return e;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return null;
|
---|
187 | }
|
---|
188 |
|
---|
189 | private String getTextValue(Element elem) {
|
---|
190 | elem.normalize();
|
---|
191 | Node n = elem.getFirstChild();
|
---|
192 | while (n!=null && n.getNodeType() !=Node.TEXT_NODE) {
|
---|
193 | n=n.getNextSibling();
|
---|
194 | }
|
---|
195 | return n.getNodeValue();
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 | private boolean loadDictionary(int type) {
|
---|
201 | switch (type) {
|
---|
202 | case GLIDICT:
|
---|
203 | if (this.glidict == null) {
|
---|
204 | glidict = ResourceBundle.getBundle("dictionary", this.locale, this.classloader);
|
---|
205 |
|
---|
206 | }
|
---|
207 | break;
|
---|
208 | case PERLMODULES:
|
---|
209 | if (this.perldict == null) {
|
---|
210 | perldict = ResourceBundle.getBundle("strings", this.locale, this.classloader);
|
---|
211 | }
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case COREDM:
|
---|
215 | if (this.core_macros == null) {
|
---|
216 | core_macros = new MacroFile(MacroFile.CORE);
|
---|
217 | }
|
---|
218 | break;
|
---|
219 |
|
---|
220 | case AUXDM:
|
---|
221 | if (this.aux_macros == null) {
|
---|
222 | aux_macros = new MacroFile(MacroFile.AUX);
|
---|
223 | }
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | return true;
|
---|
227 | }
|
---|
228 |
|
---|
229 | private boolean loadMetadata(String namespace) {
|
---|
230 | if (metadata == null) {
|
---|
231 | metadata = new HashMap();
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (metadata.containsKey(namespace)) {
|
---|
235 | // already loaded
|
---|
236 | return true;
|
---|
237 | }
|
---|
238 | // find the metadata file
|
---|
239 | String file_name = namespace+".mds";
|
---|
240 | if (namespace.equals("dc")) {
|
---|
241 | // what about qualified???
|
---|
242 | file_name = "dublin.mds";
|
---|
243 | }
|
---|
244 |
|
---|
245 | file_name = gsdl_home + File.separator + "gli" + File.separator + "metadata"+ File.separator + file_name;
|
---|
246 | //System.err.println("metadata filename = "+file_name);
|
---|
247 | File metadata_file = new File(file_name);
|
---|
248 | if (!metadata_file.exists()) {
|
---|
249 | System.err.println("metadata file "+file_name+" does not exist!");
|
---|
250 | return false;
|
---|
251 | }
|
---|
252 | // parse the file
|
---|
253 | Element elem = parseXMLFile(metadata_file);
|
---|
254 | if (elem != null) {
|
---|
255 | metadata.put(namespace, elem);
|
---|
256 | return true;
|
---|
257 | }
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 |
|
---|
261 | private Element parseXMLFile(File file) {
|
---|
262 |
|
---|
263 | try {
|
---|
264 | //DOMParser parser = new DOMParser();
|
---|
265 | //XMLReader parser = XMLReaderFactory.createXMLReader();
|
---|
266 | DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
---|
267 | Document doc = builder.parse(file);
|
---|
268 | //FileReader reader = new FileReader(file);
|
---|
269 | //InputSource xml_source = new InputSource(reader);
|
---|
270 |
|
---|
271 | //parser.parse(xml_source);
|
---|
272 | return doc.getDocumentElement();
|
---|
273 | } catch (Exception e) {
|
---|
274 | System.err.println("XML Parse Error: "+e.getMessage());
|
---|
275 | return null;
|
---|
276 | }
|
---|
277 |
|
---|
278 | }
|
---|
279 | private class MyClassLoader
|
---|
280 | extends ClassLoader {
|
---|
281 |
|
---|
282 | private String gliclassesdir = null;
|
---|
283 | private String perldir = null;
|
---|
284 |
|
---|
285 | public MyClassLoader(ClassLoader parent) {
|
---|
286 |
|
---|
287 | super(parent);
|
---|
288 | gliclassesdir = gsdl_home+File.separator+"gli"+File.separator+"classes";
|
---|
289 | perldir = gsdl_home+File.separator+"perllib";
|
---|
290 |
|
---|
291 | }
|
---|
292 |
|
---|
293 | public URL findResource(String name) {
|
---|
294 | try {
|
---|
295 | File resource_path = new File(this.gliclassesdir, name);
|
---|
296 | if (resource_path.exists()) {
|
---|
297 | return new URL("file://"+resource_path.getAbsolutePath());
|
---|
298 | }
|
---|
299 | resource_path = new File(this.perldir, name);
|
---|
300 | if (resource_path.exists()) {
|
---|
301 | return new URL("file://"+resource_path.getAbsolutePath());
|
---|
302 | }
|
---|
303 | } catch (Exception e) {};
|
---|
304 |
|
---|
305 |
|
---|
306 | return super.findResource(name);
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | }
|
---|
311 |
|
---|
312 | private class MacroFile {
|
---|
313 | public static final int CORE = 0;
|
---|
314 | public static final int AUX = 1;
|
---|
315 |
|
---|
316 | private File macro_file = null;
|
---|
317 | private HashMap macros = null;
|
---|
318 |
|
---|
319 | public MacroFile(int type) {
|
---|
320 | String filename = gsdl_home+File.separator+"macros"+File.separator;
|
---|
321 | if (lang.equals("en")) {
|
---|
322 | filename += "english";
|
---|
323 | } else if (lang.equals("fr")) {
|
---|
324 | filename += "french";
|
---|
325 | } else if (lang.equals("es")) {
|
---|
326 | filename += "spanish";
|
---|
327 | } else if (lang.equals("ru")) {
|
---|
328 | filename += "russian";
|
---|
329 | } else {
|
---|
330 | System.err.println("Couldn't work out macro file name for lang "+lang);
|
---|
331 | return;
|
---|
332 | }
|
---|
333 | if(type == CORE) {
|
---|
334 | filename += ".dm";
|
---|
335 | } else if (type == AUX) {
|
---|
336 | filename += "2.dm";
|
---|
337 | } else {
|
---|
338 | System.err.println("Unknown Macro file type: "+type+"!");
|
---|
339 | }
|
---|
340 |
|
---|
341 | macro_file = new File(filename);
|
---|
342 | if (!macro_file.exists()) {
|
---|
343 | System.err.println("Macro file "+filename+" doesn't exist!");
|
---|
344 | return;
|
---|
345 | }
|
---|
346 | macros = new HashMap();
|
---|
347 | loadMacroFile();
|
---|
348 | }
|
---|
349 |
|
---|
350 | public String get(String key) {
|
---|
351 | if (!key.startsWith("_") || !key.endsWith("_")) {
|
---|
352 | System.err.println("Error in macro key "+key);
|
---|
353 | return "";
|
---|
354 | }
|
---|
355 | String value = (String)macros.get(key.substring(1, key.length()-1));
|
---|
356 | //System.err.println("proper_key"+key.substring(1, key.length()-1));
|
---|
357 | if (value == null) {
|
---|
358 | System.err.println("No value found for key "+key+" in macro file "+macro_file.getName());
|
---|
359 | return "";
|
---|
360 | }
|
---|
361 | return value;
|
---|
362 | }
|
---|
363 | private boolean loadMacroFile() {
|
---|
364 | String current_package = "";
|
---|
365 | try {
|
---|
366 | InputStream istream = new FileInputStream(macro_file);
|
---|
367 | Reader in_reader = new InputStreamReader(istream, "UTF-8");
|
---|
368 | BufferedReader in = new BufferedReader(in_reader);
|
---|
369 | String macro_str = null;
|
---|
370 | while((macro_str = in.readLine()) != null) {
|
---|
371 | if (macro_str.startsWith("#")) {
|
---|
372 | // a comment
|
---|
373 | continue;
|
---|
374 | }
|
---|
375 | if (macro_str.startsWith("package")) {
|
---|
376 | macro_str = macro_str.substring(8);
|
---|
377 | macro_str = macro_str.trim();
|
---|
378 | if (macro_str.indexOf("#")!=-1) {
|
---|
379 | macro_str = macro_str.substring(0, macro_str.indexOf("#"));
|
---|
380 | macro_str = macro_str.trim();
|
---|
381 | }
|
---|
382 | current_package = macro_str;
|
---|
383 | //System.err.println("current package = "+current_package);
|
---|
384 | continue;
|
---|
385 | }
|
---|
386 | if (macro_str.startsWith("_")) {
|
---|
387 | if (macro_str.indexOf("#")!=-1) {
|
---|
388 | //strip off any comments - hopefully there are no # in the actual text
|
---|
389 | macro_str = macro_str.substring(0, macro_str.indexOf("#"));
|
---|
390 | }
|
---|
391 | macro_str = macro_str.trim();
|
---|
392 | String macro_name = macro_str.substring(1, macro_str.indexOf("_", 1));
|
---|
393 | //System.err.println("macro name = "+macro_name);
|
---|
394 | // find the {
|
---|
395 | int start_bracket = macro_str.indexOf("{");
|
---|
396 | int end_bracket = 0;
|
---|
397 | String line = null;
|
---|
398 | while(!macro_str.endsWith("}") && (line = in.readLine()) != null) {
|
---|
399 | macro_str += line;
|
---|
400 | macro_str = macro_str.trim();
|
---|
401 | }
|
---|
402 | if (!macro_str.endsWith("}")) {
|
---|
403 | System.err.println("can't find the end of macro "+macro_name+", value="+macro_str);
|
---|
404 | continue;
|
---|
405 | }
|
---|
406 | String macro_value = macro_str.substring(start_bracket+1, macro_str.length()-1);
|
---|
407 | //System.err.println("macro value = "+macro_value);
|
---|
408 | //System.err.println("adding key "+current_package+":"+macro_name+", value = "+macro_value);
|
---|
409 | macros.put(current_package+":"+macro_name, macro_value);
|
---|
410 | continue;
|
---|
411 | }
|
---|
412 | } //while
|
---|
413 | } catch (Exception e) {
|
---|
414 | System.err.println("Exception when trying to load macro file "+macro_file+":"+e.getMessage());
|
---|
415 | return false;
|
---|
416 | }
|
---|
417 | return true;
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | public static void main(String []args) {
|
---|
422 | if (args.length != 1) {
|
---|
423 | System.err.println("Usage: java Dictionary <iso lang code>");
|
---|
424 | return;
|
---|
425 | }
|
---|
426 |
|
---|
427 | Dictionary dict = new Dictionary(args[0]);
|
---|
428 | System.err.println("glidict::General.OK " +dict.get("glidict::General.OK"));
|
---|
429 | System.err.println("glidict::Autofilter.Order_Tooltip "+ dict.get("glidict::Autofilter.Order_Tooltip"));
|
---|
430 |
|
---|
431 | System.err.println("macros _Global:textdefaulttitle_ "+dict.get("coredm::_Global:textdefaulttitle_"));
|
---|
432 | System.err.println("macros _about:textsubcols2_ "+dict.get("coredm::_about:textsubcols2_"));
|
---|
433 | System.err.println("macros _home:textdescrcollector_ "+dict.get("auxdm::_home:textdescrcollector_"));
|
---|
434 |
|
---|
435 | //System.err.println("", dict.get(""));
|
---|
436 | //System.err.println("", dict.get(""));
|
---|
437 |
|
---|
438 | }
|
---|
439 | }
|
---|