도구 클래스 중 하나: CSVutils

package com.test.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;

import au.com.bytecode.opencsv.CSVReader;

import com.test.core.conf.SystemConf;
import com.test.core.expcetion.ServiceException;
import com.test.orgucpassport.web.model.OrgUcPassportModel;

public final class CSVUtils {

	private static Logger LOG = Logger.getLogger(CSVUtils.class);

	public static final Integer BINDS_COLUMNS = 3;
	public static final Integer UNBINDS_COLUMNS = 1;

	public static final String FILL_ERROR = "   ,            ,      !";
	public static final String HEADER_ERROR = "   ,            ,        !";
	public static final String BODY_ERROR = "   ,              ,        !";

	private CSVUtils() {
	}

	public static List readCSVWhenBinds(File CSVFileBinds)
			throws ServiceException, IOException {
		List csvData = getCSVData(CSVFileBinds);
		//       
		excludingTail(csvData);
		checkCSVFormatWhenBinds(csvData);
		return readCSVBodyWhenBinds(csvData);
	}

	public static List readCSVWhenUnbinds(
			File CSVFileUnbinds) throws ServiceException {
		List csvData = getCSVData(CSVFileUnbinds);
		//       
		excludingTail(csvData);
		checkCSVFormatWhenUnbinds(csvData);
		return readCSVBodyWhenUnbinds(csvData);
	}

	public static List readCSVBodyWhenBinds(
			List csvData) throws ServiceException {
		List CSVForm = new ArrayList();

		try {
			int rowCount = 1;
			for (String[] CSVBody : csvData) {
				//     ,        
				if (rowCount == 1) {
					rowCount++;
					continue;
				}
				OrgUcPassportModel model = new OrgUcPassportModel();
				model.setPassportName(CSVBody[0].trim());
				model.setExpiredTime(DateUtils.parseDate(CSVBody[1].trim(),
						new String[] { SystemConf.DATE_FORMAT }));
				model.setOrgId(Long.valueOf(CSVBody[2].trim()));
				CSVForm.add(model);
			}
		} catch (ParseException e) {
			new ServiceException(BODY_ERROR);
		}

		return CSVForm;
	}

	public static List readCSVBodyWhenUnbinds(
			List csvData) throws ServiceException {
		List CSVForm = new ArrayList();

		int rowCount = 1;
		for (String[] bodyData : csvData) {
			//     ,        
			if (rowCount == 1) {
				rowCount++;
				continue;
			}

			OrgUcPassportModel model = new OrgUcPassportModel();
			model.setPassportName(bodyData[0].trim());
			CSVForm.add(model);
		}
		return CSVForm;
	}

	public static void checkCSVFormatWhenBinds(List csvData)
			throws ServiceException {
		checkCSVHeaderWhenBinds(csvData);
		checkCSVBodyWhenBinds(csvData);
	}

	public static void checkCSVFormatWhenUnbinds(List csvData)
			throws ServiceException {
		checkCSVHeaderWhenUnbinds(csvData);
		checkCSVBodyWhenUnbinds(csvData);
	}

	public static void checkCSVHeaderWhenBinds(List csvData)
			throws ServiceException {

		checkCSVData(csvData);

		String[] CSVHeader = csvData.get(0);

		if (CSVHeader == null || CSVHeader.length <= 0) {
			throw new ServiceException(HEADER_ERROR);
		}

		if (!BINDS_COLUMNS.equals(CSVHeader.length)) {
			throw new ServiceException(HEADER_ERROR);
		}

		checkCSVCells(CSVHeader);

		//                ,      (          ):
		String timeColumn = CSVHeader[1].trim();
		if (RegexpUtils.isHardRegexpValidate(timeColumn,
				RegexpUtils.DATE_BARS_REGEXP)) {
			throw new ServiceException(HEADER_ERROR);
		}

		//             ID    ,      ID(          ):
		String orgColumn = CSVHeader[2].trim();
		if (RegexpUtils.isHardRegexpValidate(orgColumn,
				RegexpUtils.POSITIVE_INTEGER_REGEXP)) {
			throw new ServiceException(HEADER_ERROR);
		}

	}

	public static void checkCSVHeaderWhenUnbinds(List csvData)
			throws ServiceException {
		checkCSVData(csvData);

		String[] CSVHeader = csvData.get(0);

		if (CSVHeader == null || CSVHeader.length <= 0) {
			throw new ServiceException(HEADER_ERROR);
		}

		if (!UNBINDS_COLUMNS.equals(CSVHeader.length)) {
			throw new ServiceException(HEADER_ERROR);
		}

		checkCSVCells(CSVHeader);
	}

	public static void checkCSVBodyWhenBinds(List csvData)
			throws ServiceException {
		checkCSVData(csvData);

		int rowCount = 1;
		for (String[] bodyData : csvData) {

			//     ,        
			if (rowCount == 1) {
				rowCount++;
				continue;
			}

			if (bodyData.length <= 0) {
				throw new ServiceException(BODY_ERROR);
			}
			if (!BINDS_COLUMNS.equals(bodyData.length)) {
				throw new ServiceException(BODY_ERROR);
			}
			//              
			checkCSVCells(bodyData);
			//           
			String time = bodyData[1].trim();
			if (!RegexpUtils.isHardRegexpValidate(time,
					RegexpUtils.DATE_BARS_REGEXP)) {
				throw new ServiceException(BODY_ERROR);
			}
			//          (>0)
			String orgId = bodyData[2].trim();
			if (!RegexpUtils.isHardRegexpValidate(orgId,
					RegexpUtils.POSITIVE_INTEGER_REGEXP)) {
				throw new ServiceException(BODY_ERROR);
			}
		}

	}

	public static void checkCSVBodyWhenUnbinds(List csvData)
			throws ServiceException {
		checkCSVData(csvData);

		int rowCount = 1;
		for (String[] bodyData : csvData) {
			//     ,        
			if (rowCount == 1) {
				rowCount++;
				continue;
			}

			if (bodyData.length <= 0) {
				throw new ServiceException(BODY_ERROR);
			}
			if (!UNBINDS_COLUMNS.equals(bodyData.length)) {
				throw new ServiceException(BODY_ERROR);
			}
			//              
			checkCSVCells(bodyData);
		}
	}

	public static void checkCSVData(List csvData)
			throws ServiceException {
		if (csvData == null || csvData.size() <= 0) {
			throw new ServiceException(FILL_ERROR);
		}
	}

	public static List getCSVData(File CSVFile)
			throws ServiceException {
		if (CSVFile == null) {
			throw new ServiceException(FILL_ERROR);
		}
		List csvData = null;

		InputStream is = null;
		InputStreamReader isr = null;
		CSVReader reader = null;

		try {
			is = new FileInputStream(CSVFile);
			isr = new InputStreamReader(is, "UTF-8");
			reader = new CSVReader(isr, ',');
			csvData = reader.readAll();
		} catch (FileNotFoundException e) {
			LOG.warn("", e);
			throw new ServiceException(FILL_ERROR, e);
		} catch (UnsupportedEncodingException e) {
			LOG.warn("", e);
			throw new ServiceException(FILL_ERROR, e);
		} catch (IOException e) {
			LOG.warn("", e);
			throw new ServiceException(FILL_ERROR, e);
		} finally {

			//      
			try {
				reader.close();
			} catch (IOException e) {
				LOG.warn("", e);
			}

			IOUtils.closeQuietly(isr);
			IOUtils.closeQuietly(is);
		}

		return csvData;
	}

	public static void excludingTail(List csvData) {
		List toExclud = new ArrayList();
		//          
		for (int tail = csvData.size() - 1; tail >= 0; tail--) {
			String[] row = csvData.get(tail);

			if (row != null && row.length > 0) {
				Boolean isBlank = true;
				for (String cell : row) {
					if (!StringUtils.isBlank(cell)) {
						//            
						isBlank = false;
						break;
					}
				}
				if (!isBlank) {
					break;
				}
			}

			toExclud.add(row);
		}
		//       
		csvData.removeAll(toExclud);
	}

	public static void checkCSVCells(String[] row) throws ServiceException {
		for (String cell : row) {
			if (StringUtils.isBlank(cell)) {
				throw new ServiceException(BODY_ERROR);
			}
		}
	}

}

좋은 웹페이지 즐겨찾기