diff --git a/README.md b/README.md index ae9580a..ff40e17 100644 --- a/README.md +++ b/README.md @@ -309,6 +309,23 @@ A [NumberFormat](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/ja `0.####E0` + + + + + +`skipDoubleFormatting` + + + + +Allows for suppressing double formatting + + + + +`false` + @@ -691,6 +708,8 @@ elements in RDF lists. \* Adapted from [EditorConfig](https://editorconfig.org/#file-format-details) ## Release Notes +* 1.2.13: + * Feature: Skip double formatting * 1.2.12: * Bugfix: Handle RDF lists that start with a non-anonymous node * Bugfix: Handle blank node cycles diff --git a/src/main/java/de/atextor/turtle/formatter/FormattingStyle.java b/src/main/java/de/atextor/turtle/formatter/FormattingStyle.java index e931717..3ed5e3d 100644 --- a/src/main/java/de/atextor/turtle/formatter/FormattingStyle.java +++ b/src/main/java/de/atextor/turtle/formatter/FormattingStyle.java @@ -106,6 +106,9 @@ public class FormattingStyle { @Builder.Default public NumberFormat doubleFormat = new DecimalFormat("0.####E0" , DecimalFormatSymbols.getInstance(Locale.US)); + @Builder.Default + public boolean skipDoubleFormatting = true; + @Builder.Default public EndOfLineStyle endOfLine = EndOfLineStyle.LF; diff --git a/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java b/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java index 21da9ff..e9d2dd3 100644 --- a/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java +++ b/src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java @@ -31,6 +31,9 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; @@ -622,23 +625,28 @@ private State writeUriResource( final Resource resource, final State state ) { } private State writeLiteral( final Literal literal, final State state ) { - if ( literal.getDatatypeURI().equals( XSD.xboolean.getURI() ) ) { - return state.write( literal.getBoolean() ? "true" : "false" ); - } - if ( literal.getDatatypeURI().equals( XSD.xstring.getURI() ) ) { - return state.write( quoteAndEscape( literal ) ); - } - if ( literal.getDatatypeURI().equals( XSD.decimal.getURI() ) ) { - return state.write( literal.getLexicalForm() ); - } - if ( literal.getDatatypeURI().equals( XSD.integer.getURI() ) ) { - return state.write( literal.getValue().toString() ); - } - if ( literal.getDatatypeURI().equals( XSD.xdouble.getURI() ) ) { - return state.write( style.doubleFormat.format( literal.getDouble() ) ); - } - if ( literal.getDatatypeURI().equals( RDF.langString.getURI() ) ) { - return state.write( quoteAndEscape( literal ) + "@" + literal.getLanguage() ); + String datatypeUri = literal.getDatatypeURI(); + if (style.skipDoubleFormatting && datatypeUri.equals(XSD.xdouble.getURI())){ + return state.write(literal.getLexicalForm()); + } else { + if (datatypeUri.equals(XSD.xboolean.getURI())) { + return state.write(literal.getBoolean() ? "true" : "false"); + } + if (datatypeUri.equals(XSD.xstring.getURI())) { + return state.write(quoteAndEscape(literal)); + } + if (datatypeUri.equals(XSD.decimal.getURI())) { + return state.write(literal.getLexicalForm()); + } + if (datatypeUri.equals(XSD.integer.getURI())) { + return state.write(literal.getValue().toString()); + } + if (datatypeUri.equals(XSD.xdouble.getURI())) { + return state.write(style.doubleFormat.format(literal.getDouble())); + } + if (datatypeUri.equals(RDF.langString.getURI())) { + return state.write(quoteAndEscape(literal) + "@" + literal.getLanguage()); + } } final Resource typeResource = ResourceFactory.createResource( literal.getDatatypeURI() ); @@ -952,4 +960,16 @@ public State write( final String content ) { return withLastCharacter( end ).withAlignment( alignment + content.length() ); } } + + public static void main(String[] args) throws IOException { + System.out.println(Arrays.stream(args).collect(Collectors.joining("\n"))); + if (args.length != 1){ + throw new IllegalArgumentException("usage: TurtleFormatter "); + } + String filename = args[0]; + String content = Files.readString(Path.of(filename), StandardCharsets.UTF_8); + final FormattingStyle style = FormattingStyle.builder().alignPredicates(true).alignObjects(true).build(); + final TurtleFormatter formatter = new TurtleFormatter(style); + System.out.println(formatter.applyToContent(content)); + } } diff --git a/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java b/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java index 234fc64..b68558c 100644 --- a/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java +++ b/src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java @@ -1,25 +1,13 @@ package de.atextor.turtle.formatter; -import org.apache.jena.atlas.io.AWriter; -import org.apache.jena.atlas.io.IO; -import org.apache.jena.graph.Graph; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.RDFNode; import org.apache.jena.rdf.model.Resource; import org.apache.jena.rdf.model.Statement; -import org.apache.jena.riot.Lang; -import org.apache.jena.riot.RDFParser; -import org.apache.jena.riot.lang.LabelToNode; -import org.apache.jena.riot.out.NodeFormatter; -import org.apache.jena.riot.out.NodeFormatterNT; -import org.apache.jena.riot.system.StreamRDF; -import org.apache.jena.riot.system.StreamRDFOps; -import org.apache.jena.riot.writer.StreamWriterTriX; -import org.apache.jena.riot.writer.WriterStreamRDFPlain; +import org.apache.jena.rdf.model.impl.PropertyImpl; import org.apache.jena.vocabulary.RDF; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -28,7 +16,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; -import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -1032,6 +1019,22 @@ void testBlankNodeTriangleWithBlankNodeTriple(){ } } + @Test + public void testSkipFormattingValueOfPredicate() { + final String modelString = """ + @prefix xsd: . + @prefix ex: . + + ex:something ex:decimalProp 0.0000000006241509074460762607776240980930446 ; + ex:doubleProp 6.241509074460762607776240980930446E-10 ."""; + + final FormattingStyle style = FormattingStyle.builder().skipDoubleFormatting(true).build(); + + final TurtleFormatter formatter = new TurtleFormatter( style ); + final String result = formatter.applyToContent( modelString ); + assertThat(result.trim()).isEqualTo(modelString); + } + private Model modelFromString( final String content ) { final Model model = ModelFactory.createDefaultModel(); final InputStream stream = new ByteArrayInputStream( content.getBytes( StandardCharsets.UTF_8 ) ); @@ -1047,4 +1050,6 @@ private Model prefixModel() { model.setNsPrefix( "abcdef", "http://example.com/abc" ); return model; } + + }