From 333b2dd0c58701683c1d24ef86bd1abd676a9a46 Mon Sep 17 00:00:00 2001 From: Tobias Kuhn Date: Wed, 19 Apr 2017 08:42:09 +0200 Subject: [PATCH] Support for temporary URIs that are replaced during trustification --- .../org/nanopub/trusty/MakeTrustyNanopub.java | 11 ++- .../org/nanopub/trusty/TempUriReplacer.java | 77 +++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/nanopub/trusty/TempUriReplacer.java diff --git a/src/main/java/org/nanopub/trusty/MakeTrustyNanopub.java b/src/main/java/org/nanopub/trusty/MakeTrustyNanopub.java index d249a68..aabad2e 100644 --- a/src/main/java/org/nanopub/trusty/MakeTrustyNanopub.java +++ b/src/main/java/org/nanopub/trusty/MakeTrustyNanopub.java @@ -99,9 +99,16 @@ public static Nanopub transform(Nanopub nanopub) throws TrustyUriException { } try { RdfFileContent r = new RdfFileContent(RDFFormat.TRIG); - NanopubUtils.propagateToHandler(nanopub, r); + String npUri; + if (TempUriReplacer.hasTempUri(nanopub)) { + npUri = TempUriReplacer.normUri; + NanopubUtils.propagateToHandler(nanopub, new TempUriReplacer(nanopub, r)); + } else { + npUri = nanopub.getUri().toString(); + NanopubUtils.propagateToHandler(nanopub, r); + } NanopubRdfHandler h = new NanopubRdfHandler(); - TransformRdf.transform(r, h, nanopub.getUri().toString()); + TransformRdf.transform(r, h, npUri); np = h.getNanopub(); } catch (RDFHandlerException ex) { throw new TrustyUriException(ex); diff --git a/src/main/java/org/nanopub/trusty/TempUriReplacer.java b/src/main/java/org/nanopub/trusty/TempUriReplacer.java new file mode 100644 index 0000000..4242e81 --- /dev/null +++ b/src/main/java/org/nanopub/trusty/TempUriReplacer.java @@ -0,0 +1,77 @@ +package org.nanopub.trusty; + +import org.nanopub.Nanopub; +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; + +/** + * You can use temporary URIs for your nanopublications that start with + * "http://purl.org/nanopub/temp/". These then become short "http://purl.org/np/" + * when they are transformed to trusty nanopublications. + * + * @author Tobias Kuhn + */ +public class TempUriReplacer implements RDFHandler { + + public static final String tempUri = "http://purl.org/nanopub/temp/"; + public static final String normUri = "http://purl.org/np/"; + + private String uriPrefix; + private RDFHandler nestedHandler; + + public TempUriReplacer(Nanopub np, RDFHandler nestedHandler) { + uriPrefix = np.getUri().stringValue(); + this.nestedHandler = nestedHandler; + } + + public static boolean hasTempUri(Nanopub np) { + return np.getUri().stringValue().startsWith(tempUri); + } + + @Override + public void handleStatement(Statement st) throws RDFHandlerException { + nestedHandler.handleStatement(new ContextStatementImpl( + (Resource) replace(st.getSubject()), + (URI) replace(st.getPredicate()), + replace(st.getObject()), + (Resource) replace(st.getContext()))); + } + + @Override + public void handleNamespace(String prefix, String uri) throws RDFHandlerException { + if (uri.startsWith(uriPrefix)) { + uri = uri.replace(uriPrefix, normUri); + } + nestedHandler.handleNamespace(prefix, uri); + } + + private Value replace(Value v) { + if (v instanceof URI && v.stringValue().startsWith(uriPrefix)) { + return new URIImpl(v.stringValue().replace(uriPrefix, normUri)); + } else { + return v; + } + } + + @Override + public void startRDF() throws RDFHandlerException { + nestedHandler.startRDF(); + } + + @Override + public void endRDF() throws RDFHandlerException { + nestedHandler.endRDF(); + } + + @Override + public void handleComment(String comment) throws RDFHandlerException { + nestedHandler.handleComment(comment); + } + +}