org.seasar.fisshplate.template.FPTemplate.java Source code

Java tutorial

Introduction

Here is the source code for org.seasar.fisshplate.template.FPTemplate.java

Source

/**
 * Copyright 2004-2010 the Seasar Foundation and the Others.
 *
 * 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.seasar.fisshplate.template;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.seasar.fisshplate.consts.FPConsts;
import org.seasar.fisshplate.context.FPContext;
import org.seasar.fisshplate.context.PageContext;
import org.seasar.fisshplate.core.element.Root;
import org.seasar.fisshplate.core.element.TemplateElement;
import org.seasar.fisshplate.core.parser.FPParser;
import org.seasar.fisshplate.core.parser.RowParser;
import org.seasar.fisshplate.exception.FPMergeException;
import org.seasar.fisshplate.exception.FPParseException;
import org.seasar.fisshplate.util.InputStreamUtil;
import org.seasar.fisshplate.wrapper.SheetWrapper;
import org.seasar.fisshplate.wrapper.WorkbookWrapper;

/**
 * FiSSH Plate?{@link HSSFWorkbook}????????
 *
 * @author rokugen
 * @author a-conv
 *
 */
public class FPTemplate {
    private FPParser parser = new FPParser();

    public FPTemplate() {

    }

    /**
     * ???{@link TemplateElement}??{@link RowParser}???
     * @param rowParser 
     */
    public void addRowParser(RowParser rowParser) {
        parser.addRowParser(rowParser);
    }

    /**
     * @param templateName
     *            ??
     * @param data
     *            ??
     * @return ?????{@link HSSFWorkbook}
     * @throws FPParseException ?????????
     * @throws FPMergeException ??????????
     * @throws IOException
     *             IO????????
     */
    public HSSFWorkbook process(String templateName, Map<String, Object> data)
            throws FPParseException, FPMergeException, IOException {
        InputStream is = InputStreamUtil.getResourceAsStream(templateName);
        HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(is));
        InputStreamUtil.close(is);
        return process(workbook, data);
    }

    /**
     * @param is
     *            ?{@link InputStream}
     * @param data
     *            ??
     * @return ?????{@link HSSFWorkbook}
     * @throws FPParseException ?????????
     * @throws FPMergeException ??????????
     * @throws IOException
     *             IO????????
     */
    public HSSFWorkbook process(InputStream is, Map<String, Object> data)
            throws FPParseException, FPMergeException, IOException {
        return process(new HSSFWorkbook(new POIFSFileSystem(is)), data);
    }

    /**
     * ???????{@link HSSFWorkbook}??????
     *
     * @param hssfWorkbook
     *            {@link HSSFWorkbook}
     * @param data
     *            ??
     * @return ?????{@link HSSFWorkbook}
     * @throws FPParseException ?????????
     * @throws FPMergeException ??????????
     */
    public <T extends Workbook> T process(T ssWorkbook, Map<String, Object> data)
            throws FPParseException, FPMergeException {
        WorkbookWrapper workbook = new WorkbookWrapper(ssWorkbook);
        for (int i = 0; i < workbook.getSheetCount(); i++) {
            SheetWrapper sheet = workbook.getSheetAt(i);
            if (sheet.getRowCount() < 1) {
                continue;
            }
            Root root = parser.parse(sheet);

            sheet.prepareForMerge();
            if (data == null) {
                data = new HashMap<String, Object>();
            }
            FPContext context = new FPContext(sheet.getHSSFSheet(), data);
            // ?
            PageContext pageContext = new PageContext();
            data.put(FPConsts.PAGE_CONTEXT_NAME, pageContext);

            root.merge(context);
        }

        return ssWorkbook;
    }

}