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

com.poiji.bind.mapping.XSSFUnmarshallerFile Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package com.poiji.bind.mapping;

import com.poiji.bind.PoijiFile;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import java.io.IOException;
import java.util.function.Consumer;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.xml.sax.SAXException;

import static org.apache.poi.openxml4j.opc.PackageAccess.READ;
import static org.apache.poi.openxml4j.opc.PackageAccess.READ_WRITE;

/**
 * Created by hakan on 22/10/2017
 */
final class XSSFUnmarshallerFile extends XSSFUnmarshaller {

    private final PoijiFile poijiFile;

    XSSFUnmarshallerFile(PoijiFile poijiFile, PoijiOptions options) {
        super(options);
        this.poijiFile = poijiFile;
    }

    @Override
    public  void unmarshal(Class type, Consumer consumer) {

        if (options.getPassword() != null) {
            returnFromEncryptedFile(type, consumer);
            return;
        }
        returnFromExcelFile(type,consumer);
    }

    public  void returnFromExcelFile(Class type, Consumer consumer) {
        final PackageAccess packageAccess = options.getTransposed() ? READ_WRITE : READ;

        try (OPCPackage open = OPCPackage.open(poijiFile.file(), packageAccess)) {

            unmarshal0(type, consumer, open);

        } catch (ParserConfigurationException | SAXException | IOException | OpenXML4JException e) {
            throw new PoijiException("Problem occurred while reading data", e);
        }
    }

    public  void returnFromEncryptedFile(Class type, Consumer consumer) {

        try (POIFSFileSystem fs = new POIFSFileSystem(poijiFile.file(), true)) {

            listOfEncryptedItems(type, consumer, fs);

        } catch (IOException e) {
            throw new PoijiException("Problem occurred while reading data", e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy