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.mime.MIMEType;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
25
26
27
28
29 public class ExtractorGroup implements Iterable<ExtractorFactory<?>> {
30
31 private final Collection<ExtractorFactory<?>> factories;
32
33 public ExtractorGroup(Collection<ExtractorFactory<?>> factories) {
34 this.factories = factories;
35 }
36
37 public boolean isEmpty() {
38 return factories.isEmpty();
39 }
40
41 public int getNumOfExtractors() {
42 return factories.size();
43 }
44
45
46
47
48
49
50
51
52
53 public ExtractorGroup filterByMIMEType(MIMEType mimeType) {
54
55 Collection<ExtractorFactory<?>> matching = new ArrayList<>();
56 for (ExtractorFactory<?> factory : factories) {
57 if (supportsAllContentTypes(factory) || supports(factory, mimeType)) {
58 matching.add(factory);
59 }
60 }
61 return new ExtractorGroup(matching);
62 }
63
64 @Override
65 public Iterator<ExtractorFactory<?>> iterator() {
66 return factories.iterator();
67 }
68
69
70
71
72 public boolean allExtractorsSupportAllContentTypes() {
73 for (ExtractorFactory<?> factory : factories) {
74 if (!supportsAllContentTypes(factory))
75 return false;
76 }
77 return true;
78 }
79
80 private boolean supportsAllContentTypes(ExtractorFactory<?> factory) {
81 return factory.getSupportedMIMETypes().contains("*/*");
82 }
83
84 private boolean supports(ExtractorFactory<?> factory, MIMEType mimeType) {
85 for (MIMEType supported : factory.getSupportedMIMETypes()) {
86 if (supported.isAnyMajorType())
87 return true;
88 if (supported.isAnySubtype() && supported.getMajorType().equals(mimeType.getMajorType()))
89 return true;
90 if (supported.getFullType().equals(mimeType.getFullType()))
91 return true;
92 }
93 return false;
94 }
95
96 }