Coverage Report - org.dozer.loader.CustomMappingsLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
CustomMappingsLoader
93%
31/33
85%
17/20
6
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 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 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  
  * Internal class that loads and parses custom xml mapping files into ClassMap objects. The ClassMap objects returned
 31  
  * from the load method are fully decorated and ready to be used by the internal mapping engine. Only intended for
 32  
  * internal use.
 33  
  * 
 34  
  * @author tierney.matt
 35  
  * @author garsombke.franz
 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  
     // Decorate the raw ClassMap objects and create ClassMap "prime" instances
 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  
     // Add default mappings using matching property names if wildcard policy
 54  
     // is true. The addDefaultFieldMappings will check the wildcard policy of each classmap
 55  683
     ClassMapBuilder.addDefaultFieldMappings(customMappings, globalConfiguration);
 56  
 
 57  683
     Set<CustomConverterDescription> customConverterDescriptions = new LinkedHashSet<CustomConverterDescription>();
 58  
 
 59  
     // build up custom converter description objects
 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  
     // iterate through the classmaps and set all of the custom converters on them
 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  
         //Only allow 1 global configuration
 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  
     //If global configuration was not specified, use defaults
 95  690
     if (globalConfiguration == null) {
 96  462
       globalConfiguration = new Configuration();
 97  
     }
 98  
 
 99  690
     return globalConfiguration;
 100  
   }
 101  
 
 102  
 }