1 16 package net.sf.dozer.util.mapping.util; 17 18 import java.util.Collection ; 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import net.sf.dozer.util.mapping.MappingException; 25 import net.sf.dozer.util.mapping.cache.Cache; 26 import net.sf.dozer.util.mapping.converters.CustomConverterContainer; 27 import net.sf.dozer.util.mapping.fieldmap.Field; 28 import net.sf.dozer.util.mapping.fieldmap.FieldMap; 29 import net.sf.dozer.util.mapping.fieldmap.GenericFieldMap; 30 31 import org.apache.commons.lang.StringUtils; 32 33 39 public class MappingUtils { 40 41 public static Map storedFactories = Collections.synchronizedMap(new HashMap ()); 43 44 private final CollectionUtils collectionUtils = new CollectionUtils(); 45 46 public String getClassNameWithoutPackage(Class clazz) { 47 Package pckage = clazz.getPackage(); 48 int pckageIndex = 0; 49 if (pckage != null) { 50 pckageIndex = pckage.getName().length() + 1; 51 } 52 return clazz.getName().substring(pckageIndex); 53 } 54 55 public boolean isSupportedCollection(Class aClass) { 56 boolean collection = false; 57 if (collectionUtils.isCollection(aClass)) { 58 collection = true; 59 } else if (collectionUtils.isArray(aClass)) { 60 collection = true; 61 } 62 return collection; 63 } 64 65 public boolean isSupportedMap(Class aClass) { 66 return Map .class.isAssignableFrom(aClass); 67 } 68 69 public boolean isCustomMapMethod(FieldMap fieldMap) { 70 return (fieldMap instanceof GenericFieldMap && ((GenericFieldMap) fieldMap).isCustomMap()) ? true : false; 71 } 72 73 public boolean isPrimitiveOrWrapper(Class aClass) { 74 return (aClass.isPrimitive() || Number .class.isAssignableFrom(aClass) || aClass.equals(String .class) 75 || aClass.equals(Character .class) || aClass.equals(Boolean .class) 76 || java.util.Date .class.isAssignableFrom(aClass) || java.util.Calendar .class.isAssignableFrom(aClass)); 77 } 78 79 public void throwMappingException(Throwable e) throws MappingException { 80 if (e instanceof MappingException) { 81 throw (MappingException) e; 83 } else if (e instanceof RuntimeException ){ 84 throw (RuntimeException ) e; 86 } else { 87 throw new MappingException(e); 88 } 89 } 90 91 public boolean isBlankOrNull(String value) { 92 return value == null || value.trim().length() < 1 ? true : false; 93 } 94 95 public void addFactories(Map factories) { 96 storedFactories.putAll(factories); 97 } 98 99 public void isMethodMap(FieldMap fieldMap) { 100 boolean methodMap = false; 101 if (fieldMap.getSourceField().getTheGetMethod() != null || fieldMap.getSourceField().getTheSetMethod() != null 102 || fieldMap.getDestField().getTheGetMethod() != null || fieldMap.getDestField().getTheSetMethod() != null) { 103 methodMap = true; 104 } 105 if (methodMap && fieldMap instanceof GenericFieldMap) { 106 ((GenericFieldMap) fieldMap).setMethodMap(true); 107 } 108 } 109 110 public void isCustomMap(FieldMap fieldMap) { 111 boolean customMap = false; 112 if (fieldMap.getSourceField().getMapGetMethod() != null || fieldMap.getSourceField().getMapSetMethod() != null 113 || fieldMap.getDestField().getMapGetMethod() != null || fieldMap.getDestField().getMapSetMethod() != null) { 114 customMap = true; 115 } 116 if (customMap && fieldMap instanceof GenericFieldMap) { 117 ((GenericFieldMap) fieldMap).setCustomMap(true); 118 } 119 } 120 121 public Throwable getRootCause(Throwable ex) { 122 Throwable rootCause = ex; 123 while (rootCause.getCause() != null) { 124 rootCause = rootCause.getCause(); 125 } 126 return rootCause; 127 } 128 129 public String getParentFieldNameKey(String parentSourceField, Object srcObj, String sourceClassName, String srcFieldReadMethodName, 130 String destFieldReadMethodName, String srcFieldName, String destFieldName) { 131 StringBuffer buf = new StringBuffer (150); 132 buf.append(parentSourceField); 133 buf.append(System.identityHashCode(srcObj)); 134 buf.append(sourceClassName); 135 buf.append(srcFieldReadMethodName); 136 buf.append(destFieldReadMethodName); 137 buf.append(srcFieldName); 138 buf.append(destFieldName); 139 return buf.toString(); 140 } 141 142 public Class findCustomConverter(Cache converterByDestTypeCache, CustomConverterContainer customConverterContainer, Class sourceClass, Class destClass) throws ClassNotFoundException { 143 if (customConverterContainer == null || customConverterContainer.getConverters() == null || customConverterContainer.getConverters().size() < 1) { 144 return null; 145 } 146 147 return customConverterContainer.getCustomConverter(sourceClass, destClass, converterByDestTypeCache); 148 } 149 150 public Class determineCustomConverter(FieldMap fieldMap, Cache converterByDestTypeCache, CustomConverterContainer customConverterContainer, 151 Class sourceClass, Class destClass) throws ClassNotFoundException { 152 if (customConverterContainer == null || customConverterContainer.getConverters() == null || customConverterContainer.getConverters().size() < 1) { 153 return null; 154 } 155 156 if (fieldMap != null && fieldMap.getDestField().isIndexed()) { 159 if (destClass.isArray()) { 160 destClass = destClass.getComponentType(); 161 } else if (destClass.isAssignableFrom(Collection .class)) { 162 if (fieldMap.getDestinationTypeHint() != null && fieldMap.getDestinationTypeHint().getHints().size() < 2) { 164 destClass = Thread.currentThread().getContextClassLoader().loadClass(fieldMap.getDestinationTypeHint().getHintName()); 165 } 166 } 167 } 168 169 return findCustomConverter(converterByDestTypeCache, customConverterContainer, sourceClass, destClass); 170 } 171 172 public void reverseFields(FieldMap source, FieldMap destination) { 173 Field df = new Field(source.getSourceField().getName(), source.getSourceField().getType()); 175 df.setIndexed(source.getSourceField().isIndexed()); 176 df.setIndex(source.getSourceField().getIndex()); 177 178 Field sf = new Field(source.getDestField().getName(), source.getDestField().getType()); 179 sf.setIndexed(source.getDestField().isIndexed()); 180 sf.setIndex(source.getDestField().getIndex()); 181 182 destination.setDestField(df); 183 destination.setSourceField(sf); 184 destination.setCustomConverter(source.getCustomConverter()); 185 186 destination.getDestField().setDateFormat(source.getSourceField().getDateFormat()); 187 destination.getSourceField().setDateFormat(source.getDestField().getDateFormat()); 188 189 destination.getDestField().setTheGetMethod(source.getSourceField().getTheGetMethod()); 190 destination.getDestField().setTheSetMethod(source.getSourceField().getTheSetMethod()); 191 destination.getSourceField().setTheGetMethod(source.getDestField().getTheGetMethod()); 192 destination.getSourceField().setTheSetMethod(source.getDestField().getTheSetMethod()); 193 destination.getDestField().setKey(source.getSourceField().getKey()); 194 destination.getSourceField().setKey(source.getDestField().getKey()); 195 destination.getDestField().setMapGetMethod(source.getSourceField().getMapGetMethod()); 196 destination.getDestField().setMapSetMethod(source.getSourceField().getMapSetMethod()); 197 destination.getSourceField().setMapGetMethod(source.getDestField().getMapGetMethod()); 198 destination.getSourceField().setMapSetMethod(source.getDestField().getMapSetMethod()); 199 destination.getSourceField().setAccessible(source.getDestField().isAccessible()); 200 if (StringUtils.isNotEmpty(destination.getMapId())) { 201 destination.setMapId(source.getMapId()); 202 } 203 destination.getDestField().setCreateMethod(source.getSourceField().getCreateMethod()); 204 destination.getSourceField().setCreateMethod(source.getDestField().getCreateMethod()); 205 } 206 207 public boolean validateMap(Class sourceClass, Class destClass, FieldMap fieldMap) { 208 if (Map .class.isAssignableFrom(sourceClass) || fieldMap.getSourceField().getMapGetMethod() != null || 209 Map .class.isAssignableFrom(destClass) || fieldMap.getDestField().getMapGetMethod() != null) { 210 return true; 211 } else { 212 return false; 213 } 214 } 215 216 public String getMappedFieldKey(Object srcObj) { 217 return srcObj.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(srcObj)); 219 } 220 221 public Object getIndexedValue(Object collection, int index) { 222 Object r = null; 223 if (collection instanceof Object []) { 224 Object [] x = (Object []) collection; 225 if (index < x.length) { 226 return x[index]; 227 } 228 } else if (collection instanceof Collection ) { 229 Collection x = (Collection ) collection; 230 if (index < x.size()) { 231 Iterator iter = x.iterator(); 232 for (int i = 0; i < index; i++) { 233 iter.next(); 234 } 235 r = iter.next(); 236 } 237 } 238 return r; 239 } 240 241 } | Popular Tags |