1   /*
2    * RDFpro - An extensible tool for building stream-oriented RDF processing libraries.
3    * 
4    * Written in 2014 by Francesco Corcoglioniti with support by Marco Amadori, Michele Mostarda,
5    * Alessio Palmero Aprosio and Marco Rospocher. Contact info on http://rdfpro.fbk.eu/
6    * 
7    * To the extent possible under law, the authors have dedicated all copyright and related and
8    * neighboring rights to this software to the public domain worldwide. This software is
9    * distributed without any warranty.
10   * 
11   * You should have received a copy of the CC0 Public Domain Dedication along with this software.
12   * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13   */
14  package eu.fbk.rdfpro;
15  
16  import java.util.Objects;
17  
18  import org.openrdf.model.Statement;
19  import org.openrdf.rio.RDFHandler;
20  import org.openrdf.rio.RDFHandlerException;
21  import org.openrdf.rio.helpers.RDFHandlerWrapper;
22  
23  import eu.fbk.rdfpro.util.IO;
24  
25  /**
26   * Base implementation of an {@code RDFHandler} + {@code AutoCloseable} wrapper.
27   * <p>
28   * This class wraps a {@code RDFHandler} and delegates all the methods to that instance. If the
29   * wrapped {@code RDFHandler} is {@code AutoCloseable}, then also calls to
30   * {@link AutoCloseable#close()} are delegated, with errors logged but ignored. Differently from
31   * Sesame {@link RDFHandlerWrapper}, this class wraps a unique {@code RDFHandler} and thus does
32   * not need array traversal (and its overhead) to notify a pool of {@code RDFHandlers}.
33   * </p>
34   */
35  public abstract class AbstractRDFHandlerWrapper extends AbstractRDFHandler {
36  
37      /** The wrapped {@code RDFHandler}. */
38      protected final RDFHandler handler;
39  
40      /**
41       * Creates a new instance wrapping the supplied {@code RDFHandler}.
42       *
43       * @param handler
44       *            the {@code RDFHandler} to wrap, not null
45       */
46      protected AbstractRDFHandlerWrapper(final RDFHandler handler) {
47          this.handler = Objects.requireNonNull(handler);
48      }
49  
50      /**
51       * Delegates to the wrapped {@code RDFHandler}.
52       */
53      @Override
54      public void startRDF() throws RDFHandlerException {
55          this.handler.startRDF();
56      }
57  
58      /**
59       * Delegates to the wrapped {@code RDFHandler}.
60       */
61      @Override
62      public void handleComment(final String comment) throws RDFHandlerException {
63          this.handler.handleComment(comment);
64      }
65  
66      /**
67       * Delegates to the wrapped {@code RDFHandler}.
68       */
69      @Override
70      public void handleNamespace(final String prefix, final String uri) throws RDFHandlerException {
71          this.handler.handleNamespace(prefix, uri);
72      }
73  
74      /**
75       * Delegates to the wrapped {@code RDFHandler}.
76       */
77      @Override
78      public void handleStatement(final Statement statement) throws RDFHandlerException {
79          this.handler.handleStatement(statement);
80      }
81  
82      /**
83       * Delegates to the wrapped {@code RDFHandler}.
84       */
85      @Override
86      public void endRDF() throws RDFHandlerException {
87          this.handler.endRDF();
88      }
89  
90      /**
91       * Delegates to the wrapped {@code RDFHandler}, if it implements {@code Closeable}.
92       */
93      @Override
94      public void close() {
95          IO.closeQuietly(this.handler);
96      }
97  
98  }