Skip to content

Commit 3e89439

Browse files
committed
Fix SPARQLResultsTSVWriter to quote xsd:string literals
Related to #5256 Update `SPARQLResultsTSVWriter` to ensure all `xsd:string` literals are quoted in SPARQL TSV results. * Modify `writeLiteral` method to always quote `xsd:string` literals. * Remove conditions that allowed `xsd:string` literals to be written without quotes. * Ensure special characters in `xsd:string` literals are properly escaped using `encodeString` method. * Update tests in `SPARQLTSVCustomTest` to verify correct quoting of `xsd:string` literals and proper escaping of special characters. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/eclipse-rdf4j/rdf4j/issues/5256?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent cd6509b commit 3e89439

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

core/queryresultio/text/src/main/java/org/eclipse/rdf4j/query/resultio/text/tsv/SPARQLResultsTSVWriter.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,15 @@ private void writeLiteral(Literal lit) throws IOException {
194194
// Append the literal's language
195195
writer.write("@");
196196
writer.write(lit.getLanguage().get());
197-
} else if (!XSD.STRING.equals(datatype) || !xsdStringToPlainLiteral()) {
198-
writer.write("\"");
199-
writer.write(encoded);
200-
writer.write("\"");
201-
// Append the literal's datatype
202-
writer.write("^^");
203-
writeURI(datatype);
204-
} else if (!label.isEmpty() && encoded.equals(label) && label.charAt(0) != '<' && label.charAt(0) != '_'
205-
&& !label.matches("^[\\+\\-]?[\\d\\.].*")) {
206-
// no need to include double quotes
207-
writer.write(encoded);
208197
} else {
209198
writer.write("\"");
210199
writer.write(encoded);
211200
writer.write("\"");
201+
// Append the literal's datatype if it's not xsd:string or if xsdStringToPlainLiteral is false
202+
if (!XSD.STRING.equals(datatype) || !xsdStringToPlainLiteral()) {
203+
writer.write("^^");
204+
writeURI(datatype);
205+
}
212206
}
213207
}
214208

core/queryresultio/text/src/test/java/org/eclipse/rdf4j/query/resultio/text/tsv/SPARQLTSVCustomTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ public void testSES2126QuotedLiteralIntegerAsStringImplicitType() throws Excepti
6969
assertEquals("?test\n\"1\"\n", result);
7070
}
7171

72+
@Test
73+
public void testQuotedXSDStringLiteral() throws Exception {
74+
List<String> bindingNames = List.of("test");
75+
TupleQueryResult tqr = new IteratingTupleQueryResult(bindingNames,
76+
List.of(new ListBindingSet(bindingNames, SimpleValueFactory.getInstance().createLiteral("example", XSD.STRING))));
77+
String result = writeTupleResult(tqr);
78+
assertEquals("?test\n\"example\"\n", result);
79+
}
80+
81+
@Test
82+
public void testQuotedXSDStringLiteralWithSpecialCharacters() throws Exception {
83+
List<String> bindingNames = List.of("test");
84+
TupleQueryResult tqr = new IteratingTupleQueryResult(bindingNames,
85+
List.of(new ListBindingSet(bindingNames, SimpleValueFactory.getInstance().createLiteral("example\twith\nspecial\"characters", XSD.STRING))));
86+
String result = writeTupleResult(tqr);
87+
assertEquals("?test\n\"example\\twith\\nspecial\\\"characters\"\n", result);
88+
}
89+
7290
private String writeTupleResult(TupleQueryResult tqr)
7391
throws IOException, TupleQueryResultHandlerException, QueryEvaluationException {
7492
ByteArrayOutputStream output = new ByteArrayOutputStream();

0 commit comments

Comments
 (0)