source: documentation/trunk/shared/Dictionary.java@ 27035

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