use of nl.knaw.huygens.timbuctoo.rml.dto.RdfValue in project timbuctoo by HuygensING.
the class RrTemplate method generateValue.
@Override
public Optional<QuadPart> generateValue(Row input) {
Matcher regexMatcher = pattern.matcher(template);
StringBuffer resultString = new StringBuffer();
while (regexMatcher.find()) {
String value = input.getRawValue(regexMatcher.group(1));
if (value == null) {
return Optional.empty();
} else {
if (termType == TermType.IRI) {
try {
value = URLEncoder.encode(value, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
LOG.error("Java is being really stoopid and throws an unsupportedEncodingException for UTF-8.");
throw new RuntimeException(e);
}
}
regexMatcher.appendReplacement(resultString, value);
}
}
if (StringUtils.isBlank(resultString.toString())) {
return Optional.empty();
}
regexMatcher.appendTail(resultString);
switch(termType) {
case IRI:
return Optional.of(new RdfUri(resultString.toString()));
case BlankNode:
return Optional.of(new RdfBlankNode(resultString.toString()));
case Literal:
return Optional.of(new RdfValue(resultString.toString(), dataType));
default:
throw new UnsupportedOperationException("Not all items in the Enumerable where handled");
}
}
use of nl.knaw.huygens.timbuctoo.rml.dto.RdfValue in project timbuctoo by HuygensING.
the class RrTemplateTest method replacesTemplatesWithEmptyStringIfValueIsEmpty.
@Test
public void replacesTemplatesWithEmptyStringIfValueIsEmpty() throws Exception {
TestRow row = new TestRow(ImmutableMap.of("foo", ""), ImmutableMap.of(), new ThrowingErrorHandler());
RrTemplate rrTemplate = new RrTemplate("start {foo} end", TermType.Literal, XSD_STRING);
Optional<QuadPart> actual = rrTemplate.generateValue(row);
assertThat(actual, is(Optional.of(new RdfValue("start end", "http://www.w3.org/2001/XMLSchema#string"))));
}
Aggregations