package net.hjuergens.freemarker.models;

import freemarker.template.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Iterator;

/**
 * created on 29.09.15
 *
 * @author juergens
 */
class POIFileTemplateModel
        extends WrappingTemplateModel
        implements TemplateCollectionModelEx, TemplateHashModel, AdapterTemplateModel {

    private final File filename;

    public POIFileTemplateModel(File workbookFile, ObjectWrapper ow) {
        super(ow);  // coming from WrappingTemplateModel
        this.filename = workbookFile;
    }

    @Override  // coming from AdapterTemplateModel
    public Object getAdaptedObject(Class hint) {
        return filename;
    }

    @Override
    public TemplateModel get(String s) throws TemplateModelException {
        if (s.equals("workbook")) {
            try {
                return wrap(new XSSFWorkbook(filename));
            } catch (IOException ex) {
                throw new TemplateModelException(ex);
            } catch (InvalidFormatException ex) {
                throw new TemplateModelException(ex);
            }
        } else if (s.equals("name")) {
            return wrap(filename);
        } else {
            return null;//throw new TemplateModelException("unknown " + s);
        }
    }

    @Override
    public boolean isEmpty() throws TemplateModelException {
        return filename.toPath().getNameCount() > 0;
    }

    @Override
    public boolean contains(TemplateModel tm) throws TemplateModelException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public TemplateModelIterator iterator() throws TemplateModelException {
        final Iterator<Path> entries = filename.toPath().iterator();
        return new TemplateModelIterator() {
            public TemplateModel next() throws TemplateModelException {
                return wrap(entries.next());
            }

            public boolean hasNext() throws TemplateModelException {
                return entries.hasNext();
            }
        };
    }

    @Override
    public int size() throws TemplateModelException {
        return filename.toPath().getNameCount();
    }
}

@@ -286,16 +286,228 @@
 terator;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;%0Aimport org.apache.poi.EncryptedDocumentException;%0Aimport org.apache.poi.ss.usermodel.Workbook;%0Aimport org.apache.poi.ss.usermodel.WorkbookFactory;
 %0A%0A/**%0A *
@@ -1184,68 +1184,119 @@
     
-try %7B%0A                return wrap(new XSSFWorkbook(filename)
+Workbook workbook = null;%0A            try %7B%0A                workbook = WorkbookFactory.create(filename, s, true
 );%0A 
@@ -1483,32 +1483,32 @@
 lException(ex);%0A
-
             %7D%0A  
@@ -1496,32 +1496,175 @@
 );%0A            %7D
+ catch (EncryptedDocumentException ex) %7B%0A                throw new TemplateModelException(ex);%0A            %7D%0A            return wrap(workbook);
 %0A        %7D else 

use poi.WorkbookFactory