diff --git a/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdWriter.java b/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdFrameWriter.java similarity index 91% rename from ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdWriter.java rename to ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdFrameWriter.java index 45049648e..2f9e23ff7 100644 --- a/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdWriter.java +++ b/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdFrameWriter.java @@ -13,10 +13,10 @@ import static be.vlaanderen.informatievlaanderen.ldes.ldi.rdf.formatter.PrefixAdder.addPrefixesToModel; -public class JsonLdWriter implements LdiRdfWriter { +public class JsonLdFrameWriter implements LdiRdfWriter { private final String frame; - public JsonLdWriter(LdiRdfWriterProperties properties) { + public JsonLdFrameWriter(LdiRdfWriterProperties properties) { this.frame = properties.getJsonLdFrame(); } diff --git a/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdPrettyWriter.java b/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdPrettyWriter.java new file mode 100644 index 000000000..d4673a156 --- /dev/null +++ b/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/JsonLdPrettyWriter.java @@ -0,0 +1,18 @@ +package be.vlaanderen.informatievlaanderen.ldes.ldi.rdf.formatter; + +import org.apache.jena.rdf.model.Model; +import org.apache.jena.riot.RDFFormat; +import org.apache.jena.riot.RDFWriter; + +import static be.vlaanderen.informatievlaanderen.ldes.ldi.rdf.formatter.PrefixAdder.addPrefixesToModel; + +public class JsonLdPrettyWriter implements LdiRdfWriter { + + @Override + public String write(Model model) { + return RDFWriter.source(addPrefixesToModel(model)) + .format(RDFFormat.JSONLD10_PRETTY) + .asString(); + } + +} diff --git a/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/LdiRdfWriter.java b/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/LdiRdfWriter.java index 12d730125..9ddc78f6a 100644 --- a/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/LdiRdfWriter.java +++ b/ldi-core/ldi-common/src/main/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/LdiRdfWriter.java @@ -3,12 +3,16 @@ import org.apache.jena.rdf.model.Model; import org.apache.jena.riot.Lang; +import static org.apache.commons.lang3.StringUtils.isBlank; + public interface LdiRdfWriter { String write(Model model); static LdiRdfWriter getRdfWriter(LdiRdfWriterProperties properties) { if (Lang.JSONLD.equals(properties.getLang())) { - return new JsonLdWriter(properties); + return isBlank(properties.getJsonLdFrame()) + ? new JsonLdPrettyWriter() + : new JsonLdFrameWriter(properties); } else { return new GenericRdfWriter(properties); } diff --git a/ldi-core/ldi-common/src/test/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/LdiRdfWriterTest.java b/ldi-core/ldi-common/src/test/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/LdiRdfWriterTest.java index 189458eb5..0ceeeef6e 100644 --- a/ldi-core/ldi-common/src/test/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/LdiRdfWriterTest.java +++ b/ldi-core/ldi-common/src/test/java/be/vlaanderen/informatievlaanderen/ldes/ldi/rdf/formatter/LdiRdfWriterTest.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.*; public class LdiRdfWriterTest { + @Test void formatModel_jsonLD() throws IOException, URISyntaxException { String input = getFileContentString("rdf/formatter/product.jsonld"); @@ -45,6 +46,23 @@ void formatModel_jsonLD() throws IOException, URISyntaxException { assertTrue(outputModel.isIsomorphicWith(expected)); } + @Test + void formatModel_jsonLD_withoutFrame() throws IOException, URISyntaxException { + String input = getFileContentString("rdf/formatter/product.jsonld"); + + Model model = RDFParser.fromString(input) + .lang(Lang.JSONLD) + .toModel(); + + LdiRdfWriterProperties writerProperties = new LdiRdfWriterProperties(); + + String output = LdiRdfWriter.getRdfWriter(writerProperties.withLang(Lang.JSONLD)).write(model); + + JsonObject outputJson = JSON.parse(output); + + assertNotNull(outputJson); + } + @Test void formatModel_turtle() throws IOException, URISyntaxException { String input = getFileContentString("rdf/formatter/product.jsonld"); @@ -88,7 +106,7 @@ void getFramedContext() { } """; - JsonLDWriteContext context = (JsonLDWriteContext) JsonLdWriter.getFramedContext(frame); + JsonLDWriteContext context = (JsonLDWriteContext) JsonLdFrameWriter.getFramedContext(frame); JsonObject frameObject = JSON.parse((String) context.get(JsonLD10Writer.JSONLD_FRAME)); assertTrue(frameObject.hasKey("@type"));