001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
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:
020: package com.izforge.izpack;
021:
022: import java.io.InputStream;
023: import java.text.MessageFormat;
024: import java.util.TreeMap;
025: import java.util.Vector;
026:
027: import net.n3.nanoxml.NonValidator;
028: import net.n3.nanoxml.StdXMLParser;
029: import net.n3.nanoxml.StdXMLReader;
030: import net.n3.nanoxml.XMLElement;
031: import net.n3.nanoxml.XMLBuilderFactory;
032:
033: /**
034: * Represents a database of a locale.
035: *
036: * @author Julien Ponge
037: */
038: public class LocaleDatabase extends TreeMap {
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: // We call the superclass default constructor
050: super ();
051: add(in);
052: }
053:
054: /**
055: * Adds the contents of the given stream to the data base. The stream have to contain key value
056: * pairs as declared by the DTD langpack.dtd.
057: *
058: * @param in an InputStream to read the translation from.
059: * @throws Exception
060: */
061: public void add(InputStream in) throws Exception {
062: // Initialises the parser
063: StdXMLParser parser = new StdXMLParser();
064: parser.setBuilder(XMLBuilderFactory.createXMLBuilder());
065: parser.setReader(new StdXMLReader(in));
066: parser.setValidator(new NonValidator());
067:
068: // We get the data
069: XMLElement data = (XMLElement) parser.parse();
070:
071: // We check the data
072: if (!"langpack".equalsIgnoreCase(data.getName()))
073: throw new Exception(
074: "this is not an IzPack XML langpack file");
075:
076: // We fill the Hashtable
077: Vector children = data.getChildren();
078: int size = children.size();
079: for (int i = 0; i < size; i++) {
080: XMLElement e = (XMLElement) children.get(i);
081: String text = e.getContent();
082: if (text != null && !"".equals(text)) {
083: put(e.getAttribute("id"), text.trim());
084: } else {
085: put(e.getAttribute("id"), e.getAttribute("txt"));
086: }
087: }
088:
089: }
090:
091: /**
092: * Convenience method to retrieve an element.
093: *
094: * @param key The key of the element to retrieve.
095: * @return The element value or the key if not found.
096: */
097: public String getString(String key) {
098: String val = (String) get(key);
099: // At a change of the return value at val == null the method
100: // com.izforge.izpack.installer.IzPanel.getI18nStringForClass
101: // should be also addapted.
102: if (val == null)
103: val = key;
104: return val;
105: }
106:
107: /**
108: * Convenience method to retrieve an element and simultainiously insert variables into the
109: * string. A placeholder have to be build with the substring {n} where n is the parameter
110: * argument beginning with 0. The first argument is therefore {0}. If a parameter starts with a
111: * dollar sign the value will be used as key into the LocalDatabase. The key can be written as
112: * $MYKEY or ${MYKEY}. For all placeholder an argument should be exist and vis a versa.
113: *
114: * @param key The key of the element to retrieve.
115: * @param variables the variables to insert
116: * @return The element value with the variables inserted or the key if not found.
117: */
118: public String getString(String key, String[] variables) {
119: for (int i = 0; i < variables.length; ++i) {
120: if (variables[i].startsWith("$")) { // Argument is also a key into the LocaleDatabase.
121: String curArg = variables[i];
122: if (curArg.startsWith("${"))
123: curArg = curArg.substring(2, curArg.length() - 1);
124: else
125: curArg = curArg.substring(1);
126: variables[i] = getString(curArg);
127: }
128: }
129:
130: return MessageFormat.format(getString(key),
131: new Object[] { variables });
132: }
133:
134: }
|