Skip to content

Commit

Permalink
clean(core): Simplify RdfWriterConverter by re-using WritableResource…
Browse files Browse the repository at this point in the history
…RDFHandler
  • Loading branch information
vorburger committed Sep 22, 2024
1 parent 535f04d commit 5b991a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
39 changes: 18 additions & 21 deletions java/dev/enola/rdf/io/RdfWriterConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
import dev.enola.common.convert.ConverterInto;
import dev.enola.common.io.resource.WritableResource;

import org.eclipse.rdf4j.model.Namespace;
import org.eclipse.rdf4j.model.NamespaceAware;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.UnsupportedRDFormatException;

import java.io.IOException;
import java.io.Writer;
import java.net.URISyntaxException;

/**
Expand All @@ -35,29 +34,27 @@
*/
public class RdfWriterConverter implements ConverterInto<Iterable<Statement>, WritableResource> {

// TODO Replace usages of this with WritableResourceRDFHandler ?

@Override
public boolean convertInto(Iterable<Statement> from, WritableResource into)
throws ConversionException {
// NB: Similar code in WritableResourceRDFHandler
var writerFormat =
Rio.getWriterFormatForMIMEType(into.mediaType().withoutParameters().toString());
String baseURI = into.uri().toString();
if (!writerFormat.isPresent()) {
writerFormat = Rio.getWriterFormatForFileName(baseURI);
}
if (writerFormat.isPresent()) {
try {
try (Writer writer = into.charSink().openBufferedStream()) {
Rio.write(from, writer, baseURI, writerFormat.get());
return true;

try {
var opt = WritableResourceRDFHandler.create(into);
if (opt.isEmpty()) return false;

try (var closeableRDFHandler = opt.get()) {
if (from instanceof NamespaceAware) {
for (Namespace ns : ((NamespaceAware) from).getNamespaces()) {
closeableRDFHandler.handleNamespace(ns.getPrefix(), ns.getName());
}
}
} catch (IOException | UnsupportedRDFormatException | URISyntaxException e) {
throw new ConversionException("Failed writing to: " + into, e);
for (var statement : from) closeableRDFHandler.handleStatement(statement);

return true;
}

} catch (IOException | URISyntaxException e) {
throw new ConversionException("WritableResourceRDFHandler.create failed: " + into, e);
}
// NOT throw new ConversionException("No RDFFormat for: " + from);
return false;
}
}
1 change: 0 additions & 1 deletion java/dev/enola/rdf/io/WritableResourceRDFHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class WritableResourceRDFHandler implements RDFHandler, Closeable {

public static Optional<WritableResourceRDFHandler> create(WritableResource resource)
throws IOException, URISyntaxException {
// NB: Similar code in RdfWriterConverter
String baseURI = resource.uri().toString();
var mediaType = resource.mediaType().withoutParameters().toString();
var writerFormat = Rio.getWriterFormatForMIMEType(mediaType);
Expand Down

0 comments on commit 5b991a0

Please sign in to comment.