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

com.hfg.util.io.CSV Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util.io;

import com.hfg.util.FileUtil;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.DataColumn;
import com.hfg.util.collection.DataRow;
import com.hfg.util.collection.DataTable;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Map;

//------------------------------------------------------------------------------
/**
 Utility to help in parsing CSV (comma-separated value) files.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library 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 this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class CSV { private static DelimitedTextParser sParser = new DelimitedTextParser(','); //########################################################################### // PUBLIC FUNCTIONS //########################################################################### //--------------------------------------------------------------------------- public static String write(DataTable inDataTable) { StringBuilderPlus buffer = new StringBuilderPlus(); if (inDataTable != null) { // Header line boolean firstCol = true; for (DataColumn col : inDataTable.getDataColumns()) { if (firstCol) { firstCol = false; } else { buffer.append(", "); } buffer.append(writeField(col.getTitle())); } buffer.appendln(); // Rows for (String rowKey : inDataTable.rowKeySet()) { DataRow rowData = inDataTable.getRow(rowKey); firstCol = true; for (DataColumn col : inDataTable.getDataColumns()) { if (firstCol) { firstCol = false; } else { buffer.append(", "); } buffer.append(writeField(rowData.get(col))); } buffer.appendln(); } } return buffer.toString(); } //--------------------------------------------------------------------------- public static String writeField(String inField) { return StringUtil.isSet(inField) ? escapeField(inField) : ""; } //--------------------------------------------------------------------------- public static String writeField(Number inField) { return inField != null ? inField.toString() : ""; } //--------------------------------------------------------------------------- public static String writeField(Number inField, String inFormatString) { return inField != null ? String.format(inFormatString, inField) : ""; } //--------------------------------------------------------------------------- public static String writeField(Boolean inField) { return inField != null ? inField.toString() : ""; } //--------------------------------------------------------------------------- // When this method was public it was causing issues with signature conflicts. private static String writeField(Comparable inField) { String fieldValue = ""; if (inField != null) { if (inField instanceof String) { fieldValue = writeField((String) inField); } else if (inField instanceof Number) { fieldValue = writeField((Number) inField); } else if (inField instanceof Boolean) { fieldValue = writeField((Boolean) inField); } else { fieldValue = writeField(inField.toString()); } } return fieldValue; } //--------------------------------------------------------------------------- public static String escapeField(String inField) { return sParser.escapeField(inField); } //--------------------------------------------------------------------------- public static List parse(Reader inReader) throws IOException { return sParser.parse(inReader); } //--------------------------------------------------------------------------- /** * Returns a DataTable with the row numbers (as Strings) as keys. * @param inFile File containing the data to be parsed * @return a DataTable containing the parsed data * @throws IOException */ public static DataTable parseToDataTable(File inFile) throws IOException { FileUtil.checkFileReadability(inFile); DataTable dataTable; try (FileReader reader = new FileReader(inFile)) { dataTable = sParser.parseToDataTable(reader); } return dataTable; } //--------------------------------------------------------------------------- /** * Returns a DataTable with the row numbers (as Strings) as keys. * @param inReader Reader containing the data to be parsed * @return a DataTable containing the parsed data * @throws IOException */ public static DataTable parseToDataTable(Reader inReader) throws IOException { return sParser.parseToDataTable(inReader); } //--------------------------------------------------------------------------- /** * Returns a DataTable with the row numbers (as Strings) as keys. * @param inLines a list of lines containing the data to be parsed * @return a DataTable containing the parsed data * @throws IOException */ public static DataTable parseToDataTable(List inLines) throws IOException { return sParser.parseToDataTable(inLines); } //--------------------------------------------------------------------------- public static String[] parseLine(String inLine) throws IOException { return sParser.parseLine(inLine); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy