Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SPARQLResultsTSVWriter to quote xsd:string literals #5262

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,15 @@ private void writeLiteral(Literal lit) throws IOException {
// Append the literal's language
writer.write("@");
writer.write(lit.getLanguage().get());
} else if (!XSD.STRING.equals(datatype) || !xsdStringToPlainLiteral()) {
writer.write("\"");
writer.write(encoded);
writer.write("\"");
// Append the literal's datatype
writer.write("^^");
writeURI(datatype);
} else if (!label.isEmpty() && encoded.equals(label) && label.charAt(0) != '<' && label.charAt(0) != '_'
&& !label.matches("^[\\+\\-]?[\\d\\.].*")) {
// no need to include double quotes
writer.write(encoded);
} else {
writer.write("\"");
writer.write(encoded);
writer.write("\"");
// Append the literal's datatype if it's not xsd:string or if xsdStringToPlainLiteral is false
if (!XSD.STRING.equals(datatype) || !xsdStringToPlainLiteral()) {
writer.write("^^");
writeURI(datatype);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ public void testSES2126QuotedLiteralIntegerAsStringImplicitType() throws Excepti
assertEquals("?test\n\"1\"\n", result);
}

@Test
public void testQuotedXSDStringLiteral() throws Exception {
List<String> bindingNames = List.of("test");
TupleQueryResult tqr = new IteratingTupleQueryResult(bindingNames,
List.of(new ListBindingSet(bindingNames, SimpleValueFactory.getInstance().createLiteral("example", XSD.STRING))));
String result = writeTupleResult(tqr);
assertEquals("?test\n\"example\"\n", result);
}

@Test
public void testQuotedXSDStringLiteralWithSpecialCharacters() throws Exception {
List<String> bindingNames = List.of("test");
TupleQueryResult tqr = new IteratingTupleQueryResult(bindingNames,
List.of(new ListBindingSet(bindingNames, SimpleValueFactory.getInstance().createLiteral("example\twith\nspecial\"characters", XSD.STRING))));
String result = writeTupleResult(tqr);
assertEquals("?test\n\"example\\twith\\nspecial\\\"characters\"\n", result);
}

private String writeTupleResult(TupleQueryResult tqr)
throws IOException, TupleQueryResultHandlerException, QueryEvaluationException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
Expand Down
Loading