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.IssueReport;
26 import org.eclipse.rdf4j.rio.RDFParser;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.PrintWriter;
31 import java.io.StringWriter;
32
33
34
35
36
37
38
39 public abstract class BaseRDFExtractor implements Extractor.ContentExtractor {
40
41 private boolean verifyDataType;
42 private boolean stopAtFirstError;
43
44 public BaseRDFExtractor() {
45 this(false, false);
46 }
47
48
49
50
51
52
53
54
55
56
57 public BaseRDFExtractor(boolean verifyDataType, boolean stopAtFirstError) {
58 this.verifyDataType = verifyDataType;
59 this.stopAtFirstError = stopAtFirstError;
60 }
61
62 protected abstract RDFParser getParser(ExtractionContext extractionContext, ExtractionResult extractionResult);
63
64 public boolean isVerifyDataType() {
65 return verifyDataType;
66 }
67
68 public void setVerifyDataType(boolean verifyDataType) {
69 this.verifyDataType = verifyDataType;
70 }
71
72 public boolean isStopAtFirstError() {
73 return stopAtFirstError;
74 }
75
76 @Override
77 public void setStopAtFirstError(boolean b) {
78 stopAtFirstError = b;
79 }
80
81 @Override
82 public void run(ExtractionParameters extractionParameters, ExtractionContext extractionContext, InputStream in,
83 ExtractionResult extractionResult) throws IOException, ExtractionException {
84 try {
85 final RDFParser parser = getParser(extractionContext, extractionResult);
86 parser.parse(in, extractionContext.getDocumentIRI().stringValue());
87 } catch (Exception ex) {
88 extractionResult.notifyIssue(IssueReport.IssueLevel.FATAL, toString(ex), -1, -1);
89 }
90 }
91
92
93 static String toString(Throwable th) {
94 StringWriter writer = new StringWriter();
95 try (PrintWriter pw = new PrintWriter(writer)) {
96 th.printStackTrace(pw);
97 }
98 String string = writer.toString();
99 if (string.length() > 1024) {
100 return string.substring(0, 1021) + "...";
101 }
102 return string;
103 }
104
105 }