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