|
| 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 | +package org.apache.tika.parser.enricher; |
| 18 | + |
| 19 | +import java.io.Serializable; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.apache.tika.mime.MediaType; |
| 28 | +import org.apache.tika.parser.ParseContext; |
| 29 | +import org.apache.tika.parser.Parser; |
| 30 | + |
| 31 | +/** |
| 32 | + * Media-type-keyed registry of content enrichers: ordinary {@link Parser}s that a container |
| 33 | + * parser <em>invokes</em> on bytes it has already parsed (OCR text for an image or a |
| 34 | + * rendered PDF page), rather than being dispatched to by the composite parser. Configured |
| 35 | + * as the top-level {@code "content-enrichers"} list, mirroring {@code "renderers"}. |
| 36 | + * <p> |
| 37 | + * Members advertise their <em>real</em> media types ({@code image/png}); legacy engines |
| 38 | + * still advertising the {@code image/ocr-*} pseudo-types are keyed under the real type, so |
| 39 | + * they are nameable here unmodified. An enricher does not compete with the parser |
| 40 | + * registered for the same type: that parser still runs and calls the enricher. |
| 41 | + * |
| 42 | + * @since Apache Tika 4.1 |
| 43 | + */ |
| 44 | +public class CompositeContentEnricher implements Serializable { |
| 45 | + |
| 46 | + private static final long serialVersionUID = 1L; |
| 47 | + |
| 48 | + private final Map<MediaType, List<Parser>> enricherMap; |
| 49 | + |
| 50 | + public CompositeContentEnricher(List<Parser> enrichers) { |
| 51 | + Map<MediaType, List<Parser>> tmp = new HashMap<>(); |
| 52 | + ParseContext empty = new ParseContext(); |
| 53 | + for (Parser enricher : enrichers) { |
| 54 | + for (MediaType mediaType : enricher.getSupportedTypes(empty)) { |
| 55 | + // legacy engines advertise image/ocr-*; key under the real type |
| 56 | + MediaType keyType = stripLegacyOcrPrefix(mediaType.getBaseType()); |
| 57 | + List<Parser> forType = tmp.computeIfAbsent(keyType, k -> new ArrayList<>()); |
| 58 | + if (!forType.contains(enricher)) { |
| 59 | + forType.add(enricher); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + tmp.replaceAll((k, v) -> Collections.unmodifiableList(v)); |
| 64 | + this.enricherMap = Collections.unmodifiableMap(tmp); |
| 65 | + } |
| 66 | + |
| 67 | + private static MediaType stripLegacyOcrPrefix(MediaType mediaType) { |
| 68 | + String subtype = mediaType.getSubtype(); |
| 69 | + if (subtype.startsWith(LegacyDispatchEnricher.OCR_MEDIATYPE_PREFIX)) { |
| 70 | + return new MediaType(mediaType.getType(), |
| 71 | + subtype.substring(LegacyDispatchEnricher.OCR_MEDIATYPE_PREFIX.length())); |
| 72 | + } |
| 73 | + return mediaType; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return the enrichers for this media type in config order, empty when none; |
| 78 | + * parameters are ignored, alias normalization is the caller's job |
| 79 | + */ |
| 80 | + public List<Parser> getEnrichers(MediaType mediaType) { |
| 81 | + List<Parser> enrichers = enricherMap.get(mediaType.getBaseType()); |
| 82 | + return enrichers == null ? Collections.emptyList() : enrichers; |
| 83 | + } |
| 84 | + |
| 85 | + public Set<MediaType> getSupportedTypes() { |
| 86 | + return enricherMap.keySet(); |
| 87 | + } |
| 88 | +} |
0 commit comments