root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / LocaleDatabase.java @ 15280
History | View | Annotate | Download (2.92 KB)
1 |
/*
|
---|---|
2 |
* $Id: LocaleDatabase.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
|
3 |
* IzPack
|
4 |
* Copyright (C) 2001-2004 Julien Ponge
|
5 |
*
|
6 |
* File : LocaleDatabase.java
|
7 |
* Description : Represents a langpack database.
|
8 |
* Author's email : julien@izforge.com
|
9 |
* Author's Website : http://www.izforge.com
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
24 |
*/
|
25 |
package com.izforge.izpack; |
26 |
|
27 |
import java.io.InputStream; |
28 |
import java.util.TreeMap; |
29 |
import java.util.Vector; |
30 |
|
31 |
import net.n3.nanoxml.NonValidator; |
32 |
import net.n3.nanoxml.StdXMLBuilder; |
33 |
import net.n3.nanoxml.StdXMLParser; |
34 |
import net.n3.nanoxml.StdXMLReader; |
35 |
import net.n3.nanoxml.XMLElement; |
36 |
|
37 |
/**
|
38 |
* Represents a database of a locale.
|
39 |
*
|
40 |
* @author Julien Ponge
|
41 |
*/
|
42 |
public class LocaleDatabase extends TreeMap |
43 |
{ |
44 |
/**
|
45 |
* The constructor.
|
46 |
*
|
47 |
* @param in An InputStream to read the translation from.
|
48 |
* @exception Exception Description of the Exception
|
49 |
*/
|
50 |
public LocaleDatabase(InputStream in) throws Exception |
51 |
{ |
52 |
// We call the superclass default constructor
|
53 |
super();
|
54 |
|
55 |
// Initialises the parser
|
56 |
StdXMLParser parser = new StdXMLParser();
|
57 |
parser.setBuilder(new StdXMLBuilder());
|
58 |
parser.setReader(new StdXMLReader(in));
|
59 |
parser.setValidator(new NonValidator());
|
60 |
|
61 |
// We get the data
|
62 |
XMLElement data = (XMLElement) parser.parse(); |
63 |
|
64 |
// We check the data
|
65 |
if (!data.getName().equalsIgnoreCase("langpack")) |
66 |
throw new Exception("this is not an IzPack XML langpack file"); |
67 |
|
68 |
// We fill the Hashtable
|
69 |
Vector children = data.getChildren();
|
70 |
int size = children.size();
|
71 |
for (int i = 0; i < size; i++) |
72 |
{ |
73 |
XMLElement e = (XMLElement) children.get(i); |
74 |
String text = e.getContent();
|
75 |
if (text != null && !text.equals("")){ |
76 |
put(e.getAttribute("id"), text.trim());
|
77 |
}else{
|
78 |
put(e.getAttribute("id"), e.getAttribute("txt")); |
79 |
} |
80 |
} |
81 |
} |
82 |
|
83 |
/**
|
84 |
* Convenience method to retrieve an element.
|
85 |
*
|
86 |
* @param key The key of the element to retrieve.
|
87 |
* @return The element value or the key if not found.
|
88 |
*/
|
89 |
public String getString(String key) |
90 |
{ |
91 |
String val = (String)get(key); |
92 |
if (val == null) val = key; |
93 |
return val;
|
94 |
} |
95 |
} |