001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.jsonws;
021:
022: import java.lang.reflect.ParameterizedType;
023: import java.lang.reflect.Type;
024: import java.util.Calendar;
025: import java.util.Date;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: import de.schlund.pfixcore.beans.BeanDescriptorFactory;
031: import de.schlund.pfixcore.webservice.jsonws.deserializers.ArrayDeserializer;
032: import de.schlund.pfixcore.webservice.jsonws.deserializers.BeanDeserializer;
033: import de.schlund.pfixcore.webservice.jsonws.deserializers.BooleanDeserializer;
034: import de.schlund.pfixcore.webservice.jsonws.deserializers.CalendarDeserializer;
035: import de.schlund.pfixcore.webservice.jsonws.deserializers.NumberDeserializer;
036: import de.schlund.pfixcore.webservice.jsonws.deserializers.StringDeserializer;
037:
038: /**
039: * @author mleidig@schlund.de
040: */
041: public class DeserializerRegistry {
042:
043: Map<Class<?>, Deserializer> deserializers;
044: Deserializer beanDeserializer;
045: Deserializer arrayDeserializer;
046:
047: public DeserializerRegistry(BeanDescriptorFactory beanDescFactory) {
048:
049: deserializers = new HashMap<Class<?>, Deserializer>();
050:
051: beanDeserializer = new BeanDeserializer(beanDescFactory);
052: arrayDeserializer = new ArrayDeserializer();
053:
054: deserializers.put(String.class, new StringDeserializer());
055: Deserializer deser = new NumberDeserializer();
056: deserializers.put(byte.class, deser);
057: deserializers.put(Byte.class, deser);
058: deserializers.put(short.class, deser);
059: deserializers.put(Short.class, deser);
060: deserializers.put(int.class, deser);
061: deserializers.put(Integer.class, deser);
062: deserializers.put(long.class, deser);
063: deserializers.put(Long.class, deser);
064: deserializers.put(float.class, deser);
065: deserializers.put(Float.class, deser);
066: deserializers.put(double.class, deser);
067: deserializers.put(Double.class, deser);
068: deser = new BooleanDeserializer();
069: deserializers.put(boolean.class, deser);
070: deserializers.put(Boolean.class, deser);
071: deser = new CalendarDeserializer();
072: deserializers.put(Calendar.class, deser);
073: deserializers.put(Date.class, deser);
074:
075: }
076:
077: public Deserializer getDeserializer(Class<?> clazz) {
078: Deserializer deser = deserializers.get(clazz);
079: if (deser == null) {
080: if (clazz.isArray())
081: deser = arrayDeserializer;
082: else if (List.class.isAssignableFrom(clazz))
083: deser = arrayDeserializer;
084: else
085: deser = beanDeserializer;
086: }
087: return deser;
088: }
089:
090: public Deserializer getDeserializer(Type type) {
091: Class<?> clazz = null;
092: if (type instanceof Class)
093: clazz = (Class<?>) type;
094: else if (type instanceof ParameterizedType) {
095: Type rawType = ((ParameterizedType) type).getRawType();
096: if (rawType instanceof Class)
097: clazz = (Class<?>) rawType;
098: }
099: if (clazz != null)
100: return getDeserializer(clazz);
101: else
102: throw new RuntimeException("Type not supported: "
103: + type.getClass() + " " + type);
104: }
105:
106: }
|