All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.undercouch.citeproc.DefaultLocaleProvider Maven / Gradle / Ivy

package de.undercouch.citeproc;

import de.undercouch.citeproc.helper.CSLUtils;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Default implementation of {@link LocaleProvider}. Loads locales from
 * the classpath.
 * @author Michel Kraemer
 */
public class DefaultLocaleProvider implements LocaleProvider {
    /**
     * A cache for the serialized XML of locales
     */
    private Map locales = new HashMap<>();

    /**
     * Retrieves the serialized XML for the given locale from the classpath.
     * For example, if the locale is en-US this method loads
     * the file /locales-en-US.xml from the classpath.
     */
    @Override
    public String retrieveLocale(String lang) {
        String r = locales.get(lang);
        if (r == null) {
            try {
                URL u = getClass().getResource("/locales-" + lang + ".xml");
                if (u == null) {
                    throw new IllegalArgumentException("Unable to load locale " +
                            lang + ". Make sure you have a file called " +
                            "'/locales-" + lang + ".xml' at the root of your " +
                            "classpath. Did you add the CSL locale files to "
                            + "your classpath?");
                }
                r = CSLUtils.readURLToString(u, "UTF-8");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            locales.put(lang, r);
        }
        return r;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy