1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.extractor;
19
20 import org.apache.any23.source.MemCopyFactory;
21 import org.apache.any23.source.StringDocumentSource;
22 import org.apache.any23.writer.TripleHandler;
23 import org.apache.any23.writer.TripleHandlerException;
24 import org.apache.any23.writer.TurtleWriter;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29
30
31
32
33
34
35
36 public class ExampleInputOutput {
37
38 private final ExtractorFactory<?> factory;
39
40 public ExampleInputOutput(String extractorName) {
41 this(ExtractorRegistryImpl.getInstance().getFactory(extractorName));
42 }
43
44 public ExampleInputOutput(ExtractorFactory<?> factory) {
45 this.factory = factory;
46 }
47
48 public String getExampleInput() throws IOException {
49 if (factory.getExampleInput() == null) {
50 return null;
51 }
52 if (isBlindExtractor()) {
53 return null;
54 }
55 InputStream in = factory.createExtractor().getClass().getResourceAsStream(factory.getExampleInput());
56 if (in == null) {
57 throw new IllegalArgumentException("Example input resource not found for extractor "
58 + factory.getExtractorName() + ": " + factory.getExampleInput());
59 }
60 return new String(MemCopyFactory.toByteArray(in), "utf-8");
61 }
62
63 public String getExampleIRI() {
64 if (factory.getExampleInput() == null) {
65 return null;
66 }
67 if (isBlindExtractor()) {
68 return factory.getExampleInput();
69 }
70 return "http://example.com/";
71 }
72
73 public String getExampleOutput() throws IOException, ExtractionException {
74 if (factory.getExampleInput() == null) {
75 return null;
76 }
77 ByteArrayOutputStream out = new ByteArrayOutputStream();
78 TripleHandler writer = new TurtleWriter(out);
79 new SingleDocumentExtraction(new StringDocumentSource(getExampleInput(), getExampleIRI()), factory, writer)
80 .run();
81 try {
82 writer.close();
83 } catch (TripleHandlerException e) {
84 throw new ExtractionException("Error while closing the triple handler", e);
85 }
86 return out.toString("utf-8");
87 }
88
89 private boolean isBlindExtractor() {
90 return factory.createExtractor() instanceof Extractor.BlindExtractor;
91 }
92
93 }