Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle quoted csv files #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public class Parameters implements Cloneable {
optionArgumentDescription = "format")
public ImageFormat imageFormat = ImageFormat.SVG;

@Parameter(
name = "in.strip",
description = ""
+ "Strip extra quotes from input files.\n"
+ "Default: false",
optionName = "strip",
optionArgumentDescription = "strip")
public boolean strip = false;

@Parameter(
name = "chart",
description = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ch.obermuhlner.csv2chart.Parameters;
import ch.obermuhlner.csv2chart.model.DataModel;
Expand All @@ -19,6 +21,8 @@ public class CsvDataModelLoader {

private final String separator;
private final String comment;
private final Pattern stripMatcher;
private boolean strip = false;

public CsvDataModelLoader() {
this(",", "#");
Expand All @@ -27,6 +31,7 @@ public CsvDataModelLoader() {
public CsvDataModelLoader(String separator, String comment) {
this.separator = separator;
this.comment = comment;
this.stripMatcher = Pattern.compile("^\"(.*)\"$");
}

public DataModel load(File file, Parameters parameters) {
Expand All @@ -35,6 +40,7 @@ public DataModel load(File file, Parameters parameters) {
}

public DataModel load(Matrix<String> matrix, Parameters parameters) {
strip = parameters.strip;
int headerRowCount = countHeaderRows(matrix);
int valueRowCount = matrix.getHeight() - headerRowCount;

Expand All @@ -59,11 +65,31 @@ private DataVector toDataVector(Matrix<String> matrix, int x, int headerRowCount
List<String> values = new ArrayList<>();

for (int y = 0; y < headerRowCount; y++) {
headers.add(matrix.get(x, y));
String source = matrix.get(x,y);
if (strip) {
Matcher m = stripMatcher.matcher(source);
if (m.find()) {
headers.add(m.group(1));
} else {
headers.add(source);
}
} else {
headers.add(source);
}
}

for (int y = headerRowCount; y < matrix.getHeight(); y++) {
values.add(matrix.get(x, y));
String source = matrix.get(x,y);
if (strip) {
Matcher m = stripMatcher.matcher(source);
if (m.find()) {
values.add(m.group(1));
} else {
values.add(source);
}
} else {
values.add(source);
}
}

return new DataVector(headers, values);
Expand Down Expand Up @@ -132,7 +158,7 @@ private void parseComment(String line, Parameters parameters) {
}
}

private static boolean containsDoubles(Matrix<String> matrix, int x, int y, int width, int height) {
private boolean containsDoubles(Matrix<String> matrix, int x, int y, int width, int height) {
for (int iy = y; iy < y+height; iy++) {
for (int ix = x; ix < x+width; ix++) {
String value = matrix.get(ix, iy);
Expand All @@ -144,8 +170,15 @@ private static boolean containsDoubles(Matrix<String> matrix, int x, int y, int
return false;
}

private static boolean isDouble(String string) {
private boolean isDouble(String string) {
try {
if (strip) {
Matcher m = stripMatcher.matcher(string);
if (m.find()) {
Double.parseDouble(m.group(1));
return true;
}
}
Double.parseDouble(string);
return true;
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,26 @@ public void testLoad_HeaderRow2_HeaderCol() {
assertEquals(new DataVector(Arrays.asList("Columns", "Col1"), Arrays.asList("1.1", "2.1")), dataModel.getValues().get(0));
assertEquals(new DataVector(Arrays.asList("Columns", "Col2"), Arrays.asList("1.2", "2.2")), dataModel.getValues().get(1));
}

@Test
public void testLoad_Quoted_HeaderRow() {
CsvDataModelLoader dataModelLoader = new CsvDataModelLoader();

Matrix<String> matrix = new Matrix<>();

int row = 0;
matrix.setRow(row++, "\"Col1\"", "\"Col2\"");
matrix.setRow(row++, "\"1.1\"", "\"1.2\"");
matrix.setRow(row++, "\"2.1\"", "\"2.2\"");

Parameters parameters = new Parameters();
parameters.strip = true;
DataModel dataModel = dataModelLoader.load(matrix, parameters);

assertEquals(null, dataModel.getCategory());

assertEquals(2, dataModel.getValues().size());
assertEquals(new DataVector(Arrays.asList("Col1"), Arrays.asList("1.1", "2.1")), dataModel.getValues().get(0));
assertEquals(new DataVector(Arrays.asList("Col2"), Arrays.asList("1.2", "2.2")), dataModel.getValues().get(1));
}
}