2010-10-20 22:45:52 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright 2010, Google Inc.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the following disclaimer
|
|
|
|
in the documentation and/or other materials provided with the
|
|
|
|
distribution.
|
|
|
|
* Neither the name of Google Inc. nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
package com.google.refine.importers;
|
2010-05-18 14:41:40 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
import java.io.IOException;
|
2010-05-18 14:41:40 +02:00
|
|
|
import java.io.Reader;
|
2010-05-19 06:22:45 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2010-05-18 14:41:40 +02:00
|
|
|
import java.util.Properties;
|
2010-05-19 06:22:45 +02:00
|
|
|
import java.util.Map.Entry;
|
2010-05-18 14:41:40 +02:00
|
|
|
|
|
|
|
import org.jrdf.JRDFFactory;
|
|
|
|
import org.jrdf.SortedMemoryJRDFFactory;
|
|
|
|
import org.jrdf.collection.MemMapFactory;
|
|
|
|
import org.jrdf.graph.Graph;
|
|
|
|
import org.jrdf.graph.Triple;
|
2010-08-06 07:04:25 +02:00
|
|
|
import org.jrdf.parser.ParseException;
|
|
|
|
import org.jrdf.parser.StatementHandlerException;
|
2010-05-18 14:41:40 +02:00
|
|
|
import org.jrdf.parser.line.GraphLineParser;
|
|
|
|
import org.jrdf.parser.line.LineHandler;
|
|
|
|
import org.jrdf.parser.ntriples.NTriplesParserFactory;
|
|
|
|
import org.jrdf.util.ClosableIterable;
|
|
|
|
import static org.jrdf.graph.AnyObjectNode.ANY_OBJECT_NODE;
|
|
|
|
import static org.jrdf.graph.AnyPredicateNode.ANY_PREDICATE_NODE;
|
|
|
|
import static org.jrdf.graph.AnySubjectNode.ANY_SUBJECT_NODE;
|
|
|
|
|
2010-09-22 19:04:10 +02:00
|
|
|
import com.google.refine.ProjectMetadata;
|
|
|
|
import com.google.refine.expr.ExpressionUtils;
|
|
|
|
import com.google.refine.model.Cell;
|
|
|
|
import com.google.refine.model.Column;
|
|
|
|
import com.google.refine.model.ModelException;
|
|
|
|
import com.google.refine.model.Project;
|
|
|
|
import com.google.refine.model.Row;
|
2010-05-18 14:41:40 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
public class RdfTripleImporter implements ReaderImporter{
|
|
|
|
private JRDFFactory _jrdfFactory;
|
|
|
|
private NTriplesParserFactory _nTriplesParserFactory;
|
|
|
|
private MemMapFactory _newMapFactory;
|
2010-05-18 14:41:40 +02:00
|
|
|
|
|
|
|
public RdfTripleImporter(){
|
2010-08-06 07:04:25 +02:00
|
|
|
_jrdfFactory = SortedMemoryJRDFFactory.getFactory();
|
|
|
|
_nTriplesParserFactory = new NTriplesParserFactory();
|
|
|
|
_newMapFactory = new MemMapFactory();
|
2010-05-18 14:41:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-09-17 03:00:23 +02:00
|
|
|
public void read(Reader reader, Project project, ProjectMetadata metadata, Properties options) throws ImportException {
|
2010-05-18 14:41:40 +02:00
|
|
|
String baseUrl = options.getProperty("base-url");
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
Graph graph = _jrdfFactory.getNewGraph();
|
|
|
|
LineHandler lineHandler = _nTriplesParserFactory.createParser(graph, _newMapFactory);
|
2010-05-18 14:41:40 +02:00
|
|
|
GraphLineParser parser = new GraphLineParser(graph, lineHandler);
|
2010-08-06 07:04:25 +02:00
|
|
|
try {
|
|
|
|
parser.parse(reader, baseUrl); // fills JRDF graph
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new ImportException("i/o error while parsing RDF",e);
|
|
|
|
} catch (ParseException e) {
|
|
|
|
throw new ImportException("error parsing RDF",e);
|
|
|
|
} catch (StatementHandlerException e) {
|
|
|
|
throw new ImportException("error parsing RDF",e);
|
|
|
|
}
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2010-05-19 06:22:45 +02:00
|
|
|
Map<String, List<Row>> subjectToRows = new HashMap<String, List<Row>>();
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2010-05-19 06:22:45 +02:00
|
|
|
Column subjectColumn = new Column(0, "subject");
|
|
|
|
project.columnModel.columns.add(0, subjectColumn);
|
|
|
|
project.columnModel.setKeyColumnIndex(0);
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2010-05-18 14:41:40 +02:00
|
|
|
ClosableIterable<Triple> triples = graph.find(ANY_SUBJECT_NODE, ANY_PREDICATE_NODE, ANY_OBJECT_NODE);
|
|
|
|
try {
|
|
|
|
for (Triple triple : triples) {
|
|
|
|
String subject = triple.getSubject().toString();
|
|
|
|
String predicate = triple.getPredicate().toString();
|
|
|
|
String object = triple.getObject().toString();
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2010-05-19 06:22:45 +02:00
|
|
|
Column column = project.columnModel.getColumnByName(predicate);
|
|
|
|
if (column == null) {
|
2010-08-06 07:04:25 +02:00
|
|
|
column = new Column(project.columnModel.allocateNewCellIndex(), predicate);
|
|
|
|
try {
|
|
|
|
project.columnModel.addColumn(-1, column, true);
|
|
|
|
} catch (ModelException e) {
|
|
|
|
// ignore
|
|
|
|
}
|
2010-05-18 23:08:37 +02:00
|
|
|
}
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2010-05-19 06:22:45 +02:00
|
|
|
int cellIndex = column.getCellIndex();
|
|
|
|
if (subjectToRows.containsKey(subject)) {
|
2010-08-06 07:04:25 +02:00
|
|
|
List<Row> rows = subjectToRows.get(subject);
|
|
|
|
for (Row row : rows) {
|
|
|
|
if (!ExpressionUtils.isNonBlankData(row.getCellValue(cellIndex))) {
|
|
|
|
row.setCell(cellIndex, new Cell(object, null));
|
|
|
|
object = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object != null) {
|
|
|
|
Row row = new Row(project.columnModel.getMaxCellIndex() + 1);
|
|
|
|
rows.add(row);
|
|
|
|
|
|
|
|
row.setCell(cellIndex, new Cell(object, null));
|
|
|
|
}
|
2010-05-19 06:22:45 +02:00
|
|
|
} else {
|
2010-08-06 07:04:25 +02:00
|
|
|
List<Row> rows = new ArrayList<Row>();
|
|
|
|
subjectToRows.put(subject, rows);
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
Row row = new Row(project.columnModel.getMaxCellIndex() + 1);
|
|
|
|
rows.add(row);
|
2010-05-26 15:18:48 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
row.setCell(subjectColumn.getCellIndex(), new Cell(subject, null));
|
|
|
|
row.setCell(cellIndex, new Cell(object, null));
|
2010-05-18 15:48:52 +02:00
|
|
|
}
|
2010-05-18 14:41:40 +02:00
|
|
|
}
|
|
|
|
|
2010-05-19 06:22:45 +02:00
|
|
|
for (Entry<String, List<Row>> entry : subjectToRows.entrySet()) {
|
2010-08-06 07:04:25 +02:00
|
|
|
project.rows.addAll(entry.getValue());
|
2010-05-19 06:22:45 +02:00
|
|
|
}
|
2010-05-18 14:41:40 +02:00
|
|
|
} finally {
|
|
|
|
triples.iterator().close();
|
|
|
|
}
|
|
|
|
}
|
2010-05-18 15:48:52 +02:00
|
|
|
|
2010-08-06 07:04:25 +02:00
|
|
|
|
2010-05-18 14:41:40 +02:00
|
|
|
@Override
|
2010-05-26 15:18:48 +02:00
|
|
|
public boolean canImportData(String contentType, String fileName) {
|
|
|
|
if (contentType != null) {
|
|
|
|
contentType = contentType.toLowerCase().trim();
|
|
|
|
|
|
|
|
if("application/rdf+xml".equals(contentType)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (fileName != null) {
|
|
|
|
fileName = fileName.toLowerCase();
|
|
|
|
if (
|
|
|
|
fileName.endsWith(".rdf")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-18 14:41:40 +02:00
|
|
|
}
|