Skip to content

Commit

Permalink
Support for temporary URIs that are replaced during trustification
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuhn committed Apr 19, 2017
1 parent 7eefb1a commit 333b2dd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/org/nanopub/trusty/MakeTrustyNanopub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/org/nanopub/trusty/TempUriReplacer.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 333b2dd

Please sign in to comment.