1 22 package net.sf.anupam.csv; 23 24 import net.sf.anupam.csv.formatters.CSVFieldFormatter; 25 import net.sf.anupam.csv.formatters.CSVFormatterFactory; 26 import net.sf.anupam.csv.mapping.CSVBeanMapping; 27 import net.sf.anupam.csv.mapping.CSVFieldMapping; 28 import net.sf.anupam.csv.mapping.CSVMappingParser; 29 import net.sf.anupam.csv.exceptions.CSVOException; 30 import org.apache.commons.lang.StringUtils; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import java.io.FileNotFoundException ; 35 import java.io.FileReader ; 36 import java.io.InputStream ; 37 import java.io.InputStreamReader ; 38 import java.io.Reader ; 39 import java.util.HashMap ; 40 import java.util.Map ; 41 42 54 public final class CSVParserFactory { 55 56 59 private static final String MAPPING_FILE_NAME = "csv-mapping.xml"; 60 61 64 private static final Log LOG = LogFactory.getLog(CSVParserFactory.class); 65 66 69 private static CSVParserFactory singleton; 70 71 private static final CSVFormatterFactory FORMATTER_FACTORY = CSVFormatterFactory 72 .getSingleton(); 73 74 77 private Map <String , CSVBeanMapping> beanMappings; 78 79 static { 80 81 } 82 83 86 private CSVParserFactory() { 87 super(); 88 beanMappings = new HashMap <String , CSVBeanMapping>(); 89 } 90 91 97 public synchronized static CSVParserFactory getSingleton() 98 throws CSVOException { 99 if (singleton == null) { 100 singleton = new CSVParserFactory(); 102 singleton.loadMappings(); 103 LOG.info("Created the Singleton for: " + CSVParserFactory.class); 104 } 105 return singleton; 106 } 107 108 113 private void loadMappings() 114 throws CSVOException { 115 final CSVMappingParser parser = new CSVMappingParser(); 116 beanMappings.putAll(parser.getMappings(MAPPING_FILE_NAME, true)); 117 118 for (String beanNames : beanMappings.keySet()) { 119 final CSVBeanMapping currentBeanMapping = beanMappings 120 .get(beanNames); 121 for (CSVFieldMapping currentFieldMapping : currentBeanMapping) { 122 createFormattersFor(currentFieldMapping); 123 resolveBeanReferencesFor(currentFieldMapping); 124 } 125 } 126 LOG.debug("Loaded the CSV Mapping configuration from " 127 + MAPPING_FILE_NAME); 128 } 129 130 137 private void createFormattersFor(final CSVFieldMapping fieldMapping) 138 throws CSVOException { 139 140 final CSVFieldFormatter formatter = FORMATTER_FACTORY 141 .createFormatterFor(fieldMapping.getReformatterName()); 142 fieldMapping.setFormatter(formatter); 143 144 } 145 146 152 private void resolveBeanReferencesFor(final CSVFieldMapping fieldMapping) { 153 154 final String beanRefName = fieldMapping.getBeanReferenceName(); 155 if (!beanRefName.equalsIgnoreCase("none")) { 156 final CSVBeanMapping referencedBean = getBeanMapping(beanRefName); 157 158 if (referencedBean != null) { 159 fieldMapping.setBeanReference(referencedBean); 160 } else { 161 LOG.warn("For field " + fieldMapping 162 + " the referenced bean does not exist"); 163 fieldMapping.setBeanReferenceName("none"); 164 } 165 } 166 167 } 168 169 175 public CSVBeanMapping getBeanMapping(final String beanName) { 176 return beanMappings.get(beanName); 177 } 178 179 190 public CSVParser getCSVParser(final String mappingName, 191 final String csvFileName, final boolean inClassPath 192 ) throws FileNotFoundException { 193 194 if (StringUtils.isEmpty(csvFileName)) { 195 LOG.warn("The specified CSV Filename is empty"); 196 throw new IllegalArgumentException ("File Name is empty"); 197 } 198 199 final Reader reader; 200 201 try { 202 if (inClassPath) { 203 final InputStream is = ClassLoader 204 .getSystemResourceAsStream(csvFileName); 205 if (is == null) { 206 throw new FileNotFoundException ("The CSV File: " 207 + csvFileName + " was not found in the classpath"); 208 } 209 reader = new InputStreamReader (is); 210 } else { 211 reader = new FileReader (csvFileName); 212 213 } 214 LOG.debug("Successfully read the CSV file"); 215 } catch (final FileNotFoundException e) { 216 LOG.warn("The specified CSV File: " + csvFileName 217 + " was not found", e); 218 throw e; 219 } 220 221 return getCSVParser(mappingName, reader); 222 } 223 224 233 public CSVParser getCSVParser(final String mappingName, 234 final Reader csvReader) { 235 236 final CSVBeanMapping beanMapping = getBeanMapping(mappingName); 237 238 if (beanMapping == null) { 239 LOG.warn("Specified bean mapping was not found"); 240 throw new IllegalArgumentException ( 241 "Specified bean mapping was not found"); 242 } 243 244 if (csvReader == null) { 245 LOG.warn("Specified CSV IO Reader was null"); 246 throw new IllegalArgumentException ( 247 "Specified CSV IO Reader was null"); 248 } 249 250 final CSVReader reader = new CSVReader(csvReader, beanMapping 251 .isCsvHeaderPresent()); 252 253 return new CSVParser(beanMapping, reader); 254 } 255 } 256 | Popular Tags |