1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.writer;
19
20 import org.eclipse.rdf4j.model.Resource;
21 import org.eclipse.rdf4j.model.IRI;
22 import org.eclipse.rdf4j.model.Value;
23
24 import java.io.BufferedWriter;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.io.PrintWriter;
28 import java.nio.charset.Charset;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Collections;
31 import java.util.TreeSet;
32
33
34
35
36
37
38
39
40 public class URIListWriter extends TripleWriterHandler implements FormatWriter {
41
42 private static final Charset charset = StandardCharsets.UTF_8;
43
44 static final TripleFormat FORMAT = TripleFormat.of("URIList", Collections.singleton(URIListWriterFactory.MIME_TYPE),
45 charset, Collections.singleton("txt"), null, TripleFormat.NONSTANDARD);
46
47 private final TreeSet<String> resources = new TreeSet<>();
48
49 private PrintWriter writer;
50
51 public URIListWriter(OutputStream outputStream) {
52 writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream, charset)));
53 }
54
55 @Override
56 public void writeTriple(Resource s, IRI p, Value o, Resource g) throws TripleHandlerException {
57 String string;
58 if (s instanceof IRI && resources.add(string = s.stringValue())) {
59 writer.println(string);
60 }
61 if (o instanceof IRI && resources.add(string = o.stringValue())) {
62 writer.println(string);
63 }
64 }
65
66 @Override
67 public void writeNamespace(String prefix, String uri) throws TripleHandlerException {
68 }
69
70 @Override
71 public void endDocument(IRI documentIRI) throws TripleHandlerException {
72 writer.flush();
73 }
74
75 @Override
76 public void close() throws TripleHandlerException {
77 writer.flush();
78 writer = null;
79 resources.clear();
80 }
81
82 @Override
83 public boolean isAnnotated() {
84 return false;
85 }
86
87 @Override
88 public void setAnnotated(boolean f) {
89
90 }
91
92 }