Skip to content

Commit df42400

Browse files
committed
Fix #619
1 parent 80bde79 commit df42400

7 files changed

Lines changed: 105 additions & 8 deletions

File tree

sparql-anything-xml/src/main/java/io/github/sparqlanything/xml/XMLTriplifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public void transformSAX(Properties properties, FacadeXGraphBuilder builder) thr
319319
if (charBuilder == null) {
320320
charBuilder = new StringBuilder();
321321
}
322-
charBuilder.append(event.asCharacters().getData().trim());
322+
charBuilder.append(event.asCharacters().getData()); // .trim()); See #619
323323
}
324324
}
325325
}

sparql-anything-xml/src/test/java/io/github/sparqlanything/xml/Issue325Test.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ public void test() throws TriplifierHTTPException, IOException {
5252
DatasetGraph graph = builder.getDatasetGraph();
5353
logger.debug("{}", graph);
5454

55+
graph.find().forEachRemaining(q -> System.out.println(
56+
q.getPredicate() + " --> " +
57+
(q.getObject().isLiteral()
58+
? "\"" + q.getObject().getLiteralLexicalForm().replace("\n", "\\n") + "\"^^"
59+
+ q.getObject().getLiteralDatatypeURI()
60+
: q.getObject())
61+
));
62+
5563
Iterator<Quad> iter = graph.find(null, null, RDF.li(1).asNode(),
5664
NodeFactory.createLiteralString("THIS_TEXT_IS_INSIDE_SUBJECT"));
5765
Assert.assertTrue(iter.hasNext());
5866
Iterator<Quad> iter2 = graph.find(null, null, RDF.li(1).asNode(),
59-
NodeFactory.createLiteralString("THIS_TEXT_IS_OUTSIDE_SUBJECT"));
67+
NodeFactory.createLiteralString("\n THIS_TEXT_IS_OUTSIDE_SUBJECT"));
6068
Assert.assertTrue(iter2.hasNext());
6169
}
6270
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2026 SPARQL Anything Contributors @ http://github.com/sparql-anything
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
19+
package io.github.sparqlanything.xml;
20+
21+
import io.github.sparqlanything.model.BaseFacadeXGraphBuilder;
22+
import io.github.sparqlanything.model.FacadeXGraphBuilder;
23+
import io.github.sparqlanything.model.IRIArgument;
24+
import io.github.sparqlanything.model.TriplifierHTTPException;
25+
import org.apache.jena.graph.NodeFactory;
26+
import org.apache.jena.sparql.core.DatasetGraph;
27+
import org.apache.jena.sparql.core.Quad;
28+
import org.apache.jena.vocabulary.RDF;
29+
import org.junit.Assert;
30+
import org.junit.Test;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import java.io.IOException;
35+
import java.net.URL;
36+
import java.util.Iterator;
37+
import java.util.Objects;
38+
import java.util.Properties;
39+
40+
public class Issue619Test {
41+
42+
final static Logger logger = LoggerFactory.getLogger(Issue619Test.class);
43+
44+
@Test
45+
public void newlinesAndWhitespacePreservedWhenTrimStringsIsFalse()
46+
throws TriplifierHTTPException, IOException {
47+
Properties properties = new Properties();
48+
URL xml = getClass().getClassLoader().getResource("./Issue619.xml");
49+
properties.setProperty(IRIArgument.LOCATION.toString(),
50+
Objects.requireNonNull(xml).toString());
51+
properties.setProperty(IRIArgument.TRIM_STRINGS.toString(), "false");
52+
53+
FacadeXGraphBuilder builder = new BaseFacadeXGraphBuilder(properties);
54+
XMLTriplifier triplifier = new XMLTriplifier();
55+
triplifier.triplify(properties, builder);
56+
57+
DatasetGraph graph = builder.getDatasetGraph();
58+
logger.debug("{}", graph);
59+
60+
String expected = "Line 1\n Line 2";
61+
Iterator<Quad> it = graph.find(null, null, RDF.li(1).asNode(),
62+
NodeFactory.createLiteralString(expected));
63+
Assert.assertTrue(
64+
"Expected literal preserving the newline and indentation between 'Line 1' and 'Line 2'",
65+
it.hasNext());
66+
}
67+
}

sparql-anything-xml/src/test/java/io/github/sparqlanything/xml/MoreXMLTriplifierTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package io.github.sparqlanything.xml;
2020

2121
import io.github.sparqlanything.testutils.AbstractTriplifierTester;
22+
import org.apache.jena.riot.Lang;
23+
import org.apache.jena.riot.RDFDataMgr;
2224
import org.junit.Test;
2325
import org.slf4j.Logger;
2426
import org.slf4j.LoggerFactory;
@@ -69,7 +71,7 @@ protected void properties(Properties properties) {
6971
@Test
7072
public void testBooks$1() {
7173
L.debug("Test XML books (one go)");
72-
// RDFDataMgr.write(System.err, result, Lang.TTL);
74+
RDFDataMgr.write(System.err, result, Lang.TTL);
7375
assertResultIsIsomorphicWithExpected();
7476
}
7577

sparql-anything-xml/src/test/resources/Books.ttl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
xyz:isbn "928-3-16-148410-0" .
4545

4646
<http://www.example.org/document/books/2:book/6:description> a xyz:description ;
47-
rdf:_1 "A former architect battles corporate zombies,\n an evil sorceress, and her own childhood to become queenof the world." .
47+
rdf:_1 "A former architect battles corporate zombies,\n an evil sorceress, and her own childhood to become queen\n of the world." .
4848

4949
<http://www.example.org/document/books/3:book/3:genre> a xyz:genre ;
5050
rdf:_1 "Fantasy" .
@@ -89,4 +89,4 @@
8989
rdf:_1 "Maeve Ascendant" .
9090

9191
<http://www.example.org/document/books/3:book/6:description> a xyz:description ;
92-
rdf:_1 "After the collapse of a nanotechnology\n society in England, the young survivors lay thefoundation for a new society." .
92+
rdf:_1 "After the collapse of a nanotechnology\n society in England, the young survivors lay the\n foundation for a new society." .
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
~ Copyright (c) 2026 SPARQL Anything Contributors @ http://github.com/sparql-anything
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<root>
18+
<description>Line 1
19+
Line 2</description>
20+
</root>

sparql-anything-xml/src/test/resources/test2.ttl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[ a <http://sparql.xyz/facade-x/ns/root> , <http://sparql.xyz/facade-x/data/test>;
22
<http://www.w3.org/1999/02/22-rdf-syntax-ns#_1>
3-
"Floating string before (first)";
3+
"\n Floating string before (first)\n ";
44
<http://www.w3.org/1999/02/22-rdf-syntax-ns#_2>
55
[ a <http://sparql.xyz/facade-x/data/second>;
66
<http://www.w3.org/1999/02/22-rdf-syntax-ns#_1>
@@ -14,5 +14,5 @@
1414
"Value in third"
1515
];
1616
<http://www.w3.org/1999/02/22-rdf-syntax-ns#_4>
17-
"Floating string after (fourth)"
18-
] .
17+
"\n Floating string after (fourth)\n"
18+
] .

0 commit comments

Comments
 (0)