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

Classes in this File | Line Coverage | Branch Coverage | Complexity
RDFWriterTripleHandler |
0% | 0/43
0% | 0/6
| 2.25;2.25

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.apache.any23.rdf.RDFUtils;
22 |  |
     import org.openrdf.model.Resource;
23 |  |
     import org.openrdf.model.URI;
24 |  |
     import org.openrdf.model.Value;
25 |  |
     import org.openrdf.rio.RDFHandlerException;
26 |  |
     import org.openrdf.rio.RDFWriter;
27 |  |
28 |  |
     /**
29 |  |
      * A {@link TripleHandler} that writes
30 |  |
      * triples to a Sesame {@link org.openrdf.rio.RDFWriter},
31 |  |
      * eg for serialization using one of Sesame's writers.
32 |  |
      *
33 |  |
      * @author Richard Cyganiak (richard@cyganiak.de)
34 |  |
      * @author Michele Mostarda (mostarda@fbk.eu)
35 |  |
      */
36 |  |
     public abstract class RDFWriterTripleHandler implements FormatWriter, TripleHandler {
37 |  |
38 |  |
         private final RDFWriter writer;
39 |  |
40 | 0 |
         private boolean closed = false;
41 |  |
42 |  |
         /**
43 |  |
          * The annotation flag.
44 |  |
          */
45 | 0 |
         private boolean annotated = false;
46 |  |
47 | 0 |
         RDFWriterTripleHandler(RDFWriter destination) {
48 | 0 |
             writer = destination;
49 |  |
             try {
50 | 0 |
                 writer.startRDF();
51 | 0 |
             } catch (RDFHandlerException e) {
52 | 0 |
                 throw new RuntimeException(e);
53 | 0 |
             }
54 | 0 |
         }
55 |  |
56 |  |
         /**
57 |  |
          * If <code>true</code> then the produced <b>RDF</b> is annotated with
58 |  |
          * the extractors used to generate the specific statements.
59 |  |
          *
60 |  |
          * @return the annotation flag value.
61 |  |
          */
62 |  |
         @Override
63 |  |
         public boolean isAnnotated() {
64 | 0 |
             return annotated;
65 |  |
         }
66 |  |
67 |  |
         /**
68 |  |
          * Sets the <i>annotation</i> flag.
69 |  |
          *
70 |  |
          * @param f If <code>true</code> then the produced <b>RDF</b> is annotated with
71 |  |
          *          the extractors used to generate the specific statements.
72 |  |
          */
73 |  |
         @Override
74 |  |
         public void setAnnotated(boolean f) {
75 | 0 |
             annotated = f;
76 | 0 |
         }
77 |  |
78 |  |
         @Override
79 |  |
         public void startDocument(URI documentURI) throws TripleHandlerException {
80 |  |
             // Empty.
81 | 0 |
         }
82 |  |
83 |  |
         @Override
84 |  |
         public void openContext(ExtractionContext context) throws TripleHandlerException {
85 | 0 |
             handleComment( String.format("BEGIN: " + context) );
86 | 0 |
         }
87 |  |
88 |  |
         @Override
89 |  |
         public void receiveTriple(Resource s, URI p, Value o, URI g, ExtractionContext context)
90 |  |
         throws TripleHandlerException {
91 | 0 |
             final URI graph = g == null ? context.getDocumentURI() : g;
92 |  |
             try {
93 | 0 |
                 writer.handleStatement(
94 |  |
                         RDFUtils.quad(s, p, o, graph));
95 | 0 |
             } catch (RDFHandlerException ex) {
96 | 0 |
                 throw new TripleHandlerException(
97 |  |
                         String.format("Error while receiving triple: %s %s %s %s", s, p, o, graph),
98 |  |
                         ex
99 |  |
                 );
100 | 0 |
             }
101 | 0 |
         }
102 |  |
103 |  |
         @Override
104 |  |
         public void receiveNamespace(String prefix, String uri, ExtractionContext context)
105 |  |
         throws TripleHandlerException {
106 |  |
             try {
107 | 0 |
                 writer.handleNamespace(prefix, uri);
108 | 0 |
             } catch (RDFHandlerException ex) {
109 | 0 |
                 throw new TripleHandlerException(String.format("Error while receiving namespace: %s:%s", prefix, uri),
110 |  |
                         ex
111 |  |
                 );
112 | 0 |
             }
113 | 0 |
         }
114 |  |
115 |  |
         @Override
116 |  |
         public void closeContext(ExtractionContext context) throws TripleHandlerException {
117 | 0 |
             handleComment( String.format("END: " + context) );
118 | 0 |
         }
119 |  |
120 |  |
         @Override
121 |  |
         public void close() throws TripleHandlerException {
122 | 0 |
             if (closed) return;
123 | 0 |
             closed = true;
124 |  |
             try {
125 | 0 |
                 writer.endRDF();
126 | 0 |
             } catch (RDFHandlerException e) {
127 | 0 |
                 throw new TripleHandlerException("Error while closing the triple handler.", e);
128 | 0 |
             }
129 | 0 |
         }
130 |  |
131 |  |
         @Override
132 |  |
         public void endDocument(URI documentURI) throws TripleHandlerException {
133 |  |
             // Empty.
134 | 0 |
         }
135 |  |
136 |  |
         @Override
137 |  |
         public void setContentLength(long contentLength) {
138 |  |
             // Empty.
139 | 0 |
         }
140 |  |
141 |  |
         private void handleComment(String comment) throws TripleHandlerException {
142 | 0 |
             if( !annotated ) return;
143 |  |
             try {
144 | 0 |
                 writer.handleComment(comment);
145 | 0 |
             } catch (RDFHandlerException rdfhe) {
146 | 0 |
                 throw new TripleHandlerException("Error while handing comment.", rdfhe);
147 | 0 |
             }
148 | 0 |
         }
149 |  |
150 |  |
     }