Skip to content

Commit

Permalink
fix: handling of literals with datatype and language suffix. (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertWeichselbraun authored Jan 12, 2019
1 parent 831f500 commit ae51cba
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ hs_err_pid*

# vim
.*.swp

.checkstyle
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.weblyzard.sparql</groupId>
<artifactId>streaming-sparql</artifactId>
<version>0.0.6-SNAPSHOT</version>
<version>0.0.7-SNAPSHOT</version>
<packaging>jar</packaging>

<name>com.weblyzard.sparql</name>
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/weblyzard/sparql/tsv/TsvParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,21 @@ private enum State {
StringBuilder s = new StringBuilder("\"");
t.pop();
char ch;
// consume literal value
while (true) {
ch = t.pop();
if (ch == '"') {
if (t.popIfAvailable() == '"') {
if (t.getIfAvailable() == '"') {
t.pop();
s.append("\\\"");
continue;
}
s.append("\"");
s.append(ch);
break;
}
s.append(ch);
}
s.append(t.popTo('\t'));
parseNode(s.toString())
.ifPresent(node -> t.currentTuple.put(t.tsvHeader[t.currentTupleIdx++], node));
t.currentConsumer = consumers.get(State.START);
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/weblyzard/sparql/TsvParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
Expand Down Expand Up @@ -89,6 +91,38 @@ public void quotesInLiteralTest() throws IOException {
result.get("s").getLiteralValue());
}

@Test
public void quotesInLiteralWithLangTest() throws IOException {
Map<String, Node> result;
result = parseSingleTuple(String.format("?s\t?p\t?o\n" + "\t\t\"%s\"@en\n", QUOTED_LITERAL));
assertEquals("\"Bruder Klaus\" ist der \"Schutzpatron\" der Schweiz.",
result.get("o").getLiteralValue());
assertNull(result.get("s"));
assertNull(result.get("p"));
assertEquals("en", result.get("o").getLiteralLanguage());

result = parseSingleTuple(String.format("?s\t?p\t?o\n" + "\t\"%s\"@en\t\n", QUOTED_LITERAL));
assertEquals("\"Bruder Klaus\" ist der \"Schutzpatron\" der Schweiz.",
result.get("p").getLiteralValue());
assertNull(result.get("s"));
assertNull(result.get("o"));
assertEquals("en", result.get("p").getLiteralLanguage());

result = parseSingleTuple(String.format("?s\t?p\t?o\n" + "\"%s\"@en\t\t\n", QUOTED_LITERAL));
assertEquals("\"Bruder Klaus\" ist der \"Schutzpatron\" der Schweiz.",
result.get("s").getLiteralValue());
assertNull(result.get("p"));
assertNull(result.get("o"));
assertEquals("en", result.get("s").getLiteralLanguage());
}

@Test
public void testNodeParser() {
Node parseNode = NodeFactoryExtra.parseNode("\"Hallo \\\"Echo\\\"!\"@de");
assertEquals("de", parseNode.getLiteralLanguage());
assertEquals("Hallo \"Echo\"!", parseNode.getLiteralValue());
}

/**
* Performs a query and returns the first result tuple.
*/
Expand Down

0 comments on commit ae51cba

Please sign in to comment.