Skip to content

Commit 76bb516

Browse files
author
James Leigh
committed
Fix #876: Add BASE_DIRECTIVE writer config and baseURI parameter to writer interface
Signed-off-by: James Leigh <james.leigh@ontotext.com>
1 parent 36cb449 commit 76bb516

20 files changed

Lines changed: 535 additions & 11 deletions

File tree

core/rio/api/src/main/java/org/eclipse/rdf4j/rio/RDFWriterFactory.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.OutputStream;
1111
import java.io.Writer;
12+
import java.net.URISyntaxException;
1213

1314
/**
1415
* A RDFWriterFactory returns {@link RDFWriter}s for a specific RDF format.
@@ -30,6 +31,19 @@ public interface RDFWriterFactory {
3031
*/
3132
public RDFWriter getWriter(OutputStream out);
3233

34+
/**
35+
* Returns an RDFWriter instance that will write to the supplied output stream. Using the supplied baseURI
36+
* to relativize IRIs to relative IRIs.
37+
*
38+
* @param out
39+
* The OutputStream to write the RDF to.
40+
* @param baseURI
41+
* The URI associated with the data in the InputStream.
42+
* @throws URISyntaxException
43+
*/
44+
public RDFWriter getWriter(OutputStream out, String baseURI)
45+
throws URISyntaxException;
46+
3347
/**
3448
* Returns an RDFWriter instance that will write to the supplied writer. (Optional operation)
3549
*
@@ -39,4 +53,19 @@ public interface RDFWriterFactory {
3953
* if the RDFWriter the specific format does not support writing to a {@link java.io.Writer}
4054
*/
4155
public RDFWriter getWriter(Writer writer);
56+
57+
/**
58+
* Returns an RDFWriter instance that will write to the supplied writer. Using the supplied baseURI to
59+
* relativize IRIs to relative IRIs. (Optional operation)
60+
*
61+
* @param writer
62+
* The Writer to write the RDF to.
63+
* @param baseURI
64+
* The URI associated with the data in the InputStream.
65+
* @throws URISyntaxException
66+
* @throws UnsupportedOperationException
67+
* if the RDFWriter the specific format does not support writing to a {@link java.io.Writer}
68+
*/
69+
public RDFWriter getWriter(Writer writer, String baseURI)
70+
throws URISyntaxException;
4271
}

core/rio/api/src/main/java/org/eclipse/rdf4j/rio/Rio.java

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.OutputStream;
1515
import java.io.Reader;
1616
import java.io.Writer;
17+
import java.net.URISyntaxException;
1718
import java.util.Optional;
1819
import java.util.function.Supplier;
1920

@@ -137,6 +138,26 @@ public static RDFWriter createWriter(RDFFormat format, OutputStream out)
137138
return factory.getWriter(out);
138139
}
139140

141+
/**
142+
* Convenience methods for creating RDFWriter objects. This method uses the registry returned by
143+
* {@link RDFWriterRegistry#getInstance()} to get a factory for the specified format and uses this factory
144+
* to create the appropriate writer.
145+
*
146+
* @throws UnsupportedRDFormatException
147+
* If no writer is available for the specified RDF format.
148+
* @throws URISyntaxException
149+
* If the baseURI is invalid
150+
*/
151+
public static RDFWriter createWriter(RDFFormat format, OutputStream out, String baseURI)
152+
throws UnsupportedRDFormatException,
153+
URISyntaxException
154+
{
155+
RDFWriterFactory factory = RDFWriterRegistry.getInstance().get(format).orElseThrow(
156+
Rio.unsupportedFormat(format));
157+
158+
return factory.getWriter(out, baseURI);
159+
}
160+
140161
/**
141162
* Convenience methods for creating RDFWriter objects. This method uses the registry returned by
142163
* {@link RDFWriterRegistry#getInstance()} to get a factory for the specified format and uses this factory
@@ -154,6 +175,26 @@ public static RDFWriter createWriter(RDFFormat format, Writer writer)
154175
return factory.getWriter(writer);
155176
}
156177

178+
/**
179+
* Convenience methods for creating RDFWriter objects. This method uses the registry returned by
180+
* {@link RDFWriterRegistry#getInstance()} to get a factory for the specified format and uses this factory
181+
* to create the appropriate writer.
182+
*
183+
* @throws UnsupportedRDFormatException
184+
* If no writer is available for the specified RDF format.
185+
* @throws URISyntaxException
186+
* If the baseURI is invalid
187+
*/
188+
public static RDFWriter createWriter(RDFFormat format, Writer writer, String baseURI)
189+
throws UnsupportedRDFormatException,
190+
URISyntaxException
191+
{
192+
RDFWriterFactory factory = RDFWriterRegistry.getInstance().get(format).orElseThrow(
193+
Rio.unsupportedFormat(format));
194+
195+
return factory.getWriter(writer, baseURI);
196+
}
197+
157198
/**
158199
* Adds RDF data from an {@link InputStream} to a {@link Model}, optionally to one or more named contexts.
159200
*
@@ -324,6 +365,33 @@ public static void write(Iterable<Statement> model, OutputStream output, RDFForm
324365
write(model, output, dataFormat, new WriterConfig());
325366
}
326367

368+
/**
369+
* Writes the given statements to the given {@link OutputStream} in the given format.
370+
* <p>
371+
* If the collection is a {@link Model}, its namespaces will also be written.
372+
*
373+
* @param model
374+
* A collection of statements, such as a {@link Model}, to be written.
375+
* @param output
376+
* The {@link OutputStream} to write the statements to.
377+
* @param baseURI
378+
* The base URI to relativize IRIs against.
379+
* @param dataFormat
380+
* The {@link RDFFormat} to use when writing the statements.
381+
* @throws RDFHandlerException
382+
* Thrown if there is an error writing the statements.
383+
* @throws URISyntaxException
384+
* If the baseURI is invalid
385+
* @throws UnsupportedRDFormatException
386+
* If no {@link RDFWriter} is available for the specified RDF format.
387+
*/
388+
public static void write(Iterable<Statement> model, OutputStream output, String baseURI,
389+
RDFFormat dataFormat)
390+
throws RDFHandlerException, UnsupportedRDFormatException, URISyntaxException
391+
{
392+
write(model, output, baseURI, dataFormat, new WriterConfig());
393+
}
394+
327395
/**
328396
* Writes the given statements to the given {@link Writer} in the given format.
329397
* <p>
@@ -346,6 +414,32 @@ public static void write(Iterable<Statement> model, Writer output, RDFFormat dat
346414
write(model, output, dataFormat, new WriterConfig());
347415
}
348416

417+
/**
418+
* Writes the given statements to the given {@link Writer} in the given format.
419+
* <p>
420+
* If the collection is a {@link Model}, its namespaces will also be written.
421+
*
422+
* @param model
423+
* A collection of statements, such as a {@link Model}, to be written.
424+
* @param output
425+
* The {@link Writer} to write the statements to.
426+
* @param baseURI
427+
* The base URI to relativize IRIs against.
428+
* @param dataFormat
429+
* The {@link RDFFormat} to use when writing the statements.
430+
* @throws RDFHandlerException
431+
* Thrown if there is an error writing the statements.
432+
* @throws URISyntaxException
433+
* If the baseURI is invalid
434+
* @throws UnsupportedRDFormatException
435+
* If no {@link RDFWriter} is available for the specified RDF format.
436+
*/
437+
public static void write(Iterable<Statement> model, Writer output, String baseURI, RDFFormat dataFormat)
438+
throws RDFHandlerException, UnsupportedRDFormatException, URISyntaxException
439+
{
440+
write(model, output, baseURI, dataFormat, new WriterConfig());
441+
}
442+
349443
/**
350444
* Writes the given statements to the given {@link OutputStream} in the given format.
351445
* <p>
@@ -373,6 +467,37 @@ public static void write(Iterable<Statement> model, OutputStream output, RDFForm
373467
write(model, writer);
374468
}
375469

470+
/**
471+
* Writes the given statements to the given {@link OutputStream} in the given format.
472+
* <p>
473+
* If the collection is a {@link Model}, its namespaces will also be written.
474+
*
475+
* @param model
476+
* A collection of statements, such as a {@link Model}, to be written.
477+
* @param output
478+
* The {@link OutputStream} to write the statements to.
479+
* @param baseURI
480+
* The base URI to relativize IRIs against.
481+
* @param dataFormat
482+
* The {@link RDFFormat} to use when writing the statements.
483+
* @param settings
484+
* The {@link WriterConfig} containing settings for configuring the writer.
485+
* @throws RDFHandlerException
486+
* Thrown if there is an error writing the statements.
487+
* @throws URISyntaxException
488+
* If the baseURI is invalid
489+
* @throws UnsupportedRDFormatException
490+
* If no {@link RDFWriter} is available for the specified RDF format.
491+
*/
492+
public static void write(Iterable<Statement> model, OutputStream output, String baseURI,
493+
RDFFormat dataFormat, WriterConfig settings)
494+
throws RDFHandlerException, UnsupportedRDFormatException, URISyntaxException
495+
{
496+
final RDFWriter writer = Rio.createWriter(dataFormat, output, baseURI);
497+
writer.setWriterConfig(settings);
498+
write(model, writer);
499+
}
500+
376501
/**
377502
* Writes the given statements to the given {@link Writer} in the given format.
378503
* <p>
@@ -400,6 +525,37 @@ public static void write(Iterable<Statement> model, Writer output, RDFFormat dat
400525
write(model, writer);
401526
}
402527

528+
/**
529+
* Writes the given statements to the given {@link Writer} in the given format.
530+
* <p>
531+
* If the collection is a {@link Model}, its namespaces will also be written.
532+
*
533+
* @param model
534+
* A collection of statements, such as a {@link Model}, to be written.
535+
* @param output
536+
* The {@link Writer} to write the statements to.
537+
* @param baseURI
538+
* The base URI to relativize IRIs against.
539+
* @param dataFormat
540+
* The {@link RDFFormat} to use when writing the statements.
541+
* @param settings
542+
* The {@link WriterConfig} containing settings for configuring the writer.
543+
* @throws RDFHandlerException
544+
* Thrown if there is an error writing the statements.
545+
* @throws URISyntaxException
546+
* If the baseURI is invalid
547+
* @throws UnsupportedRDFormatException
548+
* If no {@link RDFWriter} is available for the specified RDF format.
549+
*/
550+
public static void write(Iterable<Statement> model, Writer output, String baseURI, RDFFormat dataFormat,
551+
WriterConfig settings)
552+
throws RDFHandlerException, UnsupportedRDFormatException, URISyntaxException
553+
{
554+
final RDFWriter writer = Rio.createWriter(dataFormat, output, baseURI);
555+
writer.setWriterConfig(settings);
556+
write(model, writer);
557+
}
558+
403559
/**
404560
* Writes the given statements to the given {@link RDFHandler}.
405561
* <p>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public class BasicWriterSettings {
5656
"org.eclipse.rdf4j.rio.rdf10languageliterals", "RDF-1.0 compatible Language Literals",
5757
Boolean.TRUE);
5858

59+
/**
60+
* Boolean setting for writer to determine whether it should include a base directive.
61+
* <p>
62+
* Defaults to true
63+
*/
64+
public static final RioSetting<Boolean> BASE_DIRECTIVE = new RioSettingImpl<Boolean>(
65+
"org.eclipse.rdf4j.rio.basedirective", "Serialize base directive", Boolean.TRUE);
66+
5967
/**
6068
* Private default constructor.
6169
*/

core/rio/binary/src/main/java/org/eclipse/rdf4j/rio/binary/BinaryRDFWriterFactory.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,21 @@ public RDFWriter getWriter(OutputStream out) {
3535
return new BinaryRDFWriter(out);
3636
}
3737

38+
public RDFWriter getWriter(OutputStream out, String baseURI) {
39+
return new BinaryRDFWriter(out);
40+
}
41+
3842
/**
39-
* Returns a new instance of {@link BinaryRDFWriter}.
43+
* throws UnsupportedOperationException
4044
*/
4145
public RDFWriter getWriter(Writer writer) {
4246
throw new UnsupportedOperationException();
4347
}
48+
49+
/**
50+
* throws UnsupportedOperationException
51+
*/
52+
public RDFWriter getWriter(Writer writer, String baseURI) {
53+
throw new UnsupportedOperationException();
54+
}
4455
}

core/rio/jsonld/src/main/java/org/eclipse/rdf4j/rio/jsonld/JSONLDWriter.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class JSONLDWriter extends AbstractRDFWriter implements RDFWriter {
4949

5050
private final StatementCollector statementCollector = new StatementCollector(model);
5151

52+
private final String baseURI;
53+
5254
private final Writer writer;
5355

5456
/**
@@ -58,7 +60,17 @@ public class JSONLDWriter extends AbstractRDFWriter implements RDFWriter {
5860
* The OutputStream to write to.
5961
*/
6062
public JSONLDWriter(OutputStream outputStream) {
61-
this(new BufferedWriter(new OutputStreamWriter(outputStream, Charset.forName("UTF-8"))));
63+
this(outputStream, null);
64+
}
65+
66+
/**
67+
* Create a SesameJSONLDWriter using a {@link java.io.OutputStream}
68+
*
69+
* @param outputStream
70+
* The OutputStream to write to.
71+
*/
72+
public JSONLDWriter(OutputStream outputStream, String baseURI) {
73+
this(new BufferedWriter(new OutputStreamWriter(outputStream, Charset.forName("UTF-8"))), baseURI);
6274
}
6375

6476
/**
@@ -68,6 +80,17 @@ public JSONLDWriter(OutputStream outputStream) {
6880
* The Writer to write to.
6981
*/
7082
public JSONLDWriter(Writer writer) {
83+
this(writer, null);
84+
}
85+
86+
/**
87+
* Create a SesameJSONLDWriter using a {@link java.io.Writer}
88+
*
89+
* @param writer
90+
* The Writer to write to.
91+
*/
92+
public JSONLDWriter(Writer writer, String baseURI) {
93+
this.baseURI = baseURI;
7194
this.writer = writer;
7295
}
7396

@@ -103,6 +126,9 @@ public void endRDF()
103126
opts.setUseNativeTypes(getWriterConfig().get(JSONLDSettings.USE_NATIVE_TYPES));
104127
// opts.optimize = getWriterConfig().get(JSONLDSettings.OPTIMIZE);
105128

129+
if (baseURI != null && getWriterConfig().get(BasicWriterSettings.BASE_DIRECTIVE)) {
130+
opts.setBase(baseURI);
131+
}
106132
if (mode == JSONLDMode.EXPAND) {
107133
output = JsonLdProcessor.expand(output, opts);
108134
}

core/rio/jsonld/src/main/java/org/eclipse/rdf4j/rio/jsonld/JSONLDWriterFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,19 @@ public RDFWriter getWriter(OutputStream out) {
3131
return new JSONLDWriter(out);
3232
}
3333

34+
@Override
35+
public RDFWriter getWriter(OutputStream out, String baseURI) {
36+
return new JSONLDWriter(out, baseURI);
37+
}
38+
3439
@Override
3540
public RDFWriter getWriter(Writer writer) {
3641
return new JSONLDWriter(writer);
3742
}
3843

44+
@Override
45+
public RDFWriter getWriter(Writer writer, String baseURI) {
46+
return new JSONLDWriter(writer, baseURI);
47+
}
48+
3949
}

0 commit comments

Comments
 (0)