net.lightoze.gwt.i18n.server.ConstantsProxy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gwt-i18n-server Show documentation
Show all versions of gwt-i18n-server Show documentation
A project to make GWT localization available on server side.
package net.lightoze.gwt.i18n.server;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.i18n.client.LocalizableResource;
import org.slf4j.Logger;
import java.lang.reflect.Method;
import java.util.*;
/**
* @author Vladimir Kulev
*/
public class ConstantsProxy extends LocaleProxy {
private Map descriptors = new HashMap();
protected ConstantsProxy(Class extends LocalizableResource> cls, Logger log, Locale locale) {
super(cls, log, locale);
}
@SuppressWarnings("unchecked")
protected static T cast(String value, Class target) {
if (target.isAssignableFrom(Boolean.TYPE) || target.isAssignableFrom(Boolean.class)) {
return (T) Boolean.valueOf(value);
} else if (target.isAssignableFrom(Double.TYPE) || target.isAssignableFrom(Double.class)) {
return (T) Double.valueOf(value);
} else if (target.isAssignableFrom(Float.TYPE) || target.isAssignableFrom(Float.class)) {
return (T) Float.valueOf(value);
} else if (target.isAssignableFrom(Integer.TYPE) || target.isAssignableFrom(Integer.class)) {
return (T) Integer.valueOf(value);
} else if (target.isArray() && target.getComponentType().isAssignableFrom(String.class)) {
return (T) value.split("\\s*(? 0) {
throw new IllegalArgumentException();
}
ConstantDescriptor desc = getDescriptor(method);
Map properties = getProperties();
Object returnValue;
if (method.getReturnType().isAssignableFrom(Map.class)) {
Collection keys;
Map defaultMap = (Map) desc.defaultValue;
if (properties.containsKey(desc.key)) {
keys = Arrays.asList(cast(properties.get(desc.key), String[].class));
} else if (defaultMap != null) {
keys = defaultMap.keySet();
} else {
logMissingKey(desc.key);
return null;
}
Map map = new HashMap(keys.size());
for (String key : keys) {
if (properties.containsKey(key)) {
map.put(key, properties.get(key));
} else {
String value = defaultMap != null ? defaultMap.get(key) : null;
if (value == null) {
logMissingKey(key);
}
map.put(key, value);
}
}
return map;
}
if (properties.containsKey(desc.key)) {
returnValue = cast(properties.get(desc.key), method.getReturnType());
} else {
if (desc.defaultValue == null) {
logMissingKey(desc.key);
}
returnValue = desc.defaultValue;
}
return returnValue;
}
private void logMissingKey(String key) {
log.error(String.format("Unlocalized key '%s' for locale '%s'", key, getCurrentLocale()));
}
private synchronized ConstantDescriptor getDescriptor(Method method) {
ConstantDescriptor desc = descriptors.get(method);
if (desc == null) {
desc = new ConstantDescriptor();
desc.key = getKey(method);
{
Constants.DefaultBooleanValue annotation = method.getAnnotation(Constants.DefaultBooleanValue.class);
if (annotation != null) {
desc.defaultValue = annotation.value();
}
}
{
Constants.DefaultDoubleValue annotation = method.getAnnotation(Constants.DefaultDoubleValue.class);
if (annotation != null) {
desc.defaultValue = annotation.value();
}
}
{
Constants.DefaultFloatValue annotation = method.getAnnotation(Constants.DefaultFloatValue.class);
if (annotation != null) {
desc.defaultValue = annotation.value();
}
}
{
Constants.DefaultIntValue annotation = method.getAnnotation(Constants.DefaultIntValue.class);
if (annotation != null) {
desc.defaultValue = annotation.value();
}
}
{
Constants.DefaultStringArrayValue annotation = method.getAnnotation(Constants.DefaultStringArrayValue.class);
if (annotation != null) {
desc.defaultValue = annotation.value();
}
}
{
Constants.DefaultStringMapValue annotation = method.getAnnotation(Constants.DefaultStringMapValue.class);
if (annotation != null) {
String[] values = annotation.value();
Map map = new HashMap();
for (int i = 0; (i + 1) < values.length; i += 2) {
map.put(values[i], values[i + 1]);
}
desc.defaultValue = map;
}
}
{
Constants.DefaultStringValue annotation = method.getAnnotation(Constants.DefaultStringValue.class);
if (annotation != null) {
desc.defaultValue = annotation.value();
}
}
descriptors.put(method, desc);
}
return desc;
}
private class ConstantDescriptor {
String key;
Object defaultValue;
}
}