@SuppressWarnings({"unchecked","rawtypes"})
@Override
public Representation process(Representation source) {
Object context = backend.createURI(source.getId());
Representation result = appendMode ? source : vf.createRepresentation(source.getId());
/*
* NOTE: LDPath will return Node instances of the RDFRepositroy if no
* transformation is defined for a statement (line) in the configured
* LDpath program (the ":: xsd:int" at the end). this Nodes need to be
* converted to valid Entityhub Representation values.
* As we can not know the generic type used by the RDFRepository
* implementation of the indexing source this is a little bit tricky.
* What this does is:
* - for URIs it creates References
* - for plain literal it adds natural texts
* - for typed literals it uses the NodeTransformer registered with
* the LDPath (or more precise the Configuration object parsed to
* the LDPath in the constructor) to transform the values to
* Java objects. If no transformer is found or an Exeption occurs
* than the lexical form is used and added as String to the
* Entityhub.
*/
Map<String,Collection<Object>> resultMap = (Map<String,Collection<Object>>)program.execute(backend, context);
for(Entry<String,Collection<Object>> entry : resultMap.entrySet()){
NodeTransformer fieldTransformer = program.getField(entry.getKey()).getTransformer();
if(fieldTransformer == null || fieldTransformer instanceof IdentityTransformer<?>){
//we need to convert the RDFBackend Node to an Representation object
for(Object value : entry.getValue()){
if(backend.isURI(value)){
result.addReference(entry.getKey(), backend.stringValue(value));
} else if(backend.isLiteral(value)){ //literal
Locale locale = backend.getLiteralLanguage(value);
if(locale != null){ //text with language
String lang = locale.getLanguage();
result.addNaturalText(entry.getKey(), backend.stringValue(value),
lang.isEmpty() ? null : lang);
} else { // no language
URI type = backend.getLiteralType(value);
if(type != null){ //typed literal -> need to transform
NodeTransformer nt = transformer.get(type.toString());
if(nt != null){ //add typed literal
try {
result.add(entry.getKey(), nt.transform(backend, value));
} catch (RuntimeException e) {
log.info("Unable to transform {} to dataType {} -> will use lexical form",value,type);
result.add(entry.getKey(),backend.stringValue(value));
}
} else { //no transformer
log.info("No transformer for type {} -> will use lexical form",type);
result.add(entry.getKey(),backend.stringValue(value));
}
} else { //no langauge and no type -> literal with no language
result.addNaturalText(entry.getKey(), backend.stringValue(value));
}
}
} else { //bNode
log.info("Ignore bNode {} (class: {})",value,value.getClass());
}
} //end for all values
} else { //already a transformed values
result.add(entry.getKey(), entry.getValue()); //just add all values
}
}
return result;
}