KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > core > DefaultConverterLookup


1 package com.thoughtworks.xstream.core;
2
3 import com.thoughtworks.xstream.alias.ClassMapper;
4 import com.thoughtworks.xstream.converters.ConversionException;
5 import com.thoughtworks.xstream.converters.Converter;
6 import com.thoughtworks.xstream.converters.ConverterLookup;
7 import com.thoughtworks.xstream.converters.basic.*;
8 import com.thoughtworks.xstream.converters.collections.*;
9 import com.thoughtworks.xstream.converters.extended.JavaClassConverter;
10 import com.thoughtworks.xstream.converters.extended.JavaMethodConverter;
11 import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
12 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
13
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.math.BigDecimal JavaDoc;
17 import java.math.BigInteger JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.*;
20
21 public class DefaultConverterLookup implements ConverterLookup {
22
23     private LinkedList converters = new LinkedList();
24     private Converter nullConverter = new NullConverter();
25     private HashMap typeToConverterMap = new HashMap();
26     private ReflectionProvider reflectionProvider;
27     private ClassMapper classMapper;
28     private String JavaDoc classAttributeIdentifier;
29
30     public DefaultConverterLookup(ReflectionProvider reflectionProvider,
31                                   ClassMapper classMapper,
32                                   String JavaDoc classAttributeIdentifier) {
33         this.reflectionProvider = reflectionProvider;
34         this.classMapper = classMapper;
35         this.classAttributeIdentifier = classAttributeIdentifier;
36     }
37
38     public Converter lookupConverterForType(Class JavaDoc type) {
39         if (type == null) {
40             return nullConverter;
41         }
42         Converter cachedConverter = (Converter) typeToConverterMap.get(type);
43         if (cachedConverter != null) return cachedConverter;
44         type = classMapper.lookupDefaultType(type);
45         for (Iterator iterator = converters.iterator(); iterator.hasNext();) {
46             Converter converter = (Converter) iterator.next();
47             if (converter.canConvert(type)) {
48                 typeToConverterMap.put(type, converter);
49                 return converter;
50             }
51         }
52         throw new ConversionException("No converter specified for " + type);
53     }
54
55     public void registerConverter(Converter converter) {
56         converters.addFirst(converter);
57     }
58
59     public void setupDefaults() {
60         alias("null", ClassMapper.Null.class);
61         alias("int", Integer JavaDoc.class);
62         alias("float", Float JavaDoc.class);
63         alias("double", Double JavaDoc.class);
64         alias("long", Long JavaDoc.class);
65         alias("short", Short JavaDoc.class);
66         alias("char", Character JavaDoc.class);
67         alias("byte", Byte JavaDoc.class);
68         alias("boolean", Boolean JavaDoc.class);
69         alias("number", Number JavaDoc.class);
70         alias("object", Object JavaDoc.class);
71         alias("dynamic-proxy", ClassMapper.DynamicProxy.class);
72         alias("big-int", BigInteger JavaDoc.class);
73         alias("big-decimal", BigDecimal JavaDoc.class);
74
75         alias("string-buffer", StringBuffer JavaDoc.class);
76         alias("string", String JavaDoc.class);
77         alias("java-class", Class JavaDoc.class);
78         alias("method", Method JavaDoc.class);
79         alias("constructor", Constructor JavaDoc.class);
80         alias("date", Date.class);
81         alias("url", URL JavaDoc.class);
82         alias("bit-set", BitSet.class);
83
84         alias("map", Map.class, HashMap.class);
85         alias("properties", Properties.class);
86         alias("list", List.class, ArrayList.class);
87         alias("set", Set.class, HashSet.class);
88
89         alias("linked-list", LinkedList.class);
90         alias("vector", Vector.class);
91         alias("tree-map", TreeMap.class);
92         alias("tree-set", TreeSet.class);
93         alias("hashtable", Hashtable.class);
94
95         if (JVM.is14()) {
96             alias("linked-hash-map", JVM.loadClass("java.util.LinkedHashMap"));
97             alias("linked-hash-set", JVM.loadClass("java.util.LinkedHashSet"));
98         }
99
100         registerConverter(new ReflectionConverter(classMapper, classAttributeIdentifier, "defined-in", reflectionProvider));
101
102         registerConverter(new IntConverter());
103         registerConverter(new FloatConverter());
104         registerConverter(new DoubleConverter());
105         registerConverter(new LongConverter());
106         registerConverter(new ShortConverter());
107         registerConverter(new CharConverter());
108         registerConverter(new BooleanConverter());
109         registerConverter(new ByteConverter());
110
111         registerConverter(new StringConverter());
112         registerConverter(new StringBufferConverter());
113         registerConverter(new DateConverter());
114         registerConverter(new JavaClassConverter());
115         registerConverter(new JavaMethodConverter());
116         registerConverter(new BitSetConverter());
117         registerConverter(new URLConverter());
118         registerConverter(new BigIntegerConverter());
119         registerConverter(new BigDecimalConverter());
120
121         registerConverter(new ArrayConverter(classMapper, classAttributeIdentifier));
122         registerConverter(new CharArrayConverter());
123         registerConverter(new CollectionConverter(classMapper, classAttributeIdentifier));
124         registerConverter(new MapConverter(classMapper, classAttributeIdentifier));
125         registerConverter(new TreeMapConverter(classMapper, classAttributeIdentifier));
126         registerConverter(new TreeSetConverter(classMapper, classAttributeIdentifier));
127         registerConverter(new PropertiesConverter());
128     }
129
130     public void alias(String JavaDoc elementName, Class JavaDoc type, Class JavaDoc defaultImplementation) {
131         classMapper.alias(elementName, type, defaultImplementation);
132     }
133
134     public void alias(String JavaDoc elementName, Class JavaDoc type) {
135         alias(elementName, type, type);
136     }
137
138     public String JavaDoc getClassAttributeIdentifier() {
139         return classAttributeIdentifier;
140     }
141
142 }
143
Popular Tags