Merge pull request #1783 from ostephens/extend-rdf-import

Extend RDF imports
This commit is contained in:
Antonin Delpeuch 2018-10-27 22:12:48 +02:00 committed by GitHub
commit de77550a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 158 additions and 8 deletions

View File

@ -0,0 +1,43 @@
/*
Copyright 2012, Thomas F. Morris
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.
*/
package com.google.refine.importers;
public class RdfJsonldTripleImporter extends RdfTripleImporter {
public RdfJsonldTripleImporter() {
super(RdfTripleImporter.Mode.JSONLD);
}
}

View File

@ -62,7 +62,8 @@ public class RdfTripleImporter extends ImportingParserBase {
RDFXML,
NT,
N3,
TTL
TTL,
JSONLD
}
public RdfTripleImporter() {
@ -85,9 +86,14 @@ public class RdfTripleImporter extends ImportingParserBase {
model.read(input, null, "NT");
break;
case N3:
model.read(input, null, "N3");
break;
case TTL:
model.read(input, null, "TTL");
break;
case JSONLD:
model.read(input, null, "JSON-LD");
break;
case RDFXML:
model.read(input, null);
break;

View File

@ -233,5 +233,95 @@ public class RdfTripleImporterTests extends ImporterTest {
Assert.assertEquals(project.rows.get(0).cells.get(1).value, "http://meetings.example.com/cal#m1");
Assert.assertEquals(project.rows.get(0).cells.get(2).value, "mailto:fred@example.com");
Assert.assertEquals(project.rows.get(0).cells.get(3).value, "Fred");
}
@Test
public void canParseTtl() throws UnsupportedEncodingException {
String sampleRdf = "@prefix p: <http://www.example.org/personal_details#> .\n" +
"@prefix m: <http://www.example.org/meeting_organization#> .\n\n" +
"<http://www.example.org/people#fred>\n" +
"p:GivenName \"Fred\";\n" +
"p:hasEmail <mailto:fred@example.com>;\n" +
"m:attending <http://meetings.example.com/cal#m1> .\n";
InputStream input = new ByteArrayInputStream(sampleRdf.getBytes("UTF-8"));
SUT = new RdfTripleImporter(RdfTripleImporter.Mode.TTL);
parseOneFile(SUT, input);
Assert.assertEquals(project.columnModel.columns.size(), 4);
Assert.assertEquals(project.columnModel.columns.get(0).getName(), "subject");
Assert.assertEquals(project.columnModel.columns.get(1).getName(), "http://www.example.org/meeting_organization#attending");
Assert.assertEquals(project.columnModel.columns.get(2).getName(), "http://www.example.org/personal_details#hasEmail");
Assert.assertEquals(project.columnModel.columns.get(3).getName(), "http://www.example.org/personal_details#GivenName");
Assert.assertEquals(project.rows.size(), 1);
Assert.assertEquals(project.rows.get(0).cells.size(), 4);
Assert.assertEquals(project.rows.get(0).cells.get(0).value, "http://www.example.org/people#fred");
Assert.assertEquals(project.rows.get(0).cells.get(1).value, "http://meetings.example.com/cal#m1");
Assert.assertEquals(project.rows.get(0).cells.get(2).value, "mailto:fred@example.com");
Assert.assertEquals(project.rows.get(0).cells.get(3).value, "Fred");
}
@Test
public void canParseNTriples() throws UnsupportedEncodingException {
String sampleRdf = "<http://www.example.org/people#fred> <http://www.example.org/meeting_organization#attending> <http://meetings.example.com/cal#m1> . \n" +
"<http://www.example.org/people#fred> <http://www.example.org/personal_details#hasEmail> <mailto:fred@example.com> . \n" +
"<http://www.example.org/people#fred> <http://www.example.org/personal_details#GivenName> \"Fred\" . ";
InputStream input = new ByteArrayInputStream(sampleRdf.getBytes("UTF-8"));
SUT = new RdfTripleImporter(RdfTripleImporter.Mode.NT);
parseOneFile(SUT, input);
Assert.assertEquals(project.columnModel.columns.size(), 4);
Assert.assertEquals(project.columnModel.columns.get(0).getName(), "subject");
Assert.assertEquals(project.columnModel.columns.get(1).getName(), "http://www.example.org/personal_details#GivenName");
Assert.assertEquals(project.columnModel.columns.get(2).getName(), "http://www.example.org/personal_details#hasEmail");
Assert.assertEquals(project.columnModel.columns.get(3).getName(), "http://www.example.org/meeting_organization#attending");
Assert.assertEquals(project.rows.size(), 1);
Assert.assertEquals(project.rows.get(0).cells.size(), 4);
Assert.assertEquals(project.rows.get(0).cells.get(0).value, "http://www.example.org/people#fred");
Assert.assertEquals(project.rows.get(0).cells.get(1).value, "Fred");
Assert.assertEquals(project.rows.get(0).cells.get(2).value, "mailto:fred@example.com");
Assert.assertEquals(project.rows.get(0).cells.get(3).value, "http://meetings.example.com/cal#m1");
}
@Test
public void canParseJsonld() throws UnsupportedEncodingException {
String sampleJsonld = "{\n "+
" \"@context\": {\n "+
" \"m\": \"http://www.example.org/meeting_organization#\",\n "+
" \"p\": \"http://www.example.org/personal_details#\",\n "+
" \"rdf\": \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\",\n "+
" \"rdfs\": \"http://www.w3.org/2000/01/rdf-schema#\",\n "+
" \"xsd\": \"http://www.w3.org/2001/XMLSchema#\"\n "+
" },\n "+
" \"@id\": \"http://www.example.org/people#fred\",\n "+
" \"m:attending\": {\n "+
" \"@id\": \"http://meetings.example.com/cal#m1\"\n "+
" },\n "+
" \"p:GivenName\": \"Fred\",\n "+
" \"p:hasEmail\": {\n "+
" \"@id\": \"mailto:fred@example.com\"\n "+
" }\n "+
"}";
InputStream input = new ByteArrayInputStream(sampleJsonld.getBytes("UTF-8"));
SUT = new RdfTripleImporter(RdfTripleImporter.Mode.JSONLD);
parseOneFile(SUT, input);
Assert.assertEquals(project.columnModel.columns.size(), 4);
Assert.assertEquals(project.columnModel.columns.get(0).getName(), "subject");
Assert.assertEquals(project.columnModel.columns.get(1).getName(), "http://www.example.org/personal_details#hasEmail");
Assert.assertEquals(project.columnModel.columns.get(2).getName(), "http://www.example.org/personal_details#GivenName");
Assert.assertEquals(project.columnModel.columns.get(3).getName(), "http://www.example.org/meeting_organization#attending");
Assert.assertEquals(project.rows.size(), 1);
Assert.assertEquals(project.rows.get(0).cells.size(), 4);
Assert.assertEquals(project.rows.get(0).cells.get(0).value, "http://www.example.org/people#fred");
Assert.assertEquals(project.rows.get(0).cells.get(1).value, "mailto:fred@example.com");
Assert.assertEquals(project.rows.get(0).cells.get(2).value, "Fred");
Assert.assertEquals(project.rows.get(0).cells.get(3).value, "http://meetings.example.com/cal#m1");
}
}

View File

@ -213,13 +213,18 @@ function registerImporting() {
IM.registerFormat("text/line-based/fixed-width", "Fixed-width field text files", "FixedWidthParserUI",
new Packages.com.google.refine.importers.FixedWidthImporter());
IM.registerFormat("text/rdf+n3", "RDF/N3 files", "RdfTriplesParserUI",
IM.registerFormat("text/rdf/nt", "RDF/N-Triples files", "RdfTriplesParserUI",
new Packages.com.google.refine.importers.RdfTripleImporter(Packages.com.google.refine.importers.RdfTripleImporter.Mode.NT));
IM.registerFormat("text/rdf/n3", "RDF/N3 files", "RdfTriplesParserUI",
new Packages.com.google.refine.importers.RdfTripleImporter(Packages.com.google.refine.importers.RdfTripleImporter.Mode.N3));
IM.registerFormat("text/rdf/ttl", "RDF/Turtle files", "RdfTriplesParserUI",
new Packages.com.google.refine.importers.RdfTripleImporter(Packages.com.google.refine.importers.RdfTripleImporter.Mode.TTL));
IM.registerFormat("text/rdf/xml", "RDF/XML files", "RdfTriplesParserUI", new Packages.com.google.refine.importers.RdfXmlTripleImporter());
IM.registerFormat("text/rdf/ld+json", "JSON-LD files", "RdfTriplesParserUI", new Packages.com.google.refine.importers.RdfJsonldTripleImporter());
IM.registerFormat("text/xml", "XML files", "XmlParserUI", new Packages.com.google.refine.importers.XmlImporter());
IM.registerFormat("binary/text/xml/xls/xlsx", "Excel files", "ExcelParserUI", new Packages.com.google.refine.importers.ExcelImporter());
IM.registerFormat("text/xml/ods", "Open Document Format spreadsheets (.ods)", "ExcelParserUI", new Packages.com.google.refine.importers.OdsImporter());
IM.registerFormat("text/xml/rdf", "RDF/XML files", "RdfTriplesParserUI", new Packages.com.google.refine.importers.RdfXmlTripleImporter());
IM.registerFormat("text/json", "JSON files", "JsonParserUI", new Packages.com.google.refine.importers.JsonImporter());
IM.registerFormat("text/marc", "MARC files", "XmlParserUI", new Packages.com.google.refine.importers.MarcImporter());
IM.registerFormat("text/wiki", "Wikitext", "WikitextParserUI", new Packages.com.google.refine.importers.WikitextImporter());
@ -236,7 +241,6 @@ function registerImporting() {
IM.registerExtension(".tsv", "text/line-based/*sv");
IM.registerExtension(".xml", "text/xml");
IM.registerExtension(".rdf", "text/xml/rdf");
IM.registerExtension(".json", "text/json");
IM.registerExtension(".js", "text/json");
@ -246,7 +250,11 @@ function registerImporting() {
IM.registerExtension(".ods", "text/xml/ods");
IM.registerExtension(".n3", "text/rdf+n3");
IM.registerExtension(".nt", "text/rdf/nt");
IM.registerExtension(".n3", "text/rdf/n3");
IM.registerExtension(".ttl", "text/rdf/ttl");
IM.registerExtension(".jsonld", "text/rdf/ld+json");
IM.registerExtension(".rdf", "text/rdf/xml");
IM.registerExtension(".marc", "text/marc");
IM.registerExtension(".mrc", "text/marc");
@ -263,7 +271,12 @@ function registerImporting() {
IM.registerMimeType("text/fixed-width", "text/line-based/fixed-width");
IM.registerMimeType("text/rdf+n3", "text/rdf+n3");
IM.registerMimeType("application/n-triples", "text/rdf/nt");
IM.registerMimeType("text/n3", "text/rdf/n3");
IM.registerMimeType("text/rdf+n3", "text/rdf/n3");
IM.registerMimeType("text/turtle", "text/rdf/ttl");
IM.registerMimeType("application/rdf+xml", "text/rdf/xml");
IM.registerMimeType("application/ld+json", "text/rdf/ld+json");
IM.registerMimeType("application/msexcel", "binary/text/xml/xls/xlsx");
IM.registerMimeType("application/x-msexcel", "binary/text/xml/xls/xlsx");
@ -279,8 +292,6 @@ function registerImporting() {
IM.registerMimeType("application/javascript", "text/json");
IM.registerMimeType("text/json", "text/json");
IM.registerMimeType("application/rdf+xml", "text/xml/rdf");
IM.registerMimeType("application/marc", "text/marc");
IM.registerUrlRewriter(new Packages.com.google.refine.model.metadata.DataPackageUrlRewriter());