de.xaniox.simpletrading.i18n.YMLControl.java Source code

Java tutorial

Introduction

Here is the source code for de.xaniox.simpletrading.i18n.YMLControl.java

Source

/*
 * This file is part of SimpleTrading.
 * Copyright (c) 2015-2016 Matthias Werning
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.xaniox.simpletrading.i18n;

import com.google.common.collect.Lists;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class YMLControl extends ResourceBundle.Control {

    private static final String YML_FORMAT = "yml";

    private File localeDir;
    private String classpathDir;
    private I18N.LoadingMode mode;

    public YMLControl(File localeDir, String classpathDir, I18N.LoadingMode mode) {
        this.localeDir = localeDir;
        this.classpathDir = classpathDir;
        this.mode = mode;
    }

    @Override
    public List<String> getFormats(String baseName) {
        if (baseName == null) {
            throw new NullPointerException();
        }

        return Arrays.asList(YML_FORMAT);
    }

    @Override
    public List<Locale> getCandidateLocales(String baseName, Locale locale) {
        List<Locale> candidates = Lists.newArrayList();
        candidates.add(locale);

        if (!locale.equals(Locale.US)) {
            candidates.add(Locale.US);
        }

        return candidates;
    }

    @Override
    public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader,
            boolean reload) throws IllegalAccessException, InstantiationException, IOException {
        Validate.notNull(baseName);
        Validate.notNull(locale);
        Validate.notNull(format);
        Validate.notNull(loader);

        ResourceBundle bundle = null;

        if (YML_FORMAT.equals(format)) {
            String bundleName = toBundleName(baseName, locale);
            String resourceName = toResourceName(bundleName, format);

            URL url = null;

            if (mode == I18N.LoadingMode.CLASSPATH) {
                //The mode forces us to load the resource from classpath
                url = getClass().getResource(classpathDir + resourceName);
            } else if (mode == I18N.LoadingMode.FILE_SYSTEM) {
                //If we use the file system mode, try to load the resource from file first
                //and load it from classpath if it fails
                File resourceFile = new File(localeDir, resourceName);

                if (resourceFile.exists() && resourceFile.isFile()) {
                    url = resourceFile.toURI().toURL();
                } else {
                    url = getClass().getResource(classpathDir + resourceName);
                }
            }

            if (url == null) {
                return null;
            }

            URLConnection connection = url.openConnection();
            if (reload) {
                connection.setUseCaches(false);
            }

            InputStream stream = connection.getInputStream();

            if (stream != null) {
                Reader reader = new InputStreamReader(stream, I18N.UTF8_CHARSET);
                YamlConfiguration config = new YamlConfiguration();

                StringBuilder builder;

                try (BufferedReader bufferedReader = new BufferedReader(reader)) {
                    builder = new StringBuilder();

                    String read;
                    while ((read = bufferedReader.readLine()) != null) {
                        builder.append(read);
                        builder.append('\n');
                    }
                }

                try {
                    config.loadFromString(builder.toString());
                } catch (InvalidConfigurationException e) {
                    throw new InstantiationException(e.getMessage());
                }

                bundle = new YMLResourceBundle(config);
            }
        } else {
            bundle = super.newBundle(baseName, locale, format, loader, reload);
        }

        return bundle;
    }

    @Override
    public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader,
            ResourceBundle bundle, long loadTime) {
        boolean reload;

        if (format.equals(YML_FORMAT)) {
            String bundleName = toBundleName(baseName, locale);
            String resourceName = toResourceName(bundleName, format);

            File resourceFile = new File(localeDir, resourceName);
            if (resourceFile.exists() && resourceFile.isFile()) {
                long lastModified = resourceFile.lastModified();

                reload = lastModified > loadTime;
            } else {
                reload = super.needsReload(baseName, locale, format, loader, bundle, loadTime);
            }
        } else {
            reload = super.needsReload(baseName, locale, format, loader, bundle, loadTime);
        }

        return reload;
    }

    @Override
    public long getTimeToLive(String baseName, Locale locale) {
        return TTL_DONT_CACHE;
    }

    @Override
    public Locale getFallbackLocale(String baseName, Locale locale) {
        return null;
    }

}