##### Coverage Report - org.apache.any23.writer.LoggingTripleHandler

Classes in this File | Line Coverage | Branch Coverage | Complexity
LoggingTripleHandler |
0% | 0/47
0% | 0/10
| 1.7;1.7

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
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 |  |
      * Triple handler decorator useful for logging purposes.
32 |  |
      */
33 |  |
     public class LoggingTripleHandler implements TripleHandler {
34 |  |
35 |  |
         /**
36 |  |
          * Decorated.
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 |  |
     }