use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.
the class AbstractPropertyTableBuilderTest method testIrregularTable2.
@Test
public void testIrregularTable2() {
CSVParser iterator = csv("a,b\nc,d1,d2\ne,f");
PropertyTableBuilder.fillPropertyTable(table, iterator, csvFilePath);
Assert.assertEquals(3, table.getColumns().size());
containsColumn(PropertyTableBuilder.CSV_ROW_NODE);
containsColumn("a");
containsColumn("b");
Assert.assertEquals(2, table.getAllRows().size());
containsValue(0, "a", "c");
containsValue(0, "b", "d1");
containsValue(1, "a", "e");
containsValue(1, "b", "f");
}
use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.
the class PropertyTableBuilder method fillPropertyTable.
protected static PropertyTable fillPropertyTable(PropertyTable table, String csvFilePath) {
InputStream input = IO.openFile(csvFilePath);
CSVParser iterator = CSVParser.create(input);
return fillPropertyTable(table, iterator, csvFilePath);
}
use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.
the class PropertyTableBuilder method createEmptyPropertyTableArrayImpl.
private static PropertyTable createEmptyPropertyTableArrayImpl(String csvFilePath) {
CSVParser parser = CSVParser.create(csvFilePath);
List<String> rowLine = null;
int rowNum = 0;
int columnNum = 0;
while ((rowLine = parser.parse1()) != null) {
if (rowNum == 0) {
columnNum = rowLine.size();
}
rowNum++;
}
if (rowNum != 0 && columnNum != 0) {
return new PropertyTableArrayImpl(rowNum, columnNum + 1);
} else {
return null;
}
}
use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.
the class CSVInput method fromCSV.
public static ResultSet fromCSV(InputStream in) {
CSVParser parser = CSVParser.create(in);
final List<Var> vars = vars(parser);
List<String> varNames = Var.varNames(vars);
Function<List<String>, Binding> transform = new Function<List<String>, Binding>() {
private int count = 1;
@Override
public Binding apply(List<String> row) {
if (row.size() != vars.size())
FmtLog.warn(log, "Row %d: Length=%d: expected=%d", count, row.size(), vars.size());
BindingMap binding = BindingFactory.create();
// Check.
for (int i = 0; i < vars.size(); i++) {
Var v = vars.get(i);
String field = (i < row.size()) ? row.get(i) : "";
Node n = NodeFactory.createLiteral(field);
binding.add(v, n);
}
count++;
return binding;
}
};
Iterator<Binding> bindings = Iter.map(parser.iterator(), transform);
//This will parse actual result rows as needed thus minimising memory usage
return new ResultSetStream(varNames, null, bindings);
}
use of org.apache.jena.atlas.csv.CSVParser in project jena by apache.
the class CSVInput method booleanFromCSV.
public static boolean booleanFromCSV(InputStream in) {
CSVParser parser = CSVParser.create(in);
final List<Var> vars = vars(parser);
if (vars.size() != 1) {
throw new ARQException("CSV Boolean Results malformed: variables line='" + vars + "'");
}
if (!vars.get(0).getName().equals("_askResult")) {
FmtLog.warn(log, "Boolean result variable is '%s', not '_askResult'", vars.get(0).getName());
}
List<String> line = parser.parse1();
if (line.size() != 1) {
throw new ARQException("CSV Boolean Results malformed: data line='" + line + "'");
}
String str = line.get(0);
boolean b;
if (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("yes"))
b = true;
else if (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("no"))
b = false;
else {
throw new ARQException("CSV Boolean Results malformed, expected one of - true yes false no - but got " + str);
}
List<String> line2 = parser.parse1();
if (line2 != null) {
FmtLog.warn(log, "Extra rows: first is " + line2);
}
return b;
}
Aggregations