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.IRI;
22  import org.eclipse.rdf4j.model.Resource;
23  import org.eclipse.rdf4j.model.Value;
24  
25  /**
26   * This class connects a {@link TripleHandler} to a {@link TripleWriter} by writing received data.
27   *
28   * @author Hans Brende (hansbrende@apache.org)
29   */
30  public abstract class TripleWriterHandler implements TripleHandler, TripleWriter {
31  
32      /**
33       * Writers may override this method to handle a "receiveTriple" extraction event.
34       * The default implementation calls:
35       * <pre>
36       *     {@code this.writeTriple(s, p, o, context == null || g != null ? g : context.getDocumentIRI())}
37       * </pre>
38       * @param s the subject received
39       * @param p the predicate received
40       * @param o the object received
41       * @param g the graph name received, or null
42       * @param context the extraction context
43       * @throws TripleHandlerException if there was an error responding to a received triple
44       */
45      @Override
46      public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context) throws TripleHandlerException {
47          writeTriple(s, p, o, context == null || g != null ? g : context.getDocumentIRI());
48      }
49  
50      /**
51       * Writers may override this method to handle a "receiveNamespace" extraction event.
52       * The default implementation calls:
53       * <pre>
54       *     {@code this.writeNamespace(prefix, uri)}
55       * </pre>
56       * @param prefix namespace prefix.
57       * @param uri namespace <i>IRI</i>.
58       * @param context the extraction context
59       * @throws TripleHandlerException if there was an error responding to the received namepsace.
60       */
61      @Override
62      public void receiveNamespace(String prefix, String uri, ExtractionContext context) throws TripleHandlerException {
63          writeNamespace(prefix, uri);
64      }
65  
66      /**
67       * Writers may override this method to handle a "startDocument" extraction event.
68       * The default implementation does nothing.
69       * @param documentIRI the name of the document that was started
70       * @throws TripleHandlerException if an error occurred while responding to a "startDocument"
71       * extraction event.
72       */
73      @Override
74      public void startDocument(IRI documentIRI) throws TripleHandlerException { }
75  
76      /**
77       * Writers may override this method to handle an "openContext" extraction event.
78       * The default implementation does nothing.
79       * @param context the context that was opened
80       * @throws TripleHandlerException if an error occurred while responding to a "startDocument"
81       * extraction event.
82       */
83      @Override
84      public void openContext(ExtractionContext context) throws TripleHandlerException { }
85  
86      /**
87       * Writers may override this method to handle a "closeContext" extraction event.
88       * The default implementation does nothing.
89       * @param context the context to be closed.
90       * @throws TripleHandlerException if an error occurred while responding to a "closeContext"
91       * extraction event.
92       */
93      @Override
94      public void closeContext(ExtractionContext context) throws TripleHandlerException { }
95  
96      /**
97       * Writers may override this method to handle an "endDocument" extraction event.
98       * The default implementation does nothing.
99       * @param documentIRI the document IRI.
100      * @throws TripleHandlerException if an error occurred while responding to a "endDocument"
101      * extraction event.
102      */
103     @Override
104     public void endDocument(IRI documentIRI) throws TripleHandlerException { }
105 
106     /**
107      * Writers may override this method to handle a "setContentLength" extraction event.
108      * The default implementation does nothing.
109      * @param contentLength length of the content being processed.
110      */
111     @Override
112     public void setContentLength(long contentLength) { }
113 
114 }