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 java.util.Locale;
    21
    22  import org.apache.any23.extractor.ExtractionContext;
    23  import org.eclipse.rdf4j.model.Resource;
    24  import org.eclipse.rdf4j.model.IRI;
    25  import org.eclipse.rdf4j.model.Value;
    26  import org.eclipse.rdf4j.repository.RepositoryConnection;
    27  import org.eclipse.rdf4j.repository.RepositoryException;
    28
    29  /**
    30   * An <i>RDF4J repository</i> triple writer.
    31   *
    32   * @see org.eclipse.rdf4j.repository.Repository
    33   */
    34  public class RepositoryWriter implements TripleHandler {
    35
    36      private final RepositoryConnection conn;
    37      private final Resource overrideContext;
    38
    39      public RepositoryWriter(RepositoryConnection conn) {
    40          this(conn, null);
    41      }
    42
    43      public RepositoryWriter(RepositoryConnection conn, Resource overrideContext) {
    44          this.conn = conn;
    45          this.overrideContext = overrideContext;
    46      }
    47
    48      @Override
    49      public void startDocument(IRI documentIRI) throws TripleHandlerException {
    50          // ignore
    51      }
    52
    53      @Override
    54      public void openContext(ExtractionContext context) throws TripleHandlerException {
    55          // ignore
    56      }
    57
    58      @Override
    59      public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
    60              throws TripleHandlerException {
    61          try {
    62              conn.add(conn.getValueFactory().createStatement(s, p, o, g), getContextResource(context.getDocumentIRI()));
    63          } catch (RepositoryException ex) {
    64              throw new TripleHandlerException(
    65                      String.format(Locale.ROOT, "Error while receiving triple: %s %s %s", s, p, o), ex);
    66          }
    67      }
    68
    69      @Override
    70      public void receiveNamespace(String prefix, String uri, ExtractionContext context) throws TripleHandlerException {
    71          try {
    72              conn.setNamespace(prefix, uri);
    73          } catch (RepositoryException ex) {
    74              throw new TripleHandlerException(
    75                      String.format(Locale.ROOT, "Error while receiving namespace: %s:%s", prefix, uri), ex);
    76          }
    77      }
    78
    79      @Override
    80      public void closeContext(ExtractionContext context) throws TripleHandlerException {
    81          // ignore
    82      }
    83
    84      @Override
    85      public void close() throws TripleHandlerException {
    86          // ignore
    87      }
    88
    89      @Override
    90      public void endDocument(IRI documentIRI) throws TripleHandlerException {
    91          // ignore
    92      }
    93
    94      @Override
    95      public void setContentLength(long contentLength) {
    96          // ignore
    97      }
    98
    99      private Resource getContextResource(Resource fromExtractor) {
    100         if (overrideContext != null) {
    101             return overrideContext;
    102         }
    103         return fromExtractor;
    104     }
    105 }