Skip to content

Commit

Permalink
Feature: Skip double formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fkleedorfer committed Sep 16, 2024
1 parent 3a84abd commit f56f8b4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@ A [NumberFormat](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/ja

`0.####E0`

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

`skipDoubleFormatting`

</td>
<td>

Allows for suppressing double formatting

</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 skipDoubleFormatting = true;

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

Expand Down
54 changes: 37 additions & 17 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,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() );
Expand Down Expand Up @@ -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 <file>");
}
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));
}
}
33 changes: 19 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,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;
Expand All @@ -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;
Expand Down Expand Up @@ -1032,6 +1019,22 @@ void testBlankNodeTriangleWithBlankNodeTriple(){
}
}

@Test
public void testSkipFormattingValueOfPredicate() {
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().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 ) );
Expand All @@ -1047,4 +1050,6 @@ private Model prefixModel() {
model.setNsPrefix( "abcdef", "http://example.com/abc" );
return model;
}


}

0 comments on commit f56f8b4

Please sign in to comment.