1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23;
19
20 import org.apache.any23.configuration.Configuration;
21 import org.apache.any23.configuration.DefaultConfiguration;
22 import org.apache.any23.extractor.ExtractionException;
23 import org.apache.any23.extractor.ExtractionParameters;
24 import org.apache.any23.extractor.ExtractorFactory;
25 import org.apache.any23.extractor.ExtractorGroup;
26 import org.apache.any23.extractor.ExtractorRegistryImpl;
27 import org.apache.any23.extractor.SingleDocumentExtraction;
28 import org.apache.any23.extractor.SingleDocumentExtractionReport;
29 import org.apache.any23.http.AcceptHeaderBuilder;
30 import org.apache.any23.http.DefaultHTTPClient;
31 import org.apache.any23.http.DefaultHTTPClientConfiguration;
32 import org.apache.any23.http.HTTPClient;
33 import org.apache.any23.mime.MIMEType;
34 import org.apache.any23.mime.MIMETypeDetector;
35 import org.apache.any23.mime.TikaMIMETypeDetector;
36 import org.apache.any23.mime.purifier.WhiteSpacesPurifier;
37 import org.apache.any23.source.DocumentSource;
38 import org.apache.any23.source.FileDocumentSource;
39 import org.apache.any23.source.HTTPDocumentSource;
40 import org.apache.any23.source.LocalCopyFactory;
41 import org.apache.any23.source.MemCopyFactory;
42 import org.apache.any23.source.StringDocumentSource;
43 import org.apache.any23.writer.TripleHandler;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import java.io.File;
48 import java.io.IOException;
49 import java.net.URI;
50 import java.net.URISyntaxException;
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.Collection;
54 import java.util.Locale;
55
56
57
58
59
60
61
62 public class Any23 {
63
64
65
66
67 public static final String VERSION = DefaultConfiguration.singleton().getPropertyOrFail("any23.core.version");
68
69
70
71
72 public static final String DEFAULT_HTTP_CLIENT_USER_AGENT = DefaultConfiguration.singleton()
73 .getPropertyOrFail("any23.http.user.agent.default");
74
75 protected static final Logger logger = LoggerFactory.getLogger(Any23.class);
76
77 private final Configuration configuration;
78 private final String defaultUserAgent;
79
80 private MIMETypeDetector mimeTypeDetector = new TikaMIMETypeDetector(new WhiteSpacesPurifier());
81
82 private HTTPClient httpClient = new DefaultHTTPClient();
83
84 private boolean httpClientInitialized = false;
85
86 private final ExtractorGroup factories;
87 private LocalCopyFactory streamCache;
88 private String userAgent;
89
90
91
92
93
94
95
96
97
98 public Any23(Configuration configuration, ExtractorGroup extractorGroup) {
99 if (configuration == null)
100 throw new NullPointerException("configuration must be not null.");
101 this.configuration = configuration;
102 if (logger.isDebugEnabled()) {
103 logger.debug(configuration.getConfigurationDump());
104 }
105
106 this.defaultUserAgent = configuration.getPropertyOrFail("any23.http.user.agent.default");
107
108 this.factories = (extractorGroup == null) ? ExtractorRegistryImpl.getInstance().getExtractorGroup()
109 : extractorGroup;
110 setCacheFactory(new MemCopyFactory());
111 }
112
113
114
115
116
117
118
119 public Any23(ExtractorGroup extractorGroup) {
120 this(DefaultConfiguration.singleton(), extractorGroup);
121 }
122
123
124
125
126
127
128
129
130
131 public Any23(Configuration configuration, String... extractorNames) {
132 this(configuration, extractorNames == null ? null
133 : ExtractorRegistryImpl.getInstance().getExtractorGroup(Arrays.asList(extractorNames)));
134 }
135
136
137
138
139
140
141
142 public Any23(String... extractorNames) {
143 this(DefaultConfiguration.singleton(), extractorNames);
144 }
145
146
147
148
149
150
151
152 public Any23(Configuration configuration) {
153 this(configuration, (String[]) null);
154 }
155
156
157
158
159 public Any23() {
160 this(DefaultConfiguration.singleton());
161 }
162
163
164
165
166
167
168
169 public void setHTTPUserAgent(String userAgent) {
170 if (httpClientInitialized) {
171 throw new IllegalStateException("Cannot change HTTP configuration after client has been initialized");
172 }
173 if (userAgent == null) {
174 userAgent = defaultUserAgent;
175 }
176 if (userAgent.trim().length() == 0) {
177 throw new IllegalArgumentException(String.format(Locale.ROOT, "Invalid user agent: '%s'", userAgent));
178 }
179 this.userAgent = userAgent;
180 }
181
182
183
184
185
186
187 public String getHTTPUserAgent() {
188 return this.userAgent;
189 }
190
191
192
193
194
195
196
197
198
199
200
201 public void setHTTPClient(HTTPClient httpClient) {
202 if (httpClient == null) {
203 throw new NullPointerException("httpClient cannot be null.");
204 }
205 if (httpClientInitialized) {
206 throw new IllegalStateException("Cannot change HTTP configuration after client has been initialized");
207 }
208 this.httpClient = httpClient;
209 }
210
211
212
213
214
215
216
217
218
219 public HTTPClient getHTTPClient() throws IOException {
220 if (!httpClientInitialized) {
221 if (userAgent == null) {
222 throw new IOException("Must call " + Any23.class.getSimpleName()
223 + ".setHTTPUserAgent(String) before extracting from HTTP IRI");
224 }
225 httpClient.init(new DefaultHTTPClientConfiguration(this.getAcceptHeader()));
226 httpClientInitialized = true;
227 }
228 return httpClient;
229 }
230
231
232
233
234
235
236
237 public void setCacheFactory(LocalCopyFactory cache) {
238 if (cache == null) {
239 throw new NullPointerException("cache cannot be null.");
240 }
241 this.streamCache = cache;
242 }
243
244
245
246
247
248
249
250 public void setMIMETypeDetector(MIMETypeDetector detector) {
251 this.mimeTypeDetector = detector;
252 }
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 public DocumentSource createDocumentSource(String documentIRI) throws URISyntaxException, IOException {
274 if (documentIRI == null)
275 throw new NullPointerException("documentIRI cannot be null.");
276 if (documentIRI.toLowerCase(Locale.ROOT).startsWith("file:")) {
277 return new FileDocumentSource(new File(new URI(documentIRI)));
278 }
279 if (documentIRI.toLowerCase(Locale.ROOT).startsWith("http:")
280 || documentIRI.toLowerCase(Locale.ROOT).startsWith("https:")) {
281 return new HTTPDocumentSource(getHTTPClient(), documentIRI);
282 }
283 throw new IllegalArgumentException(String.format(Locale.ROOT,
284 "Unsupported protocol for document IRI: '%s' . " + "Check that document IRI contains a protocol.",
285 documentIRI));
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 public ExtractionReport extract(ExtractionParameters eps, DocumentSource in, TripleHandler outputHandler,
310 String encoding) throws IOException, ExtractionException {
311 final SingleDocumentExtractionaction.html#SingleDocumentExtraction">SingleDocumentExtraction ex = new SingleDocumentExtraction(configuration, in, factories, outputHandler);
312 ex.setMIMETypeDetector(mimeTypeDetector);
313 ex.setLocalCopyFactory(streamCache);
314 ex.setParserEncoding(encoding);
315 final SingleDocumentExtractionReport sder = ex.run(eps);
316 return new ExtractionReport(ex.getMatchingExtractors(), ex.getParserEncoding(), ex.getDetectedMIMEType(),
317 sder.getValidationReport(), sder.getExtractorToIssues());
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 public ExtractionReport extract(String in, String documentIRI, String contentType, String encoding,
344 TripleHandler outputHandler) throws IOException, ExtractionException {
345 return extract(new StringDocumentSource(in, documentIRI, contentType, encoding), outputHandler);
346 }
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 public ExtractionReport extract(String in, String documentIRI, TripleHandler outputHandler)
367 throws IOException, ExtractionException {
368 return extract(new StringDocumentSource(in, documentIRI), outputHandler);
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 public ExtractionReport extract(File file, TripleHandler outputHandler) throws IOException, ExtractionException {
388 return extract(new FileDocumentSource(file), outputHandler);
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410 public ExtractionReport extract(ExtractionParameters eps, String documentIRI, TripleHandler outputHandler)
411 throws IOException, ExtractionException {
412 try {
413 return extract(eps, createDocumentSource(documentIRI), outputHandler);
414 } catch (URISyntaxException ex) {
415 throw new ExtractionException("Error while extracting data from document IRI.", ex);
416 }
417 }
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436 public ExtractionReport extract(String documentIRI, TripleHandler outputHandler)
437 throws IOException, ExtractionException {
438 return extract((ExtractionParameters) null, documentIRI, outputHandler);
439 }
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460 public ExtractionReport extract(DocumentSource in, TripleHandler outputHandler, String encoding)
461 throws IOException, ExtractionException {
462 return extract(null, in, outputHandler, encoding);
463 }
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481 public ExtractionReport extract(DocumentSource in, TripleHandler outputHandler)
482 throws IOException, ExtractionException {
483 return extract(null, in, outputHandler, null);
484 }
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504 public ExtractionReport extract(ExtractionParameters eps, DocumentSource in, TripleHandler outputHandler)
505 throws IOException, ExtractionException {
506 return extract(eps, in, outputHandler, null);
507 }
508
509 private String getAcceptHeader() {
510 Collection<MIMEType> mimeTypes = new ArrayList<>();
511 for (ExtractorFactory<?> factory : factories) {
512 mimeTypes.addAll(factory.getSupportedMIMETypes());
513 }
514 return new AcceptHeaderBuilder(mimeTypes).getAcceptHeader();
515 }
516
517 }