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.eclipse.rdf4j.model.Resource;
22 import org.eclipse.rdf4j.model.IRI;
23 import org.eclipse.rdf4j.model.Value;
24
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.Locale;
28 import java.util.concurrent.atomic.AtomicInteger;
29
30
31
32
33
34
35
36 public class ReportingTripleHandler implements TripleHandler {
37
38 private final TripleHandler wrapped;
39
40 private final Collection<String> extractorNames = new HashSet<>();
41 private AtomicInteger totalTriples = new AtomicInteger(0);
42 private AtomicInteger totalDocuments = new AtomicInteger(0);
43
44 public ReportingTripleHandler(TripleHandler wrapped) {
45 if (wrapped == null) {
46 throw new NullPointerException("wrapped cannot be null.");
47 }
48 this.wrapped = wrapped;
49 }
50
51 public Collection<String> getExtractorNames() {
52 return extractorNames;
53 }
54
55 public int getTotalTriples() {
56 return totalTriples.get();
57 }
58
59 public int getTotalDocuments() {
60 return totalDocuments.get();
61 }
62
63
64
65
66 public String printReport() {
67 return String.format(Locale.ROOT, "Total Documents: %d, Total Triples: %d", getTotalDocuments(),
68 getTotalTriples());
69 }
70
71 public void startDocument(IRI documentIRI) throws TripleHandlerException {
72 totalDocuments.incrementAndGet();
73 wrapped.startDocument(documentIRI);
74 }
75
76 public void openContext(ExtractionContext context) throws TripleHandlerException {
77 wrapped.openContext(context);
78 }
79
80 public void receiveNamespace(String prefix, String uri, ExtractionContext context) throws TripleHandlerException {
81 wrapped.receiveNamespace(prefix, uri, context);
82 }
83
84 public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
85 throws TripleHandlerException {
86 extractorNames.add(context.getExtractorName());
87 totalTriples.incrementAndGet();
88 wrapped.receiveTriple(s, p, o, g, context);
89 }
90
91 public void setContentLength(long contentLength) {
92 wrapped.setContentLength(contentLength);
93 }
94
95 public void closeContext(ExtractionContext context) throws TripleHandlerException {
96 wrapped.closeContext(context);
97 }
98
99 public void endDocument(IRI documentIRI) throws TripleHandlerException {
100 wrapped.endDocument(documentIRI);
101 }
102
103 public void close() throws TripleHandlerException {
104 wrapped.close();
105 }
106
107 }