| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ExtractorGroup |
|
| 2.75;2.75 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 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 | * It simple models a group of {@link ExtractorFactory} providing | |
| 28 | * simple accessing methods. | |
| 29 | */ | |
| 30 | public class ExtractorGroup implements Iterable<ExtractorFactory<?>> { | |
| 31 | ||
| 32 | private final Collection<ExtractorFactory<?>> factories; | |
| 33 | ||
| 34 | 0 | public ExtractorGroup(Collection<ExtractorFactory<?>> factories) { |
| 35 | 0 | this.factories = factories; |
| 36 | 0 | } |
| 37 | ||
| 38 | public boolean isEmpty() { | |
| 39 | 0 | return factories.isEmpty(); |
| 40 | } | |
| 41 | ||
| 42 | public int getNumOfExtractors() { | |
| 43 | 0 | return factories.size(); |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * Returns a {@link ExtractorGroup} with a set of {@link Extractor} able to | |
| 48 | * process the provided mime type. | |
| 49 | * | |
| 50 | * @param mimeType to perform the selection. | |
| 51 | * @return an {@link ExtractorGroup} able to process the provided mime type. | |
| 52 | */ | |
| 53 | public ExtractorGroup filterByMIMEType(MIMEType mimeType) { | |
| 54 | // @@@ wildcards, q values | |
| 55 | 0 | Collection<ExtractorFactory<?>> matching = new ArrayList<ExtractorFactory<?>>(); |
| 56 | 0 | for (ExtractorFactory<?> factory : factories) { |
| 57 | 0 | if (supportsAllContentTypes(factory) || supports(factory, mimeType)) { |
| 58 | 0 | matching.add(factory); |
| 59 | } | |
| 60 | } | |
| 61 | 0 | return new ExtractorGroup(matching); |
| 62 | } | |
| 63 | ||
| 64 | public Iterator<ExtractorFactory<?>> iterator() { | |
| 65 | 0 | return factories.iterator(); |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * @return <code>true</code> if all the {@link Extractor} contained in the group | |
| 70 | * supports all the content types. | |
| 71 | */ | |
| 72 | public boolean allExtractorsSupportAllContentTypes() { | |
| 73 | 0 | for (ExtractorFactory<?> factory : factories) { |
| 74 | 0 | if (!supportsAllContentTypes(factory)) return false; |
| 75 | } | |
| 76 | 0 | return true; |
| 77 | } | |
| 78 | ||
| 79 | private boolean supportsAllContentTypes(ExtractorFactory<?> factory) { | |
| 80 | 0 | return factory.getSupportedMIMETypes().contains("*/*"); |
| 81 | } | |
| 82 | ||
| 83 | private boolean supports(ExtractorFactory<?> factory, MIMEType mimeType) { | |
| 84 | 0 | for (MIMEType supported : factory.getSupportedMIMETypes()) { |
| 85 | 0 | if (supported.isAnyMajorType()) return true; |
| 86 | 0 | if (supported.isAnySubtype() && supported.getMajorType().equals(mimeType.getMajorType())) return true; |
| 87 | 0 | if (supported.getFullType().equals(mimeType.getFullType())) return true; |
| 88 | } | |
| 89 | 0 | return false; |
| 90 | } | |
| 91 | ||
| 92 | } |