Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use a switch + enum for the different types of RDF nodes
  • Loading branch information
ajs6f authored and cbeer committed Oct 15, 2014
1 parent 2ae3e04 commit 501ab45
Showing 1 changed file with 45 additions and 34 deletions.
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.http.commons.responses;

import static javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class RdfStreamStreamingOutput extends AbstractFuture<Void> implements
StreamingOutput {

private static final Logger LOGGER =
getLogger(RdfStreamStreamingOutput.class);
getLogger(RdfStreamStreamingOutput.class);

private static ValueFactory vfactory = getInstance();

Expand Down Expand Up @@ -109,8 +110,8 @@ public void write(final OutputStream output) {
}

private void write(final Iterable<Statement> model,
final OutputStream output,
final RDFFormat dataFormat)
final OutputStream output,
final RDFFormat dataFormat)
throws RDFHandlerException {
final WriterConfig settings = new WriterConfig();
final RDFWriter writer = Rio.createWriter(dataFormat, output);
Expand All @@ -131,14 +132,14 @@ private static Iterable<Map.Entry<String, String>> excludeProtectedNamespaces(
* about in some serializations (e.g. RDF/XML where xmlns:xmlns is forbidden by XML);
*/
return Iterables.filter(namespaces.entrySet(), new Predicate<Map.Entry<String, String>>() {

@Override
public boolean apply(final Map.Entry<String, String> input) {
return !input.getKey().equals("xmlns");
}
});
}


private Iterable<Statement> asStatements() {
return new Iterable<Statement>() {

Expand All @@ -150,52 +151,62 @@ public Iterator<Statement> iterator() {
}

protected static final Function<? super Triple, Statement> toStatement =
new Function<Triple, Statement>() {
new Function<Triple, Statement>() {

@Override
public Statement apply(final Triple t) {
final Resource subject = getResourceForSubject(t.getSubject());
final URI predicate = vfactory.createURI(t.getPredicate().getURI());
final Value object = getValueForObject(t.getObject());
return vfactory.createStatement(subject, predicate, object);
}
@Override
public Statement apply(final Triple t) {
final Resource subject = getResourceForSubject(t.getSubject());
final URI predicate = vfactory.createURI(t.getPredicate().getURI());
final Value object = getValueForObject(t.getObject());
return vfactory.createStatement(subject, predicate, object);
}

};
};

private static Resource getResourceForSubject(final Node subjectNode) {
final Resource subject;

if (subjectNode.isBlank()) {
subject = vfactory.createBNode(subjectNode.getBlankNodeLabel());
} else {
subject = vfactory.createURI(subjectNode.getURI());
switch (RdfNodeCategory.getType(subjectNode)) {
case BLANK:
return vfactory.createBNode(subjectNode.getBlankNodeLabel());
default:
return vfactory.createURI(subjectNode.getURI());
}

return subject;
}

protected static Value getValueForObject(final Node object) {
final Value value;
if (object.isURI()) {
value = vfactory.createURI(object.getURI());
} else if (object.isBlank()) {
value = vfactory.createBNode(object.getBlankNodeLabel());
} else if (object.isLiteral()) {
switch (RdfNodeCategory.getType(object)) {
case BLANK:
return vfactory.createBNode(object.getBlankNodeLabel());
case URI:
return vfactory.createURI(object.getURI());
case LITERAL:
final Object literalValue = object.getLiteralValue();

final String literalDatatypeURI = object.getLiteralDatatypeURI();

if (literalDatatypeURI != null) {
final URI uri = vfactory.createURI(literalDatatypeURI);
value = vfactory.createLiteral(literalValue.toString(), uri);
} else {
value = createLiteral(vfactory, literalValue);
return vfactory.createLiteral(literalValue.toString(), uri);
}
} else {
// should not happen..
throw new AssertionError("Unable to convert " + object + " to a value");
return createLiteral(vfactory, literalValue);
default:
throw new AssertionError("Received an RDF object that was neither blank nor literal nor labeled!");
}
}

private static enum RdfNodeCategory {
LITERAL, BLANK, URI;

return value;
public static RdfNodeCategory getType(final Node n) {
if (n.isURI()) {
return URI;
}
if (n.isBlank()) {
return BLANK;
}
if (n.isLiteral()) {
return LITERAL;
}
throw new AssertionError("Received an RDF node that was neither blank nor literal nor labeled!");
}
}
}

0 comments on commit 501ab45

Please sign in to comment.