1 package org.opensync.tools;
2
3 import org.xml.sax.helpers.*;
4 import org.xml.sax.*;
5 import java.util.*;
6 import java.io.*;
7
8 public final class DicoReader extends DefaultHandler {
9
10 private static Hashtable dictionaries;
11 private static Hashtable current_dico;
12 private static String filename;
13 private static String strLock = "lock";
14
15 /***
16 * Constructor
17 */
18 public DicoReader(String s) {
19 filename = s;
20 this.dictionaries = new Hashtable();
21 }
22 public DicoReader() {
23 }
24 public static void init(String s) {
25 filename = s;
26 dictionaries = new Hashtable();
27 }
28
29
30
31
32
33 public static String getLabelDicoByKey(String lang,String k) {
34 try {
35 String label="";
36 Hashtable dico = (Hashtable)dictionaries.get(lang);
37
38 if (dico==null) {
39 synchronized(strLock) {
40 current_dico = new Hashtable();
41 processDicoXml(filename + lang + ".xml");
42 dictionaries.put(lang, current_dico);
43 dico = (Hashtable) dictionaries.get(lang);
44 }
45 }
46 if (k!=null) {
47 label = (String)dico.get(k);
48 if (label == null || label.length()==0) {
49 Utils.LogToFile("translation.log","label: "+k+" needs to be translated in ["+lang+"]");
50 return k;
51 }
52 }
53
54 return label;
55 }
56 catch (Exception ex) {
57 throw new RuntimeException(ex.getMessage());
58 }
59 }
60
61 public static void processDicoXml(String filename) throws IOException,FileNotFoundException,SAXException {
62
63 XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
64 xr.setContentHandler( new DicoReader() );
65
66
67 if (new File(filename).exists()){
68 xr.parse( new InputSource(new File(filename).toURL().toExternalForm()));
69 }else{
70 xr.parse( new InputSource(filename));
71 }
72 }
73
74 public void startElement ( String namespaceURI, String localName, String qName, Attributes atts ) throws SAXException {
75 if ( qName.equals("COLUMN") ){
76 current_dico.put(atts.getValue("KeyWord"),atts.getValue("Label"));
77
78 }
79 }
80
81 }
82