All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rrsunhome.excelsql.parser.AbstractFileParser Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package com.github.rrsunhome.excelsql.parser;

import com.github.rrsunhome.excelsql.config.CellMapping;
import com.github.rrsunhome.excelsql.config.BaseParserConfig;
import com.github.rrsunhome.excelsql.config.Range;
import com.github.rrsunhome.excelsql.parser.support.BaseRowResultSet;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : wangqijia
 * create at:  2021/10/27  下午1:47
 */
public abstract class AbstractFileParser implements FileParser {

    @Override
    public boolean canParseExtension(String fileExtension) {
        if (StringUtils.isEmpty(fileExtension)) {
            return false;
        }
        String[] extensions = getSupportedFileExtensions();
        for (String extension : extensions) {
            if (extension.equalsIgnoreCase(fileExtension)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public final List parse(InputStream is, BaseParserConfig parserConfig) throws Exception {
        return doParse(is, parserConfig);
    }


    private List doParse(InputStream is, BaseParserConfig parserConfig) throws Exception {
        List rowResultSets = load(is, parserConfig);
        if (CollectionUtils.isEmpty(rowResultSets)) {
            return new ArrayList<>(1);
        }

        List subRowResultSets = subList(rowResultSets, parserConfig);

        List rowDefinitions = new ArrayList<>(subRowResultSets.size());
        for (BaseRowResultSet rowResultSet : subRowResultSets) {

            boolean isFilterRow = isFilterRow(rowResultSet, parserConfig.getCellMappings());
            if (!isFilterRow) {
                continue;
            }
            rowDefinitions.add(mapRowDefinition(rowResultSet, parserConfig.getCellMappings()));
        }

        return rowDefinitions;
    }


    /**
     * 加载具体资源
     *
     * @param is           资源
     * @param parserConfig 解析配置
     * @return 解析结果
     * @throws Exception 如果解析异常,则抛出异常
     */
    protected abstract List load(InputStream is, BaseParserConfig parserConfig) throws Exception;


    private List subList(List rowResultSets, BaseParserConfig parserConfig) {
        int rowSize = rowResultSets.size();
        Range rowRange = parserConfig.getRowRange();
        int titleRowIndex = parserConfig.getTitleRowIndex();

        int startRowIndex = titleRowIndex == 0 ? rowRange.getStart() - 1 : rowRange.getStart() - titleRowIndex;
        int endRowIndex = titleRowIndex == 0 ? rowRange.getEnd() - 1 : rowRange.getEnd() - titleRowIndex;
        if (rowSize < startRowIndex || rowSize < endRowIndex) {
            return rowResultSets;
        }
        return rowResultSets.subList(startRowIndex, endRowIndex + 1);
    }


    private RowDefinition mapRowDefinition(BaseRowResultSet rowResultSet, List cellMappings) {
        RowDefinition rowDefinition = new RowDefinition(rowResultSet.getRowIndex());

        if (CollectionUtils.isEmpty(cellMappings)) {

            for (int i = 0; i < rowResultSet.getCellSize(); i++) {
                rowDefinition.addCellDefinition(i, rowResultSet.getString(i));
            }

        } else {

            int cellIndex = 0;
            for (CellMapping cellMapping : cellMappings) {
                String fieldValue = rowResultSet.getString(cellMapping.getCellNum());
                rowDefinition.addCellDefinition(cellIndex, cellMapping.convert(fieldValue));
                cellIndex++;
            }
        }


        return rowDefinition;
    }

    private boolean isFilterRow(BaseRowResultSet rowResultSet, List cellMappings) {
        if (CollectionUtils.isEmpty(cellMappings)) {
            return true;
        }

        boolean isFilterRow = true;
        for (CellMapping cellMapping : cellMappings) {
            String fieldValue = rowResultSet.getString(cellMapping.getCellNum());
            if (!cellMapping.filter(fieldValue)) {
                isFilterRow = false;
                break;
            }
        }
        return isFilterRow;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy