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