Coverage Report - org.dozer.loader.xml.MappingFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
MappingFileReader
69%
16/23
50%
1/2
2
 
 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.xml;
 17  
 
 18  
 import org.dozer.classmap.MappingFileData;
 19  
 import org.dozer.config.BeanContainer;
 20  
 import org.dozer.loader.MappingsSource;
 21  
 import org.dozer.util.DozerClassLoader;
 22  
 import org.dozer.util.MappingUtils;
 23  
 import org.slf4j.Logger;
 24  
 import org.slf4j.LoggerFactory;
 25  
 
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.net.URL;
 29  
 
 30  
 /**
 31  
  * Internal class that reads and parses a single custom mapping xml file into raw ClassMap objects. Only intended for
 32  
  * internal use.
 33  
  *
 34  
  * @author tierney.matt
 35  
  * @author garsombke.franz
 36  
  */
 37  0
 public class MappingFileReader implements MappingsSource<URL> {
 38  
 
 39  701
   private final Logger log = LoggerFactory.getLogger(MappingFileReader.class);
 40  
 
 41  
   private final MappingStreamReader streamReader;
 42  
 
 43  701
   public MappingFileReader(XMLParserFactory parserFactory) {
 44  701
     streamReader = new MappingStreamReader(parserFactory);
 45  701
   }
 46  
 
 47  
   public MappingFileData read(String fileName) {
 48  3
     DozerClassLoader classLoader = BeanContainer.getInstance().getClassLoader();
 49  3
     URL url = classLoader.loadResource(fileName);
 50  3
     return read(url);
 51  
   }
 52  
 
 53  
   public MappingFileData read(URL url) {
 54  603
     MappingFileData result = null;
 55  603
     InputStream stream = null;
 56  
     try {
 57  603
       stream = url.openStream();
 58  603
       result = streamReader.read(stream);
 59  0
     } catch (IOException e) {
 60  0
               log.error("Error while loading dozer mapping file url: [" + url + "]", e);
 61  0
               MappingUtils.throwMappingException(e);
 62  
         } finally {
 63  8
       try {
 64  603
         if (stream != null) {
 65  603
           stream.close();
 66  
         }
 67  0
       } catch (IOException e) {
 68  0
         MappingUtils.throwMappingException(e);
 69  611
       }
 70  0
     }
 71  595
     return result;
 72  
   }
 73  
 
 74  
 }