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.apache.any23.extractor.ExtractionContext; |
21 | |
import org.apache.any23.rdf.RDFUtils; |
22 | |
import org.openrdf.model.Resource; |
23 | |
import org.openrdf.model.URI; |
24 | |
import org.openrdf.model.Value; |
25 | |
import org.openrdf.rio.RDFHandlerException; |
26 | |
import org.openrdf.rio.RDFWriter; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public abstract class RDFWriterTripleHandler implements FormatWriter, TripleHandler { |
37 | |
|
38 | |
private final RDFWriter writer; |
39 | |
|
40 | 0 | private boolean closed = false; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | private boolean annotated = false; |
46 | |
|
47 | 0 | RDFWriterTripleHandler(RDFWriter destination) { |
48 | 0 | writer = destination; |
49 | |
try { |
50 | 0 | writer.startRDF(); |
51 | 0 | } catch (RDFHandlerException e) { |
52 | 0 | throw new RuntimeException(e); |
53 | 0 | } |
54 | 0 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
@Override |
63 | |
public boolean isAnnotated() { |
64 | 0 | return annotated; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
@Override |
74 | |
public void setAnnotated(boolean f) { |
75 | 0 | annotated = f; |
76 | 0 | } |
77 | |
|
78 | |
@Override |
79 | |
public void startDocument(URI documentURI) throws TripleHandlerException { |
80 | |
|
81 | 0 | } |
82 | |
|
83 | |
@Override |
84 | |
public void openContext(ExtractionContext context) throws TripleHandlerException { |
85 | 0 | handleComment( String.format("BEGIN: " + context) ); |
86 | 0 | } |
87 | |
|
88 | |
@Override |
89 | |
public void receiveTriple(Resource s, URI p, Value o, URI g, ExtractionContext context) |
90 | |
throws TripleHandlerException { |
91 | 0 | final URI graph = g == null ? context.getDocumentURI() : g; |
92 | |
try { |
93 | 0 | writer.handleStatement( |
94 | |
RDFUtils.quad(s, p, o, graph)); |
95 | 0 | } catch (RDFHandlerException ex) { |
96 | 0 | throw new TripleHandlerException( |
97 | |
String.format("Error while receiving triple: %s %s %s %s", s, p, o, graph), |
98 | |
ex |
99 | |
); |
100 | 0 | } |
101 | 0 | } |
102 | |
|
103 | |
@Override |
104 | |
public void receiveNamespace(String prefix, String uri, ExtractionContext context) |
105 | |
throws TripleHandlerException { |
106 | |
try { |
107 | 0 | writer.handleNamespace(prefix, uri); |
108 | 0 | } catch (RDFHandlerException ex) { |
109 | 0 | throw new TripleHandlerException(String.format("Error while receiving namespace: %s:%s", prefix, uri), |
110 | |
ex |
111 | |
); |
112 | 0 | } |
113 | 0 | } |
114 | |
|
115 | |
@Override |
116 | |
public void closeContext(ExtractionContext context) throws TripleHandlerException { |
117 | 0 | handleComment( String.format("END: " + context) ); |
118 | 0 | } |
119 | |
|
120 | |
@Override |
121 | |
public void close() throws TripleHandlerException { |
122 | 0 | if (closed) return; |
123 | 0 | closed = true; |
124 | |
try { |
125 | 0 | writer.endRDF(); |
126 | 0 | } catch (RDFHandlerException e) { |
127 | 0 | throw new TripleHandlerException("Error while closing the triple handler.", e); |
128 | 0 | } |
129 | 0 | } |
130 | |
|
131 | |
@Override |
132 | |
public void endDocument(URI documentURI) throws TripleHandlerException { |
133 | |
|
134 | 0 | } |
135 | |
|
136 | |
@Override |
137 | |
public void setContentLength(long contentLength) { |
138 | |
|
139 | 0 | } |
140 | |
|
141 | |
private void handleComment(String comment) throws TripleHandlerException { |
142 | 0 | if( !annotated ) return; |
143 | |
try { |
144 | 0 | writer.handleComment(comment); |
145 | 0 | } catch (RDFHandlerException rdfhe) { |
146 | 0 | throw new TripleHandlerException("Error while handing comment.", rdfhe); |
147 | 0 | } |
148 | 0 | } |
149 | |
|
150 | |
} |