source: documentation/trunk/shared/Dictionary.java

Last change on this file was 27049, checked in by kjdon, 11 years ago

adding some code for getting gs3 properties files strings. AutoText key for xml is gs3::filename::key, eg gs3::interface_default::home_b. streamlining some code

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