1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.io.nquads; |
19 | |
|
20 | |
import org.openrdf.model.BNode; |
21 | |
import org.openrdf.model.Literal; |
22 | |
import org.openrdf.model.Resource; |
23 | |
import org.openrdf.model.Statement; |
24 | |
import org.openrdf.model.URI; |
25 | |
import org.openrdf.model.Value; |
26 | |
import org.openrdf.rio.RDFFormat; |
27 | |
import org.openrdf.rio.RDFHandlerException; |
28 | |
import org.openrdf.rio.RDFWriter; |
29 | |
import org.openrdf.rio.ntriples.NTriplesUtil; |
30 | |
|
31 | |
import java.io.IOException; |
32 | |
import java.io.OutputStream; |
33 | |
import java.io.OutputStreamWriter; |
34 | |
import java.io.Writer; |
35 | |
import java.util.HashMap; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class NQuadsWriter implements RDFWriter { |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private Map<String,String> namespaceTable; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
private Writer writer; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | private boolean started = false; |
60 | |
|
61 | |
public NQuadsWriter(OutputStream os) { |
62 | 0 | this( new OutputStreamWriter(os) ); |
63 | 0 | } |
64 | |
|
65 | 0 | public NQuadsWriter(Writer w) { |
66 | 0 | if(w == null) { |
67 | 0 | throw new NullPointerException("the writer cannot be null."); |
68 | |
} |
69 | 0 | writer = w; |
70 | 0 | } |
71 | |
|
72 | |
public RDFFormat getRDFFormat() { |
73 | 0 | return NQuads.FORMAT; |
74 | |
} |
75 | |
|
76 | |
public void startRDF() throws RDFHandlerException { |
77 | 0 | if(started) { |
78 | 0 | throw new IllegalStateException("Parsing already started."); |
79 | |
} |
80 | 0 | started = true; |
81 | 0 | } |
82 | |
|
83 | |
public void endRDF() throws RDFHandlerException { |
84 | 0 | if(!started) { |
85 | 0 | throw new IllegalStateException("Parsing never started."); |
86 | |
} |
87 | |
|
88 | |
try { |
89 | 0 | writer.flush(); |
90 | 0 | } catch (IOException ioe) { |
91 | 0 | throw new RDFHandlerException("Error while flushing writer.", ioe); |
92 | |
} finally { |
93 | 0 | started = false; |
94 | 0 | if(namespaceTable != null) { |
95 | 0 | namespaceTable.clear(); |
96 | |
} |
97 | |
} |
98 | 0 | } |
99 | |
|
100 | |
public void handleNamespace(String ns, String uri) throws RDFHandlerException { |
101 | 0 | if(!started) { |
102 | 0 | throw new IllegalStateException("Parsing never started."); |
103 | |
} |
104 | |
|
105 | 0 | if(namespaceTable == null) { |
106 | 0 | namespaceTable = new HashMap<String, String>(); |
107 | |
} |
108 | 0 | namespaceTable.put(ns, NTriplesUtil.escapeString(uri) ); |
109 | 0 | } |
110 | |
|
111 | |
public void handleStatement(Statement statement) throws RDFHandlerException { |
112 | 0 | if(!started) { |
113 | 0 | throw new IllegalStateException("Cannot handle statement without start parsing first."); |
114 | |
} |
115 | |
|
116 | |
try { |
117 | 0 | printSubject(statement); |
118 | 0 | printSpace(); |
119 | 0 | printPredicate(statement); |
120 | 0 | printSpace(); |
121 | 0 | printObject(statement); |
122 | 0 | printSpace(); |
123 | 0 | printGraph(statement); |
124 | 0 | printCloseStatement(); |
125 | 0 | } catch (IOException ioe) { |
126 | 0 | throw new RDFHandlerException("An error occurred while printing statement.", ioe); |
127 | 0 | } |
128 | 0 | } |
129 | |
|
130 | |
public void handleComment(String comment) throws RDFHandlerException { |
131 | |
try { |
132 | 0 | writer.write("# "); |
133 | 0 | writer.write(comment); |
134 | 0 | writer.append('\n'); |
135 | 0 | } catch (IOException ioe) { |
136 | 0 | throw new RDFHandlerException("An error occurred while printing comment.", ioe); |
137 | 0 | } |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
private void printSpace() throws IOException { |
146 | 0 | writer.append(' '); |
147 | 0 | } |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
private void printCloseStatement() throws IOException { |
155 | 0 | writer.append(" .\n"); |
156 | 0 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
private void printURI(URI uri) throws IOException { |
165 | 0 | final String uriString = uri.stringValue(); |
166 | 0 | int splitIdx = 0; |
167 | 0 | String namespace = null; |
168 | 0 | if(namespaceTable != null) { |
169 | 0 | splitIdx = uriString.indexOf(':'); |
170 | 0 | if (splitIdx > 0) { |
171 | 0 | String prefix = uriString.substring(0, splitIdx); |
172 | 0 | namespace = namespaceTable.get(prefix); |
173 | |
} |
174 | |
} |
175 | |
|
176 | 0 | if (namespace != null) { |
177 | 0 | writer.append('<'); |
178 | 0 | writer.append(namespace); |
179 | 0 | writer.append( NTriplesUtil.escapeString(uriString.substring(splitIdx)) ); |
180 | 0 | writer.append('>'); |
181 | |
} else { |
182 | 0 | writer.append('<'); |
183 | 0 | writer.append( NTriplesUtil.escapeString(uriString) ); |
184 | 0 | writer.append('>'); |
185 | |
} |
186 | 0 | } |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
private void printBNode(BNode b) throws IOException { |
195 | 0 | writer.append( NTriplesUtil.toNTriplesString(b) ); |
196 | 0 | } |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
private void printResource(Resource r) throws IOException { |
205 | 0 | if(r instanceof BNode) { |
206 | 0 | printBNode((BNode) r); |
207 | 0 | } else if(r instanceof URI) { |
208 | 0 | printURI((URI) r); |
209 | |
} else { |
210 | 0 | throw new IllegalStateException(); |
211 | |
} |
212 | 0 | } |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
private void printLiteral(Literal l) throws IOException { |
221 | 0 | writer.append( NTriplesUtil.toNTriplesString(l) ); |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
private void printSubject(Statement s) throws IOException { |
231 | 0 | printResource( s.getSubject() ); |
232 | 0 | } |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
private void printPredicate(Statement s) throws IOException { |
241 | 0 | printURI( s.getPredicate() ); |
242 | 0 | } |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
private void printObject(Statement s) throws IOException { |
252 | 0 | Value v = s.getObject(); |
253 | 0 | if(v instanceof Resource) { |
254 | 0 | printResource((Resource) v); |
255 | 0 | return; |
256 | |
} |
257 | 0 | printLiteral( (Literal) v ); |
258 | 0 | } |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
private void printGraph(Statement s) throws IOException { |
267 | 0 | Resource graph = s.getContext(); |
268 | 0 | if(graph != null) { |
269 | 0 | printResource( s.getContext() ); |
270 | |
} |
271 | 0 | } |
272 | |
|
273 | |
} |