ips1ap101.lib.core.CoreBundle.java Source code

Java tutorial

Introduction

Here is the source code for ips1ap101.lib.core.CoreBundle.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.core;

import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;

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

    private static final String BASE_NAME = CoreBundle.class.getName();

    private static final ResourceBundle resourceBundle = ResourceBundle.getBundle(BASE_NAME);

    private enum TrimmedTo {

        KEY, EMPTY, NULL

    }

    public static String getString(String key, String suffix) {
        return getString(key + "." + suffix);
    }

    public static String getString(String key) {
        return getString(key, TrimmedTo.KEY);
    }

    public static String getStringToEmpty(String key, String suffix) {
        return getStringToEmpty(key + "." + suffix);
    }

    public static String getStringToEmpty(String key) {
        return getString(key, TrimmedTo.EMPTY);
    }

    public static String getStringToNull(String key, String suffix) {
        return getStringToNull(key + "." + suffix);
    }

    public static String getStringToNull(String key) {
        return getString(key, TrimmedTo.NULL);
    }

    private static String getString(String key, TrimmedTo trimmedTo) {
        String string = key;
        if (StringUtils.isNotBlank(key)) {
            try {
                string = resourceBundle.getString(key);
            } catch (MissingResourceException e) {
                string = null;
            }
        }
        if (StringUtils.isNotBlank(string)) {
            return string.trim();
        }
        switch (trimmedTo) {
        case EMPTY:
            return StringUtils.EMPTY;
        case NULL:
            return null;
        default:
            return key;
        }
    }

}