From be487f3880041048bff04d2e814c4c4c4e4e32a8 Mon Sep 17 00:00:00 2001 From: Florian Kleedorfer Date: Thu, 7 Nov 2024 12:26:10 +0100 Subject: [PATCH] Fix list formatting problem Situation before this commit: If an rdf list node has additional triples, those triples are lost when it gets formatted. With this commit, TurtleFormatter.isListNode(node) returns false if the node has other properties than rdf:first and rdf:rest. --- .../turtle/formatter/TurtleFormatter.java | 16 ++++++++-- .../turtle/formatter/TurtleFormatterTest.java | 32 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java b/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java index 21345e0..f9f184c 100644 --- a/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java +++ b/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java @@ -468,8 +468,20 @@ private State writeClosingSquareBracket( final State state ) { } private boolean isList( final RDFNode node, final State state ) { - return node.equals( RDF.nil ) || - ( node.isAnon() && state.model.contains( node.asResource(), RDF.rest, (RDFNode) null ) ); + if (!node.isResource()){ + return false; + } + boolean listNodeHasAdditionalTriples = state.model.listStatements(node.asResource(), null, (RDFNode) null) + .toList() + .stream() + .map(Statement::getPredicate) + .filter(p -> ! p.equals(RDF.first)) + .anyMatch(p -> ! p.equals(RDF.rest)); + if (listNodeHasAdditionalTriples){ + return false; + } + return ( node.isAnon() + && state.model.contains( node.asResource(), RDF.rest, (RDFNode) null ) ); } private State writeResource( final Resource resource, final State state ) { diff --git a/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java b/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java index 5d90679..3d593a1 100644 --- a/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java +++ b/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java @@ -1157,6 +1157,38 @@ public void testDecimalLiteralWithFractions(){ } + @Test + public void testListNodeWithAdditionalTriples(){ + String content = """ + @prefix : . + @prefix rdf: . + :thing :hasList [ + a :SomeListClass ; + a rdf:List ; + :comment "a very special list"; + rdf:first 1 ; + rdf:rest ( 2 3 4 ); + ] . + """; + String expected = """ + @prefix rdf: . + @prefix xsd: . + @prefix : . + + :thing :hasList [ + a :SomeListClass, rdf:List ; + :comment "a very special list" ; + rdf:first 1 ; + rdf:rest ( 2 3 4 ) ; + ] ."""; + final FormattingStyle style = FormattingStyle.DEFAULT; + final TurtleFormatter formatter = new TurtleFormatter(style); + final String result = formatter.applyToContent(content); + assertThat(result.trim()).isEqualTo(expected); + } + + + private Model modelFromString( final String content ) { final Model model = ModelFactory.createDefaultModel(); final InputStream stream = new ByteArrayInputStream( content.getBytes( StandardCharsets.UTF_8 ) );