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.filter;
19  
20  import org.apache.any23.extractor.ExtractionContext;
21  import org.apache.any23.extractor.html.TitleExtractor;
22  import org.apache.any23.extractor.html.TitleExtractorFactory;
23  import org.apache.any23.writer.TripleHandler;
24  import org.apache.any23.writer.TripleHandlerException;
25  import org.eclipse.rdf4j.model.Resource;
26  import org.eclipse.rdf4j.model.IRI;
27  import org.eclipse.rdf4j.model.Value;
28  
29  /**
30   * A {@link TripleHandler} that suppresses output of the
31   * {@link TitleExtractor} unless some other triples could
32   * be parsed from the document. This is used when we don't
33   * want to have single-triple RDF documents around that
34   * contain only the title triple.
35   *
36   * @author Richard Cyganiak (richard@cyganiak.de)
37   */
38  public class IgnoreTitlesOfEmptyDocuments implements TripleHandler {
39      
40      private final ExtractionContextBlocker blocker;
41  
42      public IgnoreTitlesOfEmptyDocuments(TripleHandler wrapped) {
43          blocker = new ExtractionContextBlocker(wrapped);
44      }
45  
46      public void startDocument(IRI documentIRI) throws TripleHandlerException {
47          blocker.startDocument(documentIRI);
48      }
49  
50      public void openContext(ExtractionContext context) throws TripleHandlerException {
51          blocker.openContext(context);
52          if (isTitleContext(context)) {
53              blocker.blockContext(context);
54          }
55      }
56  
57      public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
58      throws TripleHandlerException {
59          if (!isTitleContext(context)) {
60              blocker.unblockDocument();
61          }
62          blocker.receiveTriple(s, p, o, g, context);
63      }
64  
65      public void receiveNamespace(String prefix, String uri,
66                                   ExtractionContext context) throws TripleHandlerException {
67          blocker.receiveNamespace(prefix, uri, context);
68      }
69  
70      public void closeContext(ExtractionContext context) {
71          blocker.closeContext(context);
72      }
73  
74      public void close() throws TripleHandlerException {
75          blocker.close();
76      }
77  
78      private boolean isTitleContext(ExtractionContext context) {
79          return context.getExtractorName().equals(TitleExtractorFactory.NAME);
80      }
81  
82      public void endDocument(IRI documentIRI) throws TripleHandlerException {
83          blocker.endDocument(documentIRI);
84      }
85  
86      public void setContentLength(long contentLength) {
87          //Ignore.
88      }
89  }