ips1ap101.lib.base.bundle.JRResourceBundleHandler.java Source code

Java tutorial

Introduction

Here is the source code for ips1ap101.lib.base.bundle.JRResourceBundleHandler.java

Source

/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los trminos
 * de la licencia "GNU General Public License" publicada por la Fundacin "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser til, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacin.
 */
package ips1ap101.lib.base.bundle;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import ips1ap101.lib.base.app.constant.EAB;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;

/**
 * @author Jorge Campins
 */
public class JRResourceBundleHandler {

    private String _resourceClassSimpleName;

    private String _resourceClassPackageName;

    private ResourceBundleTriad _defaultTriad;

    private Map<Locale, ResourceBundleTriad> _triads = new LinkedHashMap<>();

    private static final Locale defaultLocale = Locale.forLanguageTag("und");

    private static final String projectLanguageTag = EAB.PROJECT_LANGUAGE_TAG;

    private static final String defaultLanguageTag = EAB.PROJECT_DEFAULT_LANGUAGE_TAG;

    private static final boolean projectLanguageTagStartsWithDefaultLanguageTag;

    static {
        if (projectLanguageTag.isEmpty() || defaultLanguageTag.isEmpty()) {
            projectLanguageTagStartsWithDefaultLanguageTag = false;
        } else {
            projectLanguageTagStartsWithDefaultLanguageTag = projectLanguageTag.startsWith(defaultLanguageTag);
        }
    }

    public JRResourceBundleHandler(Class<?> resourceClass) {
        _resourceClassSimpleName = resourceClass.getSimpleName();
        //      _resourceClassPackageName = resourceClass.getPackage().getName();
        _resourceClassPackageName = ClassUtils.getPackageName(resourceClass.getName());
        _defaultTriad = new ResourceBundleTriad(defaultLocale);
    }

    public String getString(Locale locale, String key, String suffix) {
        String toNull = getTrimmedToNullString(locale, key, suffix);
        String string = toNull == null ? key : toNull;
        return string;
    }

    public String getString(Locale locale, String key) {
        String toNull = getTrimmedToNullString(locale, key);
        String string = toNull == null ? key : toNull;
        return string;
    }

    public String getTrimmedToEmptyString(Locale locale, String key, String suffix) {
        String toNull = getTrimmedToNullString(locale, key, suffix);
        String string = toNull == null ? "" : toNull;
        return string;
    }

    public String getTrimmedToEmptyString(Locale locale, String key) {
        String toNull = getTrimmedToNullString(locale, key);
        String string = toNull == null ? "" : toNull;
        return string;
    }

    public String getTrimmedToNullString(Locale locale, String key, String suffix) {
        if (key == null || suffix == null) {
            return null;
        }
        String string = getTrimmedToNullString(locale, key + "." + suffix);
        return string;
    }

    public String getTrimmedToNullString(Locale locale, String key) {
        if (key == null) {
            return null;
        }
        Set<ResourceBundle> bundles = new LinkedHashSet<>();
        add(locale, bundles);
        add(_defaultTriad, bundles);
        String string;
        for (ResourceBundle bundle : bundles) {
            string = getString(bundle, key);
            if (StringUtils.isNotBlank(string)) {
                return string.trim();
            }
        }
        return null;
    }

    private void add(Locale locale, Set<ResourceBundle> bundles) {
        ResourceBundleTriad triad;
        if (locale != null) {
            String tag = locale.toLanguageTag();
            if (_triads.containsKey(locale)) {
                triad = _triads.get(locale);
            } else if (projectLanguageTagStartsWithDefaultLanguageTag && tag.startsWith(defaultLanguageTag)) {
                triad = _defaultTriad;
                put(locale, triad);
            } else {
                triad = new ResourceBundleTriad(locale);
                put(locale, triad);
            }
            add(triad, bundles);
        }
    }

    private void add(ResourceBundleTriad triad, Set<ResourceBundle> bundles) {
        if (triad.bundle1 != null) {
            bundles.add(triad.bundle1);
        }
        if (triad.bundle2 != null) {
            bundles.add(triad.bundle2);
        }
        if (triad.bundle3 != null) {
            bundles.add(triad.bundle3);
        }
    }

    private String getString(ResourceBundle bundle, String key) {
        try {
            return bundle.getString(key);
        } catch (MissingResourceException e) {
            return null;
        }
    }

    private void put(Locale locale, ResourceBundleTriad triad) {
        _triads.put(locale, triad);
    }

    private class ResourceBundleTriad {

        private Locale _locale;

        private ResourceBundle bundle1;

        private ResourceBundle bundle2;

        private ResourceBundle bundle3;

        private ResourceBundleTriad(Locale locale) {
            _locale = locale;
            bundle1 = getResourceBundle(getBaseName("code"));
            bundle2 = getResourceBundle(getBaseName("copy"));
            bundle3 = getResourceBundle(getBaseName("crop"));
        }

        private String getBaseName(String suffix) {
            return getPackageName(suffix) + "." + _resourceClassSimpleName;
        }

        private String getPackageName(String suffix) {
            return StringUtils.isBlank(suffix) ? _resourceClassPackageName
                    : _resourceClassPackageName + "." + suffix;
        }

        private ResourceBundle getResourceBundle(String baseName) {
            try {
                ResourceBundle bundle = ResourceBundle.getBundle(baseName, _locale);
                if (_locale.equals(defaultLocale)) {
                    return bundle;
                }
                String lang1 = _locale.getLanguage();
                String lang2 = bundle.getLocale().getLanguage();
                if (StringUtils.equals(lang1, lang2)) {
                    return bundle;
                }
                return null;
            } catch (MissingResourceException e) {
                return null;
            }
        }

        @Override
        public String toString() {
            String[] strings = new String[4];
            strings[0] = _locale == null ? "?" : _locale.toLanguageTag();
            strings[1] = bundle1 == null ? "?" : bundle1.getLocale().toLanguageTag();
            strings[2] = bundle2 == null ? "?" : bundle2.getLocale().toLanguageTag();
            strings[3] = bundle3 == null ? "?" : bundle3.getLocale().toLanguageTag();
            return _resourceClassSimpleName + "(" + strings[0] + "["
                    + StringUtils.join(ArrayUtils.subarray(strings, 1, strings.length), ",") + "]" + ")";
        }

    }

}