##### Coverage Report - org.apache.any23.writer.RepositoryWriter

Classes in this File | Line Coverage | Branch Coverage | Complexity
RepositoryWriter |
0% | 0/25
0% | 0/2
| 1.5454545454545454;1.545

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