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

com.github.kunalk16.excel.factory.builder.ExcelXMLDataBuilder Maven / Gradle / Ivy

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

import com.github.kunalk16.excel.model.factory.ExcelXMLData;
import com.github.kunalk16.excel.model.jaxb.sharedstrings.SharedStringType;
import com.github.kunalk16.excel.model.jaxb.workbook.WorkBookType;
import com.github.kunalk16.excel.model.jaxb.worksheet.WorkSheetType;
import com.github.kunalk16.excel.utils.jaxb.JAXBUtils;
import com.github.kunalk16.excel.utils.logger.ExcelReaderLogger;

import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ExcelXMLDataBuilder {
    private ZipEntry workBookZipEntry;

    private ZipEntry sharedStringsEntry;

    private List sheetEntries;

    private ZipFile excelZipFile;

    public ExcelXMLDataBuilder withExcelZipFile(ZipFile excelZipFile) {
        this.excelZipFile = excelZipFile;
        return this;
    }

    public ExcelXMLDataBuilder withWorkBookEntry(ZipEntry workBookEntry) {
        this.workBookZipEntry = workBookEntry;
        return this;
    }

    public ExcelXMLDataBuilder withSharedStringsEntry(ZipEntry sharedStringsEntry) {
        this.sharedStringsEntry = sharedStringsEntry;
        return this;
    }

    public ExcelXMLDataBuilder withSheetsEntry(List sheetEntries) {
        this.sheetEntries = sheetEntries;
        return this;
    }

    public ExcelXMLData build() {
        try {
            WorkBookType workBookType = null;
            if (Objects.nonNull(workBookZipEntry)) {
                workBookType = unMarshall(workBookZipEntry, WorkBookType.class);
            }

            SharedStringType sharedStringType = null;
            if (Objects.nonNull(sharedStringsEntry)) {
                sharedStringType = unMarshall(sharedStringsEntry, SharedStringType.class);
            }

            List sheetsTypes = new ArrayList<>();

            if (Objects.nonNull(sheetEntries) && !sheetEntries.isEmpty()) {
                for (ZipEntry sheetEntry : sheetEntries) {
                    sheetsTypes.add(unMarshall(sheetEntry, WorkSheetType.class));
                }
            }

            return new ExcelXMLData(workBookType, sharedStringType, sheetsTypes);
        } catch (Exception e) {
            ExcelReaderLogger.getInstance().severe("Could not unmarshall XML files " + e.getLocalizedMessage());
            return null;
        }
    }

    private  T unMarshall(ZipEntry zipEntry, Class clazz) throws IOException, JAXBException {
        try (InputStream inputStream = this.excelZipFile.getInputStream(zipEntry)) {
            return JAXBUtils.unMarshall(inputStream, clazz);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy