1 package com.thoughtworks.xstream.mapper; 2 3 import com.thoughtworks.xstream.alias.ClassMapper; 4 5 import java.util.Map ; 6 import java.util.Collections ; 7 import java.util.HashMap ; 8 9 14 public class ClassAliasingMapper extends MapperWrapper { 15 16 protected final Map typeToNameMap = Collections.synchronizedMap(new HashMap ()); 17 protected final Map nameToTypeMap = Collections.synchronizedMap(new HashMap ()); 18 19 public ClassAliasingMapper(ClassMapper wrapped) { 20 super(wrapped); 21 } 22 23 public void addClassAlias(String name, Class type) { 24 nameToTypeMap.put(name, type.getName()); 25 typeToNameMap.put(type.getName(), name); 26 } 27 28 public String serializedClass(Class type) { 29 String name = super.serializedClass(type); 30 String alias = (String ) typeToNameMap.get(type.getName()); 31 if (alias != null) { 32 return alias; 33 } else { 34 return name; 35 } 36 } 37 38 public Class realClass(String elementName) { 39 if (elementName.equals("null")) { return null; 41 } 42 43 String mappedName = (String ) nameToTypeMap.get(mapNameFromXML(elementName)); 44 45 if (mappedName != null) { 46 elementName = mappedName; 47 } 48 49 return super.realClass(elementName); 50 } 51 52 } 53 | Popular Tags |