1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.extractor.rdf;
19
20 import org.apache.any23.extractor.ExtractionContext;
21 import org.apache.any23.extractor.ExtractionException;
22 import org.apache.any23.extractor.ExtractionParameters;
23 import org.apache.any23.extractor.ExtractionResult;
24 import org.apache.any23.extractor.Extractor;
25 import org.apache.any23.extractor.ExtractorDescription;
26 import org.openrdf.rio.RDFHandlerException;
27 import org.openrdf.rio.RDFParseException;
28 import org.openrdf.rio.RDFParser;
29 import org.openrdf.rio.helpers.RDFParserBase;
30
31 import java.io.IOException;
32 import java.io.InputStream;
33
34
35
36
37
38
39
40 public abstract class BaseRDFExtractor implements Extractor.ContentExtractor {
41
42 private boolean verifyDataType;
43 private boolean stopAtFirstError;
44
45
46
47
48
49
50
51
52
53 public BaseRDFExtractor(boolean verifyDataType, boolean stopAtFirstError) {
54 this.verifyDataType = verifyDataType;
55 this.stopAtFirstError = stopAtFirstError;
56 }
57
58 public abstract ExtractorDescription getDescription();
59
60 protected abstract RDFParser getParser(
61 ExtractionContext extractionContext,
62 ExtractionResult extractionResult
63 );
64
65 public BaseRDFExtractor() {
66 this(false, false);
67 }
68
69 public boolean isVerifyDataType() {
70 return verifyDataType;
71 }
72
73 public void setVerifyDataType(boolean verifyDataType) {
74 this.verifyDataType = verifyDataType;
75 }
76
77 public boolean isStopAtFirstError() {
78 return stopAtFirstError;
79 }
80
81 public void setStopAtFirstError(boolean b) {
82 stopAtFirstError = b;
83 }
84
85 public void run(
86 ExtractionParameters extractionParameters,
87 ExtractionContext extractionContext,
88 InputStream in,
89 ExtractionResult extractionResult
90 ) throws IOException, ExtractionException {
91 try {
92 final RDFParser parser = getParser(extractionContext, extractionResult);
93 parser.parse(in, extractionContext.getDocumentURI().stringValue());
94 } catch (RDFHandlerException ex) {
95 throw new IllegalStateException("Unexpected exception.", ex);
96 } catch (RDFParseException ex) {
97 throw new ExtractionException("Error while parsing RDF document.", ex, extractionResult);
98 }
99 }
100
101 }