KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
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 /**
34  * @author garsombke.franz
35  * @author sullins.ben
36  * @author tierney.matt
37  *
38  */

39 public class MappingUtils {
40
41   //only making public temporarily while refactoring. This static data should be relocated
42
public static Map JavaDoc storedFactories = Collections.synchronizedMap(new HashMap JavaDoc());
43   
44   private final CollectionUtils collectionUtils = new CollectionUtils();
45   
46   public String JavaDoc getClassNameWithoutPackage(Class JavaDoc clazz) {
47     Package JavaDoc 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 JavaDoc 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 JavaDoc aClass) {
66     return Map JavaDoc.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 JavaDoc aClass) {
74     return (aClass.isPrimitive() || Number JavaDoc.class.isAssignableFrom(aClass) || aClass.equals(String JavaDoc.class)
75         || aClass.equals(Character JavaDoc.class) || aClass.equals(Boolean JavaDoc.class)
76         || java.util.Date JavaDoc.class.isAssignableFrom(aClass) || java.util.Calendar JavaDoc.class.isAssignableFrom(aClass));
77   }
78
79   public void throwMappingException(Throwable JavaDoc e) throws MappingException {
80     if (e instanceof MappingException) {
81       // in this case we do not want to re-wrap an existing mapping exception
82
throw (MappingException) e;
83     } else if (e instanceof RuntimeException JavaDoc){
84       //feature request 1561837. Dont wrap any runtime exceptions in a MappingException
85
throw (RuntimeException JavaDoc) e;
86     } else {
87       throw new MappingException(e);
88     }
89   }
90
91   public boolean isBlankOrNull(String JavaDoc value) {
92     return value == null || value.trim().length() < 1 ? true : false;
93   }
94
95   public void addFactories(Map JavaDoc 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 JavaDoc getRootCause(Throwable JavaDoc ex) {
122     Throwable JavaDoc rootCause = ex;
123     while (rootCause.getCause() != null) {
124       rootCause = rootCause.getCause();
125     }
126     return rootCause;
127   }
128
129   public String JavaDoc getParentFieldNameKey(String JavaDoc parentSourceField, Object JavaDoc srcObj, String JavaDoc sourceClassName, String JavaDoc srcFieldReadMethodName,
130       String JavaDoc destFieldReadMethodName, String JavaDoc srcFieldName, String JavaDoc destFieldName) {
131     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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 JavaDoc findCustomConverter(Cache converterByDestTypeCache, CustomConverterContainer customConverterContainer, Class JavaDoc sourceClass, Class JavaDoc destClass) throws ClassNotFoundException JavaDoc {
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 JavaDoc determineCustomConverter(FieldMap fieldMap, Cache converterByDestTypeCache, CustomConverterContainer customConverterContainer,
151       Class JavaDoc sourceClass, Class JavaDoc destClass) throws ClassNotFoundException JavaDoc {
152     if (customConverterContainer == null || customConverterContainer.getConverters() == null || customConverterContainer.getConverters().size() < 1) {
153       return null;
154     }
155     
156     //This method is messy. Just trying to isolate the junk into this one method instead of spread across the mapping processor until a better solution can be put into place
157
//For indexed mapping, need to use the actual class at index to determine the custom converter.
158
if (fieldMap != null && fieldMap.getDestField().isIndexed()) {
159       if (destClass.isArray()) {
160         destClass = destClass.getComponentType();
161       } else if (destClass.isAssignableFrom(Collection JavaDoc.class)) {
162         //use hint when trying to find a custom converter
163
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     // reverse the fields
174
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 JavaDoc sourceClass, Class JavaDoc destClass, FieldMap fieldMap) {
208     if (Map JavaDoc.class.isAssignableFrom(sourceClass) || fieldMap.getSourceField().getMapGetMethod() != null ||
209         Map JavaDoc.class.isAssignableFrom(destClass) || fieldMap.getDestField().getMapGetMethod() != null) {
210       return true;
211     } else {
212       return false;
213     }
214   }
215   
216   public String JavaDoc getMappedFieldKey(Object JavaDoc srcObj) {
217     // Returns a string equivalent to the returned value of Object.toString()
218
return srcObj.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(srcObj));
219   }
220
221   public Object JavaDoc getIndexedValue(Object JavaDoc collection, int index) {
222     Object JavaDoc r = null;
223     if (collection instanceof Object JavaDoc[]) {
224       Object JavaDoc[] x = (Object JavaDoc[]) collection;
225       if (index < x.length) {
226         return x[index];
227       }
228     } else if (collection instanceof Collection JavaDoc) {
229       Collection JavaDoc x = (Collection JavaDoc) collection;
230       if (index < x.size()) {
231         Iterator JavaDoc 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