com.github.rrsunhome.excelsql.parser.FileParserManager Maven / Gradle / Ivy
package com.github.rrsunhome.excelsql.parser;
import java.util.ArrayList;
import java.util.List;
/**
* @author : wangqijia
* create at: 2021/11/8 下午8:16
*/
public class FileParserManager {
private List fileParsers = new ArrayList<>();
public FileParserManager() {
addDefaultFileParsers();
}
public void registerFileParser(FileParser fileParser) {
fileParsers.add(fileParser);
}
public FileParser findFileParser(String fileExtension) {
for (FileParser fileParser : fileParsers) {
if (fileParser.canParseExtension(fileExtension)) {
return fileParser;
}
}
throw new RuntimeException(String.format("无法找到 %s 对应的解析器", fileExtension));
}
private void addDefaultFileParsers() {
registerFileParser(new ExcelFileParser());
registerFileParser(new CsvFileParser());
registerFileParser(new TextFileParser());
}
}