| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.loader; |
| 17 | |
|
| 18 | |
import org.dozer.classmap.*; |
| 19 | |
import org.dozer.converters.CustomConverterContainer; |
| 20 | |
import org.dozer.converters.CustomConverterDescription; |
| 21 | |
import org.dozer.util.MappingUtils; |
| 22 | |
|
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.LinkedHashSet; |
| 25 | |
import java.util.List; |
| 26 | |
import java.util.Map.Entry; |
| 27 | |
import java.util.Set; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 699 | public class CustomMappingsLoader { |
| 38 | |
|
| 39 | 1 | private static final MappingsParser mappingsParser = MappingsParser.getInstance(); |
| 40 | |
|
| 41 | |
public LoadMappingsResult load(List<MappingFileData> mappings) { |
| 42 | |
|
| 43 | 691 | Configuration globalConfiguration = findConfiguration(mappings); |
| 44 | |
|
| 45 | 690 | ClassMappings customMappings = new ClassMappings(); |
| 46 | |
|
| 47 | 690 | for (MappingFileData mappingFileData : mappings) { |
| 48 | 619 | List<ClassMap> classMaps = mappingFileData.getClassMaps(); |
| 49 | 619 | ClassMappings customMappingsPrime = mappingsParser.processMappings(classMaps, globalConfiguration); |
| 50 | 614 | customMappings.addAll(customMappingsPrime); |
| 51 | 612 | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 683 | ClassMapBuilder.addDefaultFieldMappings(customMappings, globalConfiguration); |
| 56 | |
|
| 57 | 683 | Set<CustomConverterDescription> customConverterDescriptions = new LinkedHashSet<CustomConverterDescription>(); |
| 58 | |
|
| 59 | |
|
| 60 | 683 | if (globalConfiguration.getCustomConverters() != null && globalConfiguration.getCustomConverters().getConverters() != null) { |
| 61 | 683 | for (CustomConverterDescription cc : globalConfiguration.getCustomConverters().getConverters()) { |
| 62 | 388 | customConverterDescriptions.add(cc); |
| 63 | 388 | } |
| 64 | |
} |
| 65 | |
|
| 66 | |
|
| 67 | 683 | for (Entry<String, ClassMap> entry : customMappings.getAll().entrySet()) { |
| 68 | 10573 | ClassMap classMap = entry.getValue(); |
| 69 | 10573 | if (classMap.getCustomConverters() != null) { |
| 70 | 10573 | classMap.getCustomConverters().setConverters(new ArrayList<CustomConverterDescription>(customConverterDescriptions)); |
| 71 | |
} else { |
| 72 | 0 | classMap.setCustomConverters(new CustomConverterContainer()); |
| 73 | 0 | classMap.getCustomConverters().setConverters(new ArrayList<CustomConverterDescription>(customConverterDescriptions)); |
| 74 | |
} |
| 75 | 10573 | } |
| 76 | 683 | return new LoadMappingsResult(customMappings, globalConfiguration); |
| 77 | |
} |
| 78 | |
|
| 79 | |
private Configuration findConfiguration(List<MappingFileData> mappingFileDataList) { |
| 80 | 691 | Configuration globalConfiguration = null; |
| 81 | 691 | for (MappingFileData mappingFileData : mappingFileDataList) { |
| 82 | 621 | if (mappingFileData.getConfiguration() != null) { |
| 83 | |
|
| 84 | 230 | if (globalConfiguration != null) { |
| 85 | 1 | MappingUtils |
| 86 | |
.throwMappingException("More than one global configuration found. " |
| 87 | |
+ "Only one global configuration block (<configuration></configuration>) can be specified across all mapping files. " |
| 88 | |
+ "You need to consolidate all global configuration blocks into a single one."); |
| 89 | |
} |
| 90 | 229 | globalConfiguration = mappingFileData.getConfiguration(); |
| 91 | |
} |
| 92 | 620 | } |
| 93 | |
|
| 94 | |
|
| 95 | 690 | if (globalConfiguration == null) { |
| 96 | 462 | globalConfiguration = new Configuration(); |
| 97 | |
} |
| 98 | |
|
| 99 | 690 | return globalConfiguration; |
| 100 | |
} |
| 101 | |
|
| 102 | |
} |