Skip to content

Commit 8e57db7

Browse files
author
James Leigh
committed
issue #747: Don't encode literals in quotes unless needed
Signed-off-by: James Leigh <james.leigh@ontotext.com>
1 parent 38cfddf commit 8e57db7

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

compliance/queryresultio/src/test/java/org/eclipse/rdf4j/query/resultio/text/tsv/SPARQLTSVTupleBackgroundTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public void testSingleVarResults()
8181
assertRegex(
8282
"\\?a\n" + "<foo:bar>\n"
8383
+ "(2.0(E0)?|\"2.0\"\\^\\^<http://www.w3.org/2001/XMLSchema#double>)\n" + "_:bnode3\n"
84-
+ "\"''single-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
84+
+ "\"?''single-quoted string(\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\n"
8585
+ "\"\\\\\"\\\\\"double-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
86-
+ "\"space at the end \"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
87-
+ "\"space at the end \"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
86+
+ "\"?space at the end (\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\n"
87+
+ "\"?space at the end (\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\n"
8888
+ "\"\\\\\"\\\\\"double-quoted string with no datatype\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
8989
+ "\"newline at the end \\\\n\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n?",
9090
toString(createTupleSingleVarMultipleBindingSets()));
@@ -96,11 +96,11 @@ public void testmultipleVarResults()
9696
{
9797
assertRegex(
9898
"\\?a\t\\?b\t\\?c\n"
99-
+ "<foo:bar>\t_:bnode\t\"baz\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
99+
+ "<foo:bar>\t_:bnode\t(baz|\"baz\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)\n"
100100
+ "(1|\"1\"\\^\\^<http://www.w3.org/2001/XMLSchema#integer>)\t\t\"Hello World!\"@en\n"
101-
+ "<http://example.org/test/ns/bindingA>\t\"http://example.com/other/ns/bindingB\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t<http://example.com/other/ns/binding,C>\n"
102-
+ "\"string with newline at the end \\\\n\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"string with space at the end \"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\" \"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
103-
+ "\"''single-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"\\\\\"\\\\\"double-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"\\\\t\\\\tunencoded tab characters followed by encoded \\\\t\\\\t\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n?",
101+
+ "<http://example.org/test/ns/bindingA>\t\"?http://example.com/other/ns/bindingB(\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\t<http://example.com/other/ns/binding,C>\n"
102+
+ "\"string with newline at the end \\\\n\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"?string with space at the end (\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\t\"? (\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\n"
103+
+ "\"?''single-quoted string(\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\t\"\\\\\"\\\\\"double-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"\\\\t\\\\tunencoded tab characters followed by encoded \\\\t\\\\t\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n?",
104104
toString(createTupleMultipleBindingSets()));
105105
}
106106

compliance/queryresultio/src/test/java/org/eclipse/rdf4j/query/resultio/text/tsv/SPARQLTSVTupleTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public void testSingleVarResults()
7070
assertRegex(
7171
"\\?a\n" + "<foo:bar>\n"
7272
+ "(2.0(E0)?|\"2.0\"\\^\\^<http://www.w3.org/2001/XMLSchema#double>)\n" + "_:bnode3\n"
73-
+ "\"''single-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
73+
+ "\"?''single-quoted string(\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\n"
7474
+ "\"\\\\\"\\\\\"double-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
75-
+ "\"space at the end \"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
76-
+ "\"space at the end \"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
75+
+ "\"?space at the end (\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\n"
76+
+ "\"?space at the end (\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\n"
7777
+ "\"\\\\\"\\\\\"double-quoted string with no datatype\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
7878
+ "\"newline at the end \\\\n\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n?",
7979
toString(createTupleSingleVarMultipleBindingSets()));
@@ -85,11 +85,11 @@ public void testmultipleVarResults()
8585
{
8686
assertRegex(
8787
"\\?a\t\\?b\t\\?c\n"
88-
+ "<foo:bar>\t_:bnode\t\"baz\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
88+
+ "<foo:bar>\t_:bnode\t(baz|\"baz\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)\n"
8989
+ "(1|\"1\"\\^\\^<http://www.w3.org/2001/XMLSchema#integer>)\t\t\"Hello World!\"@en\n"
90-
+ "<http://example.org/test/ns/bindingA>\t\"http://example.com/other/ns/bindingB\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t<http://example.com/other/ns/binding,C>\n"
91-
+ "\"string with newline at the end \\\\n\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"string with space at the end \"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\" \"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n"
92-
+ "\"''single-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"\\\\\"\\\\\"double-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"\\\\t\\\\tunencoded tab characters followed by encoded \\\\t\\\\t\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n?",
90+
+ "<http://example.org/test/ns/bindingA>\t\"?http://example.com/other/ns/bindingB(\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\t<http://example.com/other/ns/binding,C>\n"
91+
+ "\"string with newline at the end \\\\n\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"?string with space at the end (\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\t\"? (\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\n"
92+
+ "\"?''single-quoted string(\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?)?\t\"\\\\\"\\\\\"double-quoted string\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\t\"\\\\t\\\\tunencoded tab characters followed by encoded \\\\t\\\\t\"(\\^\\^<http://www.w3.org/2001/XMLSchema#string>)?\n?",
9393
toString(createTupleMultipleBindingSets()));
9494
}
9595

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,36 @@ private void writeLiteral(Literal lit)
187187
}
188188
}
189189

190-
writer.write("\"");
190+
String encoded = encodeString(label);
191191

192-
writer.write(encodeString(label));
193-
194-
writer.write("\"");
195192

196193
if (Literals.isLanguageLiteral(lit)) {
194+
writer.write("\"");
195+
writer.write(encoded);
196+
writer.write("\"");
197197
// Append the literal's language
198198
writer.write("@");
199199
writer.write(lit.getLanguage().get());
200200
}
201201
else if (!XMLSchema.STRING.equals(datatype) || !xsdStringToPlainLiteral()) {
202+
writer.write("\"");
203+
writer.write(encoded);
204+
writer.write("\"");
202205
// Append the literal's datatype
203206
writer.write("^^");
204207
writeURI(datatype);
205208
}
209+
else if (encoded.equals(label) && label.charAt(0) != '<' && label.charAt(0) != '_'
210+
&& !label.matches("^[\\+\\-]?[\\d\\.].*"))
211+
{
212+
// no need to include double quotes
213+
writer.write(encoded);
214+
}
215+
else {
216+
writer.write("\"");
217+
writer.write(encoded);
218+
writer.write("\"");
219+
}
206220
}
207221

208222
private static String encodeString(String s) {

0 commit comments

Comments
 (0)