org.apache.jena.propertytable.impl.PropertyTableBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jena-csv Show documentation
Show all versions of jena-csv Show documentation
jena-csv is for getting CSVs into a form that is amenable to Jena SPARQL processing, and doing so in a way that is not specific to CSV files. It includes getting the right architecture in place for regular table shaped data, using the core abstraction of PropertyTable.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jena.propertytable.impl;
import java.io.InputStream ;
import java.util.ArrayList ;
import java.util.Iterator ;
import java.util.List ;
import org.apache.jena.atlas.csv.CSVParser ;
import org.apache.jena.atlas.io.IO ;
import org.apache.jena.datatypes.xsd.XSDDatatype ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.NodeFactory ;
import org.apache.jena.lang.csv.ReaderRIOTCSV;
import org.apache.jena.propertytable.PropertyTable ;
import org.apache.jena.propertytable.Row ;
import org.apache.jena.riot.system.IRIResolver ;
/**
* A tool for constructing PropertyTable from a file (e.g., a CSV file).
*
*
*/
public class PropertyTableBuilder {
public static Node CSV_ROW_NODE = NodeFactory.createURI(ReaderRIOTCSV.CSV_ROW);
public static PropertyTable buildPropetyTableHashMapImplFromCsv(String csvFilePath) {
PropertyTable table = new PropertyTableHashMapImpl();
return fillPropertyTable(table, csvFilePath);
}
public static PropertyTable buildPropetyTableArrayImplFromCsv(String csvFilePath) {
PropertyTable table = createEmptyPropertyTableArrayImpl(csvFilePath);
return fillPropertyTable(table, csvFilePath);
}
private static PropertyTable createEmptyPropertyTableArrayImpl (String csvFilePath) {
CSVParser parser = CSVParser.create(csvFilePath);
List 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;
}
}
protected static PropertyTable fillPropertyTable(PropertyTable table, String csvFilePath ){
InputStream input = IO.openFile(csvFilePath) ;
CSVParser iterator = CSVParser.create(input) ;
return fillPropertyTable(table, iterator, csvFilePath);
}
protected static PropertyTable fillPropertyTable(PropertyTable table, CSVParser parser, String csvFilePath){
if (table == null){
return null;
}
ArrayList predicates = new ArrayList();
int rowNum = 0;
Iterator> iter = parser.iterator() ;
if ( ! iter.hasNext() )
return table ;
List row1 = iter.next() ;
table.createColumn(CSV_ROW_NODE);
for (String column : row1) {
String uri = createColumnKeyURI(csvFilePath, column);
Node p = NodeFactory.createURI(uri);
predicates.add(p);
table.createColumn(p);
}
rowNum++ ;
while(iter.hasNext()) {
List rowLine = iter.next();
Node subject = ReaderRIOTCSV.calculateSubject(rowNum, csvFilePath);
Row row = table.createRow(subject);
row.setValue(table.getColumn(CSV_ROW_NODE),
NodeFactory.createLiteral((rowNum + ""), XSDDatatype.XSDinteger));
for (int col = 0; col < rowLine.size() && col
© 2015 - 2024 Weber Informatics LLC | Privacy Policy