View Javadoc
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.eclipse.rdf4j.model.Resource;
22  import org.eclipse.rdf4j.model.IRI;
23  import org.eclipse.rdf4j.model.Value;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * A simple {@link TripleHandler} that merely counts the number
29   * of triples it has received.
30   *
31   * @author Richard Cyganiak (richard@cyganiak.de)
32   * @author Michele Mostarda (mostarda@fbk.eu)
33   */
34  public class CountingTripleHandler implements TripleHandler {
35  
36      private static final Logger logger = LoggerFactory.getLogger(CountingTripleHandler.class);
37  
38      private final boolean logTriples;
39  
40      private int count = 0;
41  
42      public CountingTripleHandler(boolean logTriples) {
43          this.logTriples = logTriples;
44      }
45  
46      public CountingTripleHandler() {
47          this(false);
48      }
49  
50      public int getCount() {
51          return count;
52      }
53  
54      public void reset() {
55          count = 0;
56      }
57  
58      public void startDocument(IRI documentIRI) throws TripleHandlerException {
59          // ignore
60      }
61  
62      public void openContext(ExtractionContext context) throws TripleHandlerException {
63          // ignore
64      }
65  
66      public void closeContext(ExtractionContext context) throws TripleHandlerException {
67          // ignore
68      }
69  
70      public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
71      throws TripleHandlerException {
72          count++;
73          if(logTriples) logger.debug( String.format("%s %s %s %s %s\n", s, p, o, g, context) );
74      }
75  
76      public void receiveNamespace(String prefix, String uri, ExtractionContext context)
77      throws TripleHandlerException {
78          // ignore
79      }
80  
81      public void close() throws TripleHandlerException {
82          // ignore
83      }
84  
85      public void endDocument(IRI documentIRI) throws TripleHandlerException {
86          //ignore
87      }
88  
89      public void setContentLength(long contentLength) {
90          //ignore
91      }
92  }