Skip to content

Commit fc5bdf7

Browse files
author
James Leigh
authored
Merge pull request #877 from jamesrdf/issues/#876-base-directive
Fix #876: Add BASE_DIRECTIVE writer config and baseURI parameter to writer interface
2 parents 7d9ef68 + 76bb516 commit fc5bdf7

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

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

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

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

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

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

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

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