Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package play.data.binding;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.*;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import play.Logger;
import play.Play;
import play.data.Upload;
import play.data.binding.types.*;
import play.data.validation.Validation;
import play.db.Model;
import play.exceptions.UnexpectedException;
import play.utils.Utils;
/**
* The binder try to convert String values to Java objects.
*/
public class Binder {
static final Map, TypeBinder>> supportedTypes = new HashMap, TypeBinder>>();
// TODO: something a bit more dynamic? The As annotation allows you to inject your own binder
static {
supportedTypes.put(Date.class, new DateBinder());
supportedTypes.put(DateTime.class, new DateTimeBinder());
supportedTypes.put(File.class, new FileBinder());
supportedTypes.put(File[].class, new FileArrayBinder());
supportedTypes.put(Model.BinaryField.class, new BinaryBinder());
supportedTypes.put(Upload.class, new UploadBinder());
supportedTypes.put(Upload[].class, new UploadArrayBinder());
supportedTypes.put(Calendar.class, new CalendarBinder());
supportedTypes.put(Locale.class, new LocaleBinder());
supportedTypes.put(byte[].class, new ByteArrayBinder());
supportedTypes.put(byte[][].class, new ByteArrayArrayBinder());
}
public static void register(Class clazz, TypeBinder typeBinder) {
supportedTypes.put(clazz, typeBinder);
}
static Map, BeanWrapper> beanwrappers = new HashMap, BeanWrapper>();
static BeanWrapper getBeanWrapper(Class> clazz) {
if (!beanwrappers.containsKey(clazz)) {
BeanWrapper beanwrapper = new BeanWrapper(clazz);
beanwrappers.put(clazz, beanwrapper);
}
return beanwrappers.get(clazz);
}
public final static Object MISSING = new Object();
public final static Object NO_BINDING = new Object();
@SuppressWarnings("unchecked")
static Object bindInternal(String name, Class clazz, Type type, Annotation[] annotations, Map params, String suffix, String[] profiles) {
try {
Logger.trace("bindInternal: name [" + name + "] suffix [" + suffix + "]");
String[] value = params.get(name + suffix);
Logger.trace("bindInternal: value [" + value + "]");
Logger.trace("bindInternal: profile [" + Utils.join(profiles, ",") + "]");
// Let see if we have a BindAs annotation and a separator. If so, we need to split the values
// Look up for the BindAs annotation. Extract the profile if there is any.
if (annotations != null) {
for (Annotation annotation : annotations) {
if ((clazz.isArray() || Collection.class.isAssignableFrom(clazz)) && value != null && value.length > 0 && annotation.annotationType().equals(As.class)) {
As as = ((As) annotation);
final String separator = as.value()[0];
value = value[0].split(separator);
}
if (annotation.annotationType().equals(NoBinding.class)) {
NoBinding bind = ((NoBinding) annotation);
String[] localUnbindProfiles = bind.value();
Logger.trace("bindInternal: localUnbindProfiles [" + Utils.join(localUnbindProfiles, ",") + "]");
if (localUnbindProfiles != null && contains(profiles, localUnbindProfiles)) {
return NO_BINDING;
}
}
}
}
// Arrays types
// The array condition is not so nice... We should find another way of doing this....
if (clazz.isArray() && (clazz != byte[].class && clazz != byte[][].class && clazz != File[].class && clazz != Upload[].class)) {
if (value == null) {
value = params.get(name + suffix + "[]");
}
if (value == null) {
return MISSING;
}
Object r = Array.newInstance(clazz.getComponentType(), value.length);
for (int i = 0; i <= value.length; i++) {
try {
Array.set(r, i, directBind(name, annotations, value[i], clazz.getComponentType()));
} catch (Exception e) {
// ?? One item was bad
}
}
return r;
}
// Enums
if (Enum.class.isAssignableFrom(clazz)) {
if (value == null || value.length == 0) {
return MISSING;
} else if (StringUtils.isEmpty(value[0])) {
return null;
}
return Enum.valueOf(clazz, value[0]);
}
// Map
if (Map.class.isAssignableFrom(clazz)) {
Class keyClass = String.class;
Class valueClass = String.class;
if (type instanceof ParameterizedType) {
keyClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
valueClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[1];
}
// Special case Map
// Multivalues composite params are binded to a Map
// see http://play.lighthouseapp.com/projects/57987/tickets/443
if (keyClass==String.class && valueClass==String.class && isComposite(name, params)) {
Map stringMap = Utils.filterParams(params, name);
if (stringMap.size()>0) return stringMap;
}
// Search for all params
Map