From 5b15e5edacfed24d15f7ff8effdb91c29e4e7f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Sun, 13 Nov 2022 15:50:41 +0100 Subject: [PATCH] RdfContentGenerator needs close() --- .../content/rdf/RdfXContentGenerator.java | 16 ++-- .../xbib/content/rdf/RouteRdfXContent.java | 4 +- .../rdf/RouteRdfXContentGenerator.java | 3 +- .../rdf/io/json/JsonContentGenerator.java | 35 ++++----- .../rdf/io/rdfxml/RdfXmlContentGenerator.java | 41 ++++------ .../rdf/io/rdfxml/RdfXmlContentParser.java | 12 ++- .../content/rdf/RdfXContentGeneratorTest.java | 20 +++-- .../rdf/RouteRdfXContentBuilderTest.java | 29 +++++-- .../content/rdf/io/json/JsonReaderTest.java | 2 - .../rdf/io/ntriple/NTripleReaderTest.java | 7 +- .../xbib/content/rdf/io/ntriple/rdfxml.ttl | 78 +++++++++++++++++++ gradle.properties | 2 +- 12 files changed, 166 insertions(+), 83 deletions(-) create mode 100644 content-rdf/src/test/resources/org/xbib/content/rdf/io/ntriple/rdfxml.ttl diff --git a/content-rdf/src/main/java/org/xbib/content/rdf/RdfXContentGenerator.java b/content-rdf/src/main/java/org/xbib/content/rdf/RdfXContentGenerator.java index 3d752d2..2e5548a 100644 --- a/content-rdf/src/main/java/org/xbib/content/rdf/RdfXContentGenerator.java +++ b/content-rdf/src/main/java/org/xbib/content/rdf/RdfXContentGenerator.java @@ -55,7 +55,7 @@ public class RdfXContentGenerator

implements RdfCon @Override public RdfContentGenerator

setBaseUri(String baseUri) { - startPrefixMapping("", baseUri); + params.getNamespaceContext().addNamespace("", baseUri); return this; } @@ -92,8 +92,11 @@ public class RdfXContentGenerator

implements RdfCon } @Override - public void close() throws IOException { - flush(); + public RdfXContentGenerator

receive(Resource resource) throws IOException { + if (resource != null) { + this.resource = resource; + } + return this; } @Override @@ -102,7 +105,6 @@ public class RdfXContentGenerator

implements RdfCon return; } flushed = true; - // JSON output builder = DefaultXContentBuilder.builder(JsonXContent.jsonContent(), out); builder.startObject(); build(this.resource); @@ -110,12 +112,8 @@ public class RdfXContentGenerator

implements RdfCon } @Override - public RdfXContentGenerator

receive(Resource resource) throws IOException { - if (resource != null) { - this.resource = resource; - } + public void close() throws IOException { flush(); - return this; } public String string() throws IOException { diff --git a/content-rdf/src/main/java/org/xbib/content/rdf/RouteRdfXContent.java b/content-rdf/src/main/java/org/xbib/content/rdf/RouteRdfXContent.java index b9ca2e8..76fc1a0 100644 --- a/content-rdf/src/main/java/org/xbib/content/rdf/RouteRdfXContent.java +++ b/content-rdf/src/main/java/org/xbib/content/rdf/RouteRdfXContent.java @@ -28,12 +28,12 @@ public class RouteRdfXContent implements RdfContent { } @Override - public RdfContentGenerator createGenerator(OutputStream out) throws IOException { + public RdfContentGenerator createGenerator(OutputStream out) { return new RouteRdfXContentGenerator<>(out); } @Override - public RdfContentParser createParser(InputStream in) throws IOException { + public RdfContentParser createParser(InputStream in) { throw new UnsupportedOperationException(); } diff --git a/content-rdf/src/main/java/org/xbib/content/rdf/RouteRdfXContentGenerator.java b/content-rdf/src/main/java/org/xbib/content/rdf/RouteRdfXContentGenerator.java index efcf7c4..de0d9e7 100644 --- a/content-rdf/src/main/java/org/xbib/content/rdf/RouteRdfXContentGenerator.java +++ b/content-rdf/src/main/java/org/xbib/content/rdf/RouteRdfXContentGenerator.java @@ -13,7 +13,7 @@ public class RouteRdfXContentGenerator extends private boolean flushed; - RouteRdfXContentGenerator(OutputStream out) throws IOException { + RouteRdfXContentGenerator(OutputStream out) { super(out); } @@ -65,5 +65,4 @@ public class RouteRdfXContentGenerator extends getParams().setId(object.toString()); } } - } diff --git a/content-rdf/src/main/java/org/xbib/content/rdf/io/json/JsonContentGenerator.java b/content-rdf/src/main/java/org/xbib/content/rdf/io/json/JsonContentGenerator.java index 5b8e840..9fc1fed 100644 --- a/content-rdf/src/main/java/org/xbib/content/rdf/io/json/JsonContentGenerator.java +++ b/content-rdf/src/main/java/org/xbib/content/rdf/io/json/JsonContentGenerator.java @@ -16,9 +16,6 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.Map; -/** - * - */ public class JsonContentGenerator implements RdfContentGenerator, Flushable { private final Writer writer; @@ -29,11 +26,11 @@ public class JsonContentGenerator implements RdfContentGenerator entry : params.getNamespaceContext().getNamespaces().entrySet()) { if (entry.getValue().length() > 0) { String nsURI = entry.getValue(); @@ -108,16 +114,5 @@ public class JsonContentGenerator implements RdfContentGenerator setBaseUri(String baseUri) { - startPrefixMapping("", baseUri); + params.getNamespaceContext().addNamespace("", baseUri); return this; } @@ -111,7 +117,7 @@ public class RdfXmlContentGenerator implements RdfContentGenerator entry : params.getNamespaceContext().getNamespaces().entrySet()) { - handleNamespace(entry.getKey(), entry.getValue()); + setNamespace(entry.getKey(), entry.getValue()); } startRDF(); writeHeader(); @@ -122,11 +128,6 @@ public class RdfXmlContentGenerator implements RdfContentGenerator\n"); writeStartOfStartTag(NS_URI, "RDF"); for (Map.Entry entry : params.getNamespaceContext().getNamespaces().entrySet()) { @@ -178,11 +179,7 @@ public class RdfXmlContentGenerator implements RdfContentGenerator= 0) { - buf.append(text.substring(prevIndex, oldsIndex)); + buf.append(text, prevIndex, oldsIndex); buf.append(news); prevIndex = oldsIndex + olds.length(); oldsIndex = text.indexOf(olds, prevIndex); diff --git a/content-rdf/src/main/java/org/xbib/content/rdf/io/rdfxml/RdfXmlContentParser.java b/content-rdf/src/main/java/org/xbib/content/rdf/io/rdfxml/RdfXmlContentParser.java index 4fcd802..0c1e541 100644 --- a/content-rdf/src/main/java/org/xbib/content/rdf/io/rdfxml/RdfXmlContentParser.java +++ b/content-rdf/src/main/java/org/xbib/content/rdf/io/rdfxml/RdfXmlContentParser.java @@ -69,14 +69,22 @@ public class RdfXmlContentParser implements RdfConst // counter for blank node generation private int bn = 0; - public RdfXmlContentParser(InputStream in) { - this(new InputStreamReader(in, StandardCharsets.UTF_8)); + public RdfXmlContentParser(InputStream inputStream) { + this(createReader(inputStream)); } public RdfXmlContentParser(Reader reader) { this.reader = reader; } + private static Reader createReader(InputStream inputStream) { + if (inputStream != null) { + return new InputStreamReader(inputStream, StandardCharsets.UTF_8); + } else { + return null; + } + } + @Override public RdfContentType contentType() { return StandardRdfContentType.RDFXML; diff --git a/content-rdf/src/test/java/org/xbib/content/rdf/RdfXContentGeneratorTest.java b/content-rdf/src/test/java/org/xbib/content/rdf/RdfXContentGeneratorTest.java index d325a63..f4d171b 100644 --- a/content-rdf/src/test/java/org/xbib/content/rdf/RdfXContentGeneratorTest.java +++ b/content-rdf/src/test/java/org/xbib/content/rdf/RdfXContentGeneratorTest.java @@ -22,8 +22,9 @@ public class RdfXContentGeneratorTest { .add("urn:date", l) .add("urn:link", IRI.create("urn:pointer")); RdfXContentParams params = new RdfXContentParams(); - RdfContentBuilder builder = rdfXContentBuilder(params); - builder.receive(resource); + try (RdfContentBuilder builder = rdfXContentBuilder(params)) { + builder.receive(resource); + } String result = params.getGenerator().get(); assertEquals(result, "{\"urn:property\":\"Hello World\",\"urn:date\":2013,\"urn:link\":\"urn:pointer\"}"); @@ -40,8 +41,9 @@ public class RdfXContentGeneratorTest { .newResource("urn:embedded") .add("rdf:type", IRI.create("urn:type2")); RdfXContentParams params = new RdfXContentParams(); - RdfContentBuilder builder = rdfXContentBuilder(params); - builder.receive(resource); + try (RdfContentBuilder builder = rdfXContentBuilder(params)) { + builder.receive(resource); + } String result = params.getGenerator().get(); assertEquals("{\"urn:property\":\"Hello World\",\"urn:date\":2013,\"rdf:type\":\"urn:type1\"," + "\"urn:embedded\":{\"rdf:type\":\"urn:type2\"}}", result); @@ -60,8 +62,9 @@ public class RdfXContentGeneratorTest { resource.newResource("urn:embedded2") .add("rdf:type", IRI.create("urn:type3")); RdfXContentParams params = new RdfXContentParams(); - RdfContentBuilder builder = rdfXContentBuilder(params); - builder.receive(resource); + try (RdfContentBuilder builder = rdfXContentBuilder(params)) { + builder.receive(resource); + } String result = params.getGenerator().get(); assertEquals("{\"urn:property\":\"Hello World\",\"urn:date\":2013,\"rdf:type\":\"urn:type1\"," + "\"urn:embedded\":{\"rdf:type\":\"urn:type2\"},\"urn:embedded2\":{\"rdf:type\":\"urn:type3\"}}", result); @@ -77,8 +80,9 @@ public class RdfXContentGeneratorTest { .add("rdf:type", IRI.create("urn:type1")) .newResource("urn:embedded"); // empty resource, do not copy RdfXContentParams params = new RdfXContentParams(); - RdfContentBuilder builder = rdfXContentBuilder(params); - builder.receive(resource); + try (RdfContentBuilder builder = rdfXContentBuilder(params)) { + builder.receive(resource); + } String result = params.getGenerator().get(); assertEquals(result, "{\"urn:property\":\"Hello World\",\"urn:date\":2013,\"rdf:type\":\"urn:type1\"}"); diff --git a/content-rdf/src/test/java/org/xbib/content/rdf/RouteRdfXContentBuilderTest.java b/content-rdf/src/test/java/org/xbib/content/rdf/RouteRdfXContentBuilderTest.java index 5624609..67c4e1b 100644 --- a/content-rdf/src/test/java/org/xbib/content/rdf/RouteRdfXContentBuilderTest.java +++ b/content-rdf/src/test/java/org/xbib/content/rdf/RouteRdfXContentBuilderTest.java @@ -1,9 +1,15 @@ package org.xbib.content.rdf; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.xbib.content.rdf.RdfXContentFactory.routeRdfXContentBuilder; import static org.xbib.content.rdf.StreamTester.assertStream; +import java.text.MessageFormat; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.logging.Level; +import java.util.logging.Logger; import org.junit.jupiter.api.Test; import org.xbib.content.rdf.internal.DefaultLiteral; import org.xbib.content.rdf.internal.DefaultResource; @@ -15,6 +21,8 @@ import java.io.InputStream; public class RouteRdfXContentBuilderTest { + private static final Logger logger = Logger.getLogger(RouteRdfXContentBuilderTest.class.getName()); + @Test public void testRoute() throws Exception { Resource resource = new DefaultResource(IRI.create("urn:res")); @@ -24,11 +32,16 @@ public class RouteRdfXContentBuilderTest { .add("urn:date", l) .add("urn:link", IRI.create("urn:pointer")); RouteRdfXContentParams params = new RouteRdfXContentParams("index", "type"); - params.setHandler((content, p) -> assertEquals(p.getIndex() + " " + p.getType() + " 1 " + content, - "index type 1 {\"urn:property\":\"Hello World\",\"urn:date\":2013,\"urn:link\":\"urn:pointer\"}" - )); - RdfContentBuilder builder = routeRdfXContentBuilder(params); - builder.receive(resource); + AtomicBoolean found = new AtomicBoolean(); + params.setHandler((content, p) -> { + assertEquals(p.getIndex() + " " + p.getType() + " 1 " + content, + "index type 1 {\"urn:property\":\"Hello World\",\"urn:date\":2013,\"urn:link\":\"urn:pointer\"}"); + found.set(true); + }); + try (RdfContentBuilder builder = routeRdfXContentBuilder(params)) { + builder.receive(resource); + } + assertTrue(found.get()); } @Test @@ -39,8 +52,11 @@ public class RouteRdfXContentBuilderTest { } StringBuilder sb = new StringBuilder(); RouteRdfXContentParams params = new RouteRdfXContentParams("index", "type"); + AtomicInteger counter = new AtomicInteger(); params.setHandler((content, p) -> { - //logger.info("handle: {} {} {} {}", p.getIndex(), p.getType(), p.getId(), content); + logger.log(Level.INFO, MessageFormat.format("handle: {0} {1} {2} {3}", + p.getIndex(), p.getType(), p.getId(), content)); + counter.incrementAndGet(); }); new RdfXmlContentParser(in) .setRdfContentBuilderProvider(() -> routeRdfXContentBuilder(params)) @@ -53,5 +69,6 @@ public class RouteRdfXContentBuilderTest { .parse(); assertStream("viaf.json", getClass().getResourceAsStream("viaf.json"), sb.toString()); + assertEquals(5, counter.get()); } } diff --git a/content-rdf/src/test/java/org/xbib/content/rdf/io/json/JsonReaderTest.java b/content-rdf/src/test/java/org/xbib/content/rdf/io/json/JsonReaderTest.java index 76daeb3..dd53abb 100644 --- a/content-rdf/src/test/java/org/xbib/content/rdf/io/json/JsonReaderTest.java +++ b/content-rdf/src/test/java/org/xbib/content/rdf/io/json/JsonReaderTest.java @@ -27,14 +27,12 @@ public class JsonReaderTest extends StreamTester { if (in == null) { throw new IOException("file " + filename + " not found"); } - IRINamespaceContext namespaceContext = IRINamespaceContext.newInstance(); namespaceContext.addNamespace("dc", "http://purl.org/dc/elements/1.1/"); namespaceContext.addNamespace("dcterms", "http://purl.org/dc/terms/"); namespaceContext.addNamespace("bib", "info:srw/cql-context-set/1/bib-v1/"); namespaceContext.addNamespace("xbib", "http://xbib.org/"); namespaceContext.addNamespace("lia", "http://xbib.org/lia/"); - JsonContentParams params = new JsonContentParams(namespaceContext); JsonResourceHandler jsonHandler = new JsonResourceHandler(params) { diff --git a/content-rdf/src/test/java/org/xbib/content/rdf/io/ntriple/NTripleReaderTest.java b/content-rdf/src/test/java/org/xbib/content/rdf/io/ntriple/NTripleReaderTest.java index faf37ec..1976079 100644 --- a/content-rdf/src/test/java/org/xbib/content/rdf/io/ntriple/NTripleReaderTest.java +++ b/content-rdf/src/test/java/org/xbib/content/rdf/io/ntriple/NTripleReaderTest.java @@ -9,9 +9,6 @@ import org.xbib.content.rdf.StreamTester; import java.io.IOException; import java.io.InputStream; -/** - * - */ public class NTripleReaderTest extends StreamTester { @Test @@ -25,8 +22,8 @@ public class NTripleReaderTest extends StreamTester { NTripleContentParser reader = new NTripleContentParser<>(in); reader.setBuilder(builder); reader.parse(); - //assertStream(getClass().getResource("rdfxml.ttl").openStream(), - // builder.streamInput()); + assertStream("", getClass().getResource("rdfxml.ttl").openStream(), + builder.streamInput()); } } diff --git a/content-rdf/src/test/resources/org/xbib/content/rdf/io/ntriple/rdfxml.ttl b/content-rdf/src/test/resources/org/xbib/content/rdf/io/ntriple/rdfxml.ttl new file mode 100644 index 0000000..562b132 --- /dev/null +++ b/content-rdf/src/test/resources/org/xbib/content/rdf/io/ntriple/rdfxml.ttl @@ -0,0 +1,78 @@ + . + . + "Nobility--Ireland"@en . +_:genid1 . + . +_:genid2 . +_:genid1 _:genid2 . + . +_:genid2 . + _:genid1 . +_:genid3 . +_:genid3 . +_:genid3 "Ireland--Nobility"@en . +_:genid5 _:genid4 . +_:genid4 . +_:genid4 . +_:genid4 "Ireland"@en . +_:genid7 _:genid6 . +_:genid6 . +_:genid6 "Ireland" . +_:genid7 . +_:genid4 _:genid7 . +_:genid9 _:genid8 . +_:genid5 _:genid9 . +_:genid8 . +_:genid8 . +_:genid8 "Nobility"@en . +_:genid11 _:genid10 . +_:genid10 . +_:genid10 "Nobility" . +_:genid11 . +_:genid8 _:genid11 . +_:genid9 . +_:genid3 _:genid5 . + _:genid3 . +_:genid12 . +_:genid12 . +_:genid12 "Ireland--Peerage"@en . +_:genid14 _:genid13 . +_:genid13 . +_:genid13 . +_:genid13 "Ireland"@en . +_:genid16 _:genid15 . +_:genid15 . +_:genid15 "Ireland" . +_:genid16 . +_:genid13 _:genid16 . +_:genid18 _:genid17 . +_:genid14 _:genid18 . +_:genid17 . +_:genid17 . +_:genid17 "Peerage"@en . +_:genid20 _:genid19 . +_:genid19 . +_:genid19 "Peerage" . +_:genid20 . +_:genid17 _:genid20 . +_:genid18 . +_:genid12 _:genid14 . + _:genid12 . + . + . + . + . + . + . + . + . +_:genid21 . +_:genid21 "1986-02-11T00:00:00" . +_:genid21 "new" . +_:genid21 . + _:genid21 . +_:genid22 . +_:genid22 "2000-10-02T10:44:24" . +_:genid22 "revised" . +_:genid22 . + _:genid22 . diff --git a/gradle.properties b/gradle.properties index f73deb7..a11dddf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = content -version = 5.0.1 +version = 5.0.2 org.gradle.warning.mode = ALL