Skip to content

Commit

Permalink
rdf generator brought inline with the globbing API
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Apr 27, 2013
1 parent 981ed14 commit c42bb5c
Show file tree
Hide file tree
Showing 16 changed files with 494 additions and 124 deletions.
Expand Up @@ -12,7 +12,6 @@
import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
Expand All @@ -30,6 +29,8 @@
import org.fcrepo.generator.rdf.TripleSource;
import org.fcrepo.generator.rdf.TripleSource.Triple;
import org.fcrepo.generator.rdf.Utils;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.ObjectService;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.sail.memory.model.MemValueFactory;
Expand All @@ -41,106 +42,119 @@
@Produces({TEXT_XML, "text/turtle", TEXT_PLAIN})
public class FedoraRdfGenerator extends AbstractResource {

private List<TripleSource<FedoraObject>> objectGenerators;
private List<TripleSource<Datastream>> dsGenerators;

private static final ValueFactory valFactory = new MemValueFactory();

private static final Logger logger = getLogger(FedoraRdfGenerator.class);

private List<TripleSource<FedoraObject>> objectGenerators;

private List<TripleSource<Datastream>> datastreamGenerators;

@GET
@Produces({TEXT_XML, "text/turtle", TEXT_PLAIN})
public String getRdfXml(@PathParam("path")
final List<PathSegment> pathList, @HeaderParam("Accept")
@DefaultValue(TEXT_XML)
final String mimeType) throws IOException, RepositoryException,
TripleHandlerException {
public String getRdfXml(
@PathParam("path") final List<PathSegment> pathList,
@HeaderParam("Accept") @DefaultValue(TEXT_XML) final String mimeType
) throws IOException, RepositoryException, TripleHandlerException {

final String path = toPath(pathList);
final Session session = getAuthenticatedSession();
Node node = session.getNode(path);

final URI docURI = valFactory.createURI("info:" + path);
logger.debug("Using ValueFactory: " + valFactory.toString());
final ExtractionContext context =
new ExtractionContext("Fedora Serialization Context", docURI);
logger.debug("Using ExtractionContext: " + context.toString());


try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {

// possible serializations
final TripleHandler writer = Utils.selectWriter(mimeType, out);
logger.trace("Created RDF Writer: " + writer.getClass().getName());

writer.openContext(context);
writer.startDocument(docURI);

// add JCR-managed namespaces
final NamespaceRegistry nReg =
node.getSession().getWorkspace()
.getNamespaceRegistry();
for (final String prefix : nReg.getPrefixes()) {
final String nsURI = nReg.getURI(prefix);
if (nsURI != null && !nsURI.equals("") &&
!prefix.equals("xmlns")) {
writer.receiveNamespace(prefix, nsURI, context);
}
}

if(node.isNodeType("nt:file")) {
writeDatastreamRdfXml(datastreamService.getDatastream(path), writer, context);
} else if(node.isNodeType("nt:folder")) {
writeObjectRdfXml(objectService.getObject(path), writer, context);
} else {
throw new RepositoryException("Could not find object");
}

writer.endDocument(docURI);
writer.close();
logger.debug("Generated RDF: " + out.toString());
return out.toString();
}
final java.net.URI itemUri = uriInfo.getBaseUriBuilder().build("/rest" + path);
final URI docURI = valFactory.createURI(itemUri.toString());
logger.debug("Using ValueFactory: " + valFactory.toString());
final ExtractionContext context =
new ExtractionContext("Fedora Serialization Context", docURI);
logger.debug("Using ExtractionContext: {}", context.toString());

try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {

// possible serializations
final TripleHandler writer = Utils.selectWriter(mimeType, out);
logger.trace("Created RDF Writer: {}", writer.getClass().getName());

writer.openContext(context);
writer.startDocument(docURI);

if (objectService.isFile(path)) {
writeDatastreamTriples(path, writer, context);
} else {
writeObjectTriples(path, writer, context);
}


writer.endDocument(docURI);
writer.close();
logger.debug("Generated RDF: {}", out.toString());
return out.toString();
}

}

// add JCR-managed namespaces
private void writeNamespaces(Node node, TripleHandler writer, ExtractionContext context)
throws TripleHandlerException, RepositoryException {
final NamespaceRegistry nReg =
node.getSession().getWorkspace()
.getNamespaceRegistry();
for (final String prefix : nReg.getPrefixes()) {
final String nsURI = nReg.getURI(prefix);
if (nsURI != null && !nsURI.equals("") &&
!prefix.equals("xmlns")) {
writer.receiveNamespace(prefix, nsURI, context);
}
}
}

private void writeDatastreamTriples(String path, TripleHandler writer, ExtractionContext context)
throws TripleHandlerException, RepositoryException {

final Datastream obj = datastreamService.getDatastream(path);
// add namespaces
writeNamespaces(obj.getNode(), writer, context);
// add triples from each TripleSource
for (final TripleSource<Datastream> tripleSource : datastreamGenerators) {
logger.trace("Using TripleSource: {}",
tripleSource.getClass().getName());
for (final Triple t : tripleSource.getTriples(obj, uriInfo)) {
writer.receiveTriple(valFactory.createURI(t.subject),
valFactory.createURI(t.predicate), valFactory
.createLiteral(t.object), null, context);
}
}
}

private void writeObjectTriples(String path, TripleHandler writer, ExtractionContext context)
throws TripleHandlerException, RepositoryException {
final FedoraObject obj = objectService.getObject(path);
// add namespaces
writeNamespaces(obj.getNode(), writer, context);
// add triples from each TripleSource
for (final TripleSource<FedoraObject> tripleSource : objectGenerators) {
logger.trace("Using TripleSource: {}",
tripleSource.getClass().getName());
for (final Triple t : tripleSource.getTriples(obj, uriInfo)) {
writer.receiveTriple(valFactory.createURI(t.subject),
valFactory.createURI(t.predicate), valFactory
.createLiteral(t.object), null, context);
}
}
}

private void writeObjectRdfXml(final FedoraObject obj, TripleHandler writer, ExtractionContext context) throws TripleHandlerException, RepositoryException {

// add triples from each TripleSource
for (final TripleSource<FedoraObject> tripleSource : objectGenerators) {
logger.trace("Using TripleSource: " +
tripleSource.getClass().getName());
for (final Triple t : tripleSource.getTriples(obj, uriInfo)) {
writer.receiveTriple(valFactory.createURI(t.subject),
valFactory.createURI(t.predicate), valFactory
.createLiteral(t.object), null, context);
}
}

}

private void writeDatastreamRdfXml(final Datastream ds, TripleHandler writer, ExtractionContext context) throws RepositoryException, TripleHandlerException {
// add triples from each TripleSource
for (final TripleSource<Datastream> tripleSource : dsGenerators) {
logger.trace("Using TripleSource: " +
tripleSource.getClass().getName());
for (final Triple t : tripleSource.getTriples(ds, uriInfo)) {
writer.receiveTriple(valFactory.createURI(t.subject),
valFactory.createURI(t.predicate), valFactory
.createLiteral(t.object), null, context);
}
}
}

public void setObjectGenerators(
public void setObjectGenerators(
final List<TripleSource<FedoraObject>> objectGenerators) {
this.objectGenerators = objectGenerators;
}

public void setDsGenerators(
final List<TripleSource<Datastream>> dsGenerators) {
this.dsGenerators = dsGenerators;
}
public void setDatastreamGenerators(
final List<TripleSource<Datastream>> dsGenerators) {
this.datastreamGenerators = dsGenerators;
}

void setObjectService(ObjectService objectService) {
this.objectService = objectService;
}

void setDatastreamService(DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}

}
Expand Up @@ -48,8 +48,7 @@ public Triple apply(final Triple t) {
}
try {
return new Triple(uriInfo.getBaseUriBuilder().path(
"objects").path(ds.getObject().getName())
.path("datastreams").path(ds.getDsId())
"rest").path(node.getPath())
.build().toString(), t.predicate, t.object);
} catch (UriBuilderException | RepositoryException e) {
throw new IllegalStateException(e);
Expand Down
Expand Up @@ -47,7 +47,7 @@ public Triple apply(final Triple t) {
if (t == null) {
return null;
}
return new Triple(uriInfo.getBaseUriBuilder().path(
return new Triple(uriInfo.getBaseUriBuilder().path("rest").path(
t.subject).build().toString(), t.predicate,
t.object);
}
Expand Down
Expand Up @@ -29,8 +29,8 @@ public List<org.fcrepo.generator.rdf.TripleSource.Triple> getTriples(
throws RepositoryException {
logger.trace("Entering getTriples()...");
final Builder<Triple> triples = builder();
logger.debug("Generating triples for node: " + source.getPath());
final String subject = source.getPath();
logger.debug("Generating triples for node: {}", subject);

// add primary NodeType
final Triple pt =
Expand Down
Expand Up @@ -11,7 +11,6 @@

import java.util.List;

import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
Expand All @@ -33,12 +32,12 @@ public class PropertiesGenerator implements TripleSource<Node> {
public List<Triple> getTriples(final Node source, final UriInfo... uriInfo)
throws RepositoryException {
logger.trace("Entering getTriples()...");
logger.debug("Generating triples for node: " + source.getPath());
logger.debug("Generating triples for node: {}", source.getPath());
final Builder<Triple> triples = builder();

@SuppressWarnings("unchecked")
final List<Property> properties = copyOf(source.getProperties());
logger.debug("Retrieved properties: " + properties.toString());
logger.debug("Retrieved properties: {}", properties.toString());

triples.addAll(transform(filter(properties,
not(isMultipleValuedProperty)), singlevaluedprop2triple));
Expand All @@ -60,8 +59,7 @@ public Triple apply(final Property p) {
try {
return new Triple(
p.getParent().getPath(),
expandJCRNamespace(p.getName(), p.getSession()
.getWorkspace().getNamespaceRegistry()),
Utils.expandJCRNamespace(p),
p.getString());
} catch (final RepositoryException e) {
throw new IllegalStateException(e);
Expand All @@ -82,9 +80,7 @@ public List<Triple> apply(final Property p) {
try {
for (final Value v : p.getValues()) {
triples.add(new Triple(p.getParent().getPath(),
expandJCRNamespace(p.getName(), p
.getSession().getWorkspace()
.getNamespaceRegistry()), v
Utils.expandJCRNamespace(p), v
.getString()));
}
return triples.build();
Expand All @@ -95,11 +91,4 @@ public List<Triple> apply(final Property p) {

};

private static String expandJCRNamespace(final String name,
final NamespaceRegistry nReg) throws RepositoryException {
final String predicatePrefix = name.substring(0, name.indexOf(':'));
return name.replaceFirst(predicatePrefix + ":", nReg
.getURI(predicatePrefix));
}

}
Expand Up @@ -5,6 +5,10 @@

import java.io.OutputStream;

import javax.jcr.NamespaceRegistry;
import javax.jcr.Property;
import javax.jcr.RepositoryException;

import org.apache.any23.writer.NTriplesWriter;
import org.apache.any23.writer.RDFXMLWriter;
import org.apache.any23.writer.TripleHandler;
Expand All @@ -24,4 +28,12 @@ public static TripleHandler selectWriter(final String mimeType,
}
}

public static String expandJCRNamespace(Property p) throws RepositoryException {
String name = p.getName();
NamespaceRegistry nReg = p.getSession().getWorkspace().getNamespaceRegistry();
final String predicatePrefix = name.substring(0, name.indexOf(':'));
return name.replaceFirst(predicatePrefix + ":", nReg
.getURI(predicatePrefix));
}

}

0 comments on commit c42bb5c

Please sign in to comment.