tim@117: package de.intevation.gnv.artifacts.ressource;
tim@117:
sascha@779: import de.intevation.artifacts.PreferredLocale;
sascha@779:
ingo@313: import java.io.BufferedReader;
ingo@313: import java.io.FileNotFoundException;
sascha@779: import java.io.IOException;
ingo@313: import java.io.InputStream;
ingo@313: import java.io.InputStreamReader;
sascha@779:
ingo@313: import java.util.ArrayList;
sascha@779: import java.util.List;
ingo@313: import java.util.Locale;
tim@123: import java.util.MissingResourceException;
tim@117: import java.util.ResourceBundle;
tim@117:
tim@117: import org.apache.log4j.Logger;
tim@117:
tim@117: /**
sascha@780: * @author Tim Englich
sascha@778: *
tim@117: */
tim@117: public class RessourceFactory {
tim@117:
tim@117: /**
tim@117: * the logger, used to log exceptions and additonaly information
tim@117: */
tim@117: private static Logger log = Logger.getLogger(RessourceFactory.class);
tim@171:
tim@117: /**
tim@117: * The singleton Instance of this Factory.
tim@117: */
tim@117: private static RessourceFactory instance = null;
tim@117:
tim@117: private static String RESSOURCE_BASE_ID = "artifact.ressource.dir";
tim@171:
ingo@313: private static String ressourceName = "artifactMessages";
ingo@313: private static String DEFAULT_DIR = "lang";
ingo@313: private static String LANG_CONFIG_FILE = "lang.conf";
tim@171:
tim@117: private String ressourceDir = null;
tim@118:
ingo@313: private Locale[] locales = null;
ingo@313:
tim@117: /**
tim@117: * Basic-Constructor of this Class
tim@117: */
tim@117: private RessourceFactory() {
tim@117: super();
sascha@121: ressourceDir = System.getProperty(RESSOURCE_BASE_ID, DEFAULT_DIR);
tim@117: }
tim@117:
tim@117: /**
tim@117: * This Method provides an singleton Instance of this Class.
sascha@778: *
tim@117: * @return an singleton Instance of this Class
tim@117: */
tim@171: public static synchronized RessourceFactory getInstance() {
tim@171: if (instance == null) {
tim@117: instance = new RessourceFactory();
tim@117: }
tim@117: return instance;
tim@117: }
tim@171:
ingo@313:
ingo@313: /**
ingo@313: * This method reads locales, configured in LANG_CONFIG_FILE, from
ingo@313: * filesystem and returns them as array.
ingo@313: *
ingo@313: * @return Array of locales supported by the the server.
ingo@313: */
ingo@313: public Locale[] getLocales() {
ingo@313:
ingo@313: if (locales != null)
ingo@313: return locales;
ingo@313:
ingo@313: log.debug("Supported locales have not been read - read now.");
ingo@313: try {
ingo@313: String config = "/" + ressourceDir + "/" + LANG_CONFIG_FILE;
ingo@313: InputStream in = RessourceFactory.class.getResourceAsStream(config);
ingo@313:
ingo@313: BufferedReader reader = new BufferedReader(
ingo@313: new InputStreamReader(in)
ingo@313: );
ingo@313:
ingo@313: String line = null;
ingo@313: String country = null;
ingo@313: String language = null;
ingo@313: int idx = -1;
ingo@313:
ingo@313: List tmpLocales = new ArrayList();
ingo@313:
ingo@313: while((line = reader.readLine()) != null) {
ingo@313: // validate if defined locale has a valid length
ingo@313: if (line.length() != 2 && line.length() != 5) {
ingo@313: log.warn("Illegal locale definition found.");
ingo@313: continue;
ingo@313: }
ingo@313:
ingo@313: idx = line.indexOf("_");
ingo@313: if (idx > 0) {
ingo@313: // found locale containing language and country code
ingo@313: language = line.substring(0, idx);
ingo@313: country = line.substring(idx+1);
ingo@313: tmpLocales.add(new Locale(language, country));
ingo@313: }
ingo@313: else {
ingo@313: // found locale containing languagey code only
ingo@313: tmpLocales.add(new Locale(line));
ingo@313: }
ingo@313: }
ingo@313:
ingo@313: locales = (Locale[]) tmpLocales.toArray(
ingo@313: new Locale[tmpLocales.size()]
ingo@313: );
ingo@313:
ingo@313: return locales;
ingo@313: }
ingo@313: catch (FileNotFoundException fnfe) {
ingo@313: log.warn("File not found: " + LANG_CONFIG_FILE, fnfe);
ingo@313: }
ingo@313: catch (IOException ioe) {
ingo@313: log.warn(ioe.getLocalizedMessage(), ioe);
ingo@313: }
ingo@313:
ingo@313: return null;
ingo@313: }
ingo@313:
ingo@313:
tim@117: /**
tim@117: * Deliveres the translated Value for an Key to an given Language
sascha@778: *
ingo@792: * @param preferredLocales
ingo@792: * @param key the key
ingo@792: * @param defaultValue the Value that should be returned.
tim@117: * @return the translated Value
tim@117: */
tim@171: public String getRessource(PreferredLocale[] preferredLocales, String key,
tim@171: String defaultValue) {
sascha@121: if (key == null || preferredLocales.length == 0) {
sascha@121: return defaultValue;
sascha@121: }
sascha@121:
ingo@331: return getRessource(preferredLocales[0].getLocale(), key, defaultValue);
ingo@331: }
ingo@331:
ingo@331:
ingo@792: /**
ingo@792: * Deliveres the language specific value for the given key
sascha@803: *
ingo@792: * @param locale
ingo@792: * @param key
ingo@792: * @param defaultVal
ingo@792: * @return language specific string.
ingo@792: */
ingo@331: public String getRessource(Locale locale, String key, String defaultVal) {
ingo@331: if (key == null || locale == null)
ingo@331: return defaultVal;
ingo@331:
tim@117: try {
ingo@331: ResourceBundle rb = ResourceBundle.getBundle(
ingo@331: ressourceDir + "/" + ressourceName,
ingo@331: locale
ingo@331: );
sascha@121:
tim@117: return rb.getString(key);
ingo@331: }
ingo@331: catch (MissingResourceException mre) {
ingo@331: log.warn("No resource bundle: " + locale.toString(), mre);
ingo@331: return defaultVal;
tim@117: }
tim@117: }
tim@117: }
ingo@792: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :