org.bbreak.excella.core.SheetParser.java Source code

Java tutorial

Introduction

Here is the source code for org.bbreak.excella.core.SheetParser.java

Source

/*************************************************************************
 *
 * Copyright 2009 by bBreak Systems.
 *
 * ExCella Core - ExcelJava?????
 *
 * $Id: SheetParser.java 158 2013-03-15 04:19:39Z kamisono_bb $
 * $Revision: 158 $
 *
 * This file is part of ExCella Core.
 *
 * ExCella Core is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * ExCella Core is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the COPYING.LESSER file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with ExCella Core.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0-standalone.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
package org.bbreak.excella.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.bbreak.excella.core.exception.ParseException;
import org.bbreak.excella.core.tag.TagParser;
import org.bbreak.excella.core.util.PoiUtil;
import org.bbreak.excella.core.util.TagUtil;

/**
 * ???
 * 
 * ?????????????
 * ?SheetData??
 * 
 * ???([1,A] [1,B]?[2,A] [2,B])??
 * 
 * ?LastTag=True???????????????
 * 
 * @since 1.0
 */
public class SheetParser {

    /** ? */
    protected static final String PARAM_RESULT_KEY = "ResultKey";

    /**  */
    protected static final String PARAM_LAST_TAG = "LastTag";

    /**  */
    private static Log log = LogFactory.getLog(SheetParser.class);

    /** ? */
    private List<TagParser<?>> tagParsers = new ArrayList<TagParser<?>>();

    /**
     * ??
     * 
     * @param sheet ?
     * @param data BookController?parseBook(), parseSheet()?
     *              SheetParser?parseSheet?????
     *              TagParser??????
     * @return ??
     * @throws ParseException ??????Throw?
     */
    public SheetData parseSheet(Sheet sheet, Object data) throws ParseException {
        // ??
        String sheetName = PoiUtil.getSheetName(sheet);
        SheetData sheetData = new SheetData(sheetName);

        int firstRowNum = sheet.getFirstRowNum();

        // ?
        for (int rowCnt = firstRowNum; rowCnt <= sheet.getLastRowNum(); rowCnt++) {
            // ??
            Row row = sheet.getRow(rowCnt);
            if (row == null) {
                continue;
            }
            if (row != null) {
                for (int columnIdx = 0; columnIdx < row.getLastCellNum(); columnIdx++) {
                    // ??
                    Cell cell = row.getCell(columnIdx);
                    if (cell == null) {
                        continue;
                    }
                    if (parseCell(sheet, data, sheetData, cell, row, columnIdx)) {
                        // ?
                        return sheetData;
                    }
                }
            }
        }

        return sheetData;
    }

    /**
     * ??
     * 
     * @param sheet ?
     * @param data ?
     * @param sheetData ?
     * @param cell ?
     * @param row ?
     * @param columnIdx ??
     * @return ??????
     * @throws ParseException ??????Throw?
     */
    private boolean parseCell(Sheet sheet, Object data, SheetData sheetData, Cell cell, Row row, int columnIdx)
            throws ParseException {
        for (TagParser<?> parser : tagParsers) {
            // ??
            if (parser.isParse(sheet, cell)) {
                String strCellValue = cell.getStringCellValue();
                Map<String, String> paramDef = TagUtil.getParams(strCellValue);
                // ?
                Object result = parser.parse(sheet, cell, data);

                // ??
                if (result != null) {
                    // ?ResultKey?????ResultKey?
                    // ???Tag???
                    String resultKey = parser.getTag();
                    if (paramDef.containsKey(PARAM_RESULT_KEY)) {
                        resultKey = paramDef.get(PARAM_RESULT_KEY);
                    }
                    sheetData.put(resultKey, result);
                }
                // ??
                if (log.isInfoEnabled()) {
                    StringBuilder resultBuf = new StringBuilder(strCellValue + "???:");
                    if (result instanceof Map) {
                        Map<?, ?> mapResult = (Map<?, ?>) result;
                        Set<?> keyset = mapResult.keySet();
                        for (Object key : keyset) {
                            resultBuf.append("[" + key + ":" + mapResult.get(key) + "]");
                        }
                    } else if (result instanceof Collection) {
                        Collection<?> listResult = (Collection<?>) result;
                        resultBuf.append(listResult.getClass() + " Size=" + listResult.size());
                    } else {
                        resultBuf.append(result);
                    }
                    log.info(resultBuf.toString());
                }

                // ??????????????

                if (paramDef.containsKey(PARAM_LAST_TAG)) {
                    String strLastTag = paramDef.get(PARAM_LAST_TAG);
                    try {
                        if (Boolean.parseBoolean(strLastTag)) {
                            return true;
                        }
                    } catch (Exception e) {
                        throw new ParseException(cell);
                    }
                }

                cell = row.getCell(columnIdx);
                if (cell != null && cell.getCellTypeEnum() == CellType.STRING
                        && !strCellValue.equals(cell.getStringCellValue())) {
                    // ?????????????
                    if (parseCell(sheet, data, sheetData, cell, row, columnIdx)) {
                        // ?
                        return true;
                    }
                }
                break;
            }
        }
        return false;
    }

    /**
     * ?
     * 
     * @param tagParser ?TagParser
     */
    public void addTagParser(TagParser<?> tagParser) {
        tagParsers.add(tagParser);
    }

    /**
     * ??
     * 
     * @return ?????
     */
    public List<TagParser<?>> getTagParsers() {
        return tagParsers;
    }
}