1 16 package net.sf.dozer.util.mapping.util; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import net.sf.dozer.util.mapping.fieldmap.ClassMap; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 34 public class ClassMapFinder { 35 36 private static final Log log = LogFactory.getLog(ClassMapFinder.class); 37 38 public ClassMap findClassMap(Map customMappings, Object sourceObj, Class destClass, String mapId, boolean isInstance) { 39 ClassMap mapping = (ClassMap) customMappings.get(ClassMapKeyFactory.createKey(sourceObj.getClass(), destClass, 40 mapId)); 41 42 if (mapping == null) { 48 mapping = findInterfaceOrAbstractMapping(customMappings, destClass, sourceObj, mapId); 49 } 50 51 if (mapping != null) { 52 return mapping; 53 } 54 55 if (mapId != null && mapping == null) { 58 Iterator iter = customMappings.keySet().iterator(); 60 ClassMap classMap = null; 61 while (iter.hasNext()) { 62 String key = (String ) iter.next(); 63 classMap = (ClassMap) customMappings.get(key); 64 if (StringUtils.equals(classMap.getMapId(), mapId)) { 65 return classMap; 66 } 67 } 68 log.warn("No ClassMap found for mapId:" + mapId); 69 } 70 71 return mapping; 72 } 73 74 public List findInterfaceMappings(Map customMappings, Class sourceClass, Class destClass) { 75 Class [] sourceInterfaces = sourceClass.getInterfaces(); 79 Class [] destInterfaces = destClass.getInterfaces(); 80 List interfaceMaps = new ArrayList (); 81 int size = destInterfaces.length; 82 for (int i = 0; i < size; i++) { 83 ClassMap interfaceClassMap = (ClassMap) customMappings.get(ClassMapKeyFactory.createKey(sourceClass, 85 destInterfaces[i])); 86 if (interfaceClassMap != null) { 87 interfaceMaps.add(interfaceClassMap); 88 } 89 } 90 91 for (int i = 0; i < sourceInterfaces.length; i++) { 92 ClassMap interfaceClassMap = (ClassMap) customMappings.get(ClassMapKeyFactory.createKey(sourceInterfaces[i], 94 destClass)); 95 if (interfaceClassMap != null) { 96 interfaceMaps.add(interfaceClassMap); 97 } 98 } 99 100 Collections.reverse(interfaceMaps); 103 104 return interfaceMaps; 105 } 106 107 public ClassMap findInterfaceOrAbstractMapping(Map customMappings, Class destClass, Object sourceObj, String mapId) { 113 ClassMap newClassMap = null; 114 Class newClass = null; 115 Object [] keys = customMappings.keySet().toArray(); 119 for (int i = 0; i < keys.length; i++) { 120 ClassMap map = (ClassMap) customMappings.get(keys[i]); 121 Class dest = map.getDestClass().getClassToMap(); 122 123 if (destClass.isAssignableFrom(dest) 129 && (map.getSourceClass().getClassToMap().isAssignableFrom(sourceObj.getClass()) || map.getSourceClass() 130 .getClassToMap().isInstance(sourceObj)) 131 && (newClass == null || newClass.isAssignableFrom(dest)) 133 && (mapId == null || ((String ) keys[i]).endsWith(mapId))) { 134 newClassMap = map; 135 newClass = dest; 136 } 137 } 138 return newClassMap; 139 } 140 141 } | Popular Tags |