KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import net.sf.dozer.util.mapping.fieldmap.ClassMap;
25 import net.sf.dozer.util.mapping.fieldmap.Configuration;
26 import net.sf.dozer.util.mapping.fieldmap.DozerClass;
27 import net.sf.dozer.util.mapping.fieldmap.ExcludeFieldMap;
28 import net.sf.dozer.util.mapping.fieldmap.FieldMap;
29 import net.sf.dozer.util.mapping.fieldmap.GenericFieldMap;
30 import net.sf.dozer.util.mapping.fieldmap.Mappings;
31
32 import org.apache.commons.lang.StringUtils;
33
34 /**
35  * @author garsombke.franz
36  */

37 public class MappingsParser {
38
39   private final MappingUtils mappingUtils = new MappingUtils();
40   private final ReflectionUtils reflectionUtils = new ReflectionUtils();
41   private final MappingValidator mappingValidator = new MappingValidator();
42   private final ClassMapBuilder classMapBuilder = new ClassMapBuilder();
43
44   public Map JavaDoc parseMappings(Mappings mappings) throws IllegalArgumentException JavaDoc {
45     Iterator JavaDoc iterator = null;
46     Map JavaDoc result = new HashMap JavaDoc();
47     FieldMap fieldMapPrime = null;
48     try {
49       // verify that we even have any mappings
50
if (mappings.getMapping() == null || mappings.getMapping().size() == 0) {
51         return result;
52       }
53       Iterator JavaDoc iter = mappings.getMapping().iterator();
54       // need to create bi-directional mappings now.
55
ClassMap classMap = null;
56       ClassMap classMapPrime = null;
57       Set JavaDoc mapIds = new HashSet JavaDoc();
58       while (iter.hasNext()) {
59         classMap = (ClassMap) iter.next();
60         // set the global configuration for each classmap
61
if (mappings.getConfiguration() != null) {
62           classMap.setConfiguration(mappings.getConfiguration());
63         } else {
64           classMap.setConfiguration(new Configuration());
65         }
66
67         // Apply top level config attrs to ClassMap unless it has been overridden
68
if (mappingUtils.isBlankOrNull(classMap.getDateFormat())) {
69           classMap.setDateFormat(classMap.getConfiguration().getDateFormat());
70         }
71
72         if (!classMap.getWildcardOveridden()) {
73           classMap.setWildcard(classMap.getConfiguration().getWildcard());
74         }
75
76         if (!classMap.getStopOnErrorsOveridden()) {
77           classMap.setStopOnErrors(classMap.getConfiguration().getStopOnErrors());
78         }
79         if (classMap.getAllowedExceptions().isEmpty() && classMap.getConfiguration().getAllowedExceptions() != null) {
80             classMap.setAllowedExceptions(classMap.getConfiguration().getAllowedExceptions().getExceptions());
81         }
82
83         if (mappingUtils.isBlankOrNull(classMap.getBeanFactory())) {
84           classMap.setBeanFactory(classMap.getConfiguration().getBeanFactory());
85         }
86
87         // Apply ClassMap(Mapping) attributes to Dest and Source Class obj's unless it has been overridden
88
if (mappingUtils.isBlankOrNull(classMap.getSourceClass().getBeanFactory())) {
89           classMap.getSourceClass().setBeanFactory(classMap.getBeanFactory());
90         }
91
92         if (mappingUtils.isBlankOrNull(classMap.getDestClass().getBeanFactory())) {
93           classMap.getDestClass().setBeanFactory(classMap.getBeanFactory());
94         }
95
96         if (classMap.getSourceClass().getMapNull() == null) {
97           classMap.getSourceClass().setMapNull(Boolean.valueOf(classMap.getMapNull()));
98         }
99
100         if (classMap.getSourceClass().getMapEmptyString() == null) {
101           classMap.getSourceClass().setMapEmptyString(Boolean.valueOf(classMap.getMapEmptyString()));
102         }
103
104         if (classMap.getDestClass().getMapNull() == null) {
105           classMap.getDestClass().setMapNull(Boolean.valueOf(classMap.getMapNull()));
106         }
107
108         if (classMap.getDestClass().getMapEmptyString() == null) {
109           classMap.getDestClass().setMapEmptyString(Boolean.valueOf(classMap.getMapEmptyString()));
110         }
111
112         // add our first class map to the result map
113
// initialize PropertyDescriptor Cache
114
reflectionUtils.getPropertyDescriptor(classMap.getSourceClass().getClassToMap(), "");
115         reflectionUtils.getPropertyDescriptor(classMap.getDestClass().getClassToMap(), "");
116         String JavaDoc theClassMapKey = ClassMapKeyFactory.createKey(classMap.getSourceClass().getClassToMap(), classMap
117             .getDestClass().getClassToMap(), classMap.getMapId());
118
119         /*
120          * Check to see if this is a duplicate mapping. If so, throw an Exception
121          */

122         if (result.containsKey(theClassMapKey)) {
123           throw new IllegalArgumentException JavaDoc("Duplicate Class Mapping Found. Source: "
124               + classMap.getSourceClass().getClassToMap().getName() + " Destination: "
125               + classMap.getDestClass().getClassToMap().getName());
126         }
127         
128         //Check to see if this is a duplicate map id, irregardless of src and dest class names. Duplicate map-ids are
129
//not allowed
130
if (!mappingUtils.isBlankOrNull(classMap.getMapId())) {
131           if (mapIds.contains(classMap.getMapId())) {
132             throw new IllegalArgumentException JavaDoc("Duplicate Map Id's Found. Map Id: " + classMap.getMapId());
133           }
134           mapIds.add(classMap.getMapId());
135         }
136         
137
138         result.put(theClassMapKey, classMap);
139         // now create class map prime
140
classMapPrime = new ClassMap();
141         DozerClass destClass = classMap.getDestClass();
142         DozerClass srcClass = classMap.getSourceClass();
143         classMapPrime.setSourceClass(new DozerClass(destClass.getName(), destClass.getClassToMap(), destClass
144             .getBeanFactory(), destClass.getFactoryBeanId(), destClass.getMapGetMethod(), destClass.getMapSetMethod(),
145             destClass.getMapNull(), destClass.getMapEmptyString()));
146         classMapPrime.setDestClass(new DozerClass(srcClass.getName(), srcClass.getClassToMap(), srcClass
147             .getBeanFactory(), srcClass.getFactoryBeanId(), srcClass.getMapGetMethod(), srcClass.getMapSetMethod(),
148             srcClass.getMapNull(), srcClass.getMapEmptyString()));
149         classMapPrime.setType(classMap.getType());
150         classMapPrime.setWildcard(classMap.isWildcard());
151         classMapPrime.setDateFormat(classMap.getDateFormat());
152         classMapPrime.setStopOnErrors(classMap.getStopOnErrors());
153         classMapPrime.setAllowedExceptions(classMap.getAllowedExceptions());//TODO *NEW*
154
classMapPrime.setConfiguration(classMap.getConfiguration());
155         if (classMap.getSourceClass().getMapGetMethod() != null) {
156           classMap.getSourceClass().setCustomMap(true);
157         }
158         if (classMap.getDestClass().getMapGetMethod() != null) {
159           classMap.getDestClass().setCustomMap(true);
160         }
161         classMapPrime.getSourceClass().setCustomMap(classMap.getDestClass().isCustomMap());
162         classMapPrime.getDestClass().setCustomMap(classMap.getSourceClass().isCustomMap());
163         classMapPrime.getSourceClass().setCreateMethod(classMap.getDestClass().getCreateMethod());
164         classMapPrime.getDestClass().setCreateMethod(classMap.getSourceClass().getCreateMethod());
165         if (StringUtils.isNotEmpty(classMap.getMapId())) {
166           classMapPrime.setMapId(classMap.getMapId());
167         }
168         // if it is mapping map backed object need to create fieldmaps
169
if (StringUtils.isNotEmpty(classMap.getMapId())) {
170           classMapBuilder.addMapDefaultFieldMappings(classMap);
171         }
172         if (classMap.getFieldMaps() != null) {
173           iterator = classMap.getFieldMaps().iterator();
174           // iterate through the fields and see wether or not they should be mapped
175
// one way class mappings we do not need to add any fields
176
if (!StringUtils.equals(classMap.getType(), MapperConstants.ONE_WAY)) {
177             while (iterator.hasNext()) {
178               FieldMap fieldMap = (FieldMap) iterator.next();
179               mappingValidator.validateFieldMapping(fieldMap, classMap);
180               mappingUtils.isMethodMap(fieldMap);
181               mappingUtils.isCustomMap(fieldMap);
182               if (!(StringUtils.equals(fieldMap.getType(), MapperConstants.ONE_WAY) && !(fieldMap instanceof ExcludeFieldMap))) {
183                 // make a prime field map
184
fieldMapPrime = (FieldMap) fieldMap.clone();
185                 // check to see if it is only an exclude one way
186
if (fieldMapPrime instanceof ExcludeFieldMap
187                     && StringUtils.equals(fieldMap.getType(), MapperConstants.ONE_WAY)) {
188                   // need to make a generic field map for the other direction
189
fieldMapPrime = new GenericFieldMap();
190                 }
191                 // reverse the fields
192
mappingUtils.reverseFields(fieldMap, fieldMapPrime);
193                 // determine if we have method mapping
194
mappingUtils.isMethodMap(fieldMapPrime);
195                 mappingUtils.isCustomMap(fieldMapPrime);
196
197                 if (fieldMapPrime instanceof GenericFieldMap && !(fieldMap instanceof ExcludeFieldMap)) {
198                   ((GenericFieldMap) fieldMapPrime).setRelationshipType(((GenericFieldMap) fieldMap)
199                       .getRelationshipType());
200                 }
201                 // reverse the hints
202
fieldMapPrime.setSourceTypeHint(fieldMap.getDestinationTypeHint());
203                 fieldMapPrime.setDestinationTypeHint(fieldMap.getSourceTypeHint());
204                 // iterate through copyByReferences and set accordingly
205
if (!(fieldMap instanceof ExcludeFieldMap)) {
206                   mappingValidator.validateCopyByReference(fieldMap, classMap);
207                 }
208                 if (!(fieldMapPrime instanceof ExcludeFieldMap)) {
209                   mappingValidator.validateCopyByReference(fieldMapPrime, classMapPrime);
210                 }
211               } else { // if it is a one-way field map make the other field map excluded
212
// make a prime field map
213
fieldMapPrime = new ExcludeFieldMap();
214                 mappingUtils.reverseFields(fieldMap, fieldMapPrime);
215               }
216               classMapPrime.addFieldMapping((FieldMap) fieldMapPrime);
217             }
218           } else {
219             // since it is one-way...we still need to validate if it has some type of method mapping and validate the
220
// field maps
221
while (iterator.hasNext()) {
222               FieldMap oneWayFieldMap = (FieldMap) iterator.next();
223               mappingValidator.validateFieldMapping(oneWayFieldMap, classMap);
224               mappingUtils.isMethodMap(oneWayFieldMap);
225               mappingUtils.isCustomMap(oneWayFieldMap);
226               mappingValidator.validateCopyByReference(oneWayFieldMap, classMap);
227               // check to see if we need to exclude the map
228
if ((StringUtils.equals(oneWayFieldMap.getType(), MapperConstants.ONE_WAY))) {
229                 fieldMapPrime = new ExcludeFieldMap();
230                 mappingUtils.reverseFields(oneWayFieldMap, fieldMapPrime);
231                 classMapPrime.addFieldMapping(fieldMapPrime);
232               }
233             }
234           }
235         }
236         // if it is a one way mapping or a method/iterate method mapping we can not bi-directionally map
237
// Map Prime could actually be empty
238
if (!StringUtils.equals(classMap.getType(), MapperConstants.ONE_WAY)) {
239           result.put(ClassMapKeyFactory.createKey(classMap.getDestClass().getClassToMap(), classMap.getSourceClass()
240               .getClassToMap(), classMap.getMapId()), classMapPrime);
241         }
242       }
243     } catch (Throwable JavaDoc t) {
244       mappingUtils.throwMappingException(t);
245     }
246     return result;
247   }
248
249 }
250
Popular Tags