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