Skip to content

Commit

Permalink
Render rdf:type correctly when used as an object
Browse files Browse the repository at this point in the history
  • Loading branch information
atextor committed Mar 1, 2024
1 parent fe32ace commit f156a12
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,6 @@ private State writeAnonymousResource( final Resource resource, final State state
}

private String uriResource( final Resource resource, final State state ) {
if ( resource.getURI().equals( RDF.type.getURI() ) && style.useAForRdfType ) {
return "a";
}

final String uri = resource.getURI();
// Workaround to force writing out URIs without a base that is "automatically determined" by Jena:
// when calling model.read(inputStream, base, language) and passing an empty String as base, Jena will
Expand Down Expand Up @@ -576,6 +572,9 @@ private State writeRdfNode( final RDFNode node, final State state ) {
}

private State writeProperty( final Property property, final State state ) {
if ( property.getURI().equals( RDF.type.getURI() ) && style.useAForRdfType ) {
return state.write( "a" );
}
return writeUriResource( property, state );
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,26 @@ void testRespectEndOfLineConfig() {
}
}

@Test
void testRdfTypeAsObjectIsValid() {
final String modelString = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <http://example.com/> .
:foo a :bar ;
:something rdf:type .
""";
final Model model = modelFromString( modelString );

final FormattingStyle style = FormattingStyle.builder()
.useAForRdfType( true )
.knownPrefixes( Set.of() )
.build();
final TurtleFormatter formatter = new TurtleFormatter( style );
final String result = formatter.apply( model );
assertThat( result.trim() ).isEqualTo( modelString.trim() );
}

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 f156a12

Please sign in to comment.