Mercurial > dive4elements > gnv-client
view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/ressource/RessourceFactory.java @ 592:3c939c95c477
Removed accidental checkin of Test-Shapefiles
gnv-artifacts/trunk@647 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Fri, 29 Jan 2010 13:28:28 +0000 |
parents | 1c427acb6c76 |
children | 9a828e5a2390 |
line wrap: on
line source
/** * */ package de.intevation.gnv.artifacts.ressource; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.util.ArrayList; import java.util.Locale; import java.util.List; import java.util.MissingResourceException; import java.util.ResourceBundle; import org.apache.log4j.Logger; import de.intevation.artifacts.PreferredLocale; /** * @author Tim Englich <tim.englich@intevation.de> * */ public class RessourceFactory { /** * the logger, used to log exceptions and additonaly information */ private static Logger log = Logger.getLogger(RessourceFactory.class); /** * The singleton Instance of this Factory. */ private static RessourceFactory instance = null; private static String RESSOURCE_BASE_ID = "artifact.ressource.dir"; private static String ressourceName = "artifactMessages"; private static String DEFAULT_DIR = "lang"; private static String LANG_CONFIG_FILE = "lang.conf"; private String ressourceDir = null; private Locale[] locales = null; /** * Basic-Constructor of this Class */ private RessourceFactory() { super(); ressourceDir = System.getProperty(RESSOURCE_BASE_ID, DEFAULT_DIR); } /** * This Method provides an singleton Instance of this Class. * * @return an singleton Instance of this Class */ public static synchronized RessourceFactory getInstance() { if (instance == null) { instance = new RessourceFactory(); } return instance; } /** * This method reads locales, configured in LANG_CONFIG_FILE, from * filesystem and returns them as array. * * @return Array of locales supported by the the server. */ public Locale[] getLocales() { if (locales != null) return locales; log.debug("Supported locales have not been read - read now."); try { String config = "/" + ressourceDir + "/" + LANG_CONFIG_FILE; InputStream in = RessourceFactory.class.getResourceAsStream(config); BufferedReader reader = new BufferedReader( new InputStreamReader(in) ); String line = null; String country = null; String language = null; int idx = -1; List tmpLocales = new ArrayList(); while((line = reader.readLine()) != null) { // validate if defined locale has a valid length if (line.length() != 2 && line.length() != 5) { log.warn("Illegal locale definition found."); continue; } idx = line.indexOf("_"); if (idx > 0) { // found locale containing language and country code language = line.substring(0, idx); country = line.substring(idx+1); tmpLocales.add(new Locale(language, country)); } else { // found locale containing languagey code only tmpLocales.add(new Locale(line)); } } locales = (Locale[]) tmpLocales.toArray( new Locale[tmpLocales.size()] ); return locales; } catch (FileNotFoundException fnfe) { log.warn("File not found: " + LANG_CONFIG_FILE, fnfe); } catch (IOException ioe) { log.warn(ioe.getLocalizedMessage(), ioe); } return null; } /** * Deliveres the translated Value for an Key to an given Language * * @param locale * The choosen locale * @param key * the key * @param defaultValue * the Value that should be returned. * @return the translated Value */ public String getRessource(PreferredLocale[] preferredLocales, String key, String defaultValue) { if (key == null || preferredLocales.length == 0) { return defaultValue; } return getRessource(preferredLocales[0].getLocale(), key, defaultValue); } public String getRessource(Locale locale, String key, String defaultVal) { if (key == null || locale == null) return defaultVal; try { ResourceBundle rb = ResourceBundle.getBundle( ressourceDir + "/" + ressourceName, locale ); return rb.getString(key); } catch (MissingResourceException mre) { log.warn("No resource bundle: " + locale.toString(), mre); return defaultVal; } } }