com.spun.util.io.CommaDelimitedFileParser Maven / Gradle / Ivy
package com.spun.util.io;
import com.spun.util.StringUtils;
import com.spun.util.logger.SimpleLogger;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CommaDelimitedFileParser
{
public static String[][] parse(File databaseFile)
{
return parse(FileUtils.readFileWithSuppressedExceptions(databaseFile));
}
public static String[][] parse(String data)
{
return parse(new StringReader(data));
}
public static String[][] parse(Reader data)
{
try
{
List records = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(data))
{
boolean done = false;
StringBuffer pastLines = null;
while (!done)
{
String line = reader.readLine();
if (line == null)
{
done = true;
}
else
{
if (pastLines != null)
{
pastLines.append("\n");
pastLines.append(line);
line = pastLines.toString();
}
String[] parseLine = parseLine(line);
if (parseLine == null)
{
if (pastLines == null)
{
pastLines = new StringBuffer(line);
}
}
else
{
records.add(parseLine);
pastLines = null;
}
}
}
}
return records.toArray(new String[records.size()][]);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
private static String[] parseLine(String line) throws IOException
{
String[] rawtokens = StringUtils.split(line, ",", false);
boolean in = false;
List tokens = new ArrayList();
String fullToken = null;
for (int i = 0; i < rawtokens.length; i++)
{
String string = rawtokens[i];
if (in)
{
fullToken += ",";
fullToken += string;
if (fullToken.endsWith("\""))
{
tokens.add(clearQuotes(fullToken));
in = false;
}
}
else
{
if (string.startsWith("\"") && !string.endsWith("\""))
{
fullToken = string;
in = true;
}
else
{
tokens.add(clearQuotes(string));
}
}
}
return in ? null : StringUtils.toArray(tokens);
}
private static String clearQuotes(String string)
{
String s = (string.startsWith("\"") && string.endsWith("\""))
? string.substring(1, string.length() - 1)
: string;
return s.trim();
}
public static Map[] parseToMap(File file)
{
try
{
return parseToMap(FileUtils.readFileWithSuppressedExceptions(file));
}
catch (RuntimeException e)
{
SimpleLogger.variable("Filename", file);
throw e;
}
}
public static Map[] parseToMap(String data)
{
return parseToMap(new StringReader(data));
}
public static Map[] parseToMap(Reader data)
{
String[][] out = parse(data);
Map[] maps = new HashMap[out.length - 1];
String[] labels = out[0];
for (int i = 1; i < out.length; i++)
{
Map map = new HashMap(labels.length);
for (int j = 0; j < labels.length; j++)
{
map.put(labels[j], out[i][j]);
}
maps[i - 1] = map;
}
return maps;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy