Skip to content

Commit 201d4f0

Browse files
author
James Leigh
authored
Merge pull request #890 from jamesrdf/issues/#889-pretty-print
Fix #889: Add INLINE_BLANK_NODE setting for TurtleWriter
2 parents 20f39fd + 661a508 commit 201d4f0

8 files changed

Lines changed: 966 additions & 133 deletions

File tree

compliance/rio/src/test/java/org/eclipse/rdf4j/rio/turtle/TurtlePrettyWriterTest.java

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,124 @@
77
*******************************************************************************/
88
package org.eclipse.rdf4j.rio.turtle;
99

10+
import java.io.IOException;
11+
12+
import org.eclipse.rdf4j.rio.RDFHandlerException;
13+
import org.eclipse.rdf4j.rio.RDFParseException;
1014
import org.eclipse.rdf4j.rio.RDFWriterTest;
1115
import org.eclipse.rdf4j.rio.WriterConfig;
1216
import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings;
13-
import org.eclipse.rdf4j.rio.turtle.TurtleParserFactory;
14-
import org.eclipse.rdf4j.rio.turtle.TurtleWriterFactory;
1517

1618
/**
1719
* @author Arjohn Kampman
1820
*/
1921
public class TurtlePrettyWriterTest extends RDFWriterTest {
2022

23+
private boolean inlineBlankNodes = true;
24+
2125
public TurtlePrettyWriterTest() {
2226
super(new TurtleWriterFactory(), new TurtleParserFactory());
2327
}
24-
28+
2529
@Override
2630
protected void setupWriterConfig(WriterConfig config) {
27-
config.set(BasicWriterSettings.PRETTY_PRINT, true);
31+
config.set(BasicWriterSettings.PRETTY_PRINT, true).set(BasicWriterSettings.INLINE_BLANK_NODES,
32+
inlineBlankNodes);
33+
}
34+
35+
@Override
36+
public void testPerformance()
37+
throws Exception
38+
{
39+
try {
40+
inlineBlankNodes = false;
41+
super.testPerformance();
42+
}
43+
finally {
44+
inlineBlankNodes = true;
45+
}
46+
}
47+
48+
@Override
49+
public void testPerformanceNoHandling()
50+
throws Exception
51+
{
52+
try {
53+
inlineBlankNodes = false;
54+
super.testPerformanceNoHandling();
55+
}
56+
finally {
57+
inlineBlankNodes = true;
58+
}
59+
}
60+
61+
@Override
62+
public void testWriteTwoStatementsObjectBNodeSinglePredicateSingleContextBNodeWithNamespace()
63+
throws Exception
64+
{
65+
try {
66+
inlineBlankNodes = false;
67+
super.testWriteTwoStatementsObjectBNodeSinglePredicateSingleContextBNodeWithNamespace();
68+
}
69+
finally {
70+
inlineBlankNodes = true;
71+
}
72+
}
73+
74+
@Override
75+
public void testWriteTwoStatementsObjectBNodeSinglePredicateSingleContextBNodeReusedWithNamespace()
76+
throws Exception
77+
{
78+
try {
79+
inlineBlankNodes = false;
80+
super.testWriteTwoStatementsObjectBNodeSinglePredicateSingleContextBNodeReusedWithNamespace();
81+
}
82+
finally {
83+
inlineBlankNodes = true;
84+
}
85+
}
86+
87+
@Override
88+
public void testRoundTripWithXSDString()
89+
throws RDFHandlerException,
90+
IOException,
91+
RDFParseException
92+
{
93+
try {
94+
inlineBlankNodes = false;
95+
super.testRoundTripWithXSDString();
96+
}
97+
finally {
98+
inlineBlankNodes = true;
99+
}
100+
}
101+
102+
@Override
103+
public void testRoundTripWithoutXSDString()
104+
throws RDFHandlerException,
105+
IOException,
106+
RDFParseException
107+
{
108+
try {
109+
inlineBlankNodes = false;
110+
super.testRoundTripWithoutXSDString();
111+
}
112+
finally {
113+
inlineBlankNodes = true;
114+
}
115+
}
116+
117+
@Override
118+
public void testRoundTripPreserveBNodeIds()
119+
throws Exception
120+
{
121+
try {
122+
inlineBlankNodes = false;
123+
super.testRoundTripPreserveBNodeIds();
124+
}
125+
finally {
126+
inlineBlankNodes = true;
127+
}
28128
}
29129

30130
}

core/repository/manager/src/main/java/org/eclipse/rdf4j/repository/manager/LocalRepositoryManager.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.InputStream;
1919
import java.io.OutputStream;
2020
import java.net.MalformedURLException;
21+
import java.net.URISyntaxException;
2122
import java.net.URL;
2223
import java.util.ArrayList;
2324
import java.util.Collections;
@@ -59,7 +60,11 @@
5960
import org.eclipse.rdf4j.repository.event.base.RepositoryConnectionListenerAdapter;
6061
import org.eclipse.rdf4j.repository.sparql.federation.SPARQLServiceResolver;
6162
import org.eclipse.rdf4j.rio.RDFFormat;
63+
import org.eclipse.rdf4j.rio.RDFHandlerException;
6264
import org.eclipse.rdf4j.rio.Rio;
65+
import org.eclipse.rdf4j.rio.UnsupportedRDFormatException;
66+
import org.eclipse.rdf4j.rio.WriterConfig;
67+
import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings;
6368

6469
/**
6570
* An implementation of the {@link RepositoryManager} interface that operates directly on the repository data
@@ -79,6 +84,10 @@ public class LocalRepositoryManager extends RepositoryManager {
7984

8085
private static final String CFG_FILE = "config." + CONFIG_FORMAT.getDefaultFileExtension();
8186

87+
private static final WriterConfig CFG_CONFIG = new WriterConfig().set(BasicWriterSettings.BASE_DIRECTIVE,
88+
false).set(BasicWriterSettings.PRETTY_PRINT, true).set(BasicWriterSettings.INLINE_BLANK_NODES,
89+
true);
90+
8291
/*-----------*
8392
* Variables *
8493
*-----------*/
@@ -424,9 +433,9 @@ private synchronized void addRepositoryConfig(RepositoryConfig config, boolean u
424433
config.export(model, SimpleValueFactory.getInstance().createIRI(ns, config.getID()));
425434
File part = new File(configFile.getParentFile(), configFile.getName() + ".part");
426435
try (OutputStream output = new FileOutputStream(part)) {
427-
Rio.write(model, output, CONFIG_FORMAT);
436+
Rio.write(model, output, configFile.toURI().toString(), CONFIG_FORMAT, CFG_CONFIG);
428437
}
429-
catch (IOException e) {
438+
catch (IOException | RDFHandlerException | UnsupportedRDFormatException | URISyntaxException e) {
430439
throw new RepositoryConfigException(e);
431440
}
432441
if (updateSystem) {

core/rio/api/src/main/java/org/eclipse/rdf4j/rio/helpers/BasicWriterSettings.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,27 @@ public class BasicWriterSettings {
1919
/**
2020
* Boolean setting for writer to determine whether pretty printing is preferred.
2121
* <p>
22-
* Defaults to false because it isn't always scalable.
22+
* Defaults to true
2323
*/
2424
public static final RioSetting<Boolean> PRETTY_PRINT = new RioSettingImpl<Boolean>(
25-
"org.eclipse.rdf4j.rio.prettyprint", "Pretty print", Boolean.FALSE);
25+
"org.eclipse.rdf4j.rio.prettyprint", "Pretty print", Boolean.TRUE);
26+
27+
/**
28+
* Inline blanks nodes by their value and don't write any blank node labels when this setting is true.
29+
* This setting should only be used when blank nodes never appear in the context and there are no blank
30+
* node cycles.
31+
* <p>
32+
* WARNING: This setting requires all triples to be processed before being written and could use a lot of
33+
* memory in the process and should be set to false for large RDF files.
34+
* <p>
35+
* Defaults to false.
36+
*
37+
* @since 2.3
38+
*/
39+
public static final RioSetting<Boolean> INLINE_BLANK_NODES = new RioSettingImpl<Boolean>(
40+
"org.eclipse.rdf4j.rio.inlineblanknodes",
41+
"Use blank node property lists, collections, and anonymous nodes instead of blank node labels",
42+
Boolean.FALSE);
2643

2744
/**
2845
* Boolean setting for writer to determine whether it should remove the xsd:string datatype from literals

0 commit comments

Comments
 (0)