From e85f7d36e051e900a387b663bd061c9acb6bcf8b Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Sun, 28 Oct 2018 15:19:22 +0100 Subject: [PATCH] Add Wikibase schema exporter, for issue #1776 --- .../wikidata/exporters/SchemaExporter.java | 31 +++++++++++++++++++ .../exporters/SchemaExporterTest.java | 30 ++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 extensions/wikidata/src/org/openrefine/wikidata/exporters/SchemaExporter.java create mode 100644 extensions/wikidata/tests/src/org/openrefine/wikidata/exporters/SchemaExporterTest.java diff --git a/extensions/wikidata/src/org/openrefine/wikidata/exporters/SchemaExporter.java b/extensions/wikidata/src/org/openrefine/wikidata/exporters/SchemaExporter.java new file mode 100644 index 000000000..59698a3ef --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/exporters/SchemaExporter.java @@ -0,0 +1,31 @@ +package org.openrefine.wikidata.exporters; + +import java.io.IOException; +import java.io.Writer; +import java.util.Properties; + +import org.json.JSONWriter; +import org.openrefine.wikidata.schema.WikibaseSchema; + +import com.google.refine.browsing.Engine; +import com.google.refine.exporters.WriterExporter; +import com.google.refine.model.Project; + +public class SchemaExporter implements WriterExporter { + + @Override + public String getContentType() { + return "application/octet-stream"; + } + + @Override + public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException { + WikibaseSchema schema = (WikibaseSchema) project.overlayModels.get("wikibaseSchema"); + if (schema == null) { + schema = new WikibaseSchema(); + } + JSONWriter jsonWriter = new JSONWriter(writer); + schema.write(jsonWriter, new Properties()); + } + +} diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/exporters/SchemaExporterTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/exporters/SchemaExporterTest.java new file mode 100644 index 000000000..16d7740c1 --- /dev/null +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/exporters/SchemaExporterTest.java @@ -0,0 +1,30 @@ +package org.openrefine.wikidata.exporters; + +import static org.openrefine.wikidata.testing.JacksonSerializationTest.assertJsonEquals; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Properties; + +import org.testng.annotations.Test; + +import com.google.refine.browsing.Engine; +import com.google.refine.model.Project; +import com.google.refine.tests.RefineTest; + +public class SchemaExporterTest extends RefineTest { + + private SchemaExporter exporter = new SchemaExporter(); + + @Test + public void testNoSchema() + throws IOException { + Project project = this.createCSVProject("a,b\nc,d"); + Engine engine = new Engine(project); + StringWriter writer = new StringWriter(); + Properties properties = new Properties(); + exporter.export(project, properties, engine, writer); + assertJsonEquals("{\"itemDocuments\":[]}", writer.toString()); + } + +}