KickJava   Java API By Example, From Geeks To Geeks.

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


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

39 public class ClassMapBuilder {
40   private final ReflectionUtils reflectionUtils = new ReflectionUtils();
41   private final MappingUtils mappingUtils = new MappingUtils();
42   
43   public ClassMap createDefaultClassMap(Configuration globalConfiguration, Class JavaDoc sourceClass, Class JavaDoc destClass) {
44     ClassMap classMap = new ClassMap();
45     classMap.setSourceClass(new DozerClass(sourceClass.getName(), sourceClass, null, null, null, null,
46         Boolean.valueOf(MapperConstants.DEFAULT_MAP_NULL_POLICY), Boolean.valueOf(MapperConstants.DEFAULT_MAP_EMPTY_STRING_POLICY)));
47     classMap.setDestClass(new DozerClass(destClass.getName(), destClass, null, null, null, null,
48         Boolean.valueOf(MapperConstants.DEFAULT_MAP_NULL_POLICY), Boolean.valueOf(MapperConstants.DEFAULT_MAP_EMPTY_STRING_POLICY)));
49
50     if (globalConfiguration == null) {
51       // a default class map inherits the global properties
52
classMap.setWildcard(classMap.getConfiguration().getWildcard());
53       classMap.setStopOnErrors(classMap.getConfiguration().getStopOnErrors());
54       classMap.setDateFormat(classMap.getConfiguration().getDateFormat());
55     } else {
56       classMap.setWildcard(globalConfiguration.getWildcard());
57       classMap.setStopOnErrors(globalConfiguration.getStopOnErrors());
58       classMap.setDateFormat(globalConfiguration.getDateFormat());
59       classMap.setConfiguration(globalConfiguration);
60     }
61     // Add default field mappings if wildcard policy is true
62
if (classMap.isWildcard()) {
63       addDefaultFieldMappings(classMap);
64     }
65
66     return classMap;
67   }
68   
69   public void addDefaultFieldMappings(Map JavaDoc customMappings) {
70     Set JavaDoc entries = customMappings.entrySet();
71     Iterator JavaDoc iter = entries.iterator();
72     while (iter.hasNext()) {
73       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
74       ClassMap classMap = (ClassMap) entry.getValue();
75
76       if (classMap.isWildcard()) {
77         addDefaultFieldMappings(classMap);
78       }
79     }
80   }
81
82   public void addMapDefaultFieldMappings(ClassMap classMap) {
83     Class JavaDoc sourceClass = classMap.getSourceClass().getClassToMap();
84     Class JavaDoc destClass = classMap.getDestClass().getClassToMap();
85     PropertyDescriptor JavaDoc[] properties = null;
86     boolean destIsMap = false;
87     // determine which is the map
88
if (mappingUtils.isSupportedMap(sourceClass) || classMap.getSourceClass().getMapGetMethod() != null) {
89       properties = reflectionUtils.getPropertyDescriptors(destClass);
90       destIsMap = false;
91     } else if (mappingUtils.isSupportedMap(destClass) || classMap.getDestClass().getMapGetMethod() != null) {
92       properties = reflectionUtils.getPropertyDescriptors(sourceClass);
93       destIsMap = true;
94     } else {
95       return;
96     }
97     for (int i = 0; i < properties.length; i++) {
98       String JavaDoc fieldName = properties[i].getName();
99       // if the sourceProperty is null we know that there is not a
100
// corresponding property to map to.
101
if (fieldName.equals("class")) {
102         continue;
103       }
104       MapFieldMap map = new MapFieldMap();
105       if (destIsMap) {
106         map.setSourceField(new Field(fieldName, null));
107         Field df = new Field(MapperConstants.SELF_KEYWORD, null);
108         if (StringUtils.isNotEmpty(classMap.getDestClass().getMapGetMethod())) {
109           df.setMapGetMethod(classMap.getDestClass().getMapGetMethod());
110           df.setTheGetMethod(classMap.getDestClass().getMapGetMethod());
111         } else {
112           df.setMapGetMethod("get");
113           df.setTheGetMethod("get");
114         }
115         if (StringUtils.isNotEmpty(classMap.getDestClass().getMapSetMethod())) {
116           df.setMapSetMethod(classMap.getDestClass().getMapSetMethod());
117           df.setTheSetMethod(classMap.getDestClass().getMapSetMethod());
118         } else {
119           df.setMapSetMethod("put");
120           df.setTheSetMethod("put");
121         }
122         map.setDestField(df);
123         FieldMap fieldMap = classMap.getFieldMapUsingSource(fieldName);
124         // this means we have an existing fieldmap. set default values accordingly
125
if (fieldMap != null && !(fieldMap instanceof ExcludeFieldMap)) {
126           map.getSourceField().setTheGetMethod(fieldMap.getSourceField().getTheGetMethod());
127           map.getSourceField().setTheSetMethod(fieldMap.getSourceField().getTheSetMethod());
128           df.setKey(fieldMap.getDestField().getKey());
129           map.getSourceField().setKey(fieldMap.getDestField().getKey());
130           classMap.removeFieldMapping(fieldMap);
131         } else if (fieldMap instanceof ExcludeFieldMap) {
132           fieldMap.setDestField(df);
133           continue;
134         }
135       } else {
136         map.setDestField(new Field(fieldName, null));
137         Field sourceField = new Field(MapperConstants.SELF_KEYWORD, null);
138         if (StringUtils.isNotEmpty(classMap.getSourceClass().getMapGetMethod())) {
139           sourceField.setMapGetMethod(classMap.getSourceClass().getMapGetMethod());
140           sourceField.setTheGetMethod(classMap.getSourceClass().getMapGetMethod());
141         } else {
142           sourceField.setMapGetMethod("get");
143           sourceField.setTheGetMethod("get");
144         }
145         if (StringUtils.isNotEmpty(classMap.getSourceClass().getMapSetMethod())) {
146           sourceField.setMapSetMethod(classMap.getSourceClass().getMapSetMethod());
147           sourceField.setTheSetMethod(classMap.getSourceClass().getMapSetMethod());
148         } else {
149           sourceField.setMapSetMethod("put");
150           sourceField.setTheSetMethod("put");
151         }
152         map.setSourceField(sourceField);
153         FieldMap fieldMap = classMap.getFieldMapUsingDest(fieldName);
154         // this means we have an existing fieldmap. set default values accordingly
155
if (fieldMap != null && !(fieldMap instanceof ExcludeFieldMap)) {
156           map.getDestField().setTheGetMethod(fieldMap.getDestField().getTheGetMethod());
157           map.getDestField().setTheSetMethod(fieldMap.getDestField().getTheSetMethod());
158           sourceField.setKey(fieldMap.getSourceField().getKey());
159           map.getDestField().setKey(fieldMap.getSourceField().getKey());
160           classMap.removeFieldMapping(fieldMap);
161         } else if (fieldMap instanceof ExcludeFieldMap) {
162           fieldMap.setSourceField(sourceField);
163           continue;
164         }
165       }
166       classMap.addFieldMapping(map);
167     }
168   }
169
170   private void addDefaultFieldMappings(ClassMap classMap) {
171     Class JavaDoc sourceClass = classMap.getSourceClass().getClassToMap();
172     Class JavaDoc destClass = classMap.getDestClass().getClassToMap();
173     PropertyDescriptor JavaDoc[] destProperties = reflectionUtils.getPropertyDescriptors(destClass);
174     for (int i = 0; i < destProperties.length; i++) {
175       String JavaDoc destFieldName = destProperties[i].getName();
176       PropertyDescriptor JavaDoc sourceProperty = reflectionUtils.getPropertyDescriptor(sourceClass, destFieldName);
177       // if the sourceProperty is null we know that there is not a
178
// corresponding property to map to.
179
if (destFieldName.equals("class") || sourceProperty == null) {
180         continue;
181       }
182
183       if (classMap.getFieldMapUsingDest(destFieldName) != null) {
184         continue;
185       }
186       GenericFieldMap map = new GenericFieldMap();
187       map.setSourceField(new Field(destFieldName, null));
188       map.setDestField(new Field(destFieldName, null));
189       classMap.addFieldMapping(map);
190     }
191   }
192 }
Popular Tags