| 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.openrdf.model.Resource; |
| 22 | |
import org.openrdf.model.URI; |
| 23 | |
import org.openrdf.model.Value; |
| 24 | |
import org.openrdf.repository.RepositoryConnection; |
| 25 | |
import org.openrdf.repository.RepositoryException; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public class RepositoryWriter implements TripleHandler { |
| 33 | |
|
| 34 | |
private final RepositoryConnection conn; |
| 35 | |
private final Resource overrideContext; |
| 36 | |
|
| 37 | |
public RepositoryWriter(RepositoryConnection conn) { |
| 38 | 0 | this(conn, null); |
| 39 | 0 | } |
| 40 | |
|
| 41 | 0 | public RepositoryWriter(RepositoryConnection conn, Resource overrideContext) { |
| 42 | 0 | this.conn = conn; |
| 43 | 0 | this.overrideContext = overrideContext; |
| 44 | 0 | } |
| 45 | |
|
| 46 | |
public void startDocument(URI documentURI) throws TripleHandlerException { |
| 47 | |
|
| 48 | 0 | } |
| 49 | |
|
| 50 | |
public void openContext(ExtractionContext context) throws TripleHandlerException { |
| 51 | |
|
| 52 | 0 | } |
| 53 | |
|
| 54 | |
public void receiveTriple( |
| 55 | |
Resource s, |
| 56 | |
URI p, |
| 57 | |
Value o, |
| 58 | |
URI g, |
| 59 | |
ExtractionContext context |
| 60 | |
) throws TripleHandlerException { |
| 61 | |
try { |
| 62 | 0 | conn.add( |
| 63 | |
conn.getValueFactory().createStatement(s, p, o, g), |
| 64 | |
getContextResource(context.getDocumentURI()) |
| 65 | |
); |
| 66 | 0 | } catch (RepositoryException ex) { |
| 67 | 0 | throw new TripleHandlerException(String.format("Error while receiving triple: %s %s %s", s, p , o), |
| 68 | |
ex |
| 69 | |
); |
| 70 | 0 | } |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
public void receiveNamespace( |
| 74 | |
String prefix, |
| 75 | |
String uri, |
| 76 | |
ExtractionContext context |
| 77 | |
) throws TripleHandlerException { |
| 78 | |
try { |
| 79 | 0 | conn.setNamespace(prefix, uri); |
| 80 | 0 | } catch (RepositoryException ex) { |
| 81 | 0 | throw new TripleHandlerException(String.format("Error while receiving namespace: %s:%s", prefix, uri), |
| 82 | |
ex |
| 83 | |
); |
| 84 | 0 | } |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
public void closeContext(ExtractionContext context) throws TripleHandlerException { |
| 88 | |
|
| 89 | 0 | } |
| 90 | |
|
| 91 | |
public void close()throws TripleHandlerException { |
| 92 | |
|
| 93 | 0 | } |
| 94 | |
|
| 95 | |
public void endDocument(URI documentURI) throws TripleHandlerException { |
| 96 | |
|
| 97 | 0 | } |
| 98 | |
|
| 99 | |
public void setContentLength(long contentLength) { |
| 100 | |
|
| 101 | 0 | } |
| 102 | |
|
| 103 | |
private Resource getContextResource(Resource fromExtractor) { |
| 104 | 0 | if (overrideContext != null) { |
| 105 | 0 | return overrideContext; |
| 106 | |
} |
| 107 | 0 | return fromExtractor; |
| 108 | |
} |
| 109 | |
} |