| 1 22 23 package net.sf.anupam.csv.formatters; 24 25 import org.apache.commons.digester.Digester; 26 import org.apache.commons.digester.xmlrules.FromXmlRuleSet; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 32 import java.io.BufferedInputStream ; 33 import java.io.FileInputStream ; 34 import java.io.FileNotFoundException ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.List ; 40 import java.util.Map ; 41 42 51 class CSVFormatterConfigParser { 52 53 56 private static final Log LOG = LogFactory 57 .getLog(CSVFormatterConfigParser.class); 58 59 62 private static FromXmlRuleSet ruleSet; 63 64 private static CSVFormatterConfigParser singleton; 65 private static final Map <String , FormatterConfiguration> formatCfgMapping = new HashMap <String , FormatterConfiguration>(); 66 private static boolean isLoaded; 67 68 71 private CSVFormatterConfigParser() { 72 super(); 73 74 75 } 76 77 84 public synchronized Map <String , FormatterConfiguration> getFormatMappings( 85 final String xmlFileName, final boolean inClassPath) { 86 87 if (!isLoaded) { 88 loadMappings(xmlFileName, inClassPath); 89 isLoaded = true; 90 } 91 92 93 return formatCfgMapping; 94 } 95 96 97 103 private void loadMappings(final String xmlFileName, final boolean inClassPath) { 104 try { 105 final InputStream xmlStream = (inClassPath) 106 ? getClass().getClassLoader() 107 .getResourceAsStream(xmlFileName) 108 : new BufferedInputStream (new FileInputStream (xmlFileName)); 109 110 final InputSource inputSrc = new InputSource (xmlStream); 111 final Digester digester = new Digester(); 112 digester.clear(); 113 114 CSVFormatterConfigParser.ruleSet 115 .addRuleInstances(digester); 116 digester.push(new ArrayList <FormatterConfiguration>()); 117 final List <FormatterConfiguration> formatMappingList = (List <FormatterConfiguration>) digester 118 .parse(inputSrc); 119 120 for (FormatterConfiguration formatConfig : formatMappingList) { 121 formatCfgMapping.put(formatConfig.getFormatterName(), formatConfig); 122 } 123 } catch (final FileNotFoundException e) { 124 LOG.warn("The XML File: " 125 + xmlFileName + " was not found", e); 126 } catch (final IOException e) { 127 LOG.warn("The XML File: " 128 + xmlFileName + " could not be read", e); 129 } catch (final SAXException e) { 130 LOG.warn("The XML File: " 131 + xmlFileName + " could not be parsed", e); 132 } 133 } 134 135 public synchronized static CSVFormatterConfigParser getConfigParser() { 136 137 if (singleton == null) { 138 final InputStream is = CSVFormatterConfigParser.class.getClassLoader() 139 .getResourceAsStream("net/sf/anupam/csv/formatters/csv-formatter-config-digester-rules.xml"); 140 if (is != null) { 141 final InputSource isrc = new InputSource (is); 142 ruleSet = new FromXmlRuleSet(isrc); 143 LOG.info("Loaded Digester Rules for " 144 + CSVFormatterConfigParser.class); 145 } else { 146 LOG 147 .error("The CSV Formatter Configuration Digester Rules XML was not found"); 148 } 149 singleton = new CSVFormatterConfigParser(); 150 } 151 return singleton; 152 } 153 } 154 | Popular Tags |