001/*
002 *  Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.agentsflex.core.file2text.extractor;
017
018
019import com.agentsflex.core.file2text.extractor.impl.*;
020import com.agentsflex.core.file2text.source.DocumentSource;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Objects;
025import java.util.stream.Collectors;
026
027/**
028 * Extractor 注册中心
029 */
030public class ExtractorRegistry {
031
032    private final List<FileExtractor> extractors = new ArrayList<>();
033
034    public ExtractorRegistry() {
035        register(new PdfTextExtractor());
036        register(new DocxExtractor());
037        register(new DocExtractor());
038        register(new PptxExtractor());
039        register(new HtmlExtractor());
040        register(new PlainTextExtractor());
041    }
042
043    /**
044     * 注册一个 Extractor
045     */
046    public synchronized void register(FileExtractor extractor) {
047        Objects.requireNonNull(extractor, "Extractor cannot be null");
048        extractors.add(extractor);
049    }
050
051    /**
052     * 批量注册
053     */
054    public void registerAll(List<FileExtractor> extractors) {
055        extractors.forEach(this::register);
056    }
057
058
059    public List<FileExtractor> findExtractors(DocumentSource source) {
060        return extractors.stream()
061            .filter(extractor -> extractor.supports(source))
062            .sorted(FileExtractor.ORDER_COMPARATOR)
063            .collect(Collectors.toList());
064    }
065
066
067}