1 package com.thoughtworks.xstream.mapper; 2 3 import com.thoughtworks.xstream.alias.ClassMapper; 4 5 public abstract class MapperWrapper implements ClassMapper { 6 7 private final ClassMapper wrapped; 8 9 public MapperWrapper(ClassMapper wrapped) { 10 this.wrapped = wrapped; 11 } 12 13 public String serializedClass(Class type) { 14 return wrapped.serializedClass(type); 15 } 16 17 public Class realClass(String elementName) { 18 return wrapped.realClass(elementName); 19 } 20 21 public String serializedMember(Class type, String memberName) { 22 return wrapped.serializedMember(type, memberName); 23 } 24 25 public String realMember(Class type, String serialized) { 26 return wrapped.realMember(type, serialized); 27 } 28 29 public String mapNameFromXML(String xmlName) { 30 return wrapped.mapNameFromXML(xmlName); 31 } 32 33 public String mapNameToXML(String javaName) { 34 return wrapped.mapNameToXML(javaName); 35 } 36 37 public boolean isImmutableValueType(Class type) { 38 return wrapped.isImmutableValueType(type); 39 } 40 41 public Class defaultImplementationOf(Class type) { 42 return wrapped.defaultImplementationOf(type); 43 } 44 45 public String attributeForClassDefiningField() { 46 return wrapped.attributeForClassDefiningField(); 47 } 48 49 public String attributeForImplementationClass() { 50 return wrapped.attributeForImplementationClass(); 51 } 52 53 public String attributeForReadResolveField() { 54 return wrapped.attributeForReadResolveField(); 55 } 56 57 public String attributeForEnumType() { 58 return wrapped.attributeForEnumType(); 59 } 60 61 public String getFieldNameForItemTypeAndName(Class definedIn, Class itemType, String itemFieldName) { 62 return wrapped.getFieldNameForItemTypeAndName(definedIn, itemType, itemFieldName); 63 } 64 65 public Class getItemTypeForItemFieldName(Class definedIn, String itemFieldName) { 66 return wrapped.getItemTypeForItemFieldName(definedIn, itemFieldName); 67 } 68 69 public ImplicitCollectionMapping getImplicitCollectionDefForFieldName(Class itemType, String fieldName) { 70 return wrapped.getImplicitCollectionDefForFieldName(itemType, fieldName); 71 } 72 73 public boolean shouldSerializeMember(Class definedIn, String fieldName) { 74 return wrapped.shouldSerializeMember(definedIn, fieldName); 75 } 76 77 80 public Class lookupDefaultType(Class baseType) { 81 return defaultImplementationOf(baseType); 82 } 83 84 public String lookupName(Class type) { 85 return serializedClass(type); 86 } 87 88 public Class lookupType(String elementName) { 89 return realClass(elementName); 90 } 91 92 97 public void alias(String elementName, Class type, Class defaultImplementation) { 98 ClassAliasingMapper classAliasingMapper = (ClassAliasingMapper) findWrapped(ClassAliasingMapper.class); 99 if (classAliasingMapper == null) { 100 throw new UnsupportedOperationException ("ClassMapper.alias() longer supported. Use ClassAliasingMapper.alias() instead."); 101 } else { 102 classAliasingMapper.addClassAlias(elementName, type); 103 } 104 if (defaultImplementation != null && defaultImplementation != type) { 105 DefaultImplementationsMapper defaultImplementationsMapper = (DefaultImplementationsMapper) findWrapped(DefaultImplementationsMapper.class); 106 if (defaultImplementationsMapper == null) { 107 throw new UnsupportedOperationException ("ClassMapper.alias() longer supported. Use DefaultImplementatoinsMapper.add() instead."); 108 } else { 109 defaultImplementationsMapper.addDefaultImplementation(defaultImplementation, type); 110 } 111 } 112 } 113 114 private ClassMapper findWrapped(Class typeOfMapper) { 115 ClassMapper current = this; 116 while (true) { 117 if (current.getClass().isAssignableFrom(typeOfMapper)) { 118 return current; 119 } else if (current instanceof MapperWrapper) { 120 MapperWrapper wrapper = (MapperWrapper) current; 121 current = wrapper.wrapped; 122 } else { 123 return null; 124 } 125 } 126 } 127 } 128 | Popular Tags |