Search in sources :

Example 1 with RdfValue

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");
    }
}
Also used : Matcher(java.util.regex.Matcher) RdfUri(nl.knaw.huygens.timbuctoo.rml.dto.RdfUri) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RdfValue(nl.knaw.huygens.timbuctoo.rml.dto.RdfValue) RdfBlankNode(nl.knaw.huygens.timbuctoo.rml.dto.RdfBlankNode)

Example 2 with RdfValue

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"))));
}
Also used : TestRow(nl.knaw.huygens.timbuctoo.rml.TestRow) ThrowingErrorHandler(nl.knaw.huygens.timbuctoo.rml.ThrowingErrorHandler) RdfValue(nl.knaw.huygens.timbuctoo.rml.dto.RdfValue) QuadPart(nl.knaw.huygens.timbuctoo.rml.dto.QuadPart) Test(org.junit.Test)

Aggregations

RdfValue (nl.knaw.huygens.timbuctoo.rml.dto.RdfValue)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Matcher (java.util.regex.Matcher)1 TestRow (nl.knaw.huygens.timbuctoo.rml.TestRow)1 ThrowingErrorHandler (nl.knaw.huygens.timbuctoo.rml.ThrowingErrorHandler)1 QuadPart (nl.knaw.huygens.timbuctoo.rml.dto.QuadPart)1 RdfBlankNode (nl.knaw.huygens.timbuctoo.rml.dto.RdfBlankNode)1 RdfUri (nl.knaw.huygens.timbuctoo.rml.dto.RdfUri)1 Test (org.junit.Test)1