KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > MappingValidator


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Iterator JavaDoc;
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 /**
31  * @author tierney.matt
32  * @author garsombke.franz
33  */

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 JavaDoc srcObj) {
40     if (srcObj == null) {
41       throw new MappingException("source object must not be null");
42     }
43   }
44
45   public void validateMappingRequest(Object JavaDoc srcObj, Object JavaDoc 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 JavaDoc srcObj, Class JavaDoc 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 JavaDoc,
64       ClassNotFoundException JavaDoc, NoSuchFieldException JavaDoc {
65     String JavaDoc destFieldTypeName = null;
66     if (classMap.getConfiguration().getCopyByReferences() != null) {
67       Iterator JavaDoc copyIterator = classMap.getConfiguration().getCopyByReferences().getCopyByReferences().iterator();
68       Class JavaDoc 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 JavaDoc validateField(FieldMap fieldMap, Object JavaDoc destObj, Class JavaDoc destFieldType) throws InvocationTargetException JavaDoc,
93       IllegalAccessException JavaDoc, InstantiationException JavaDoc, NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc,
94       NoSuchFieldException JavaDoc {
95     Object JavaDoc field = null;
96     // verify that the dest obj is not null
97
if (destObj == null) {
98       return null;
99     }
100     if (!fieldMap.isGenericFieldMap()) {
101     } else {
102       // call the getXX method to see if the field is already
103
// instantiated
104
field = fieldMap.getDestinationObject(destObj);
105     }
106     // When we are recursing through a list we need to make sure
107
// that we are not in the list
108
// by checking the destFieldType
109
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           // do nothing
115
} else {
116           // this means the getXX field is a List but we
117
// are actually trying to map one of its
118
// elements
119
field = null;
120         }
121       }
122     }
123     return field;
124   }
125
126   public URL JavaDoc validateURL(String JavaDoc fileName) {
127     Loader loader = new Loader();
128     URL JavaDoc 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 JavaDoc stream = null;
134     try {
135       stream = url.openStream();
136     } catch (IOException JavaDoc 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 JavaDoc 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