Skip to content

Commit

Permalink
Merge pull request #42 from fkleedorfer/fix-additional-properties-on-…
Browse files Browse the repository at this point in the history
…list-node

Fix list formatting problem
  • Loading branch information
atextor authored Nov 8, 2024
2 parents 2585540 + be487f3 commit c45393d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,38 @@ public void testDecimalLiteralWithFractions(){
}


@Test
public void testListNodeWithAdditionalTriples(){
String content = """
@prefix : <http://example.com/ns#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
:thing :hasList [
a :SomeListClass ;
a rdf:List ;
:comment "a very special list";
rdf:first 1 ;
rdf:rest ( 2 3 4 );
] .
""";
String expected = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix : <http://example.com/ns#> .
: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 ) );
Expand Down

0 comments on commit c45393d

Please sign in to comment.