001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *     
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package com.izforge.izpack;
021
022import java.io.InputStream;
023import java.util.TreeMap;
024import java.util.Vector;
025
026import net.n3.nanoxml.NonValidator;
027import net.n3.nanoxml.StdXMLBuilder;
028import net.n3.nanoxml.StdXMLParser;
029import net.n3.nanoxml.StdXMLReader;
030import net.n3.nanoxml.XMLElement;
031
032/**
033 * Represents a database of a locale.
034 * 
035 * @author Julien Ponge
036 */
037public class LocaleDatabase extends TreeMap
038{
039
040    static final long serialVersionUID = 4941525634108401848L;
041
042    /**
043     * The constructor.
044     * 
045     * @param in An InputStream to read the translation from.
046     * @exception Exception Description of the Exception
047     */
048    public LocaleDatabase(InputStream in) throws Exception
049    {
050        // We call the superclass default constructor
051        super();
052        add(in);
053    }
054
055    public void add(InputStream in) throws Exception
056    {
057        // Initialises the parser
058        StdXMLParser parser = new StdXMLParser();
059        parser.setBuilder(new StdXMLBuilder());
060        parser.setReader(new StdXMLReader(in));
061        parser.setValidator(new NonValidator());
062
063        // We get the data
064        XMLElement data = (XMLElement) parser.parse();
065
066        // We check the data
067        if (!data.getName().equalsIgnoreCase("langpack"))
068            throw new Exception("this is not an IzPack XML langpack file");
069
070        // We fill the Hashtable
071        Vector children = data.getChildren();
072        int size = children.size();
073        for (int i = 0; i < size; i++)
074        {
075            XMLElement e = (XMLElement) children.get(i);
076            String text = e.getContent();
077            if (text != null && !text.equals(""))
078            {
079                put(e.getAttribute("id"), text.trim());
080            }
081            else
082            {
083                put(e.getAttribute("id"), e.getAttribute("txt"));
084            }
085        }
086
087    }
088
089    /**
090     * Convenience method to retrieve an element.
091     * 
092     * @param key The key of the element to retrieve.
093     * @return The element value or the key if not found.
094     */
095    public String getString(String key)
096    {
097        String val = (String) get(key);
098        // At a change of the return value at val == null the method
099        // com.izforge.izpack.installer.IzPanel.getI18nStringForClass
100        // should be also addapted.
101        if (val == null) val = key;
102        return val;
103    }
104}
105