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

net.sf.gluebooster.java.booster.basic.container.ResourceBundleFactory Maven / Gradle / Ivy

Go to download

Basic classes to support the development of applications. There should be as few dependencies on other frameworks as possible.

The newest version!
package net.sf.gluebooster.java.booster.basic.container;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import net.sf.gluebooster.java.booster.basic.meta.DocumentationContext;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.Callable;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableByReflection;
import net.sf.gluebooster.java.booster.essentials.utils.Check;
import net.sf.gluebooster.java.booster.essentials.utils.Constants;
import net.sf.gluebooster.java.booster.essentials.utils.ThrowableBoostUtils;

/**
 * Returns/Creates resource bundles
 * 
 * @defaultParamText name the name of the instance
 * @defaultParamText locale the given locale
 * @defaultParamText key the given key
 * 
 * @author cbauer
 *
 */
public class ResourceBundleFactory extends CallableAbstraction {

	/**
	 * default values of the merged resource bundle
	 */
	private Map> defaultValues = new HashMap();

	/**
	 * Create parts of the merged resource bundle
	 */
	private Callable[] factories;

	/**
	 * Modifies the created resource bundle
	 */
	private Callable modifier;

	/**
	 * Resource bundles for different locales
	 */
	private Map result = new HashMap();

	private ResourceBundleFactory() {
	}

	public ResourceBundleFactory(Object name, Callable modifier, Callable... factories) {
		super(name);
		this.modifier = modifier;
		this.factories = factories;
	}

	/**
	 * 
	 * @param basename
	 *            the basename of the resources
	 */
	public ResourceBundleFactory(Object name, String basename, Callable modifier) throws RuntimeException {
		this(name, modifier, new CallableByReflection("get resourceboundle for basename " + basename, null, getBundleMethod(), basename, Constants.REPLACE));
		this.factories = factories;
	}

	/**
	 * Returns the method ResourceBundle.getBundle
	 * 
	 * @return the method
	 */
	private static Method getBundleMethod() {
		try {
			return ResourceBundle.class.getMethod("getBundle", String.class, Locale.class);
		} catch (Exception ex) {
			throw ThrowableBoostUtils.toRuntimeException(ex);
		}

	}

	@Override
	protected ResourceBundle callImpl(Object... localeOrDocumentationContext) throws Exception {
		if (localeOrDocumentationContext.length != 1) {
			throw new IllegalStateException("exactly 1 locale allowed, not " + localeOrDocumentationContext.length);
		}

		Locale locale;
		if (localeOrDocumentationContext[0] instanceof Locale) {
			locale = (Locale) localeOrDocumentationContext[0];
		} else if (localeOrDocumentationContext[0] instanceof DocumentationContext) {
			locale = ((DocumentationContext) localeOrDocumentationContext[0]).getLocale();
		} else {
			throw new IllegalStateException("unsupported parameterclass: " + localeOrDocumentationContext[0].getClass().getName());
		}

		if (!result.containsKey(locale)) {
			ArrayList bundles = new ArrayList();
			for (Locale loc : new Locale[] { new Locale(locale.getLanguage()), new Locale(locale.getLanguage(), locale.getCountry()),
					new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant()) }) {
				if (defaultValues.containsKey(loc)) {
					bundles.add(new BoostedListResourceBundle(defaultValues.get(loc)));
				}
			}

			for (Callable factory : factories) {
				bundles.add(factory.call(null, locale));// null = object
			}

			ResourceBundle merged = new BoostedListResourceBundle(bundles.toArray(new ResourceBundle[bundles.size()]));
			if (modifier != null) {
				merged = modifier.call(merged);
			}

			result.put(locale, merged);
		}

		return result.get(locale);
	}


	/**
	 * Put the default value for a given key in a given locale
	 * 
	 * @param value
	 *            the new value (null = remove the key)
	 * @return the old value of the key
	 */
	public Object putDefaultValue(Locale locale, String key, Object value) {
		if (!defaultValues.containsKey(locale)) {
			defaultValues.put(locale, new HashMap());
		}

		Map map = defaultValues.get(locale);
		if (value == null) {
			return map.remove(key);
		} else {
			return map.put(key, value);
		}
	}

	/**
	 * Is a given key available
	 * 
	 * @return true iff the key is in the localized map
	 */
	public boolean containsKey(Locale locale, String key) throws Exception {
		return callImpl(locale).containsKey(key);
	}

	/**
	 * Is a given key available
	 * 
	 * @param context
	 *            contains the locale
	 * @return true iff the key is in the localized map
	 */
	public boolean containsKey(DocumentationContext context, Object key) throws Exception {
		String keyString = (key == null) ? null : key.toString();
		return containsKey(context.getLocale(), keyString);
	}

	/**
	 * Gets a value from the localized map.
	 * 
	 * @param context
	 *            contains the locale
	 * @param throwExceptionIfKeyNotFound
	 *            should an exception be thrown when the key is not available
	 * @return localizedMap.get(key)
	 */
	public Object getObject(DocumentationContext context, Object key, boolean throwExceptionIfKeyNotFound) throws Exception {
		Check.notNull(key, "key");
		ResourceBundle termsTranslator = callImpl(context);// getTermsDefinitionsTranslator(context);
		String keyString = key.toString(); // default
		if (termsTranslator.containsKey(keyString)) {
			return termsTranslator.getObject(keyString);
		} else if (throwExceptionIfKeyNotFound) {
			String message = "term not defined " + keyString + " in locale " + context.getLocale();
			// System.out.println(message);
			// termsTranslator.containsKey(keyString);// debug
			throw new IllegalStateException(message);
		} else {
			return null;
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy