Skip to content

Commit

Permalink
Support rdf language types
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 23, 2014
1 parent 762f049 commit 6ba9d5d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
Expand Up @@ -261,7 +261,7 @@ public void addProperty(final FedoraResource resource,
}

final String propertyName =
getPropertyNameFromPredicate(node, predicate, namespaces);
getPropertyNameFromPredicate(node, predicate, value, namespaces);
final Value v = createValue(node, value, propertyName);
nodePropertiesTools.appendOrReplaceNodeProperty(idTranslator, node, propertyName, v);
}
Expand Down Expand Up @@ -303,7 +303,7 @@ public void removeProperty(final FedoraResource resource,
final Map<String, String> nsPrefixMap) throws RepositoryException {

final Node node = resource.getNode();
final String propertyName = getPropertyNameFromPredicate(node, predicate, nsPrefixMap);
final String propertyName = getPropertyNameFromPredicate(node, predicate, objectNode, nsPrefixMap);

if (isManagedPredicate.apply(predicate)) {

Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Converter;
import com.google.common.collect.ImmutableBiMap;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import org.modeshape.jcr.api.NamespaceRegistry;
import org.modeshape.jcr.api.Namespaced;
Expand Down Expand Up @@ -60,6 +61,8 @@ protected Property doForward(final javax.jcr.Property property) {

if (isReferenceProperty.apply(property)) {
rdfLocalName = getReferencePropertyOriginalName(localName);
} else if (localName.contains("@")) {
rdfLocalName = localName.substring(0, localName.indexOf("@"));
} else {
rdfLocalName = localName;
}
Expand All @@ -79,6 +82,24 @@ protected javax.jcr.Property doBackward(final Property property) {
throw new UnsupportedOperationException();
}

/**
* Given an RDF predicate value (namespace URI + local name), figure out
* what JCR property to use
*
* @param node the JCR node we want a property for
* @param predicate the predicate to map to a property name
* @param namespaceMapping prefix to uri namespace mapping
* @return the JCR property name
* @throws RepositoryException
*/
public static String getPropertyNameFromPredicate(final Node node,
final Resource predicate,
final Map<String,String> namespaceMapping)
throws RepositoryException {
return getPropertyNameFromPredicate(node, predicate, null, namespaceMapping);
}


/**
* Given an RDF predicate value (namespace URI + local name), figure out
* what JCR property to use
Expand All @@ -91,10 +112,12 @@ protected javax.jcr.Property doBackward(final Property property) {
*/
public static String getPropertyNameFromPredicate(final Node node,
final Resource predicate,
final RDFNode object,
final Map<String,String> namespaceMapping) throws RepositoryException {
final NamespaceRegistry namespaceRegistry = getNamespaceRegistry.apply(node);
return getPropertyNameFromPredicate(namespaceRegistry,
predicate,
object,
namespaceMapping);
}

Expand All @@ -109,6 +132,7 @@ public static String getPropertyNameFromPredicate(final Node node,
*/
public static String getPropertyNameFromPredicate(final NamespaceRegistry namespaceRegistry,
final Resource predicate,
final RDFNode object,
final Map<String, String> namespaceMapping)
throws RepositoryException {

Expand Down Expand Up @@ -138,7 +162,19 @@ public static String getPropertyNameFromPredicate(final NamespaceRegistry namesp
}
}

final String propertyName = prefix + ":" + rdfLocalname;
final StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append(prefix);
stringBuilder.append(":");
stringBuilder.append(rdfLocalname);


if (object != null && object.isLiteral() && !object.asLiteral().getLanguage().isEmpty()) {
stringBuilder.append("@");
stringBuilder.append(object.asLiteral().getLanguage());
}

final String propertyName = stringBuilder.toString();

LOGGER.debug("Took RDF predicate {} and translated it to JCR property {}", namespace, propertyName);

Expand Down
Expand Up @@ -31,6 +31,9 @@
import com.google.common.base.Converter;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.impl.LiteralLabel;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
Expand Down Expand Up @@ -122,10 +125,29 @@ public Triple apply(final Value v) {
private Triple propertyvalue2triple(final Property p, final Value v) {
LOGGER.trace("Rendering triple for Property: {} with Value: {}", p, v);
try {
final Triple triple =
create(graphSubjects.convert(p.getParent()).asNode(),
propertyConverter.convert(p).asNode(),
valueConverter.convert(v).asNode());
com.hp.hpl.jena.graph.Node object = valueConverter.convert(v).asNode();

final Triple triple;
if (object.isLiteral()) {
final String propertyName = p.getName();
final int i = propertyName.indexOf("@");

if (i > 0) {
final LiteralLabel literal = object.getLiteral();
final String datatypeURI = literal.getDatatypeURI();

if (datatypeURI.isEmpty() || datatypeURI.equals(XSDDatatype.XSDstring.getURI())) {

final String lang = propertyName.substring(i);
object = NodeFactory.createLiteral(literal.getLexicalForm(), lang, literal.getDatatype());
}
}
}

triple = create(graphSubjects.convert(p.getParent()).asNode(),
propertyConverter.convert(p).asNode(),
object);

LOGGER.trace("Created triple: {} ", triple);
return triple;
} catch (final RepositoryException e) {
Expand Down
Expand Up @@ -90,7 +90,6 @@ public final void shouldMapInternalReferencePropertiesToPublicUris() throws Repo

@Test
public void testGetPredicateForProperty() throws RepositoryException {
testObj.convert(mockNamespacedProperty);
when(mockNamespacedProperty.getNamespaceURI()).thenThrow(new RepositoryException());
try {
testObj.convert(mockNamespacedProperty);
Expand Down

0 comments on commit 6ba9d5d

Please sign in to comment.