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

org.ttzero.excel.reader.BIFF8Drawings Maven / Gradle / Ivy

/*
 * Copyright (c) 2017-2021, [email protected] All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.ttzero.excel.reader;

import org.ttzero.excel.entity.e3.Block;
import org.ttzero.excel.entity.e3.odraw.ArtDggContainerParser;
import org.ttzero.excel.entity.e3.odraw.ArtSpContainerParser;
import org.ttzero.excel.util.FileUtil;
import org.ttzero.excel.util.StringUtil;

import java.io.Closeable;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author guanquan.wang at 2021-07-06 13:54
 */
public class BIFF8Drawings implements Drawings, Closeable {

    /**
     * Lazy to parse the Worksheet and Dimension info
     */
    public static class BIFF8Picture extends Picture {
        private Consumer consumer;

        @Override
        public Sheet getSheet() {
            if (sheet == null) {
                consumer.accept(this);
            }
            return sheet;
        }

        @Override
        public Dimension getDimension() {
            if (dimension == null) {
                consumer.accept(this);
            }
            return dimension;
        }

        @Override
        public String toString() {
            return background ? "Background picture [" + localPath + "]" + (sheet != null ? " in worksheet " + sheet.getName() : "") + (StringUtil.isNotEmpty(srcUrl) ? " from internet url " + srcUrl : "")
                : "Picture [" + localPath + "]" + (sheet != null ? " in worksheet " + sheet.getName() + " at " + dimension : "") + (StringUtil.isNotEmpty(srcUrl) ? " from internet url " + srcUrl : "");
        }
    }

    /**
     * Source
     */
    private List pictures;
    /**
     * Mark parse flag
     */
    private boolean parsed;
    /**
     * Sector block
     */
    private final Block block;

    private Sheet[] sheets;

    BIFF8Drawings(Block block, Sheet[] sheets) {
        this.block = block;
        this.sheets = sheets;
    }

    @Override
    public List listPictures() {
        if (!parsed) {
            parsed = true;
            List list = ArtDggContainerParser.get(block);
            if (list != null) {
                pictures = new ArrayList<>(list.size());
                for (Path path : list) {
                    BIFF8Drawings.BIFF8Picture pic = new BIFF8Drawings.BIFF8Picture();
                    pic.localPath = path;
                    pic.consumer = this::lazyParse;
                    pictures.add(pic);
                }
            }
        }
        return pictures;
    }

    /**
     * List all picture in specify worksheet
     *
     * @param sheet Specifies witch {@code Worksheet} to get the picture from
     * @return list of {@link Picture}, or null if not exists.
     */
    @Override
    public List listPictures(final Sheet sheet) {
        // Collect all drawings
        if (!parsed) listPictures();

        if (pictures == null) return null;

        List list = null;
        int n = 0;
        for (Picture p : pictures) {
            boolean notNull;
            if ((notNull = p.sheet != null) && p.sheet.getIndex() == sheet.getIndex()) {
                if (list == null) list = new ArrayList<>();
                list.add(p);
            } else if (!notNull) n++;
        }

        return list != null ? list : n > 0 ? lazyParse((BIFF8Sheet) sheet) : null;
    }

    // Lazy parse dimension in specify sheep
    private List lazyParse(BIFF8Sheet sheet) {
        List spContainers = sheet.stream.artDgContainer();
        if (spContainers != null) {
            List sub = new ArrayList<>(spContainers.size());
            for (ArtSpContainerParser.ArtSpContainer sp : spContainers) {
                // Bound check
                if (pictures.size() <= sp.fopte.op - 1) {
                    continue;
                }
                Picture p = pictures.get(sp.fopte.op - 1);
                p.sheet = sheet;
                p.dimension = sp.dimension;
                sub.add(p);
            }
            return sub.size() > 0 ? sub : null;
        }
        return null;
    }

    // Lazy parse dimension
    private void lazyParse(Picture picture) {
        for (Sheet sheet : sheets) {
            lazyParse((BIFF8Sheet) sheet);
        }
    }

    @Override
    public void close() {
        if (pictures != null && !pictures.isEmpty()) {
            for (Drawings.Picture p : pictures) {
                FileUtil.rm(p.localPath);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy