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

com.github.kunalk16.excel.factory.WorkBookFactory Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.github.kunalk16.excel.factory;

import com.github.kunalk16.excel.factory.extractor.SheetByNameExtractor;
import com.github.kunalk16.excel.factory.extractor.SheetNamesExtractor;
import com.github.kunalk16.excel.file.validation.ExcelFileValidation;
import com.github.kunalk16.excel.model.factory.ExcelArchiveFiles;
import com.github.kunalk16.excel.model.factory.ExcelWorkBook;
import com.github.kunalk16.excel.model.factory.ExcelXMLData;
import com.github.kunalk16.excel.model.user.Sheet;
import com.github.kunalk16.excel.model.user.WorkBook;
import com.github.kunalk16.excel.utils.logger.ExcelReaderLogger;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

public class WorkBookFactory {
    public static WorkBook create(String filePath) {
        if (ExcelFileValidation.validate(filePath)) {
            return getWorkBookData(new File(filePath));
        }
        return null;
    }

    private static WorkBook getWorkBookData(File excelFile) {
        try (ExcelArchiveFiles excelArchiveFiles = ArchiveFilesFactory.create(excelFile)) {
            ExcelXMLData excelXMLData = XMLDataFactory.create(excelArchiveFiles);

            return getWorkBookData(excelXMLData);
        } catch (Exception exception) {
            ExcelReaderLogger.getInstance().severe("Excel Resources not handles properly!" + exception.getLocalizedMessage());
            return null;
        }
    }

    private static WorkBook getWorkBookData(ExcelXMLData excelXMLData) {
        return Optional.ofNullable(excelXMLData)
                .map(WorkBookFactory::getSheets)
                .map(new SheetByNameExtractor())
                .map(ExcelWorkBook::new)
                .orElse(null);
    }

    private static List getSheets(ExcelXMLData excelXMLData) {
        List sheetNames = new SheetNamesExtractor().apply(excelXMLData.getWorkBook());
        List sheets = new ArrayList<>();

        IntStream.range(0, sheetNames.size())
                .mapToObj(index -> SheetFactory.create(excelXMLData.getSharedString(),
                        excelXMLData.getSheets().get(index), sheetNames.get(index)))
                .forEach(sheets::add);

        return sheets;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy