Coverage Report - org.naftulin.configmgr.parsers.XmlFileParserImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlFileParserImpl
62%
24/39
50%
4/8
0
 
 1  
 package org.naftulin.configmgr.parsers;
 2  
 
 3  
 import java.beans.IntrospectionException;
 4  
 import java.io.IOException;
 5  
 import java.net.URL;
 6  
 
 7  
 import org.apache.commons.betwixt.io.BeanReader;
 8  
 import org.apache.log4j.Logger;
 9  
 import org.naftulin.configmgr.ConfigurationManagementEntryImpl;
 10  
 import org.naftulin.configmgr.ConfigurationManagerException;
 11  
 import org.naftulin.configmgr.ConfigurationManagementEntry;
 12  
 import org.naftulin.configmgr.ConfigurationType;
 13  
 import org.xml.sax.SAXException;
 14  
 
 15  
 /**
 16  
  * Configuration entry parser converts xml configuration record into supplied class instance configuration management entry.
 17  
  * It parses the property file that the record describes, and stores the configuration
 18  
  * as a properties in content of an {@link org.naftulin.configmgr.ConfigurationManagementEntry entry}.
 19  
  * 
 20  
  * @author Henry Naftulin
 21  
  * @since 1.0
 22  
  */
 23  5
 public class XmlFileParserImpl implements ConfigEntryParser {
 24  
         private static final long serialVersionUID = 1L;
 25  5
         private static final Logger log = Logger.getLogger(XmlFileParserImpl.class);
 26  
         private final String className;
 27  
         
 28  10
         public XmlFileParserImpl(final String className) {
 29  10
                 this.className = className;
 30  10
         }
 31  
         
 32  
         /**
 33  
          * Returns configuration management entry by parsing the record passed in.
 34  
          * Note xml file has to match the class passed in, per betwix specfications. 
 35  
          * @param key the key configuration entry will be assigned
 36  
          * @param fileUrl the file URL to be parsed.
 37  
          * @return a configuration managment entry by parsing the record passed in.
 38  
          * @throws ConfigurationManagerException if an error occurs while parsing an entry.
 39  
          */
 40  
         public ConfigurationManagementEntry getConfigurationManagementEntry(final String key, 
 41  
                         final URL fileUrl) throws ConfigurationManagerException {
 42  5
                 validateParameters(key, fileUrl);
 43  
                 Class<Object> beanClass;
 44  
                 
 45  
                 try {
 46  5
                         beanClass = (Class<Object>) Class.forName(className);
 47  0
                 } catch (ClassNotFoundException e) {
 48  0
                         throw new ConfigurationManagerException("className " + className + " could not be found in the classpath.", e);
 49  
                 }
 50  
                 
 51  5
                 final String fileName = fileUrl.getFile();
 52  5
                 Object content = null;
 53  
                 try {
 54  
                         // Now convert this to a bean using betwixt
 55  
                 // Create BeanReader
 56  5
                 final BeanReader beanReader  = new BeanReader();                
 57  
                 //beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
 58  
                 //beanReader.getBindingConfiguration().setMapIDs(false);
 59  
                 
 60  
                 // Register beans so that betwixt knows what the xml is to be converted to
 61  
                 // Since the element mapped to a PersonBean isn't called the same 
 62  
                 // as Betwixt would have guessed, need to register the path as well
 63  5
                 log.debug("class name " + getSimpleName(beanClass));
 64  5
                 beanReader.registerBeanClass(getSimpleName(beanClass), beanClass);
 65  
                 
 66  
                 // Now we parse the xml
 67  5
                 content =  beanReader.parse(fileUrl);
 68  
                 
 69  
                 // send bean to system out
 70  5
                 log.debug("xml object parsed by XML Parser is: " + content);
 71  0
                 } catch (IOException e) {
 72  0
                         log.warn("Error while reading property file", e);
 73  0
                         throw new ConfigurationManagerException("Error while reading property file",e);
 74  0
                 } catch (IntrospectionException e) {
 75  0
                         log.warn("Error while parsing xml.", e);
 76  0
                         throw new ConfigurationManagerException("Error while parsing xml.",e);
 77  0
                 } catch (SAXException e) {
 78  0
                         log.warn("Error while parsing xml.", e);
 79  0
                         throw new ConfigurationManagerException("Error while parsing xml.",e);
 80  
                 }
 81  5
                 ConfigurationManagementEntry entry = new ConfigurationManagementEntryImpl(key, fileName, content, this, ConfigurationType.XML);
 82  
                 
 83  5
                 return entry;
 84  
         }
 85  
 
 86  
         private void validateParameters(final String key, final URL fileUrl)
 87  
                         throws ConfigurationManagerException {
 88  5
                 if (fileUrl == null) {
 89  0
                         throw new ConfigurationManagerException("file URL is null");
 90  
                 }
 91  5
                 if (fileUrl.getFile() == null) {
 92  0
                         throw new ConfigurationManagerException("file name passed in the URL " + fileUrl + " is null");
 93  
                 }
 94  5
                 if (key == null) { 
 95  0
                         throw new ConfigurationManagerException("key is null for file " + fileUrl );
 96  
                 }
 97  5
                 if (className == null) { 
 98  0
                         throw new ConfigurationManagerException("className is null for file " + fileUrl + " and key " + key );
 99  
                 }
 100  5
         }
 101  
         
 102  
         /**
 103  
          * Immitates the JDK 1.5 API to get the simple class name. Class is not null.
 104  
          * @param cl class
 105  
          * @return simple class name as defined in JDK 1.5
 106  
          */
 107  
         private String getSimpleName(final Class<Object> cl) {
 108  10
                 final int lastDot = cl.getName().lastIndexOf(".");
 109  10
                 return cl.getName().substring(lastDot +1);
 110  
         }
 111  
         /**
 112  
          * Returns a string representation of this parser.
 113  
          * @return a string representation of this parser.
 114  
          */
 115  
         public String toString() {
 116  5
                 return "Xml file parser";
 117  
         }
 118  
 }
 119