KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > LocaleDatabase


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.izforge.izpack;
21
22 import java.io.InputStream JavaDoc;
23 import java.util.TreeMap JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import net.n3.nanoxml.NonValidator;
27 import net.n3.nanoxml.StdXMLBuilder;
28 import net.n3.nanoxml.StdXMLParser;
29 import net.n3.nanoxml.StdXMLReader;
30 import net.n3.nanoxml.XMLElement;
31
32 /**
33  * Represents a database of a locale.
34  *
35  * @author Julien Ponge
36  */

37 public class LocaleDatabase extends TreeMap JavaDoc
38 {
39
40     static final long serialVersionUID = 4941525634108401848L;
41
42     /**
43      * The constructor.
44      *
45      * @param in An InputStream to read the translation from.
46      * @exception Exception Description of the Exception
47      */

48     public LocaleDatabase(InputStream JavaDoc in) throws Exception JavaDoc
49     {
50         // We call the superclass default constructor
51
super();
52         add(in);
53     }
54
55     public void add(InputStream JavaDoc in) throws Exception JavaDoc
56     {
57         // Initialises the parser
58
StdXMLParser parser = new StdXMLParser();
59         parser.setBuilder(new StdXMLBuilder());
60         parser.setReader(new StdXMLReader(in));
61         parser.setValidator(new NonValidator());
62
63         // We get the data
64
XMLElement data = (XMLElement) parser.parse();
65
66         // We check the data
67
if (!"langpack".equalsIgnoreCase(data.getName()))
68             throw new Exception JavaDoc("this is not an IzPack XML langpack file");
69
70         // We fill the Hashtable
71
Vector JavaDoc children = data.getChildren();
72         int size = children.size();
73         for (int i = 0; i < size; i++)
74         {
75             XMLElement e = (XMLElement) children.get(i);
76             String JavaDoc text = e.getContent();
77             if (text != null && !"".equals(text))
78             {
79                 put(e.getAttribute("id"), text.trim());
80             }
81             else
82             {
83                 put(e.getAttribute("id"), e.getAttribute("txt"));
84             }
85         }
86
87     }
88
89     /**
90      * Convenience method to retrieve an element.
91      *
92      * @param key The key of the element to retrieve.
93      * @return The element value or the key if not found.
94      */

95     public String JavaDoc getString(String JavaDoc key)
96     {
97         String JavaDoc val = (String JavaDoc) get(key);
98         // At a change of the return value at val == null the method
99
// com.izforge.izpack.installer.IzPanel.getI18nStringForClass
100
// should be also addapted.
101
if (val == null) val = key;
102         return val;
103     }
104 }
105
106
Popular Tags