view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/ressource/RessourceFactory.java @ 836:05bf8534a35a

Using unix line endings only. gnv-artifacts/trunk@938 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 18 Apr 2010 09:17:25 +0000
parents feae2f9d6c6f
children f953c9a559d8
line wrap: on
line source
package de.intevation.gnv.artifacts.ressource;

import de.intevation.artifacts.PreferredLocale;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/**
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 *
 */
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 preferredLocales
     * @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);
    }


    /**
     * Deliveres the language specific value for the given <code>key</code>
     *
     * @param locale
     * @param key
     * @param defaultVal
     * @return language specific string.
     */
    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;
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org