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.extractor.rdf;
19  
20  import org.apache.any23.extractor.ExtractionResult;
21  import org.eclipse.rdf4j.model.Resource;
22  import org.eclipse.rdf4j.model.Statement;
23  import org.eclipse.rdf4j.model.IRI;
24  import org.eclipse.rdf4j.rio.RDFHandler;
25  import org.eclipse.rdf4j.rio.RDFHandlerException;
26  
27  /**
28   * An RDFHandler that relays statements and prefix definitions to
29   * an {@link ExtractionResult}. Used to feed output from Sesame's
30   * RDF parsers into Any23.
31   *
32   * @author Richard Cyganiak (richard@cyganiak.de)
33   */
34  public class RDFHandlerAdapter implements RDFHandler {
35  
36      private ExtractionResult target;
37  
38      public RDFHandlerAdapter(ExtractionResult target) {
39          this.target = target;
40      }
41  
42      public void startRDF() throws RDFHandlerException {
43      }
44  
45      public void handleNamespace(String prefix, String uri) {
46          target.writeNamespace(prefix, uri);
47      }
48  
49      public void handleStatement(Statement stmt) {
50          if(stmt != null) {
51              final Resource context = stmt.getContext();
52              if(context instanceof IRI) {
53                  target.writeTriple(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), (IRI) context);
54              } else {
55                  target.writeTriple(stmt.getSubject(), stmt.getPredicate(), stmt.getObject());
56              }
57          }
58      }
59  
60      public void handleComment(String comment) {
61          // Empty.
62      }
63  
64      public void endRDF() throws RDFHandlerException {
65          // Empty.
66      }
67  
68  }