From 1f34fbbec835176538cf8a26f845d3a8f8224828 Mon Sep 17 00:00:00 2001 From: Florian Kleedorfer Date: Sun, 22 Sep 2024 20:17:32 +0200 Subject: [PATCH] Fix leading zeros in integer literals The formatter used to swallow leading zeros in integer literals, as in the triple, ``` :thing :value 060 . ``` With this change, by rendering the lexical form of the literal, the exact input form is reproduced. --- .../turtle/formatter/TurtleFormatter.java | 2 +- .../turtle/formatter/TurtleFormatterTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java b/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java index ff3629d..5fcc3ef 100644 --- a/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java +++ b/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java @@ -640,7 +640,7 @@ private State writeLiteral( final Literal literal, final State state ) { return state.write( literal.getLexicalForm() ); } if ( datatypeUri.equals( XSD.integer.getURI() ) ) { - return state.write( literal.getValue().toString() ); + return state.write( literal.getLexicalForm() ); } if ( datatypeUri.equals( RDF.langString.getURI() ) ) { return state.write( quoteAndEscape( literal ) + "@" + literal.getLanguage() ); diff --git a/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java b/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java index f3ce6f8..ea50f15 100644 --- a/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java +++ b/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java @@ -1066,6 +1066,24 @@ public void testDisableDoubleFormatting() { assertThat(result.trim()).isEqualTo(modelString); } + @Test + public void testIntegerLiteralWithLeadingZeros(){ + String content = """ + @prefix : . + :thing :value 060. + + """; + String expected = """ + @prefix xsd: . + @prefix : . + + :thing :value 060 ."""; + 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 ) );