Skip to content

Commit

Permalink
Apply requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fkleedorfer committed Sep 12, 2024
1 parent 143a3e5 commit 34ae2d7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
63 changes: 50 additions & 13 deletions src/main/java/de/atextor/turtle/formatter/TurtleFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
import org.apache.jena.atlas.io.AWriter;
import org.apache.jena.atlas.lib.Pair;
import org.apache.jena.irix.IRIException;
import org.apache.jena.rdf.model.*;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFList;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.riot.out.NodeFormatterTTL;
import org.apache.jena.riot.system.PrefixLib;
import org.apache.jena.riot.system.PrefixMap;
Expand All @@ -24,7 +31,13 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -105,13 +118,28 @@ private static List<Statement> statements( final Model model, final Property pre
return model.listStatements( null, predicate, object ).toList();
}

/**
* Serializes the specified model as TTL according to the {@link TurtleFormatter}'s {@link FormattingStyle}.
*
* <br>
* Note: Using this method, ordering of blank nodes may differ between multiple runs using identical data.
*
* @param model the model to serialize.
* @return the formatted TTL serialization of the model
*/
@Override
public String apply( final Model model ) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
accept( model, outputStream );
return outputStream.toString();
}

/**
* Format the specified TTL content according to the {@link TurtleFormatter}'s {@link FormattingStyle}.
*
* @param content RDF content in TTL format.
* @return the formatted content
*/
public String applyToContent( final String content ) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
process( content, outputStream );
Expand All @@ -138,6 +166,17 @@ private void writeByteOrderMark( final OutputStream outputStream ) {
}
}

/**
* Serializes the specified model as TTL according to the {@link TurtleFormatter}'s {@link FormattingStyle}
* and writes it to the specified outputStream.
*
* <br>
* Note: Using this method, ordering of blank nodes may differ between multiple runs using identical data.
*
* @param model the model to serialize.
* @param outputStream the stream to write to
* @return the formatted TTL serialization of the model
*/
@Override
public void accept( final Model model, final OutputStream outputStream ) {
if ( style.charset == FormattingStyle.Charset.UTF_8_BOM ) {
Expand Down Expand Up @@ -256,17 +295,15 @@ private Set<Resource> anonymousResourcesThatNeedAnId( final Model model, State c
.map( RDFNode::asResource )
.filter( RDFNode::isAnon ).collect(Collectors.toSet());
candidates.removeAll(currentState.getBlankNodeMetadata().getLabeledBlankNodes());
List<Resource> candidatesInOrder = new ArrayList<>();
candidatesInOrder.addAll(
new ArrayList<>(currentState.getBlankNodeMetadata().getLabeledBlankNodes())
.stream()
.sorted( currentState.getRDFNodeComparatorFactory().comparator())
.toList());
candidatesInOrder.addAll(
candidates
.stream()
.sorted( currentState.getRDFNodeComparatorFactory().comparator())
.toList());
List<Resource> candidatesInOrder =
Stream.concat(
currentState.getBlankNodeMetadata().getLabeledBlankNodes()
.stream()
.sorted( currentState.getRDFNodeComparatorFactory().comparator()),
candidates
.stream()
.sorted( currentState.getRDFNodeComparatorFactory().comparator()))
.toList();
for (Resource candidate: candidatesInOrder) {
if (identifiedResources.contains (candidate)){
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import org.apache.jena.graph.Node;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.*;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.LangBuilder;
import org.apache.jena.riot.RDFParser;
import org.apache.jena.riot.RDFParserRegistry;
import org.apache.jena.riot.ReaderRIOT;
import org.apache.jena.riot.ReaderRIOTFactory;
import org.apache.jena.riot.lang.LabelToNode;
import org.apache.jena.riot.lang.LangRIOT;
import org.apache.jena.riot.lang.RiotParsers;
Expand Down

0 comments on commit 34ae2d7

Please sign in to comment.