Coverage Report - org.dozer.loader.xml.DozerResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
DozerResolver
82%
14/17
50%
2/4
5
 
 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.config.BeanContainer;
 19  
 import org.dozer.util.DozerClassLoader;
 20  
 import org.dozer.util.DozerConstants;
 21  
 import org.slf4j.Logger;
 22  
 import org.slf4j.LoggerFactory;
 23  
 import org.xml.sax.EntityResolver;
 24  
 import org.xml.sax.InputSource;
 25  
 
 26  
 import java.io.InputStream;
 27  
 import java.net.URL;
 28  
 
 29  
 /**
 30  
  * Internal EntityResolver implementation to load Xml Schema from the dozer classpath resp. JAR file.
 31  
  * 
 32  
  * <p>
 33  
  * Fetches "beanmapping.xsd" from the classpath resource "/beanmapping.xsd", no matter if specified as some
 34  
  * local URL or as "http://dozer.sourceforge.net/schema/beanmapping.xsd".
 35  
  * 
 36  
  * @author garsombke.franz
 37  
  */
 38  707
 public class DozerResolver implements EntityResolver {
 39  
 
 40  707
   private final Logger log = LoggerFactory.getLogger(DozerResolver.class);
 41  
 
 42  
   public InputSource resolveEntity(String publicId, String systemId) {
 43  608
     log.debug("Trying to resolve XML entity with public ID [{}] and system ID [{}]", publicId, systemId);
 44  608
     if (systemId != null && systemId.indexOf(DozerConstants.XSD_NAME) > systemId.lastIndexOf("/")) {
 45  608
       String fileName = systemId.substring(systemId.indexOf(DozerConstants.XSD_NAME));
 46  608
       log.debug("Trying to locate [{}] in classpath", fileName);
 47  
       try {
 48  608
         DozerClassLoader classLoader = BeanContainer.getInstance().getClassLoader();
 49  608
         URL url = classLoader.loadResource(fileName);
 50  608
         InputStream stream = url.openStream();
 51  608
         InputSource source = new InputSource(stream);
 52  608
         source.setPublicId(publicId);
 53  608
         source.setSystemId(systemId);
 54  608
         log.debug("Found beanmapping XML Schema [{}] in classpath", systemId);
 55  608
         return source;
 56  0
       } catch (Exception ex) {
 57  0
         log.error("Could not resolve beanmapping XML Schema [" + systemId + "]: not found in classpath", ex);
 58  
       }
 59  
     }
 60  
     // use the default behaviour -> download from website or wherever
 61  0
     return null;
 62  
   }
 63  
 }