1 22 package net.sf.anupam.csv.formatters; 23 24 import net.sf.anupam.csv.exceptions.CSVOException; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 42 public final class CSVFormatterFactory { 43 44 48 private static final String FMT_MAPPING_FILE_NAME = "net/sf/anupam/csv/formatters/csv-formatter-config.xml"; 49 50 53 private static CSVFormatterFactory singleton; 54 55 59 private static final CSVFieldFormatter DO_NOTHING_FORMATTER = new DoNothingFormatter(); 60 61 64 private static final Log LOG = LogFactory 65 .getLog(CSVFormatterFactory.class); 66 67 70 private Map <String , FormatterConfiguration> formatterLookupMap; 71 72 75 private Map <String , CSVFieldFormatter> formatterCache; 76 77 78 82 private CSVFormatterFactory() { 83 super(); 84 formatterLookupMap = new HashMap <String , FormatterConfiguration>(); 85 formatterCache = new HashMap <String , CSVFieldFormatter>(); 86 } 87 88 93 public synchronized static CSVFormatterFactory getSingleton() { 94 if (singleton == null) { 95 singleton = new CSVFormatterFactory(); 96 singleton.loadMappings(); 97 LOG.info("Created the CSVFormatter Factory"); 98 } 99 return singleton; 100 } 101 102 105 private void loadMappings() { 106 final CSVFormatterConfigParser parser = CSVFormatterConfigParser.getConfigParser(); 107 final FormatterConfiguration doNothingConfiguration = new FormatterConfiguration(); 108 doNothingConfiguration.setFormatterName("none"); 109 doNothingConfiguration.setFormatterClass("net.sf.anupam.csv.formatters.DoNothingFormatter"); 110 doNothingConfiguration.setConstructionNeeded(false); 111 formatterLookupMap.put("none", doNothingConfiguration); 112 formatterLookupMap.putAll(parser.getFormatMappings(FMT_MAPPING_FILE_NAME, 113 true)); 114 createCache(); 115 LOG.debug("Loaded the CSV Mapping configuration from " 116 + FMT_MAPPING_FILE_NAME); 117 } 118 119 123 private void createCache() { 124 for (String formatterName : formatterLookupMap.keySet()) { 125 final FormatterConfiguration currentFormatter = formatterLookupMap 126 .get(formatterName); 127 if (!currentFormatter.isConstructionNeeded()) { 130 131 final CSVFieldFormatter formatter = createFormatterForClass(currentFormatter 132 .getFormatterClass()); 133 134 formatterCache.put(formatterName, formatter); 135 } 136 } 137 } 138 139 145 private CSVFieldFormatter createFormatterForClass(final String className) { 146 147 Object formatter; 148 try { 149 formatter = Class.forName(className.trim()).newInstance(); 150 } catch (final InstantiationException e) { 151 LOG.warn("Could not create formatter for class: " 152 + className, e); 153 formatter = DO_NOTHING_FORMATTER; 154 } catch (final IllegalAccessException e) { 155 LOG.warn("Could not create formatter for class: " 156 + className, e); 157 formatter = DO_NOTHING_FORMATTER; 158 } catch (final ClassNotFoundException e) { 159 LOG.warn("Could not create formatter for class: " 160 + className, e); 161 formatter = DO_NOTHING_FORMATTER; 162 } 163 return (CSVFieldFormatter) formatter; 164 165 } 166 167 175 public CSVFieldFormatter createFormatterFor(final String formatterName) 176 throws CSVOException { 177 178 if (formatterCache.containsKey(formatterName)) { 180 return formatterCache.get(formatterName); 181 } else { 182 LOG.warn("Formatter: " + formatterName + " not found"); 183 throw new CSVOException("Formatter: " + formatterName + " not found"); 184 } 185 186 } 187 } 188 | Popular Tags |