Skip to content

Commit

Permalink
Merge pull request #15 from fkleedorfer/bugfix-list-and-numberformat
Browse files Browse the repository at this point in the history
Bugfix for Blank node ordering, RDF lists and DecimalFormat
  • Loading branch information
atextor authored Sep 13, 2024
2 parents 3aaef1b + 6477790 commit bfcd99d
Show file tree
Hide file tree
Showing 9 changed files with 712 additions and 42 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Andreas Textor <[email protected]>
Florian Kleedorfer <[email protected]>
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,13 @@ model:
```

There is no way to serialize this model in RDF/Turtle while using the inline blank node syntax `[ ]`
for the anonymous node `_:b0`. In this case, the anonymousNodeIdGenerator is called to determine
the name of the blank node in the output.
for the anonymous node `_:b0`. If, as in this example, the node in question already has a label, the label is re-used.
Otherwise, the anonymousNodeIdGenerator is used to generate it.

</td>
<td>

`(r, i) -> "_:gen" + i`
`(r, i) -> "gen" + i`

</td>
</tr>
Expand Down Expand Up @@ -691,7 +691,11 @@ elements in RDF lists.
\* Adapted from [EditorConfig](https://editorconfig.org/#file-format-details)

## Release Notes

* 1.2.12:
* Bugfix: Handle RDF lists that start with a non-anonymous node
* Bugfix: Handle blank node cycles
* Bugfix: Ensure constant blank node ordering
* Bugfix: Set Locale for NumberFormat to US
* 1.2.11:
* Bugfix: `rdf:type` is not printed as `a` when used as an object
* Update all dependencies, including Apache Jena to 4.10.0
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ tasks.test {
}

jacoco {
toolVersion = "0.8.7"
toolVersion = "0.8.12"
}

tasks.jacocoTestReport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

import java.net.URI;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -102,7 +104,7 @@ public class FormattingStyle {
public Charset charset = Charset.UTF_8;

@Builder.Default
public NumberFormat doubleFormat = new DecimalFormat( "0.####E0" );
public NumberFormat doubleFormat = new DecimalFormat("0.####E0" , DecimalFormatSymbols.getInstance(Locale.US));

@Builder.Default
public EndOfLineStyle endOfLine = EndOfLineStyle.LF;
Expand Down Expand Up @@ -210,7 +212,7 @@ public class FormattingStyle {
);

@Builder.Default
public BiFunction<Resource, Integer, String> anonymousNodeIdGenerator = ( resource, integer ) -> "_:gen" + integer;
public BiFunction<Resource, Integer, String> anonymousNodeIdGenerator = ( resource, integer ) -> "gen" + integer;

public enum Alignment {
LEFT,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.atextor.turtle.formatter;

import de.atextor.turtle.formatter.blanknode.BlankNodeMetadata;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.shared.PrefixMapping;

import java.util.Comparator;
import java.util.Optional;

public class RDFNodeComparatorFactory {

private final PrefixMapping prefixMapping;
private final BlankNodeMetadata blankNodeOrdering;
private final RDFNodeComparator rdfNodeComparator = new RDFNodeComparator();

public RDFNodeComparatorFactory(PrefixMapping prefixMapping, BlankNodeMetadata blankNodeOrdering) {
this.prefixMapping = prefixMapping;
this.blankNodeOrdering = blankNodeOrdering;
}

public RDFNodeComparatorFactory(PrefixMapping prefixMapping) {
this(prefixMapping, null);
}

public RDFNodeComparator comparator() {
return rdfNodeComparator;
}

private class RDFNodeComparator implements Comparator<RDFNode> {
@Override public int compare(RDFNode left, RDFNode right) {
if (left.isURIResource()){
if (right.isURIResource()){
return prefixMapping.shortForm(left.asResource().getURI()).compareTo(prefixMapping.shortForm(right.asResource().getURI()));
} else if (right.isAnon()) {
return -1 ; // uris first
}
} else if (left.isAnon()) {
if (right.isAnon()) {
if (blankNodeOrdering != null) {
return Optional.ofNullable(blankNodeOrdering.getOrder(left.asResource().asNode()))
.orElse(Long.MAX_VALUE)
.compareTo(Optional.ofNullable(
blankNodeOrdering.getOrder(right.asResource().asNode()))
.orElse(Long.MAX_VALUE));
}
} else if (right.isResource()) {
return 1; // uris first
}
}
//fall-through for all other cases, especially if we don't have a blank node ordering
return left.toString().compareTo(right.toString());
}
}
}
Loading

0 comments on commit bfcd99d

Please sign in to comment.