Search in sources :

Example 6 with CSVParser

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");
}
Also used : CSVParser(org.apache.jena.atlas.csv.CSVParser) BaseTest(org.apache.jena.propertytable.BaseTest) Test(org.junit.Test)

Example 7 with CSVParser

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);
}
Also used : InputStream(java.io.InputStream) CSVParser(org.apache.jena.atlas.csv.CSVParser)

Example 8 with CSVParser

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;
    }
}
Also used : CSVParser(org.apache.jena.atlas.csv.CSVParser)

Example 9 with CSVParser

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);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) Function(java.util.function.Function) CSVParser(org.apache.jena.atlas.csv.CSVParser) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with CSVParser

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;
}
Also used : ARQException(org.apache.jena.sparql.ARQException) Var(org.apache.jena.sparql.core.Var) CSVParser(org.apache.jena.atlas.csv.CSVParser)

Aggregations

CSVParser (org.apache.jena.atlas.csv.CSVParser)10 BaseTest (org.apache.jena.propertytable.BaseTest)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 Node (org.apache.jena.graph.Node)3 Var (org.apache.jena.sparql.core.Var)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Function (java.util.function.Function)1 ARQException (org.apache.jena.sparql.ARQException)1 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)1