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

com.googlecode.gwt.test.internal.i18n.LocalizableResourceCreateHandler Maven / Gradle / Ivy

There is a newer version: 0.63
Show newest version
package com.googlecode.gwt.test.internal.i18n;

import com.google.gwt.i18n.client.*;
import com.google.gwt.i18n.client.impl.CldrImpl;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl;
import com.googlecode.gwt.test.GwtCreateHandler;
import com.googlecode.gwt.test.exceptions.GwtTestI18NException;
import com.googlecode.gwt.test.internal.GwtConfig;
import com.googlecode.gwt.test.utils.GwtReflectionUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Class in charge of the instanciation of all {@link LocalizableResource} sub-interfaces through
 * deferred binding. For internal use only.
 *
 * @author Gael Lazzari
 */
public class LocalizableResourceCreateHandler implements GwtCreateHandler {

    private static class LocalizableResourceProxyFactory {

        private static Map factoryMap = new HashMap();

        public static  LocalizableResourceProxyFactory getFactory(
                Class clazz) {
            LocalizableResourceProxyFactory factory = factoryMap.get(clazz.getName());
            if (factory == null) {
                factory = new LocalizableResourceProxyFactory(clazz);
                factoryMap.put(clazz.getName(), factory);
            }

            return factory;
        }

        private final Class proxiedClass;

        private LocalizableResourceProxyFactory(Class proxiedClass) {
            this.proxiedClass = proxiedClass;
        }

        @SuppressWarnings("unchecked")
        public  T createProxy() {
            InvocationHandler ih = createInvocationHandler(proxiedClass);
            return (T) Proxy.newProxyInstance(proxiedClass.getClassLoader(),
                    new Class[]{proxiedClass}, ih);
        }

        private InvocationHandler createInvocationHandler(Class clazz) {
            if (ConstantsWithLookup.class.isAssignableFrom(clazz)) {
                return new ConstantsWithLookupInvocationHandler(clazz);
            }
            if (Constants.class.isAssignableFrom(clazz)) {
                return new ConstantsInvocationHandler(clazz);
            } else if (Messages.class.isAssignableFrom(clazz)) {
                return new MessagesInvocationHandler(clazz);
            } else {
                throw new GwtTestI18NException("Not managed GWT i18n interface for testing : "
                        + clazz.getSimpleName());
            }
        }
    }

    @SuppressWarnings("unchecked")
    public Object create(Class classLiteral) throws Exception {
        if (LocalizableResource.class.isAssignableFrom(classLiteral)) {

            if (!classLiteral.isInterface()) {
                throw new GwtTestI18NException(classLiteral.getSimpleName() + " must be an interface");
            }
            return LocalizableResourceProxyFactory.getFactory(
                    (Class) classLiteral).createProxy();
        } else if (CldrImpl.class == classLiteral) {
            return getLocalizedClassImpl(CldrImpl.class, CldrImpl.class);
        } else if (DateTimeFormatInfoImpl.class == classLiteral) {
            return getLocalizedClassImpl(DateTimeFormatInfoImpl.class, DefaultDateTimeFormatInfo.class);
        }

        return null;

    }

    private Object getLocalizedClassImpl(Class localizedClass, Class defaultImpl)
            throws Exception {
        Locale locale = GwtConfig.get().getModuleRunner().getLocale();
        if (locale == null) {
            return defaultImpl.newInstance();
        }

        Class implementationClass = getLocalizedClassImpl(localizedClass, locale.getLanguage() + "_" + locale.getCountry());
        if (implementationClass == null) {
            implementationClass = getLocalizedClassImpl(localizedClass, locale.getLanguage());
        }

        if (implementationClass == null) {
            implementationClass = getLocalizedClassImpl(localizedClass, locale.getCountry());
        }

        if (implementationClass == null) {
            implementationClass = defaultImpl;
        }

        return implementationClass.newInstance();
    }

    private Class getLocalizedClassImpl(Class localizedClass, String suffix) {
        try {
            return GwtReflectionUtils.getClass(localizedClass.getName() + "_" + suffix);
        } catch (ClassNotFoundException e) {
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy