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. The default implementation calls:
34       * 
35       * <pre>
36       *     {@code this.writeTriple(s, p, o, context == null || g != null ? g : context.getDocumentIRI())}
37       * </pre>
38       * 
39       * @param s
40       *            the subject received
41       * @param p
42       *            the predicate received
43       * @param o
44       *            the object received
45       * @param g
46       *            the graph name received, or null
47       * @param context
48       *            the extraction context
49       * 
50       * @throws TripleHandlerException
51       *             if there was an error responding to a received triple
52       */
53      @Override
54      public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
55              throws TripleHandlerException {
56          writeTriple(s, p, o, context == null || g != null ? g : context.getDocumentIRI());
57      }
58  
59      /**
60       * Writers may override this method to handle a "receiveNamespace" extraction event. The default implementation
61       * calls:
62       * 
63       * <pre>
64       *     {@code this.writeNamespace(prefix, uri)}
65       * </pre>
66       * 
67       * @param prefix
68       *            namespace prefix.
69       * @param uri
70       *            namespace <i>IRI</i>.
71       * @param context
72       *            the extraction context
73       * 
74       * @throws TripleHandlerException
75       *             if there was an error responding to the received namepsace.
76       */
77      @Override
78      public void receiveNamespace(String prefix, String uri, ExtractionContext context) throws TripleHandlerException {
79          writeNamespace(prefix, uri);
80      }
81  
82      /**
83       * Writers may override this method to handle a "startDocument" extraction event. The default implementation does
84       * nothing.
85       * 
86       * @param documentIRI
87       *            the name of the document that was started
88       * 
89       * @throws TripleHandlerException
90       *             if an error occurred while responding to a "startDocument" extraction event.
91       */
92      @Override
93      public void startDocument(IRI documentIRI) throws TripleHandlerException {
94      }
95  
96      /**
97       * Writers may override this method to handle an "openContext" extraction event. The default implementation does
98       * nothing.
99       * 
100      * @param context
101      *            the context that was opened
102      * 
103      * @throws TripleHandlerException
104      *             if an error occurred while responding to a "startDocument" extraction event.
105      */
106     @Override
107     public void openContext(ExtractionContext context) throws TripleHandlerException {
108     }
109 
110     /**
111      * Writers may override this method to handle a "closeContext" extraction event. The default implementation does
112      * nothing.
113      * 
114      * @param context
115      *            the context to be closed.
116      * 
117      * @throws TripleHandlerException
118      *             if an error occurred while responding to a "closeContext" extraction event.
119      */
120     @Override
121     public void closeContext(ExtractionContext context) throws TripleHandlerException {
122     }
123 
124     /**
125      * Writers may override this method to handle an "endDocument" extraction event. The default implementation does
126      * nothing.
127      * 
128      * @param documentIRI
129      *            the document IRI.
130      * 
131      * @throws TripleHandlerException
132      *             if an error occurred while responding to a "endDocument" extraction event.
133      */
134     @Override
135     public void endDocument(IRI documentIRI) throws TripleHandlerException {
136     }
137 
138     /**
139      * Writers may override this method to handle a "setContentLength" extraction event. The default implementation does
140      * nothing.
141      * 
142      * @param contentLength
143      *            length of the content being processed.
144      */
145     @Override
146     public void setContentLength(long contentLength) {
147     }
148 
149 }