KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > XMLMMLanguageReader


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.io.File JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.w3c.dom.Element JavaDoc;
18
19 /**
20  * XMLLanguageReader parses the .xml file in its argument in its constructor.
21  * This .xml file should be formatted according to mmlanguage.dtd.
22  *
23  * - getLanguageCode() - returns the language code of the .xml file
24  * - getDictionary() - returns a hashtable with mmbase term identifiers to their
25  * translations in this specific language.
26  *
27  * Uglinesses:
28  * - no check whether the input file exists
29  * - doesn't read when the xml:lang attribute is absent, but doesn't generate an error either
30  *
31  * @application SCAN
32  * @deprecated not used anywhere
33  * @author cjr@dds.nl
34  * @version $Id: XMLMMLanguageReader.java,v 1.12 2005/07/09 15:29:12 nklasens Exp $
35  */

36 public class XMLMMLanguageReader extends XMLBasicReader {
37
38     Hashtable JavaDoc languageList; // Hashtable from languagecode to Hashtables with dictionaries
39

40     String JavaDoc languagecode; // code for language, e.g. 'nl'
41
Hashtable JavaDoc dictionary; // dictionary of mmbase term identifiers to translations in language
42

43
44     public XMLMMLanguageReader(String JavaDoc filename) {
45         super(filename, XMLMMLanguageReader.class);
46
47         dictionary = null;
48
49         generateFromDOM();
50     }
51
52
53     /**
54      * generateFromDOM() walks the DOM-tree and converts the result to a Hashtable
55      *
56      */

57     protected void generateFromDOM() {
58         dictionary = new Hashtable JavaDoc();
59         Element JavaDoc e = document.getDocumentElement();
60         languagecode = getElementAttributeValue(e,"xml:lang");
61         Element JavaDoc d = getElementByPath("mmlanguage.dictionary");
62         
63         for (Iterator JavaDoc iter = getChildElements(d); iter.hasNext();) {
64             Element JavaDoc a = (Element JavaDoc) iter.next();
65             dictionary.put(getElementName(a),getElementValue(a));
66         }
67     }
68
69     /**
70      * @return Language code of the language of the read .xml file
71      */

72     public String JavaDoc getLanguageCode() {
73         return languagecode;
74     }
75
76     /*
77      * @return Hashtable from mmbase term identifiers to their translations in the language.
78      */

79     public Hashtable JavaDoc getDictionary() {
80         return dictionary;
81     }
82
83     /**
84      * Test code:
85      *
86      */

87     public static void main(String JavaDoc[] argv) {
88         String JavaDoc path = "/opt2/mmbase/org/mmbase/config/future/modules/languages/nl.xml";
89
90         File JavaDoc f = new File JavaDoc(path);
91         if (f.exists()) {
92             System.out.println("file exists");
93             XMLMMLanguageReader reader = new XMLMMLanguageReader(path);
94             //reader.generateLanguageList();
95

96             System.out.println("language = "+reader.getLanguageCode());
97             Hashtable JavaDoc dict = reader.getDictionary();
98             Enumeration JavaDoc enumeration = dict.keys();
99             while (enumeration.hasMoreElements()) {
100                 String JavaDoc s = (String JavaDoc)enumeration.nextElement();
101                 System.out.println(s+" => "+dict.get(s));
102             }
103
104         } else {
105             System.out.println(path + "doesn't exist");
106         }
107
108         System.out.println(path);
109         System.out.println("test");
110     }
111
112 }
113
114
115
116
117
118
119
120
Popular Tags