From 049c24e491141f900110ce87951824562ef8338c Mon Sep 17 00:00:00 2001 From: Tobias Kuhn Date: Fri, 8 Feb 2019 11:48:11 +0100 Subject: [PATCH] Migrate from Sesame to RDF4J --- pom.xml | 54 ++++++++------- .../trustyuri/AbstractTrustyUriModule.java | 4 +- src/main/java/net/trustyuri/Run.java | 6 +- src/main/java/net/trustyuri/RunBatch.java | 4 +- .../java/net/trustyuri/TrustyUriModule.java | 4 +- .../java/net/trustyuri/TrustyUriResource.java | 16 +++-- .../java/net/trustyuri/rdf/CheckLargeRdf.java | 29 ++++---- .../java/net/trustyuri/rdf/CheckRdfGraph.java | 14 ++-- .../net/trustyuri/rdf/CheckSortedRdf.java | 23 +++---- .../java/net/trustyuri/rdf/HashAdder.java | 37 +++++------ .../net/trustyuri/rdf/RdfFileContent.java | 27 ++++---- .../java/net/trustyuri/rdf/RdfHasher.java | 34 +++++----- .../net/trustyuri/rdf/RdfPreprocessor.java | 55 ++++++++-------- src/main/java/net/trustyuri/rdf/RdfUtils.java | 66 ++++++++++--------- .../trustyuri/rdf/SerStatementComparator.java | 47 +++++++------ .../trustyuri/rdf/StatementComparator.java | 33 +++++----- .../net/trustyuri/rdf/TransformLargeRdf.java | 36 +++++----- .../java/net/trustyuri/rdf/TransformRdf.java | 61 ++++++++--------- .../net/trustyuri/rdf/TransformRdfGraph.java | 32 ++++----- .../net/trustyuri/file/CheckFileTest.java | 4 +- .../net/trustyuri/file/ProcessFileTest.java | 7 +- .../net/trustyuri/rdf/CheckLargeRdfTest.java | 2 - .../java/net/trustyuri/rdf/CheckRdfTest.java | 4 +- .../net/trustyuri/rdf/CheckSortedRdfTest.java | 2 - .../trustyuri/rdf/TransformLargeRdfTest.java | 7 +- .../net/trustyuri/rdf/TransformRdfTest.java | 7 +- 26 files changed, 310 insertions(+), 305 deletions(-) diff --git a/pom.xml b/pom.xml index e1b670b..cacec9d 100644 --- a/pom.xml +++ b/pom.xml @@ -23,9 +23,19 @@ 1.7 UTF-8 - 2.9.0 + 2.4.4 + + com.sun.activation + javax.activation + 1.2.0 + + + javax.xml.bind + jaxb-api + 2.3.1 + junit junit @@ -48,39 +58,39 @@ 1.7.2 - org.openrdf.sesame - sesame-rio-trig - ${sesame.version} + org.eclipse.rdf4j + rdf4j-rio-trig + ${rdf4j.version} - org.openrdf.sesame - sesame-rio-rdfxml - ${sesame.version} + org.eclipse.rdf4j + rdf4j-rio-rdfxml + ${rdf4j.version} - org.openrdf.sesame - sesame-rio-turtle - ${sesame.version} + org.eclipse.rdf4j + rdf4j-rio-turtle + ${rdf4j.version} - org.openrdf.sesame - sesame-rio-ntriples - ${sesame.version} + org.eclipse.rdf4j + rdf4j-rio-ntriples + ${rdf4j.version} - org.openrdf.sesame - sesame-rio-trix - ${sesame.version} + org.eclipse.rdf4j + rdf4j-rio-trix + ${rdf4j.version} - org.openrdf.sesame - sesame-rio-nquads - ${sesame.version} + org.eclipse.rdf4j + rdf4j-rio-nquads + ${rdf4j.version} - org.openrdf.sesame - sesame-repository-sparql - ${sesame.version} + org.eclipse.rdf4j + rdf4j-repository-sparql + ${rdf4j.version} org.apache.commons diff --git a/src/main/java/net/trustyuri/AbstractTrustyUriModule.java b/src/main/java/net/trustyuri/AbstractTrustyUriModule.java index b51f8c6..863fd30 100644 --- a/src/main/java/net/trustyuri/AbstractTrustyUriModule.java +++ b/src/main/java/net/trustyuri/AbstractTrustyUriModule.java @@ -1,6 +1,6 @@ package net.trustyuri; -import org.openrdf.model.URI; +import org.eclipse.rdf4j.model.IRI; /** * A partial implementation of a trusty URI module. @@ -18,7 +18,7 @@ public AbstractTrustyUriModule() { pattern = ".*[^A-Za-z0-9\\-_]" + getModuleId() + "[A-Za-z0-9\\-_]{" + getDataPartLength() + "}"; } - public boolean matches(URI uri) { + public boolean matches(IRI uri) { return uri.stringValue().matches(pattern); } diff --git a/src/main/java/net/trustyuri/Run.java b/src/main/java/net/trustyuri/Run.java index d3f9681..0fc9111 100644 --- a/src/main/java/net/trustyuri/Run.java +++ b/src/main/java/net/trustyuri/Run.java @@ -3,7 +3,7 @@ import java.io.IOException; import java.util.Arrays; -import org.openrdf.OpenRDFException; +import org.eclipse.rdf4j.RDF4JException; import net.trustyuri.file.ProcessFile; import net.trustyuri.rdf.CheckLargeRdf; @@ -27,7 +27,7 @@ private Run() {} // no instances allowed * * @param args the command */ - public static void main(String[] args) throws IOException, OpenRDFException, TrustyUriException { + public static void main(String[] args) throws IOException, RDF4JException, TrustyUriException { run(args); } @@ -36,7 +36,7 @@ public static void main(String[] args) throws IOException, OpenRDFException, Tru * * @param command the command, as a String array */ - public static void run(String[] command) throws IOException, OpenRDFException, TrustyUriException { + public static void run(String[] command) throws IOException, RDF4JException, TrustyUriException { String cmd = command[0]; String[] cmdArgs = Arrays.copyOfRange(command, 1, command.length); if (cmd.equals("CheckFile")) { diff --git a/src/main/java/net/trustyuri/RunBatch.java b/src/main/java/net/trustyuri/RunBatch.java index 0d6f67f..8a62b8a 100644 --- a/src/main/java/net/trustyuri/RunBatch.java +++ b/src/main/java/net/trustyuri/RunBatch.java @@ -8,13 +8,13 @@ import java.nio.charset.Charset; import java.nio.file.Files; -import org.openrdf.OpenRDFException; +import org.eclipse.rdf4j.RDF4JException; public class RunBatch { private RunBatch() {} // no instances allowed - public static void main(String[] args) throws IOException, OpenRDFException, TrustyUriException { + public static void main(String[] args) throws IOException, RDF4JException, TrustyUriException { String batchFile = args[0]; BufferedReader reader = new BufferedReader(new FileReader(batchFile)); diff --git a/src/main/java/net/trustyuri/TrustyUriModule.java b/src/main/java/net/trustyuri/TrustyUriModule.java index 9e95f55..44b7cfe 100644 --- a/src/main/java/net/trustyuri/TrustyUriModule.java +++ b/src/main/java/net/trustyuri/TrustyUriModule.java @@ -3,7 +3,7 @@ import java.io.File; import java.io.IOException; -import org.openrdf.model.URI; +import org.eclipse.rdf4j.model.IRI; /** * A trusty URI module handles a particular type of content (such as RDF graphs or the byte content @@ -51,6 +51,6 @@ public interface TrustyUriModule { * @param uri the URI * @return true if the URI matches the format of this module */ - public boolean matches(URI uri); + public boolean matches(IRI uri); } diff --git a/src/main/java/net/trustyuri/TrustyUriResource.java b/src/main/java/net/trustyuri/TrustyUriResource.java index e249ad9..3296740 100644 --- a/src/main/java/net/trustyuri/TrustyUriResource.java +++ b/src/main/java/net/trustyuri/TrustyUriResource.java @@ -8,10 +8,11 @@ import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; +import java.util.Optional; import java.util.zip.GZIPInputStream; -import org.openrdf.rio.RDFFormat; -import org.openrdf.rio.Rio; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.Rio; public class TrustyUriResource { @@ -106,11 +107,14 @@ public String getModuleId() { } public RDFFormat getFormat(RDFFormat defaultFormat) { - RDFFormat format = Rio.getParserFormatForMIMEType(getMimetype()); - if (format == null) { - format = Rio.getParserFormatForFileName(getFilename(), defaultFormat); + Optional format = Rio.getParserFormatForMIMEType(getMimetype()); + if (!format.isPresent()) { + format = Rio.getParserFormatForFileName(getFilename()); } - return format; + if (format.isPresent()) { + return format.get(); + } + return null; } } diff --git a/src/main/java/net/trustyuri/rdf/CheckLargeRdf.java b/src/main/java/net/trustyuri/rdf/CheckLargeRdf.java index 044adbb..638afd9 100644 --- a/src/main/java/net/trustyuri/rdf/CheckLargeRdf.java +++ b/src/main/java/net/trustyuri/rdf/CheckLargeRdf.java @@ -10,20 +10,18 @@ import java.util.Comparator; import java.util.List; -import net.trustyuri.TrustyUriException; -import net.trustyuri.TrustyUriResource; - -import org.openrdf.OpenRDFException; -import org.openrdf.model.Statement; -import org.openrdf.rio.RDFFormat; -import org.openrdf.rio.RDFHandlerException; -import org.openrdf.rio.RDFParser; -import org.openrdf.rio.Rio; -import org.openrdf.rio.helpers.RDFHandlerBase; -import org.openrdf.rio.helpers.RDFaParserSettings; +import org.eclipse.rdf4j.RDF4JException; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.RDFHandlerException; +import org.eclipse.rdf4j.rio.RDFParser; +import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler; import com.google.code.externalsorting.ExternalSort; +import net.trustyuri.TrustyUriException; +import net.trustyuri.TrustyUriResource; + public class CheckLargeRdf { public static void main(String[] args) throws IOException, TrustyUriException { @@ -52,12 +50,11 @@ public boolean check() throws IOException, TrustyUriException { md = RdfHasher.getDigest(); RDFFormat format = r.getFormat(RDFFormat.TURTLE); - RDFParser p = Rio.createParser(format); - p.getParserConfig().set(RDFaParserSettings.FAIL_ON_RDFA_UNDEFINED_PREFIXES, true); + RDFParser p = RdfUtils.getParser(format); File sortInFile = new File(dir, fileName + ".temp.sort-in"); final FileOutputStream preOut = new FileOutputStream(sortInFile); - p.setRDFHandler(new RdfPreprocessor(new RDFHandlerBase() { - + p.setRDFHandler(new RdfPreprocessor(new AbstractRDFHandler() { + @Override public void handleStatement(Statement st) throws RDFHandlerException { String s = SerStatementComparator.toString(st) + "\n"; @@ -72,7 +69,7 @@ public void handleStatement(Statement st) throws RDFHandlerException { BufferedReader reader = new BufferedReader(r.getInputStreamReader(), 64*1024); try { p.parse(reader, ""); - } catch (OpenRDFException ex) { + } catch (RDF4JException ex) { throw new TrustyUriException(ex); } finally { reader.close(); diff --git a/src/main/java/net/trustyuri/rdf/CheckRdfGraph.java b/src/main/java/net/trustyuri/rdf/CheckRdfGraph.java index 04cac47..4dc9174 100644 --- a/src/main/java/net/trustyuri/rdf/CheckRdfGraph.java +++ b/src/main/java/net/trustyuri/rdf/CheckRdfGraph.java @@ -7,14 +7,14 @@ import java.util.ArrayList; import java.util.List; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; + import net.trustyuri.TrustyUriException; import net.trustyuri.TrustyUriResource; import net.trustyuri.TrustyUriUtils; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.impl.URIImpl; - public class CheckRdfGraph { public static void main(String[] args) throws IOException, TrustyUriException { @@ -30,7 +30,7 @@ public static void main(String[] args) throws IOException, TrustyUriException { c = new CheckRdfGraph(new File(fileName)); } for (int i = 1 ; i < args.length ; i++) { - URI graphUri = new URIImpl(args[i]); + IRI graphUri = SimpleValueFactory.getInstance().createIRI(args[i]); boolean valid = c.check(graphUri); if (valid) { System.out.println("Correct hash: " + getArtifactCode(graphUri)); @@ -57,7 +57,7 @@ private void init() throws IOException, TrustyUriException { content = RdfUtils.load(r); } - public boolean check(URI graphUri) throws TrustyUriException { + public boolean check(IRI graphUri) throws TrustyUriException { String artifactCode = getArtifactCode(graphUri); if (artifactCode == null) { throw new TrustyUriException("Not a trusty URI: " + graphUri); @@ -74,7 +74,7 @@ public boolean check(URI graphUri) throws TrustyUriException { return artifactCode.equals(ac); } - private static String getArtifactCode(URI graphUri) { + private static String getArtifactCode(IRI graphUri) { return TrustyUriUtils.getArtifactCode(graphUri.stringValue()); } diff --git a/src/main/java/net/trustyuri/rdf/CheckSortedRdf.java b/src/main/java/net/trustyuri/rdf/CheckSortedRdf.java index e3d7795..f5fcdd1 100644 --- a/src/main/java/net/trustyuri/rdf/CheckSortedRdf.java +++ b/src/main/java/net/trustyuri/rdf/CheckSortedRdf.java @@ -5,18 +5,16 @@ import java.io.IOException; import java.security.MessageDigest; +import org.eclipse.rdf4j.RDF4JException; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.RDFHandlerException; +import org.eclipse.rdf4j.rio.RDFParser; +import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler; + import net.trustyuri.TrustyUriException; import net.trustyuri.TrustyUriResource; -import org.openrdf.OpenRDFException; -import org.openrdf.model.Statement; -import org.openrdf.rio.RDFFormat; -import org.openrdf.rio.RDFHandlerException; -import org.openrdf.rio.RDFParser; -import org.openrdf.rio.Rio; -import org.openrdf.rio.helpers.RDFHandlerBase; -import org.openrdf.rio.helpers.RDFaParserSettings; - public class CheckSortedRdf { public static void main(String[] args) throws IOException, TrustyUriException { @@ -54,10 +52,9 @@ public boolean check() throws IOException, TrustyUriException { } RDFFormat format = r.getFormat(RDFFormat.TURTLE); - RDFParser p = Rio.createParser(format); + RDFParser p = RdfUtils.getParser(format); previous = null; - p.getParserConfig().set(RDFaParserSettings.FAIL_ON_RDFA_UNDEFINED_PREFIXES, true); - p.setRDFHandler(new RdfPreprocessor(new RDFHandlerBase() { + p.setRDFHandler(new RdfPreprocessor(new AbstractRDFHandler() { @Override public void handleStatement(Statement st) throws RDFHandlerException { @@ -74,7 +71,7 @@ public void handleStatement(Statement st) throws RDFHandlerException { BufferedReader reader = new BufferedReader(r.getInputStreamReader(), 64*1024); try { p.parse(reader, ""); - } catch (OpenRDFException ex) { + } catch (RDF4JException ex) { throw new TrustyUriException(ex); } finally { reader.close(); diff --git a/src/main/java/net/trustyuri/rdf/HashAdder.java b/src/main/java/net/trustyuri/rdf/HashAdder.java index cd253c8..20cad8d 100644 --- a/src/main/java/net/trustyuri/rdf/HashAdder.java +++ b/src/main/java/net/trustyuri/rdf/HashAdder.java @@ -3,25 +3,24 @@ import java.util.HashMap; import java.util.Map; -import org.openrdf.model.BNode; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.impl.ContextStatementImpl; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.rio.RDFHandler; -import org.openrdf.rio.RDFHandlerException; +import org.eclipse.rdf4j.model.BNode; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; +import org.eclipse.rdf4j.rio.RDFHandler; +import org.eclipse.rdf4j.rio.RDFHandlerException; public class HashAdder implements RDFHandler { - private URI baseURI; + private IRI baseURI; private String artifactCode; private RDFHandler handler; private Map ns; - private Map transformMap; + private Map transformMap; - public HashAdder(URI baseURI, String artifactCode, RDFHandler handler, Map ns) { + public HashAdder(IRI baseURI, String artifactCode, RDFHandler handler, Map ns) { this.baseURI = baseURI; this.artifactCode = artifactCode; this.handler = handler; @@ -29,7 +28,7 @@ public HashAdder(URI baseURI, String artifactCode, RDFHandler handler, Map(); } - transformMap = new HashMap(); + transformMap = new HashMap(); } @Override @@ -57,12 +56,12 @@ public void handleNamespace(String prefix, String uri) throws RDFHandlerExceptio public void handleStatement(Statement st) throws RDFHandlerException { Resource context = transform(st.getContext()); Resource subject = transform(st.getSubject()); - URI predicate = transform(st.getPredicate()); + IRI predicate = transform(st.getPredicate()); Value object = st.getObject(); if (object instanceof Resource) { object = transform((Resource) object); } - Statement n = new ContextStatementImpl(subject, predicate, object, context); + Statement n = SimpleValueFactory.getInstance().createStatement(subject, predicate, object, context); handler.handleStatement(n); } @@ -71,19 +70,19 @@ public void handleComment(String comment) throws RDFHandlerException { handler.handleComment(comment); } - private URI transform(Resource r) { + private IRI transform(Resource r) { if (r == null) { return null; } else if (r instanceof BNode) { throw new RuntimeException("Unexpected blank node encountered"); } else { - URI transformedURI = new URIImpl(r.toString().replace(" ", artifactCode)); - transformMap.put((URI) r, transformedURI); + IRI transformedURI = SimpleValueFactory.getInstance().createIRI(r.toString().replace(" ", artifactCode)); + transformMap.put((IRI) r, transformedURI); return transformedURI; } } - public Map getTransformMap() { + public Map getTransformMap() { return transformMap; } diff --git a/src/main/java/net/trustyuri/rdf/RdfFileContent.java b/src/main/java/net/trustyuri/rdf/RdfFileContent.java index c11eac7..08ddacf 100644 --- a/src/main/java/net/trustyuri/rdf/RdfFileContent.java +++ b/src/main/java/net/trustyuri/rdf/RdfFileContent.java @@ -6,15 +6,14 @@ import java.util.Map; import org.apache.commons.lang3.tuple.Pair; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.impl.ContextStatementImpl; -import org.openrdf.model.impl.StatementImpl; -import org.openrdf.rio.RDFFormat; -import org.openrdf.rio.RDFHandler; -import org.openrdf.rio.RDFHandlerException; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.RDFHandler; +import org.eclipse.rdf4j.rio.RDFHandlerException; public class RdfFileContent implements RDFHandler { @@ -50,7 +49,7 @@ public void handleStatement(Statement st) throws RDFHandlerException { subj = st.getSubject(); rdfEntityMap.put(subj, subj); } - URI pred = (URI) rdfEntityMap.get(st.getPredicate()); + IRI pred = (IRI) rdfEntityMap.get(st.getPredicate()); if (pred == null) { pred = st.getPredicate(); rdfEntityMap.put(pred, pred); @@ -62,14 +61,14 @@ public void handleStatement(Statement st) throws RDFHandlerException { } Resource context = null; if (st.getContext() == null) { - st = new StatementImpl(subj, pred, obj); + st = SimpleValueFactory.getInstance().createStatement(subj, pred, obj); } else { context = (Resource) rdfEntityMap.get(st.getContext()); if (context == null) { context = st.getContext(); rdfEntityMap.put(context, context); } - st = new ContextStatementImpl(subj, pred, obj, context); + st = SimpleValueFactory.getInstance().createStatement(subj, pred, obj, context); } statements.add(st); @@ -93,7 +92,11 @@ public void propagate(RDFHandler handler, boolean doStartAndEnd) throws RDFHandl for (Pair ns : namespaces) { handler.handleNamespace(ns.getLeft(), ns.getRight()); } + Statement prev = null; for (Statement st : statements) { + // omitting duplicates + if (prev != null && prev.equals(st)) continue; + prev = st; handler.handleStatement(st); } if (doStartAndEnd) handler.endRDF(); diff --git a/src/main/java/net/trustyuri/rdf/RdfHasher.java b/src/main/java/net/trustyuri/rdf/RdfHasher.java index 6397d74..49292f9 100644 --- a/src/main/java/net/trustyuri/rdf/RdfHasher.java +++ b/src/main/java/net/trustyuri/rdf/RdfHasher.java @@ -6,17 +6,17 @@ import java.util.Collections; import java.util.List; +import org.eclipse.rdf4j.model.BNode; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Literal; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.vocabulary.XMLSchema; + import net.trustyuri.TrustyUriException; import net.trustyuri.TrustyUriUtils; -import org.openrdf.model.BNode; -import org.openrdf.model.Literal; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.vocabulary.XMLSchema; - public class RdfHasher { // TODO Make this a command line argument: @@ -29,7 +29,7 @@ public static String makeArtifactCode(List statements) { } public static String makeGraphArtifactCode(List statements) throws TrustyUriException { - URI graphUri = null; + IRI graphUri = null; List graph = new ArrayList(); for (Statement st : statements) { Resource c = st.getContext(); @@ -40,7 +40,7 @@ public static String makeGraphArtifactCode(List statements) throws Tr } else if (graphUri != null && !c.equals(graphUri)) { throw new TrustyUriException("Multiple graphs"); } - graphUri = (URI) c; + graphUri = (IRI) c; graph.add(st); } if (graph.size() == 0) { @@ -49,8 +49,8 @@ public static String makeGraphArtifactCode(List statements) throws Tr return getGraphArtifactCode(digest(graph)); } - public static String makeGraphArtifactCode(List statements, URI baseUri) throws TrustyUriException { - URI graphUri = RdfUtils.getTrustyUri(baseUri, " "); + public static String makeGraphArtifactCode(List statements, IRI baseUri) throws TrustyUriException { + IRI graphUri = RdfUtils.getTrustyUri(baseUri, " "); List graph = new ArrayList(); for (Statement st : statements) { Resource c = st.getContext(); @@ -129,14 +129,14 @@ public static String getDigestString(Statement st) { } private static String valueToString(Value v) { - if (v instanceof URI) { - return ((URI) v).toString() + "\n"; + if (v instanceof IRI) { + return ((IRI) v).toString() + "\n"; } else if (v instanceof Literal) { Literal l = (Literal) v; - if (l.getLanguage() != null) { - return "@" + l.getLanguage() + " " + escapeString(l.stringValue()) + "\n"; + if (l.getLanguage().isPresent()) { + return "@" + l.getLanguage().get().toLowerCase() + " " + escapeString(l.stringValue()) + "\n"; } else { - URI dataType = l.getDatatype(); + IRI dataType = l.getDatatype(); if (dataType == null) dataType = XMLSchema.STRING; return "^" + dataType.stringValue() + " " + escapeString(l.stringValue()) + "\n"; } diff --git a/src/main/java/net/trustyuri/rdf/RdfPreprocessor.java b/src/main/java/net/trustyuri/rdf/RdfPreprocessor.java index 28c646a..9e77d21 100644 --- a/src/main/java/net/trustyuri/rdf/RdfPreprocessor.java +++ b/src/main/java/net/trustyuri/rdf/RdfPreprocessor.java @@ -5,31 +5,30 @@ import java.util.List; import java.util.Map; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; +import org.eclipse.rdf4j.rio.RDFHandler; +import org.eclipse.rdf4j.rio.RDFHandlerException; + import net.trustyuri.ModuleDirectory; import net.trustyuri.TrustyUriException; import net.trustyuri.TrustyUriModule; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.impl.ContextStatementImpl; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.rio.RDFHandler; -import org.openrdf.rio.RDFHandlerException; - public class RdfPreprocessor implements RDFHandler { // TODO check that already hashed graphs are not changed private RDFHandler nestedHandler; - private URI baseUri; + private IRI baseUri; private String artifactCode; private Map blankNodeMap; private TrustyUriModule moduleRB; - private Map transformMap; + private Map transformMap; - public static RdfFileContent run(RdfFileContent content, URI baseUri) throws TrustyUriException { + public static RdfFileContent run(RdfFileContent content, IRI baseUri) throws TrustyUriException { RdfFileContent p = new RdfFileContent(content.getOriginalFormat()); try { content.propagate(new RdfPreprocessor(p, baseUri)); @@ -49,7 +48,7 @@ public static RdfFileContent run(RdfFileContent content, String artifactCode) th return p; } - public static List run(List statements, URI baseUri) { + public static List run(List statements, IRI baseUri) { return run(statements, baseUri, null); } @@ -57,7 +56,7 @@ public static List run(List statements, String artifactCod return run(statements, null, artifactCode); } - private static List run(List statements, URI baseUri, String artifactCode) { + private static List run(List statements, IRI baseUri, String artifactCode) { List r = new ArrayList(); RdfPreprocessor obj = new RdfPreprocessor(baseUri, artifactCode); for (Statement st : statements) { @@ -66,13 +65,13 @@ private static List run(List statements, URI baseUri, Stri return r; } - private RdfPreprocessor(URI baseUri, String artifactCode) { + private RdfPreprocessor(IRI baseUri, String artifactCode) { this.baseUri = baseUri; this.artifactCode = artifactCode; init(); } - public RdfPreprocessor(RDFHandler nestedHandler, URI baseUri) { + public RdfPreprocessor(RDFHandler nestedHandler, IRI baseUri) { this(nestedHandler, baseUri, null); } @@ -80,7 +79,7 @@ public RdfPreprocessor(RDFHandler nestedHandler, String artifactCode) { this(nestedHandler, artifactCode, null); } - public RdfPreprocessor(RDFHandler nestedHandler, URI baseUri, Map blankNodeMap) { + public RdfPreprocessor(RDFHandler nestedHandler, IRI baseUri, Map blankNodeMap) { this.nestedHandler = nestedHandler; this.baseUri = baseUri; this.artifactCode = null; @@ -101,7 +100,7 @@ private void init() { this.blankNodeMap = new HashMap(); } moduleRB = ModuleDirectory.getModule(RdfGraphModule.MODULE_ID); - transformMap = new HashMap(); + transformMap = new HashMap(); } @Override @@ -130,34 +129,34 @@ public void handleComment(String comment) throws RDFHandlerException { nestedHandler.handleComment(comment); } - public Map getTransformMap() { + public Map getTransformMap() { return transformMap; } private Statement preprocess(Statement st) { Resource context = st.getContext(); - URI trustyGraph = null; + IRI trustyGraph = null; if (context != null) { - if (context instanceof URI && moduleRB.matches((URI) context)) { - trustyGraph = (URI) context; + if (context instanceof IRI && moduleRB.matches((IRI) context)) { + trustyGraph = (IRI) context; } context = transform(context, trustyGraph); } Resource subject = transform(st.getSubject(), trustyGraph); - URI predicate = transform(st.getPredicate(), trustyGraph); + IRI predicate = transform(st.getPredicate(), trustyGraph); Value object = st.getObject(); if (object instanceof Resource) { object = transform((Resource) object, trustyGraph); } - return new ContextStatementImpl(subject, predicate, object, context); + return SimpleValueFactory.getInstance().createStatement(subject, predicate, object, context); } - private URI transform(Resource r, URI trustyGraph) { + private IRI transform(Resource r, IRI trustyGraph) { if (baseUri == null) { - RdfUtils.checkUri((URI) r); - return new URIImpl(RdfUtils.normalize((URI) r, artifactCode)); + RdfUtils.checkUri((IRI) r); + return SimpleValueFactory.getInstance().createIRI(RdfUtils.normalize((IRI) r, artifactCode)); } - URI uri = RdfUtils.getPreUri(r, baseUri, blankNodeMap, trustyGraph != null); + IRI uri = RdfUtils.getPreUri(r, baseUri, blankNodeMap, trustyGraph != null); if (uri == null) { // TODO Allow for 'force' option; URI might only look like a trusty URI... throw new RuntimeException("Transformation would break existing trusty URI graph: " + diff --git a/src/main/java/net/trustyuri/rdf/RdfUtils.java b/src/main/java/net/trustyuri/rdf/RdfUtils.java index 54da460..aa8d221 100644 --- a/src/main/java/net/trustyuri/rdf/RdfUtils.java +++ b/src/main/java/net/trustyuri/rdf/RdfUtils.java @@ -12,24 +12,26 @@ import java.util.Map; import java.util.zip.GZIPOutputStream; +import org.eclipse.rdf4j.RDF4JException; +import org.eclipse.rdf4j.model.BNode; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.RDFHandler; +import org.eclipse.rdf4j.rio.RDFHandlerException; +import org.eclipse.rdf4j.rio.RDFParser; +import org.eclipse.rdf4j.rio.RDFWriter; +import org.eclipse.rdf4j.rio.Rio; +import org.eclipse.rdf4j.rio.helpers.BasicParserSettings; +import org.eclipse.rdf4j.rio.helpers.RDFaParserSettings; +import org.eclipse.rdf4j.rio.helpers.RioSettingImpl; + import net.trustyuri.TrustyUriException; import net.trustyuri.TrustyUriResource; import net.trustyuri.TrustyUriUtils; -import org.openrdf.OpenRDFException; -import org.openrdf.model.BNode; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.rio.RDFFormat; -import org.openrdf.rio.RDFHandler; -import org.openrdf.rio.RDFHandlerException; -import org.openrdf.rio.RDFParser; -import org.openrdf.rio.RDFWriter; -import org.openrdf.rio.Rio; -import org.openrdf.rio.helpers.RDFaParserSettings; - public class RdfUtils { public static final char bnodeChar = '_'; @@ -39,7 +41,7 @@ public class RdfUtils { private RdfUtils() {} // no instances allowed - public static String getTrustyUriString(URI baseUri, String artifactCode, String suffix) { + public static String getTrustyUriString(IRI baseUri, String artifactCode, String suffix) { String s = expandBaseUri(baseUri) + artifactCode; if (suffix != null) { suffix = suffix.replace("#", "%23"); @@ -53,25 +55,25 @@ public static String getTrustyUriString(URI baseUri, String artifactCode, String return s; } - public static String getTrustyUriString(URI baseUri, String artifactCode) { + public static String getTrustyUriString(IRI baseUri, String artifactCode) { return getTrustyUriString(baseUri, artifactCode, null); } - public static URI getTrustyUri(URI baseUri, String artifactCode, String suffix) { + public static IRI getTrustyUri(IRI baseUri, String artifactCode, String suffix) { if (baseUri == null) return null; - return new URIImpl(getTrustyUriString(baseUri, artifactCode, suffix)); + return SimpleValueFactory.getInstance().createIRI(getTrustyUriString(baseUri, artifactCode, suffix)); } - public static URI getTrustyUri(URI baseUri, String artifactCode) { + public static IRI getTrustyUri(IRI baseUri, String artifactCode) { if (baseUri == null) return null; - return new URIImpl(getTrustyUriString(baseUri, artifactCode, null)); + return SimpleValueFactory.getInstance().createIRI(getTrustyUriString(baseUri, artifactCode, null)); } - public static URI getPreUri(Resource resource, URI baseUri, Map bnodeMap, boolean frozen) { + public static IRI getPreUri(Resource resource, IRI baseUri, Map bnodeMap, boolean frozen) { if (resource == null) { throw new RuntimeException("Resource is null"); - } else if (resource instanceof URI) { - URI plainUri = (URI) resource; + } else if (resource instanceof IRI) { + IRI plainUri = (IRI) resource; checkUri(plainUri); // TODO Add option to disable suffixes appended to trusty URIs String suffix = getSuffix(plainUri, baseUri); @@ -93,7 +95,7 @@ public static URI getPreUri(Resource resource, URI baseUri, Map } } - public static void checkUri(URI uri) { + public static void checkUri(IRI uri) { try { // Raise error if not well-formed new java.net.URI(uri.stringValue()); @@ -102,19 +104,19 @@ public static void checkUri(URI uri) { } } - public static char getPostAcChar(URI baseUri) { + public static char getPostAcChar(IRI baseUri) { if (baseUri.stringValue().contains("#")) { return postAcFallbackChar; } return postAcChar; } - private static URI getSkolemizedUri(BNode bnode, URI baseUri, Map bnodeMap) { + private static IRI getSkolemizedUri(BNode bnode, IRI baseUri, Map bnodeMap) { int n = getBlankNodeNumber(bnode, bnodeMap); - return new URIImpl(expandBaseUri(baseUri) + " " + getPostAcChar(baseUri) + bnodeChar + n); + return SimpleValueFactory.getInstance().createIRI(expandBaseUri(baseUri) + " " + getPostAcChar(baseUri) + bnodeChar + n); } - private static String getSuffix(URI plainUri, URI baseUri) { + private static String getSuffix(IRI plainUri, IRI baseUri) { if (baseUri == null) return null; String b = baseUri.toString(); String p = plainUri.toString(); @@ -127,7 +129,7 @@ private static String getSuffix(URI plainUri, URI baseUri) { } } - public static String normalize(URI uri, String artifactCode) { + public static String normalize(IRI uri, String artifactCode) { String s = uri.toString(); if (s.indexOf('\n') > -1 || s.indexOf('\t') > -1) { throw new RuntimeException("Newline or tab character in URI: " + s); @@ -146,7 +148,7 @@ private static int getBlankNodeNumber(BNode blankNode, Map bnode return n; } - private static String expandBaseUri(URI baseUri) { + private static String expandBaseUri(IRI baseUri) { String s = baseUri.toString(); s = s.replaceFirst("ARTIFACTCODE-PLACEHOLDER[\\.#/]?$", ""); if (s.matches(".*[A-Za-z0-9\\-_]")) { @@ -161,7 +163,8 @@ public static RdfFileContent load(InputStream in, RDFFormat format) throws IOExc p.setRDFHandler(content); try { p.parse(new InputStreamReader(in, Charset.forName("UTF-8")), ""); - } catch (OpenRDFException ex) { + } catch (RDF4JException ex) { + ex.printStackTrace(); throw new TrustyUriException(ex); } in.close(); @@ -170,6 +173,7 @@ public static RdfFileContent load(InputStream in, RDFFormat format) throws IOExc public static RDFParser getParser(RDFFormat format) { RDFParser p = Rio.createParser(format); + p.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_URI_SYNTAX); p.getParserConfig().set(RDFaParserSettings.FAIL_ON_RDFA_UNDEFINED_PREFIXES, true); return p; } diff --git a/src/main/java/net/trustyuri/rdf/SerStatementComparator.java b/src/main/java/net/trustyuri/rdf/SerStatementComparator.java index 146afbe..8441fcd 100644 --- a/src/main/java/net/trustyuri/rdf/SerStatementComparator.java +++ b/src/main/java/net/trustyuri/rdf/SerStatementComparator.java @@ -2,17 +2,15 @@ import java.util.Comparator; -import org.openrdf.model.BNode; -import org.openrdf.model.Literal; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.impl.ContextStatementImpl; -import org.openrdf.model.impl.LiteralImpl; -import org.openrdf.model.impl.StatementImpl; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.model.vocabulary.XMLSchema; +import org.eclipse.rdf4j.model.BNode; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Literal; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.ValueFactory; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; +import org.eclipse.rdf4j.model.vocabulary.XMLSchema; public class SerStatementComparator implements Comparator { @@ -58,16 +56,17 @@ public int compare(String s1, String s2) { } public static Statement fromString(String string) { + ValueFactory vf = SimpleValueFactory.getInstance(); String[] parts = string.split("\\t"); Resource context = null; if (!parts[0].isEmpty()) { - context = new URIImpl(parts[0]); + context = vf.createIRI(parts[0]); } - Resource subj = new URIImpl(parts[1]); - URI pred = new URIImpl(parts[2]); + Resource subj = vf.createIRI(parts[1]); + IRI pred = vf.createIRI(parts[2]); Value obj = null; if (!parts[3].isEmpty()) { - obj = new URIImpl(parts[3]); + obj = SimpleValueFactory.getInstance().createIRI(parts[3]); } else { String o = parts[4]; int i1 = o.indexOf(32); @@ -76,17 +75,17 @@ public static Statement fromString(String string) { String l = o.substring(i1+1, i2); String v = unescape(o.substring(i2+1)); if (!d.isEmpty()) { - obj = new LiteralImpl(v, new URIImpl(d)); + obj = vf.createLiteral(v, vf.createIRI(d)); } else if (!l.isEmpty()) { - obj = new LiteralImpl(v, l); + obj = vf.createLiteral(v, l); } else { - obj = new LiteralImpl(v); + obj = vf.createLiteral(v); } } if (context == null) { - return new StatementImpl(subj, pred, obj); + return vf.createStatement(subj, pred, obj); } else { - return new ContextStatementImpl(subj, pred, obj, context); + return vf.createStatement(subj, pred, obj, context); } } @@ -106,7 +105,7 @@ public static String toString(Statement st) { } else { sb.append(subj.stringValue() + "\t"); } - URI pred = st.getPredicate(); + IRI pred = st.getPredicate(); sb.append(pred.stringValue() + "\t"); Value obj = st.getObject(); if (obj instanceof BNode) { @@ -114,12 +113,12 @@ public static String toString(Statement st) { } else if (obj instanceof Literal) { sb.append("\t"); Literal objl = (Literal) obj; - if (objl.getLanguage() == null) { - URI dataType = objl.getDatatype(); + if (!objl.getLanguage().isPresent()) { + IRI dataType = objl.getDatatype(); if (dataType == null) dataType = XMLSchema.STRING; sb.append(dataType.stringValue() + " "); } else { - sb.append(" " + objl.getLanguage() + " "); + sb.append(" " + objl.getLanguage().get().toLowerCase() + " "); } sb.append(escape(obj.stringValue())); } else { diff --git a/src/main/java/net/trustyuri/rdf/StatementComparator.java b/src/main/java/net/trustyuri/rdf/StatementComparator.java index 47a20e5..fd54d8b 100644 --- a/src/main/java/net/trustyuri/rdf/StatementComparator.java +++ b/src/main/java/net/trustyuri/rdf/StatementComparator.java @@ -1,13 +1,14 @@ package net.trustyuri.rdf; import java.util.Comparator; +import java.util.Optional; -import org.openrdf.model.BNode; -import org.openrdf.model.Literal; -import org.openrdf.model.Resource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; +import org.eclipse.rdf4j.model.BNode; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Literal; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.Value; public class StatementComparator implements Comparator { @@ -70,7 +71,7 @@ private static int compareResource(Resource r1, Resource r2) { if (r1 instanceof BNode) { throw new RuntimeException("Unexpected blank node"); } else { - return compareURIs((URI) r1, (URI) r2); + return compareURIs((IRI) r1, (IRI) r2); } } @@ -83,9 +84,9 @@ private static int compareLiteral(Literal l1, Literal l2) { s1 = null; s2 = null; if (l1.getDatatype() != null) s1 = l1.getDatatype().toString(); - if (l1.getLanguage() != null) s1 = null; + if (l1.getLanguage().isPresent()) s1 = null; if (l2.getDatatype() != null) s2 = l2.getDatatype().toString(); - if (l2.getLanguage() != null) s2 = null; + if (l2.getLanguage().isPresent()) s2 = null; if (s1 == null && s2 != null) { return -1; } else if (s1 != null && s2 == null) { @@ -93,19 +94,19 @@ private static int compareLiteral(Literal l1, Literal l2) { } else if (s1 != null && !s1.equals(s2)) { return s1.compareTo(s2); } - s1 = l1.getLanguage(); - s2 = l2.getLanguage(); - if (s1 == null && s2 != null) { + Optional lang1 = l1.getLanguage(); + Optional lang2 = l2.getLanguage(); + if (!lang1.isPresent() && lang2.isPresent()) { return -1; - } else if (s1 != null && s2 == null) { + } else if (lang1.isPresent() && !lang2.isPresent()) { return 1; - } else if (s1 != null && !s1.equals(s2)) { - return s1.compareTo(s2); + } else if (lang1.isPresent() && !lang1.get().toLowerCase().equals(lang2.get().toLowerCase())) { + return lang1.get().toLowerCase().compareTo(lang2.get().toLowerCase()); } return 0; } - private static int compareURIs(URI uri1, URI uri2) { + private static int compareURIs(IRI uri1, IRI uri2) { return uri1.toString().compareTo(uri2.toString()); } diff --git a/src/main/java/net/trustyuri/rdf/TransformLargeRdf.java b/src/main/java/net/trustyuri/rdf/TransformLargeRdf.java index 5d30eba..0a67ccb 100644 --- a/src/main/java/net/trustyuri/rdf/TransformLargeRdf.java +++ b/src/main/java/net/trustyuri/rdf/TransformLargeRdf.java @@ -13,22 +13,21 @@ import java.util.List; import java.util.zip.GZIPOutputStream; -import net.trustyuri.TrustyUriException; -import net.trustyuri.TrustyUriResource; - -import org.openrdf.OpenRDFException; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.rio.RDFFormat; -import org.openrdf.rio.RDFHandlerException; -import org.openrdf.rio.RDFParser; -import org.openrdf.rio.RDFWriter; -import org.openrdf.rio.Rio; -import org.openrdf.rio.helpers.RDFHandlerBase; -import org.openrdf.rio.helpers.RDFaParserSettings; +import org.eclipse.rdf4j.RDF4JException; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.RDFHandlerException; +import org.eclipse.rdf4j.rio.RDFParser; +import org.eclipse.rdf4j.rio.RDFWriter; +import org.eclipse.rdf4j.rio.Rio; +import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler; import com.google.code.externalsorting.ExternalSort; +import net.trustyuri.TrustyUriException; +import net.trustyuri.TrustyUriResource; + public class TransformLargeRdf { public static void main(String[] args) throws IOException, TrustyUriException { @@ -47,7 +46,7 @@ public static void main(String[] args) throws IOException, TrustyUriException { private String inputDir; private String baseName; private MessageDigest md; - private URI baseUri; + private IRI baseUri; private String fileName, ext; public TransformLargeRdf(File inputFile, String baseName) { @@ -55,7 +54,7 @@ public TransformLargeRdf(File inputFile, String baseName) { this.baseName = baseName; } - public URI transform() throws IOException, TrustyUriException { + public IRI transform() throws IOException, TrustyUriException { baseUri = TransformRdf.getBaseURI(baseName); md = RdfHasher.getDigest(); inputDir = inputFile.getParent(); @@ -72,11 +71,10 @@ public URI transform() throws IOException, TrustyUriException { ext = "." + format.getFileExtensions().get(0); } - RDFParser p = Rio.createParser(format); - p.getParserConfig().set(RDFaParserSettings.FAIL_ON_RDFA_UNDEFINED_PREFIXES, true); + RDFParser p = RdfUtils.getParser(format); File sortInFile = new File(inputDir, fileName + ".temp.sort-in"); final FileOutputStream preOut = new FileOutputStream(sortInFile); - p.setRDFHandler(new RdfPreprocessor(new RDFHandlerBase() { + p.setRDFHandler(new RdfPreprocessor(new AbstractRDFHandler() { @Override public void handleStatement(Statement st) throws RDFHandlerException { @@ -92,7 +90,7 @@ public void handleStatement(Statement st) throws RDFHandlerException { BufferedReader reader = new BufferedReader(r.getInputStreamReader(), 64*1024); try { p.parse(reader, ""); - } catch (OpenRDFException ex) { + } catch (RDF4JException ex) { throw new TrustyUriException(ex); } finally { reader.close(); diff --git a/src/main/java/net/trustyuri/rdf/TransformRdf.java b/src/main/java/net/trustyuri/rdf/TransformRdf.java index fe2ef67..c81190d 100644 --- a/src/main/java/net/trustyuri/rdf/TransformRdf.java +++ b/src/main/java/net/trustyuri/rdf/TransformRdf.java @@ -12,19 +12,20 @@ import java.util.Map; import java.util.zip.GZIPOutputStream; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.Value; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.RDFHandler; +import org.eclipse.rdf4j.rio.RDFHandlerException; +import org.eclipse.rdf4j.rio.RDFWriter; +import org.eclipse.rdf4j.rio.Rio; +import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings; + import net.trustyuri.TrustyUriException; import net.trustyuri.TrustyUriResource; -import org.openrdf.model.Statement; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.rio.RDFFormat; -import org.openrdf.rio.RDFHandler; -import org.openrdf.rio.RDFHandlerException; -import org.openrdf.rio.RDFWriter; -import org.openrdf.rio.Rio; - public class TransformRdf { // TODO Use RB module by default if trusty URI represents a single RDF graph @@ -38,13 +39,13 @@ public static void main(String[] args) throws IOException, TrustyUriException { transform(inputFile, baseName); } - public static URI transform(File inputFile, String baseName) + public static IRI transform(File inputFile, String baseName) throws IOException, TrustyUriException { if (baseName == null) { baseName = inputFile.getName().replaceFirst("[.][^.]+$", ""); } RdfFileContent content = RdfUtils.load(new TrustyUriResource(inputFile)); - URI baseUri = getBaseURI(baseName); + IRI baseUri = getBaseURI(baseName); String name = baseName; if (baseName.indexOf("/") > 0) { name = baseName.replaceFirst("^.*[^A-Za-z0-9.\\-_]([A-Za-z0-9.\\-_]*)$", "$1"); @@ -70,45 +71,45 @@ public static URI transform(File inputFile, String baseName) out = new FileOutputStream(new File(inputFile.getParent(), fileName)); } RDFWriter writer = Rio.createWriter(format, new OutputStreamWriter(out, Charset.forName("UTF-8"))); - URI uri = includeArtifactCode(content, artifactCode, baseUri, writer); + IRI uri = includeArtifactCode(content, artifactCode, baseUri, writer); out.close(); return uri; } - public static URI transform(RdfFileContent content, RDFHandler handler, String baseName) + public static IRI transform(RdfFileContent content, RDFHandler handler, String baseName) throws TrustyUriException { - URI baseUri = getBaseURI(baseName); + IRI baseUri = getBaseURI(baseName); content = RdfPreprocessor.run(content, baseUri); - URI uri = transformPreprocessed(content, baseUri, handler); + IRI uri = transformPreprocessed(content, baseUri, handler); return uri; } - public static URI transform(InputStream in, RDFFormat format, OutputStream out, String baseName) + public static IRI transform(InputStream in, RDFFormat format, OutputStream out, String baseName) throws IOException, TrustyUriException { - URI baseUri = getBaseURI(baseName); + IRI baseUri = getBaseURI(baseName); RdfFileContent content = RdfUtils.load(in, format); content = RdfPreprocessor.run(content, baseUri); RDFWriter writer = Rio.createWriter(format, new OutputStreamWriter(out, Charset.forName("UTF-8"))); - URI uri = transformPreprocessed(content, baseUri, writer); + IRI uri = transformPreprocessed(content, baseUri, writer); out.close(); return uri; } - public static URI transformPreprocessed(RdfFileContent preprocessedContent, URI baseUri, RDFWriter writer) + public static IRI transformPreprocessed(RdfFileContent preprocessedContent, IRI baseUri, RDFWriter writer) throws TrustyUriException { String artifactCode = RdfHasher.makeArtifactCode(preprocessedContent.getStatements()); - URI uri = includeArtifactCode(preprocessedContent, artifactCode, baseUri, writer); + IRI uri = includeArtifactCode(preprocessedContent, artifactCode, baseUri, writer); return uri; } - public static URI transformPreprocessed(RdfFileContent preprocessedContent, URI baseUri, RDFHandler handler) + public static IRI transformPreprocessed(RdfFileContent preprocessedContent, IRI baseUri, RDFHandler handler) throws TrustyUriException { String artifactCode = RdfHasher.makeArtifactCode(preprocessedContent.getStatements()); - URI uri = includeArtifactCode(preprocessedContent, artifactCode, baseUri, handler); + IRI uri = includeArtifactCode(preprocessedContent, artifactCode, baseUri, handler); return uri; } - private static URI includeArtifactCode(RdfFileContent preprocessedContent, String artifactCode, URI baseUri, Object writerOrHandler) + private static IRI includeArtifactCode(RdfFileContent preprocessedContent, String artifactCode, IRI baseUri, Object writerOrHandler) throws TrustyUriException { Map ns = makeNamespaceMap(preprocessedContent.getStatements(), baseUri, artifactCode); HashAdder hashAdder; @@ -126,15 +127,15 @@ private static URI includeArtifactCode(RdfFileContent preprocessedContent, Strin } - static URI getBaseURI(String baseName) { - URI baseURI = null; + static IRI getBaseURI(String baseName) { + IRI baseURI = null; if (baseName.indexOf("://") > 0) { - baseURI = new URIImpl(baseName); + baseURI = SimpleValueFactory.getInstance().createIRI(baseName); } return baseURI; } - static Map makeNamespaceMap(List statements, URI baseURI, String artifactCode) { + static Map makeNamespaceMap(List statements, IRI baseURI, String artifactCode) { Map ns = new HashMap(); if (baseURI == null) return ns; String u = RdfUtils.getTrustyUriString(baseURI, artifactCode); @@ -148,8 +149,8 @@ static Map makeNamespaceMap(List statements, URI baseU return ns; } - static void addToNamespaceMap(Value v, URI baseURI, String artifactCode, Map ns) { - if (!(v instanceof URI)) return; + static void addToNamespaceMap(Value v, IRI baseURI, String artifactCode, Map ns) { + if (!(v instanceof IRI)) return; String uri = RdfUtils.getTrustyUriString(baseURI, artifactCode); String s = v.toString().replace(" ", artifactCode); if (!s.startsWith(uri)) return; diff --git a/src/main/java/net/trustyuri/rdf/TransformRdfGraph.java b/src/main/java/net/trustyuri/rdf/TransformRdfGraph.java index 836b930..e853191 100644 --- a/src/main/java/net/trustyuri/rdf/TransformRdfGraph.java +++ b/src/main/java/net/trustyuri/rdf/TransformRdfGraph.java @@ -12,16 +12,16 @@ import java.util.ArrayList; import java.util.List; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.eclipse.rdf4j.rio.RDFHandler; +import org.eclipse.rdf4j.rio.RDFHandlerException; +import org.eclipse.rdf4j.rio.Rio; + import net.trustyuri.TrustyUriException; import net.trustyuri.TrustyUriResource; -import org.openrdf.model.URI; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.rio.RDFFormat; -import org.openrdf.rio.RDFHandler; -import org.openrdf.rio.RDFHandlerException; -import org.openrdf.rio.Rio; - public class TransformRdfGraph { // TODO only transform blank nodes that appear within the given graph @@ -31,16 +31,16 @@ public static void main(String[] args) throws IOException, TrustyUriException { throw new RuntimeException("Not enough arguments: ( ...)"); } File inputFile = new File(args[0]); - List baseUris = new ArrayList(); + List baseUris = new ArrayList(); for (int i = 1 ; i < args.length ; i++) { String arg = args[i]; if (arg.contains("://")) { - baseUris.add(new URIImpl(arg)); + baseUris.add(SimpleValueFactory.getInstance().createIRI(arg)); } else { BufferedReader reader = new BufferedReader(new FileReader(arg)); String line; while ((line = reader.readLine()) != null) { - baseUris.add(new URIImpl(line)); + baseUris.add(SimpleValueFactory.getInstance().createIRI(line)); } reader.close(); } @@ -51,10 +51,10 @@ public static void main(String[] args) throws IOException, TrustyUriException { if (!format.getFileExtensions().isEmpty()) { outputFilePath += "." + format.getFileExtensions().get(0); } - transform(content, new File(outputFilePath), baseUris.toArray(new URI[baseUris.size()])); + transform(content, new File(outputFilePath), baseUris.toArray(new IRI[baseUris.size()])); } - public static void transform(RdfFileContent content, File outputFile, URI... baseUris) + public static void transform(RdfFileContent content, File outputFile, IRI... baseUris) throws IOException, TrustyUriException { try { OutputStream out = new FileOutputStream(outputFile); @@ -65,7 +65,7 @@ public static void transform(RdfFileContent content, File outputFile, URI... bas } } - public static void transform(RdfFileContent content, RDFHandler handler, URI... baseUris) + public static void transform(RdfFileContent content, RDFHandler handler, IRI... baseUris) throws IOException, TrustyUriException { try { processBaseUris(content, handler, baseUris); @@ -74,7 +74,7 @@ public static void transform(RdfFileContent content, RDFHandler handler, URI... } } - public static void transform(InputStream in, RDFFormat format, OutputStream out, URI... baseUris) + public static void transform(InputStream in, RDFFormat format, OutputStream out, IRI... baseUris) throws IOException, TrustyUriException { RdfFileContent content = RdfUtils.load(in, format); try { @@ -85,9 +85,9 @@ public static void transform(InputStream in, RDFFormat format, OutputStream out, out.close(); } - private static void processBaseUris(RdfFileContent content, RDFHandler handler, URI... baseUris) + private static void processBaseUris(RdfFileContent content, RDFHandler handler, IRI... baseUris) throws RDFHandlerException, TrustyUriException { - for (URI baseUri : baseUris) { + for (IRI baseUri : baseUris) { RdfFileContent newContent = new RdfFileContent(content.getOriginalFormat()); RdfPreprocessor preprocessor = new RdfPreprocessor(newContent, baseUri); content.propagate(preprocessor); diff --git a/src/test/java/net/trustyuri/file/CheckFileTest.java b/src/test/java/net/trustyuri/file/CheckFileTest.java index b8f4f17..0493901 100644 --- a/src/test/java/net/trustyuri/file/CheckFileTest.java +++ b/src/test/java/net/trustyuri/file/CheckFileTest.java @@ -2,12 +2,12 @@ import java.io.File; -import net.trustyuri.CheckFile; - import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import net.trustyuri.CheckFile; + public class CheckFileTest { diff --git a/src/test/java/net/trustyuri/file/ProcessFileTest.java b/src/test/java/net/trustyuri/file/ProcessFileTest.java index d976c54..c977393 100644 --- a/src/test/java/net/trustyuri/file/ProcessFileTest.java +++ b/src/test/java/net/trustyuri/file/ProcessFileTest.java @@ -2,15 +2,14 @@ import java.io.File; -import net.trustyuri.CheckFile; -import net.trustyuri.TrustyUriUtils; -import net.trustyuri.file.ProcessFile; - import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import net.trustyuri.CheckFile; +import net.trustyuri.TrustyUriUtils; + public class ProcessFileTest { diff --git a/src/test/java/net/trustyuri/rdf/CheckLargeRdfTest.java b/src/test/java/net/trustyuri/rdf/CheckLargeRdfTest.java index 254b5a5..9980714 100644 --- a/src/test/java/net/trustyuri/rdf/CheckLargeRdfTest.java +++ b/src/test/java/net/trustyuri/rdf/CheckLargeRdfTest.java @@ -2,8 +2,6 @@ import java.io.File; -import net.trustyuri.rdf.CheckLargeRdf; - import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; diff --git a/src/test/java/net/trustyuri/rdf/CheckRdfTest.java b/src/test/java/net/trustyuri/rdf/CheckRdfTest.java index dce86ab..1f39497 100644 --- a/src/test/java/net/trustyuri/rdf/CheckRdfTest.java +++ b/src/test/java/net/trustyuri/rdf/CheckRdfTest.java @@ -2,12 +2,12 @@ import java.io.File; -import net.trustyuri.CheckFile; - import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import net.trustyuri.CheckFile; + public class CheckRdfTest { diff --git a/src/test/java/net/trustyuri/rdf/CheckSortedRdfTest.java b/src/test/java/net/trustyuri/rdf/CheckSortedRdfTest.java index 21f3d12..7624c15 100644 --- a/src/test/java/net/trustyuri/rdf/CheckSortedRdfTest.java +++ b/src/test/java/net/trustyuri/rdf/CheckSortedRdfTest.java @@ -2,8 +2,6 @@ import java.io.File; -import net.trustyuri.rdf.CheckSortedRdf; - import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; diff --git a/src/test/java/net/trustyuri/rdf/TransformLargeRdfTest.java b/src/test/java/net/trustyuri/rdf/TransformLargeRdfTest.java index ad65bb1..deac7c8 100644 --- a/src/test/java/net/trustyuri/rdf/TransformLargeRdfTest.java +++ b/src/test/java/net/trustyuri/rdf/TransformLargeRdfTest.java @@ -2,15 +2,14 @@ import java.io.File; -import net.trustyuri.CheckFile; -import net.trustyuri.TrustyUriUtils; -import net.trustyuri.rdf.TransformLargeRdf; - import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import net.trustyuri.CheckFile; +import net.trustyuri.TrustyUriUtils; + public class TransformLargeRdfTest { @Rule diff --git a/src/test/java/net/trustyuri/rdf/TransformRdfTest.java b/src/test/java/net/trustyuri/rdf/TransformRdfTest.java index 6e0ff4d..cbb25a6 100644 --- a/src/test/java/net/trustyuri/rdf/TransformRdfTest.java +++ b/src/test/java/net/trustyuri/rdf/TransformRdfTest.java @@ -2,15 +2,14 @@ import java.io.File; -import net.trustyuri.CheckFile; -import net.trustyuri.TrustyUriUtils; -import net.trustyuri.rdf.TransformRdf; - import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import net.trustyuri.CheckFile; +import net.trustyuri.TrustyUriUtils; + public class TransformRdfTest {