| 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 | |
|
| 25 | |
import java.io.PrintWriter; |
| 26 | |
import java.util.HashMap; |
| 27 | |
import java.util.Map; |
| 28 | |
import java.util.Map.Entry; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public class LoggingTripleHandler implements TripleHandler { |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
private final TripleHandler underlyingHandler; |
| 39 | |
|
| 40 | 0 | private final Map<String, Integer> contextTripleMap = new HashMap<String, Integer>(); |
| 41 | 0 | private long startTime = 0; |
| 42 | 0 | private long contentLength = 0; |
| 43 | |
private final PrintWriter destination; |
| 44 | |
|
| 45 | 0 | public LoggingTripleHandler(TripleHandler tripleHandler, PrintWriter destination) { |
| 46 | 0 | if(tripleHandler == null) { |
| 47 | 0 | throw new NullPointerException("tripleHandler cannot be null."); |
| 48 | |
} |
| 49 | 0 | if(destination == null) { |
| 50 | 0 | throw new NullPointerException("destination cannot be null."); |
| 51 | |
} |
| 52 | 0 | underlyingHandler = tripleHandler; |
| 53 | 0 | this.destination = destination; |
| 54 | |
|
| 55 | 0 | printHeader(destination); |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
public void startDocument(URI documentURI) throws TripleHandlerException { |
| 59 | 0 | underlyingHandler.startDocument(documentURI); |
| 60 | 0 | startTime = System.currentTimeMillis(); |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
public void close() throws TripleHandlerException { |
| 64 | 0 | underlyingHandler.close(); |
| 65 | 0 | destination.flush(); |
| 66 | 0 | destination.close(); |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
public void closeContext(ExtractionContext context) throws TripleHandlerException { |
| 70 | 0 | underlyingHandler.closeContext(context); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
public void openContext(ExtractionContext context) throws TripleHandlerException { |
| 74 | 0 | underlyingHandler.openContext(context); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
public void receiveTriple(Resource s, URI p, Value o, URI g, ExtractionContext context) |
| 78 | |
throws TripleHandlerException { |
| 79 | 0 | underlyingHandler.receiveTriple(s, p, o, g, context); |
| 80 | 0 | Integer i = contextTripleMap.get(context.getExtractorName()); |
| 81 | 0 | if (i == null) i = 0; |
| 82 | 0 | contextTripleMap.put(context.getExtractorName(), (i + 1)); |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
public void receiveNamespace(String prefix, String uri, ExtractionContext context) |
| 86 | |
throws TripleHandlerException { |
| 87 | 0 | underlyingHandler.receiveNamespace(prefix, uri, context); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
public void endDocument(URI documentURI) throws TripleHandlerException { |
| 91 | 0 | underlyingHandler.endDocument(documentURI); |
| 92 | 0 | long elapsedTime = System.currentTimeMillis() - startTime; |
| 93 | 0 | boolean success = true; |
| 94 | 0 | StringBuffer sb = new StringBuffer("["); |
| 95 | 0 | for (Entry<String, Integer> ent : contextTripleMap.entrySet()) { |
| 96 | 0 | sb.append(" ").append(ent.getKey()).append(":").append(ent.getValue()); |
| 97 | 0 | if (ent.getValue() > 0) { |
| 98 | 0 | success = true; |
| 99 | |
} |
| 100 | |
} |
| 101 | 0 | sb.append("]"); |
| 102 | 0 | destination.println( |
| 103 | |
documentURI + "\t" + contentLength + "\t" + elapsedTime + "\t" + success + "\t" + sb.toString() |
| 104 | |
); |
| 105 | 0 | contextTripleMap.clear(); |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
public void setContentLength(long contentLength) { |
| 109 | 0 | underlyingHandler.setContentLength(contentLength); |
| 110 | 0 | this.contentLength = contentLength; |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
private void printHeader(PrintWriter writer) { |
| 114 | 0 | writer.println("# Document-URI\tContent-Length\tElapsed-Time\tSuccess\tExtractors"); |
| 115 | 0 | } |
| 116 | |
} |