Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Skip double formatting #31

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,30 @@ One of `LATIN1`, `UTF_16_BE`, `UTF_16_LE`, `UTF_8`, `UTF_8_BOM`
</td>
<td>

A [NumberFormat](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/NumberFormat.html) that describes how `xsd:double` literals are formatted
A [NumberFormat](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/NumberFormat.html) that describes how `xsd:double` literals are formatted if `enableDoubleFormatting` is `true`.

</td>
<td>

`0.####E0`

</td>
</tr>
<tr>
<td>

`enableDoubleFormatting`

</td>
<td>

Enables formatting of `xsd:double` values (see `doubleFormat` option)

</td>
<td>

`false`

</td>
</tr>

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public class FormattingStyle {
@Builder.Default
public NumberFormat doubleFormat = new DecimalFormat("0.####E0" , DecimalFormatSymbols.getInstance(Locale.US));

@Builder.Default
public boolean enableDoubleFormatting = false;

@Builder.Default
public EndOfLineStyle endOfLine = EndOfLineStyle.LF;

Expand Down
34 changes: 21 additions & 13 deletions src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -622,26 +625,31 @@ 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" );
String datatypeUri = literal.getDatatypeURI();
if (datatypeUri.equals(XSD.xdouble.getURI())) {
if (style.enableDoubleFormatting){
return state.write(style.doubleFormat.format(literal.getDouble()));
} else {
return state.write(literal.getLexicalForm());
}
}
if ( literal.getDatatypeURI().equals( XSD.xstring.getURI() ) ) {
return state.write( quoteAndEscape( literal ) );
if (datatypeUri.equals(XSD.xboolean.getURI())) {
return state.write(literal.getBoolean() ? "true" : "false");
}
if ( literal.getDatatypeURI().equals( XSD.decimal.getURI() ) ) {
return state.write( literal.getLexicalForm() );
if (datatypeUri.equals(XSD.xstring.getURI())) {
return state.write(quoteAndEscape(literal));
}
if ( literal.getDatatypeURI().equals( XSD.integer.getURI() ) ) {
return state.write( literal.getValue().toString() );
if (datatypeUri.equals(XSD.decimal.getURI())) {
return state.write(literal.getLexicalForm());
}
if ( literal.getDatatypeURI().equals( XSD.xdouble.getURI() ) ) {
return state.write( style.doubleFormat.format( literal.getDouble() ) );
if (datatypeUri.equals(XSD.integer.getURI())) {
return state.write(literal.getValue().toString());
}
if ( literal.getDatatypeURI().equals( RDF.langString.getURI() ) ) {
return state.write( quoteAndEscape( literal ) + "@" + literal.getLanguage() );
if (datatypeUri.equals(RDF.langString.getURI())) {
return state.write(quoteAndEscape(literal) + "@" + literal.getLanguage());
}

final Resource typeResource = ResourceFactory.createResource( literal.getDatatypeURI() );
final Resource typeResource = ResourceFactory.createResource( datatypeUri );
final State literalWritten = state.write( quoteAndEscape( literal ) + "^^" );
return writeUriResource( typeResource, literalWritten );
}
Expand Down
64 changes: 50 additions & 14 deletions src/test/java/de/atextor/turtle/formatter/TurtleFormatterTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
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.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;
Expand All @@ -28,7 +15,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;
Expand Down Expand Up @@ -1032,6 +1018,54 @@ void testBlankNodeTriangleWithBlankNodeTriple(){
}
}

@Test
public void testEnableDoubleFormatting() {
final String modelString = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://example.com/ns#> .

ex:something ex:decimalProp 0.0000000006241509074460762607776240980930446 ;
ex:doubleProp 6.241509074460762607776240980930446E-10 .""";

final FormattingStyle style = FormattingStyle.builder().enableDoubleFormatting(false).build();

final TurtleFormatter formatter = new TurtleFormatter( style );
final String result = formatter.applyToContent( modelString );
assertThat(result.trim()).isEqualTo(modelString);
}

@Test
public void testDoubleFormattingDefault() {
final String modelString = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://example.com/ns#> .

ex:something ex:decimalProp 0.0000000006241509074460762607776240980930446 ;
ex:doubleProp 6.241509074460762607776240980930446E-10 .""";

final FormattingStyle style = FormattingStyle.builder().build();

final TurtleFormatter formatter = new TurtleFormatter( style );
final String result = formatter.applyToContent( modelString );
assertThat(result.trim()).isEqualTo(modelString);
}

@Test
public void testDisableDoubleFormatting() {
final String modelString = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://example.com/ns#> .

ex:something ex:decimalProp 0.0000000006241509074460762607776240980930446 ;
ex:doubleProp 6.2415E-10 .""";

final FormattingStyle style = FormattingStyle.builder().enableDoubleFormatting(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 ) );
Expand All @@ -1047,4 +1081,6 @@ private Model prefixModel() {
model.setNsPrefix( "abcdef", "http://example.com/abc" );
return model;
}


}
Loading