All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csv.CSVFactory Maven / Gradle / Ivy

Go to download

A library for easily accessing CSV, Excel and and other table-like data from Java

There is a newer version: 4.3.4
Show newest version
/*
 * This file is part of CSV package.
 *
 *  CSV is free software: you can redistribute it 
 *  and/or modify it under the terms of version 3 of the GNU 
 *  Lesser General Public  License as published by the Free Software 
 *  Foundation.
 *  
 *  CSV is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with CSV.  If not, see 
 *  .
 */
package csv;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.activation.MimetypesFileTypeMap;

import csv.impl.AbstractStreamTableReader;
import csv.impl.AbstractStreamTableWriter;

/**
 * This factory returns correct reader and writer implementations
 * for given files.
 * @author RalphSchuster
 *
 */
public class CSVFactory {

	private Map> readers;
	private Map> writers;
	
	private static CSVFactory factory = null;
	
	/**
	 * Returns the factory for rading/writing tables.
	 * @return factory factory object (singleton)
	 */
	public static CSVFactory getFactory() {
		if (factory == null) {
			factory = new CSVFactory();
		}
		return factory;
	}
	
	/**
	 * Creates the factory and initializes.
	 */
	protected CSVFactory() {
		init();
	}

	/**
	 * Initializes the factory.
	 */
	protected void init() {
		initReaderMap();
		initWriterMap();
		register(MimeTypeInfo.CSV_INFO);
		register(MimeTypeInfo.EXCEL_INFO);
		register(MimeTypeInfo.XML_INFO);
	}
	
	/**
	 * Initializes the reader map.
	 */
	protected void initReaderMap() {
		if (readers != null) return;
		readers = new HashMap>();
	}
	
	/**
	 * Initializes the writer map.
	 */
	protected void initWriterMap() {
		if (writers != null) return;
		writers = new HashMap>();
	}
	
	/**
	 * Registers a new MIME type.
	 * @param mimeTypeInfo the info to register
	 */
	public void register(MimeTypeInfo mimeTypeInfo) {
		String types[] = mimeTypeInfo.getMimeTypes();
		Class reader = mimeTypeInfo.getReaderClass();
		Class writer = mimeTypeInfo.getWriterClass();
		for (int i=0; i readerClass = readers.get(mimeType);
		if (readerClass == null) throw new CsvException("Cannot find reader class for: "+mimeType);
		try {
			AbstractStreamTableReader reader = readerClass.newInstance();
			return reader;
		} catch (Exception e) {
			throw new CsvException("Cannot create reader instance: ", e);
		}		
	}
	
	/**
	 * Returns the correct reader for the given file.
	 * @param file filename
	 * @return reader class instance to be used
	 * @throws IOException when the file cannot be written
	 */
	public AbstractStreamTableWriter getWriter(String file) throws IOException {
		return getWriter(new File(file));
	}
	
	/**
	 * Returns the correct reader for the given file.
	 * @param file file
	 * @return reader class instance to be used
	 * @throws IOException when the file cannot be written
	 */
	public AbstractStreamTableWriter getWriter(File file) throws IOException {
		String mimeType = getMimeType(file);
		if (mimeType == null) throw new CsvException("No MIME type found: "+file.getAbsolutePath());
		AbstractStreamTableWriter writer = getMimeTypeWriter(mimeType);
		writer.setOutputStream(new FileOutputStream(file));
		return writer;
	}
	
	/**
	 * Returns a writer for the given MIME type.
	 * @param mimeType MIME type
	 * @return writer to be used
	 */
	public AbstractStreamTableWriter getMimeTypeWriter(String mimeType) {
		if (mimeType == null) throw new CsvException("NULL MIME type");
		Class writerClass = writers.get(mimeType);
		if (writerClass == null) throw new CsvException("Cannot find writer class for: "+mimeType);
		try {
			AbstractStreamTableWriter writer = writerClass.newInstance();
			return writer;
		} catch (Exception e) {
			throw new CsvException("Cannot create writer instance: ", e);
		}		
	}
	
	/**
	 * Returns the MIME type for the given file.
	 * @param file file to check
	 * @return MIME type of file
	 */
	public String getMimeType(File file) {
		return new MimetypesFileTypeMap().getContentType(file);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy