Michael Yang
2025-05-07 5afa5041ea166f462a90a996d5264c64837f4341
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package tech.aiflowy.common.ai;
 
import com.agentsflex.core.document.DocumentParser;
import com.agentsflex.core.document.parser.DefaultTextDocumentParser;
import com.agentsflex.document.parser.PdfBoxDocumentParser;
import com.agentsflex.document.parser.PoiDocumentParser;
 
public class DocumentParserFactory {
 
    public static DocumentParser getDocumentParser(String typeOrFileName) {
        if (typeOrFileName == null) {
            throw new NullPointerException("typeOrFileName can not be null");
        } else {
            typeOrFileName = typeOrFileName.trim().toLowerCase();
        }
        if (typeOrFileName.endsWith(".pdf")) {
            return new PdfBoxDocumentParser();
        }
        if (typeOrFileName.endsWith(".docx")) {
            return new PoiDocumentParser();
        }
        if (typeOrFileName.endsWith(".txt")) {
            return new DefaultTextDocumentParser();
        }
        if (typeOrFileName.endsWith(".md")) {
            return new MarkdownDocumentParser();
        }
        return null;
    }
}