KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.core;
2
3 import com.thoughtworks.xstream.alias.CannotResolveClassException;
4 import com.thoughtworks.xstream.alias.ClassMapper;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.lang.reflect.Proxy JavaDoc;
9
10 public class DefaultClassMapper implements ClassMapper {
11
12     protected Map JavaDoc typeToNameMap = new HashMap JavaDoc();
13     protected Map JavaDoc nameToTypeMap = new HashMap JavaDoc();
14     protected Map JavaDoc baseTypeToDefaultTypeMap = new HashMap JavaDoc();
15     private Map JavaDoc lookupTypeCache = new HashMap JavaDoc();
16
17     public DefaultClassMapper() {
18         // register primitive types
19
baseTypeToDefaultTypeMap.put(boolean.class, Boolean JavaDoc.class);
20         baseTypeToDefaultTypeMap.put(char.class, Character JavaDoc.class);
21         baseTypeToDefaultTypeMap.put(int.class, Integer JavaDoc.class);
22         baseTypeToDefaultTypeMap.put(float.class, Float JavaDoc.class);
23         baseTypeToDefaultTypeMap.put(double.class, Double JavaDoc.class);
24         baseTypeToDefaultTypeMap.put(short.class, Short JavaDoc.class);
25         baseTypeToDefaultTypeMap.put(byte.class, Byte JavaDoc.class);
26         baseTypeToDefaultTypeMap.put(long.class, Long JavaDoc.class);
27     }
28
29     public String JavaDoc mapNameToXML( String JavaDoc javaName ) {
30         return javaName;
31     }
32
33     public String JavaDoc mapNameFromXML( String JavaDoc xmlName ) {
34         return xmlName;
35     }
36
37     public void alias(String JavaDoc elementName, Class JavaDoc type, Class JavaDoc defaultImplementation) {
38         nameToTypeMap.put(elementName, type.getName());
39         typeToNameMap.put(type, elementName);
40         if (!type.equals(defaultImplementation)) {
41             typeToNameMap.put(defaultImplementation, elementName);
42         }
43         baseTypeToDefaultTypeMap.put(type, defaultImplementation);
44     }
45
46     public String JavaDoc lookupName(Class JavaDoc type) {
47         boolean isArray = type.isArray();
48         if (type.isArray()) {
49             type = type.getComponentType();
50         }
51         String JavaDoc result = (String JavaDoc) typeToNameMap.get(type);
52         if (result == null && Proxy.isProxyClass(type)) {
53             result = (String JavaDoc) typeToNameMap.get(DynamicProxy.class);
54         }
55         if (result == null) {
56             // the $ used in inner class names is illegal as an xml element getNodeName
57
result = type.getName().replace('$', '-');
58             if (result.charAt(0) == '-') {
59                 // special case for classes named $Blah with no package; <-Blah> is illegal XML
60
result = "default" + result;
61             }
62         }
63         if (isArray) {
64             result += "-array";
65         }
66         return result;
67     }
68
69     /** Lookup table for primitive types. */
70     private static Class JavaDoc primitiveClassNamed(String JavaDoc name) {
71         return
72             name.equals("void") ? Void.TYPE :
73             name.equals("boolean") ? Boolean.TYPE :
74             name.equals("byte") ? Byte.TYPE :
75             name.equals("char") ? Character.TYPE :
76             name.equals("short") ? Short.TYPE :
77             name.equals("int") ? Integer.TYPE :
78             name.equals("long") ? Long.TYPE :
79             name.equals("float") ? Float.TYPE :
80             name.equals("double") ? Double.TYPE :
81             null;
82     }
83
84     public Class JavaDoc lookupType(String JavaDoc elementName) {
85         final String JavaDoc key = elementName;
86         if (elementName.equals("null")) {
87             return null;
88         }
89         if (lookupTypeCache.containsKey(key)) {
90             return (Class JavaDoc) lookupTypeCache.get(key);
91         }
92
93         boolean isArray = elementName.endsWith("-array");
94         
95         Class JavaDoc primvCls = null;
96         if (isArray) {
97             elementName = elementName.substring(0, elementName.length() - 6); // cut off -array
98

99             // try to determine if the array type is a primitive
100
primvCls = primitiveClassNamed(elementName);
101         }
102         
103         String JavaDoc mappedName = null;
104
105         // only look for a mappedName if no primitive array type has been found
106
if (primvCls == null) {
107             mappedName = (String JavaDoc) nameToTypeMap.get(mapNameFromXML(elementName));
108         }
109         
110         if (mappedName != null) {
111             elementName = mappedName;
112         }
113
114         
115         // the $ used in inner class names is illegal as an xml element getNodeName
116
elementName = elementName.replace('-', '$');
117         if (elementName.startsWith("default$")) {
118             // special case for classes named $Blah with no package; <-Blah> is illegal XML
119
elementName = elementName.substring(7);
120         }
121
122         Class JavaDoc result;
123
124         try {
125             if (isArray) {
126                 
127                 // if a primitive array type exists, return its array
128
if (primvCls != null) {
129                     result =
130                         (primvCls == boolean.class) ? boolean[].class :
131                         (primvCls == byte.class) ? byte[].class :
132                         (primvCls == char.class) ? char[].class :
133                         (primvCls == short.class) ? short[].class :
134                         (primvCls == int.class) ? int[].class :
135                         (primvCls == long.class) ? long[].class :
136                         (primvCls == float.class) ? float[].class :
137                         (primvCls == double.class) ? double[].class :
138                         null;
139                         
140                 // otherwise look it up like normal
141
} else {
142                      result = Class.forName("[L" + elementName + ";");
143                 }
144             } else {
145                 result = Class.forName(elementName);
146             }
147         } catch (ClassNotFoundException JavaDoc e) {
148             throw new CannotResolveClassException(elementName);
149         }
150         lookupTypeCache.put(key, result);
151         return result;
152     }
153
154     public Class JavaDoc lookupDefaultType(Class JavaDoc baseType) {
155         Class JavaDoc result = (Class JavaDoc) baseTypeToDefaultTypeMap.get(baseType);
156         return result == null ? baseType : result;
157     }
158
159 }
160
Popular Tags