Skip to content

Commit

Permalink
Include type information for unknown string types
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 24, 2014
1 parent 9a63db4 commit 3170534
Showing 1 changed file with 22 additions and 4 deletions.
Expand Up @@ -16,7 +16,9 @@
package org.fcrepo.kernel.impl.rdf.converters;

import com.google.common.base.Converter;
import com.hp.hpl.jena.datatypes.BaseDatatype;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
Expand Down Expand Up @@ -56,6 +58,7 @@
public class ValueConverter extends Converter<Value, RDFNode> {

private static final Logger LOGGER = getLogger(ValueConverter.class);
public static final String LITERAL_TYPE_SEP = "\30^^\30";

private final Session session;
private final Converter<Node, Resource> graphSubjects;
Expand Down Expand Up @@ -92,7 +95,7 @@ protected RDFNode doForward(final Value value) {
case PATH:
return traverseLink(value);
default:
return literal2node(value.getString());
return stringliteral2node(value.getString());
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
Expand Down Expand Up @@ -120,10 +123,10 @@ protected Value doBackward(final RDFNode resource) {
final RDFDatatype dataType = literal.getDatatype();
final Object rdfValue = literal.getValue();

if (dataType == null && rdfValue instanceof String) {
if (dataType == null && rdfValue instanceof String || (dataType != null && dataType.equals(XSDDatatype.XSDstring))) {
// short-circuit the common case
return valueFactory.createValue(literal.getString(), STRING);
} else if (rdfValue instanceof Boolean) {
} else if (rdfValue instanceof Boolean) {
return valueFactory.createValue((Boolean) rdfValue);
} else if (rdfValue instanceof Byte
|| (dataType != null && dataType.getJavaClass() == Byte.class)) {
Expand All @@ -147,7 +150,11 @@ protected Value doBackward(final RDFNode resource) {
return valueFactory.createValue(((XSDDateTime) rdfValue)
.asCalendar());
} else {
return valueFactory.createValue(literal.getString(), STRING);
if (dataType != null && !dataType.getURI().isEmpty()) {
return valueFactory.createValue(literal.getString() + LITERAL_TYPE_SEP + dataType.getURI(), STRING);
} else {
return valueFactory.createValue(literal.getString(), STRING);
}
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
Expand All @@ -160,6 +167,17 @@ private static Literal literal2node(final Object literal) {
return result;
}


private static Literal stringliteral2node(final String literal) {
int i = literal.indexOf(LITERAL_TYPE_SEP);

if (i < 0) {
return literal2node(literal);
} else {
return ResourceFactory.createTypedLiteral(literal.substring(0,i), new BaseDatatype(literal.substring(i+LITERAL_TYPE_SEP.length())));
}
}

private RDFNode traverseLink(final Value v)
throws RepositoryException {
final javax.jcr.Node refNode;
Expand Down

0 comments on commit 3170534

Please sign in to comment.