1 16 package net.sf.dozer.util.mapping.util; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.net.URL ; 22 import java.util.Iterator ; 23 24 import net.sf.dozer.util.mapping.MappingException; 25 import net.sf.dozer.util.mapping.fieldmap.ClassMap; 26 import net.sf.dozer.util.mapping.fieldmap.CopyByReference; 27 import net.sf.dozer.util.mapping.fieldmap.Field; 28 import net.sf.dozer.util.mapping.fieldmap.FieldMap; 29 30 34 public class MappingValidator { 35 36 private final MappingUtils mappingUtils = new MappingUtils(); 37 private final CollectionUtils collectionUtils = new CollectionUtils(); 38 39 public void validateMappingRequest(Object srcObj) { 40 if (srcObj == null) { 41 throw new MappingException("source object must not be null"); 42 } 43 } 44 45 public void validateMappingRequest(Object srcObj, Object destObj) { 46 if (srcObj == null) { 47 throw new MappingException("source object must not be null"); 48 } 49 if (destObj == null) { 50 throw new MappingException("destination object must not be null"); 51 } 52 } 53 54 public void validateMappingRequest(Object srcObj, Class destClass) { 55 if (srcObj == null) { 56 throw new MappingException("source object must not be null"); 57 } 58 if (destClass == null) { 59 throw new MappingException("destination class must not be null"); 60 } 61 } 62 63 public void validateCopyByReference(FieldMap fieldMap, ClassMap classMap) throws NoSuchMethodException , 64 ClassNotFoundException , NoSuchFieldException { 65 String destFieldTypeName = null; 66 if (classMap.getConfiguration().getCopyByReferences() != null) { 67 Iterator copyIterator = classMap.getConfiguration().getCopyByReferences().getCopyByReferences().iterator(); 68 Class clazz = fieldMap.getDestFieldType(classMap.getDestClass().getClassToMap()); 69 if (clazz != null) { 70 destFieldTypeName = clazz.getName(); 71 } 72 while (copyIterator.hasNext()) { 73 CopyByReference copyByReference = (CopyByReference) copyIterator.next(); 74 if (copyByReference.getReferenceName().equals(destFieldTypeName) && !fieldMap.getCopyByReferenceOveridden()) { 75 fieldMap.setCopyByReference(true); 76 } 77 } 78 } 79 } 80 81 public void validateFieldMapping(FieldMap fm, ClassMap classMap) { 82 Field srcField = fm.getSourceField(); 83 Field destField = fm.getDestField(); 84 if (srcField == null) { 85 throw new MappingException("src field must be specified"); 86 } 87 if (destField == null) { 88 throw new MappingException("dest field must be specified"); 89 } 90 } 91 92 public Object validateField(FieldMap fieldMap, Object destObj, Class destFieldType) throws InvocationTargetException , 93 IllegalAccessException , InstantiationException , NoSuchMethodException , ClassNotFoundException , 94 NoSuchFieldException { 95 Object field = null; 96 if (destObj == null) { 98 return null; 99 } 100 if (!fieldMap.isGenericFieldMap()) { 101 } else { 102 field = fieldMap.getDestinationObject(destObj); 105 } 106 if (field != null) { 110 if (collectionUtils.isList(field.getClass()) || collectionUtils.isArray(field.getClass()) 111 || collectionUtils.isSet(field.getClass()) || mappingUtils.isSupportedMap(field.getClass())) { 112 if (collectionUtils.isList(destFieldType) || collectionUtils.isArray(destFieldType) 113 || collectionUtils.isSet(destFieldType) || mappingUtils.isSupportedMap(destFieldType)) { 114 } else { 116 field = null; 120 } 121 } 122 } 123 return field; 124 } 125 126 public URL validateURL(String fileName) { 127 Loader loader = new Loader(); 128 URL url = loader.getResource(fileName); 129 if (url == null) { 130 throw new MappingException("Unable to locate dozer mapping file [" + fileName + "] in the classpath!!!"); 131 } 132 133 InputStream stream = null; 134 try { 135 stream = url.openStream(); 136 } catch (IOException e) { 137 throw new MappingException("Unable to open URL input stream for dozer mapping file [" + url + "]"); 138 } finally { 139 if (stream != null) { 140 try { 141 stream.close(); 142 } catch (IOException e) { 143 throw new MappingException("Unable to close input stream for dozer mapping file [" + url + "]"); 144 } 145 } 146 } 147 148 return url; 149 } 150 } 151 | Popular Tags |