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 ExcelExtractor()); 040 register(new HtmlExtractor()); 041 register(new PlainTextExtractor()); 042 } 043 044 /** 045 * 注册一个 Extractor 046 */ 047 public synchronized void register(FileExtractor extractor) { 048 Objects.requireNonNull(extractor, "Extractor cannot be null"); 049 extractors.add(extractor); 050 } 051 052 /** 053 * 批量注册 054 */ 055 public void registerAll(List<FileExtractor> extractors) { 056 extractors.forEach(this::register); 057 } 058 059 060 public List<FileExtractor> findExtractors(DocumentSource source) { 061 return extractors.stream() 062 .filter(extractor -> extractor.supports(source)) 063 .sorted(FileExtractor.ORDER_COMPARATOR) 064 .collect(Collectors.toList()); 065 } 066 067 068}