001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.system;
025:
026: import jacareto.toolkit.EnhancedHashtable;
027: import jacareto.toolkit.HashtableException;
028:
029: import org.apache.log4j.Logger;
030:
031: import java.io.IOException;
032:
033: /**
034: * A storage for texts depending on a language (keyword -> text mappings like the ResourceBundle
035: * class of the java API) The mapping can be given as a enhanced hashtable object. All values will
036: * be interpreted as Strings. TODO: Default language if a string cannot be found in the specified
037: * language
038: *
039: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
040: * @version 1.01
041: */
042: public class Language {
043: /** The mapping. */
044: private EnhancedHashtable mapping;
045:
046: /** The logger object. */
047: private Logger logger;
048:
049: /** The identifier string of this language (en, for example). */
050: private String idstring;
051:
052: /**
053: * Creates a new language object with the given mapping and the given logger instance.
054: *
055: * @param mapping the mapping of keywords to language dependent texts
056: * @param logger the logger instance for error messages
057: */
058: public Language(EnhancedHashtable mapping, Logger logger) {
059: setMapping(mapping);
060: setLogger(logger);
061: setIdentifier("");
062: }
063:
064: /**
065: * Creates a new language object with an empty mapping and the root logger.
066: */
067: public Language() {
068: this (new EnhancedHashtable(), Logger.getRootLogger());
069: }
070:
071: /**
072: * Creates a new language object which loads the mapping from a properties file specified in
073: * the customization instance for a given language.
074: *
075: * @param customization the customization instance with th language -> properties filename
076: * mapping
077: * @param basedir the basedir for the properties files
078: * @param language the language ("en" for English, for example)
079: * @param logger the logger instance
080: *
081: * @throws IOException if the language file could not be read
082: * @throws HashtableException if there is no setting for the language file map or the language
083: * key is not contained in the map
084: */
085: public Language(Customization customization, String basedir,
086: String language, Logger logger) throws IOException,
087: HashtableException {
088: this (new Customization(basedir
089: + "/"
090: + customization.getMap("Language.Files").getString(
091: language)), logger);
092: setIdentifier(language);
093: }
094:
095: /**
096: * Creates a new language object which loads the mapping from a properties file specified in
097: * the customization instance for a given language.
098: *
099: * @param customization the customization instance with th language -> properties filename
100: * mapping
101: * @param language the language ("en" for English, for example)
102: * @param logger the logger instance
103: *
104: * @throws IOException if the language file could not be read
105: * @throws HashtableException if there is no setting for the language file map or the language
106: * key is not contained in the map
107: */
108: public Language(Customization customization, String language,
109: Logger logger) throws IOException, HashtableException {
110: this (new Customization(customization.getMap("Language.Files")
111: .getString(language)), logger);
112: setIdentifier(language);
113: }
114:
115: /**
116: * Sets the logger instance
117: *
118: * @param logger the new logger object
119: */
120: public void setLogger(Logger logger) {
121: this .logger = logger;
122: }
123:
124: /**
125: * Returns the current logger instance.
126: *
127: * @return DOCUMENT ME!
128: */
129: public Logger getLogger() {
130: return logger;
131: }
132:
133: /**
134: * Sets the identifier of this language (en, for example.
135: *
136: * @param idstring the identifier string
137: */
138: public void setIdentifier(String idstring) {
139: this .idstring = idstring;
140: }
141:
142: /**
143: * Returns the identifier of this language (en, for example).
144: *
145: * @return the identifier string
146: */
147: public String getIdentifier() {
148: return idstring;
149: }
150:
151: /**
152: * Sets the mapping
153: *
154: * @param mapping the new mapping
155: */
156: public void setMapping(EnhancedHashtable mapping) {
157: this .mapping = mapping;
158: }
159:
160: /**
161: * Returns the current mapping.
162: *
163: * @return DOCUMENT ME!
164: */
165: public EnhancedHashtable getMapping() {
166: return mapping;
167: }
168:
169: /**
170: * Returns the text belonging to a given keyword. If the key does not exist or the text is
171: * <code>null</code>, an error message will be printed and the empty string. will be returned.
172: *
173: * @param key the keyword
174: *
175: * @return the text belonging to the keyword, or the empty string if the key does not exist or
176: * the text is <code>null</code>.
177: */
178: public String getString(String key) {
179: try {
180: return mapping.getString(key);
181: } catch (HashtableException h) {
182: logger.error("Language Error: Non-existent key \"" + key
183: + "\".");
184:
185: return "";
186: }
187: }
188:
189: /**
190: * Sets a value for a key.
191: *
192: * @param key the keyword
193: * @param value the value
194: */
195: public void setString(String key, String value) {
196: mapping.put(key, value);
197: }
198:
199: /**
200: * Returns whether or not a key is contained.
201: *
202: * @param key the key
203: *
204: * @return <code>true</code> if the key is contained, otherwise <code>false</code>
205: */
206: public boolean containsKey(String key) {
207: return mapping.containsKey(key);
208: }
209: }
|