
src.main.java.com.px100systems.util.CsvParser Maven / Gradle / Ivy
/*
* This file is part of Px100 Data.
*
* Px100 Data is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
*/
package com.px100systems.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVStrategy;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.StringBuilderWriter;
/**
* CSV parser.
*
* @version 0.3
Copyright (c) 2015 Px100 Systems. All Rights Reserved.
* @author Alex Rogachevsky
*/
public class CsvParser {
private CSVStrategy csvStrategy;
/**
* Default constructor (should be enough)
*/
public CsvParser() {
this (',', '"', '#');
}
/**
* Custom constructor for non-standard CSV files
* @param csvSeparator separator (,)
* @param csvQuote quote (")
* @param csvComment comment (#)
*/
public CsvParser(char csvSeparator, char csvQuote, char csvComment) {
csvStrategy = new CSVStrategy(csvSeparator, csvQuote, csvComment);
}
/**
* Parsing into a list of beans (requires CSV header).
*
* @param content text
* @param cls class
* @return the list of beans of teh specified class.
*/
public List parse(String content, Class cls) {
List result = new ArrayList();
CSVParser parser = new CSVParser(new BufferedReader(new StringReader(content)), csvStrategy);
try {
List properties = new ArrayList();
for (String h : parser.getLine())
properties.add(new PropertyAccessor(cls, h));
if (!properties.isEmpty())
for (String[] line = parser.getLine(); line != null; line = parser.getLine()) {
T object = cls.newInstance();
int fieldCount = 0;
for (int i = 0, n = properties.size(); i < n && i < line.length; i++)
if (line[i] != null && !line[i].trim().isEmpty()) {
properties.get(i).set(object, line[i]);
fieldCount++;
}
if (fieldCount > 0)
result.add(object);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result.isEmpty() ? null : result;
}
private String[] currentLine = null;
private String[] header;
/**
* Parsing into a list of maps of fields (requires header)
*
* @param is imput stream
* @return List of maps of {field, value} pairs
*/
public Iterator