Example usage for org.apache.poi.ss.usermodel Cell getRowIndex

List of usage examples for org.apache.poi.ss.usermodel Cell getRowIndex

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getRowIndex.

Prototype

int getRowIndex();

Source Link

Document

Returns row index of a row in the sheet that contains this cell

Usage

From source file:org.bbreak.excella.core.tag.excel2java.MapsParser.java

License:Open Source License

/**
 * ?//from www .j  a  v  a2  s. c om
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 * SheetParser?parseSheet?????<BR>
 * TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public List<Map<?, ?>> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    List<Map<?, ?>> resultList = new ArrayList<Map<?, ?>>();

    // 
    int tagRowIdx = tagCell.getRowIndex();
    // 
    int keyRowIdx;
    // 
    int valueRowFromIdx;
    // 
    int valueRowToIdx = sheet.getLastRowNum();

    try {
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        keyRowIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_KEY_ROW, DEFAULT_KEY_ROW_ADJUST);
        if (keyRowIdx < 0 || keyRowIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_KEY_ROW);
        }

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    // ??
    List<Integer> targetColNums = new ArrayList<Integer>();
    Row keyRow = sheet.getRow(keyRowIdx);
    if (keyRow == null) {
        // ?null??
        return resultList;
    }
    int firstCellNum = keyRow.getFirstCellNum();
    int lastCellNum = keyRow.getLastCellNum();
    for (int cellCnt = firstCellNum; cellCnt < lastCellNum; cellCnt++) {
        Cell cell = keyRow.getCell(cellCnt);
        Object cellValue = PoiUtil.getCellValue(cell);
        if (cellValue instanceof String) {
            String keyName = (String) cellValue;
            if (keyName.startsWith(BookController.COMMENT_PREFIX)) {
                continue;
            }
        }
        if (cellValue != null) {
            targetColNums.add(cellCnt);
        }
    }

    if (targetColNums.size() > 0) {
        // ????

        // ??
        for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {
            Row dataRow = sheet.getRow(rowCnt);
            if (dataRow == null) {
                continue;
            }
            Map<Object, Object> map = new LinkedHashMap<Object, Object>();
            for (Integer colCnt : targetColNums) {
                Cell keyCell = keyRow.getCell(colCnt);
                Cell valueCell = dataRow.getCell(colCnt);

                Object key = PoiUtil.getCellValue(keyCell);
                Object value = PoiUtil.getCellValue(valueCell);

                map.put(key, value);
            }
            resultList.add(map);
        }
    }

    return resultList;
}

From source file:org.bbreak.excella.core.tag.excel2java.ObjectsParser.java

License:Open Source License

/**
 * ?//  w  ww  .jav  a  2 s  .  co m
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 *              SheetParser?parseSheet?????<BR>
 *              TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public List<Object> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    List<Object> resultList = new ArrayList<Object>();
    Class<?> clazz = null;

    // 
    int tagRowIdx = tagCell.getRowIndex();
    // 
    int propertyRowIdx;
    // 
    int valueRowFromIdx;
    // 
    int valueRowToIdx = sheet.getLastRowNum();

    try {
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        clazz = Class.forName(paramDef.get(PARAM_CLASS));

        // ?
        propertyRowIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_PROPERTY_ROW,
                DEFAULT_PROPERTY_ROW_ADJUST);
        if (propertyRowIdx < 0 || propertyRowIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_PROPERTY_ROW);
        }

        // ?
        valueRowFromIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_FROM,
                DEFAULT_VALUE_ROW_FROM_ADJUST);
        if (valueRowFromIdx < 0 || valueRowFromIdx > sheet.getLastRowNum()) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_FROM);
        }

        // ?
        valueRowToIdx = TagUtil.adjustValue(tagRowIdx, paramDef, PARAM_DATA_ROW_TO, valueRowToIdx - tagRowIdx);
        if (valueRowToIdx > sheet.getLastRowNum() || valueRowToIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_DATA_ROW_TO);
        }

        // ???
        if (valueRowFromIdx > valueRowToIdx) {
            throw new ParseException(tagCell,
                    "?" + PARAM_DATA_ROW_FROM + "," + PARAM_DATA_ROW_TO);
        }

    } catch (Exception e) {
        if (e instanceof ParseException) {
            throw (ParseException) e;
        } else {
            throw new ParseException(tagCell, e);
        }
    }

    // ???
    Map<Integer, Class<?>> propertyClassMap = new HashMap<Integer, Class<?>>();
    // ????
    Map<Integer, String> propertyNameMap = new HashMap<Integer, String>();
    // ??
    Map<String, List<ObjectsPropertyParser>> customPropertyParserMap = new HashMap<String, List<ObjectsPropertyParser>>();

    // ??
    List<Integer> targetColNums = new ArrayList<Integer>();
    Row propertyRow = sheet.getRow(propertyRowIdx);
    if (propertyRow == null) {
        // ?null??
        return resultList;
    }
    int firstCellNum = propertyRow.getFirstCellNum();
    int lastCellNum = propertyRow.getLastCellNum();
    for (int cellCnt = firstCellNum; cellCnt < lastCellNum; cellCnt++) {
        Cell cell = propertyRow.getCell(cellCnt);
        if (cell == null) {
            continue;
        }
        try {
            String propertyName = cell.getStringCellValue();
            if (propertyName.startsWith(BookController.COMMENT_PREFIX)) {
                continue;
            }

            Object obj = clazz.newInstance();
            Class<?> propertyClass = PropertyUtils.getPropertyType(obj, propertyName);
            if (propertyClass != null) {
                propertyClassMap.put(cellCnt, propertyClass);
                propertyNameMap.put(cellCnt, propertyName);
                targetColNums.add(cellCnt);
            } else {
                // ????
                for (ObjectsPropertyParser parser : customPropertyParsers) {
                    if (parser.isParse(sheet, cell)) {
                        List<ObjectsPropertyParser> propertyParsers = customPropertyParserMap.get(propertyName);
                        if (propertyParsers == null) {
                            propertyParsers = new ArrayList<ObjectsPropertyParser>();
                        }
                        // ???????
                        if (!propertyParsers.contains(parser)) {
                            propertyParsers.add(parser);
                        }
                        customPropertyParserMap.put(propertyName, propertyParsers);

                        if (!targetColNums.contains(cellCnt)) {
                            propertyNameMap.put(cellCnt, propertyName);
                            targetColNums.add(cellCnt);
                        }
                    }
                }
            }

        } catch (Exception e) {
            throw new ParseException(cell, e);
        }
    }

    if (targetColNums.size() > 0) {
        // ????

        // ??
        for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {
            Row dataRow = sheet.getRow(rowCnt);
            if (dataRow == null) {
                continue;
            }
            Object obj;
            try {
                obj = clazz.newInstance();
                for (Integer colCnt : targetColNums) {
                    Cell cell = dataRow.getCell(colCnt);

                    try {
                        Class<?> propertyClass = propertyClassMap.get(colCnt);
                        String propertyName = propertyNameMap.get(colCnt);
                        // ?
                        if (customPropertyParserMap.containsKey(propertyName)) {
                            List<ObjectsPropertyParser> propertyParsers = customPropertyParserMap
                                    .get(propertyName);
                            Map<String, String> params = TagUtil.getParams(propertyName);
                            Object cellValue = PoiUtil.getCellValue(cell);

                            // ??
                            for (ObjectsPropertyParser propertyParser : propertyParsers) {
                                propertyParser.parse(obj, cellValue, TagUtil.getTag(propertyName), params);
                            }
                        } else {
                            Object value = null;
                            if (cell != null) {
                                value = PoiUtil.getCellValue(cell, propertyClass);
                            }
                            PropertyUtils.setProperty(obj, propertyName, value);
                        }
                    } catch (Exception e) {
                        throw new ParseException(cell, e);
                    }
                }
            } catch (Exception e) {
                if (e instanceof ParseException) {
                    throw (ParseException) e;
                } else {
                    throw new ParseException(tagCell, e);
                }
            }
            resultList.add(obj);
        }
    }
    return resultList;
}

From source file:org.bbreak.excella.core.test.util.TestUtil.java

License:Open Source License

public static void checkCell(Cell expected, Cell actual) throws CheckException {

    List<CheckMessage> errors = new ArrayList<CheckMessage>();

    // ----------------------
    // ????//from   w ww  . j  a  v  a  2s.c  o  m
    // ----------------------

    if (expected == null && actual == null) {
        return;
    }

    if (expected == null) {
        errors.add(new CheckMessage("(" + actual.getRowIndex() + "," + actual.getColumnIndex() + ")",
                null, actual.toString()));
        throw new CheckException(errors);
    }
    if (actual == null) {
        errors.add(new CheckMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")",
                expected.toString(), null));
        throw new CheckException(errors);
    }

    // 
    if (expected.getCellTypeEnum() != actual.getCellTypeEnum()) {
        errors.add(new CheckMessage(
                "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]",
                String.valueOf(expected.getCellTypeEnum()), String.valueOf(actual.getCellTypeEnum())));
        throw new CheckException(errors);
    }

    try {
        checkCellStyle(expected.getRow().getSheet().getWorkbook(), expected.getCellStyle(),
                actual.getRow().getSheet().getWorkbook(), actual.getCellStyle());
    } catch (CheckException e) {
        CheckMessage checkMessage = e.getCheckMessages().iterator().next();
        checkMessage.setMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")"
                + checkMessage.getMessage());
        errors.add(checkMessage);
        throw new CheckException(errors);
    }

    // 
    log.error("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")");
    if (!getCellValue(expected).equals(getCellValue(actual))) {
        log.error(getCellValue(expected) + " / " + getCellValue(actual));
        errors.add(new CheckMessage(
                "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]",
                String.valueOf(getCellValue(actual)), String.valueOf(getCellValue(expected))));
        throw new CheckException(errors);
    }

}

From source file:org.bbreak.excella.reports.ReportsTestUtil.java

License:Open Source License

/**
 * /*from  w ww .ja  v  a  2s. com*/
 * 
 * @param expected 
 * @param actual 
 * @throws ReportsCheckException 
 */
public static void checkCell(Cell expected, Cell actual) throws ReportsCheckException {

    List<CheckMessage> errors = new ArrayList<CheckMessage>();

    // ----------------------
    // ????
    // ----------------------

    if (expected == null && actual == null) {
        return;
    }

    if (expected == null) {
        // if(actual.getCellStyle() != null || actual.getCellType() != Cell.CELL_TYPE_BLANK){
        errors.add(new CheckMessage("(" + actual.getRowIndex() + "," + actual.getColumnIndex() + ")",
                null, actual.toString()));
        throw new ReportsCheckException(errors);
        // }
    }
    if (actual == null) {
        errors.add(new CheckMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")",
                expected.toString(), null));
        throw new ReportsCheckException(errors);
    }

    // 
    if (expected.getCellTypeEnum() != actual.getCellTypeEnum()) {
        errors.add(new CheckMessage(
                "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]",
                getCellTypeString(expected.getCellTypeEnum()), getCellTypeString(actual.getCellTypeEnum())));
        throw new ReportsCheckException(errors);
    }

    try {
        checkCellStyle(expected.getRow().getSheet().getWorkbook(), expected.getCellStyle(),
                actual.getRow().getSheet().getWorkbook(), actual.getCellStyle());
    } catch (ReportsCheckException e) {
        CheckMessage checkMessage = e.getCheckMessages().iterator().next();
        checkMessage.setMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")"
                + checkMessage.getMessage());
        errors.add(checkMessage);
        throw new ReportsCheckException(errors);
    }

    // 
    if (!getCellValue(expected).equals(getCellValue(actual))) {
        log.error(getCellValue(expected) + " / " + getCellValue(actual));
        errors.add(new CheckMessage(
                "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]",
                String.valueOf(getCellValue(expected)), String.valueOf(getCellValue(actual))));
        throw new ReportsCheckException(errors);
    }

}

From source file:org.bbreak.excella.reports.tag.BlockColRepeatParamParser.java

License:Open Source License

@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {
    try {/*from   w  w w  . j  a va 2s  .com*/
        // ??
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        checkParam(paramDef, tagCell);

        ReportsParserInfo info = (ReportsParserInfo) data;
        ParamInfo paramInfo = info.getParamInfo();

        // 
        ParsedReportInfo parsedReportInfo = new ParsedReportInfo();
        List<Object> resultList = new ArrayList<Object>();

        // BC?????
        String bcTagname = paramDef.get(PARAM_VALUE);
        if (log.isDebugEnabled()) {
            log.debug("BC?? : " + bcTagname);
        }

        // ?????
        Object[] paramInfos = getParamData(paramInfo, bcTagname);
        if (paramInfos == null) {
            return parsedReportInfo;
        }
        // ????
        List<SingleParamParser> singleParsers = getSingleReplaceParsers(info);

        // POJOParamInfo???
        List<ParamInfo> paramInfoList = new ArrayList<ParamInfo>();
        for (Object obj : paramInfos) {
            if (obj instanceof ParamInfo) {
                paramInfoList.add((ParamInfo) obj);
                continue;
            }
            ParamInfo childParamInfo = new ParamInfo();
            Map<String, Object> map = PropertyUtils.describe(obj);
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                for (ReportsTagParser<?> parser : singleParsers) {
                    childParamInfo.addParam(parser.getTag(), entry.getKey(), entry.getValue());
                }
            }
            paramInfoList.add(childParamInfo);
        }
        // ??
        if (paramDef.containsKey(PARAM_MIN_REPEAT_NUM)) {
            Integer minRepeatNum = Integer.valueOf(paramDef.get(PARAM_MIN_REPEAT_NUM));
            if (minRepeatNum > paramInfoList.size()) {
                int addEmptyRowNum = minRepeatNum - paramInfoList.size();
                for (int num = 0; num < addEmptyRowNum; num++) {
                    ParamInfo childParamInfo = new ParamInfo();
                    paramInfoList.add(childParamInfo);
                }
            }
        }
        paramInfos = paramInfoList.toArray(new ParamInfo[paramInfoList.size()]);

        // ?
        Integer repeatNum = paramInfos.length;
        if (paramDef.containsKey(PARAM_REPEAT)) {
            if (Integer.valueOf(paramDef.get(PARAM_REPEAT)) < repeatNum) {
                repeatNum = Integer.valueOf(paramDef.get(PARAM_REPEAT));
            }
        }

        // ?
        // TODO ?
        /*
         * Integer turnNum = null; if (paramDef.containsKey( PARAM_TURN)) { turnNum = Integer.valueOf( paramDef.get( PARAM_TURN)); }
         */

        // ??????????
        Map<String, Object> beforeBlockSingleDataMap = new HashMap<String, Object>();
        // ???
        if (paramDef.containsKey(PARAM_DUPLICATE)) {
            String[] tmp = paramDef.get(PARAM_DUPLICATE).split(";");
            // single???
            for (String str : tmp) {
                for (ReportsTagParser<?> parser : singleParsers) {
                    str = parser.getTag() + TAG_PARAM_PREFIX + str + TAG_PARAM_SUFFIX;
                    // ??key???
                    beforeBlockSingleDataMap.put(str, "");
                }
            }
        }

        // removeTag
        boolean removeTag = false;
        if (paramDef.containsKey(PARAM_REMOVE_TAG)) {
            removeTag = Boolean.valueOf(paramDef.get(PARAM_REMOVE_TAG));
        }

        // fromCell
        int[] fromCellPosition = ReportsUtil.getCellIndex(paramDef.get(PARAM_FROM), PARAM_FROM);
        int defaultFromCellRowIndex = tagCell.getRow().getRowNum() + fromCellPosition[0];
        int defaultFromCellColIndex = tagCell.getColumnIndex() + fromCellPosition[1];

        // toCell
        int[] toCellIndex = ReportsUtil.getCellIndex(paramDef.get(PARAM_TO), PARAM_TO);
        int defaultToCellRowIndex = tagCell.getRow().getRowNum() + toCellIndex[0];
        int defaultToCellColIndex = tagCell.getColumnIndex() + toCellIndex[1];

        // ?(??)
        int blockEndRowIndex = defaultToCellRowIndex;
        int blockEndColIndex = defaultToCellColIndex;
        int blockStartRowIndex = defaultFromCellRowIndex;
        int blockStartColIndex = defaultFromCellColIndex;

        // BC??Cell[row][col]
        Object[][] blockCellsValue = ReportsUtil.getBlockCellValue(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        CellStyle[][] blockCellsStyle = ReportsUtil.getBlockCellStyle(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        CellType[][] blockCellTypes = ReportsUtil.getBlockCellType(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        // ?????
        int[] columnWidths = ReportsUtil.getColumnWidth(sheet, defaultFromCellColIndex, defaultToCellColIndex);

        // ?
        int rowlen = defaultToCellRowIndex - defaultFromCellRowIndex + 1;
        int collen = defaultToCellColIndex - defaultFromCellColIndex + 1;

        // ???
        CellRangeAddress[] margedCells = ReportsUtil.getMargedCells(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);

        // ????
        int turnCount = 0;

        int maxblockEndRowIndex = blockEndRowIndex;

        TagParser<?> parser = null;

        ParsedReportInfo result = null;

        // 
        for (int repeatCount = 0; repeatCount < repeatNum; repeatCount++) {

            collen = defaultToCellColIndex - defaultFromCellColIndex + 1;

            // ??????blockStartRowIndex??blockEndColIndex?
            // TODO ?
            /*
             * if (turnNum != null && turnNum <= turnCount) { blockStartRowIndex = blockEndRowIndex + 1; blockEndColIndex = 0; turnCount = 0; }
             */

            // 
            if (turnCount > 0) {
                blockStartColIndex = blockEndColIndex + 1;
                blockEndColIndex = blockStartColIndex + collen - 1;
            } else {
                // ??????? 2009/11/16
                // blockStartColIndex = tagCell.getColumnIndex();
            }
            turnCount++;

            // ???BC
            if (repeatCount > 0) {
                CellRangeAddress rangeAddress = new CellRangeAddress(blockStartRowIndex, sheet.getLastRowNum(),
                        blockStartColIndex, blockStartColIndex + collen - 1);
                PoiUtil.insertRangeRight(sheet, rangeAddress);
                if (log.isDebugEnabled()) {
                    log.debug("");
                    log.debug(blockStartRowIndex + ":" + sheet.getLastRowNum() + ":" + blockStartColIndex + ":"
                            + (blockStartColIndex + collen - 1));
                }

                // ???
                // ???????????
                // ??????
                int targetColNum = blockEndColIndex - defaultToCellColIndex;
                for (CellRangeAddress address : margedCells) {
                    // ???(BC????????)
                    // ???(BC????????)
                    // ??? + BC?? - BC?
                    // ??? + BC?? - BC?
                    int firstRowNum = address.getFirstRow();
                    int lastRowNum = address.getLastRow();
                    int firstColumnNum = address.getFirstColumn() + targetColNum;
                    int lastColumnNum = address.getLastColumn() + targetColNum;

                    CellRangeAddress copyAddress = new CellRangeAddress(firstRowNum, lastRowNum, firstColumnNum,
                            lastColumnNum);
                    sheet.addMergedRegion(copyAddress);
                }

            }

            // ?
            if (log.isDebugEnabled()) {
                log.debug("?????? =" + repeatCount);
            }

            for (int rowIdx = 0; rowIdx < blockCellsValue.length; rowIdx++) {
                // ?
                // 
                int copyToRowIndex = blockStartRowIndex + rowIdx;
                Row row = sheet.getRow(copyToRowIndex);
                // ??null???????????????
                if (row == null && !ReportsUtil.isEmptyRow(blockCellTypes[rowIdx], blockCellsValue[rowIdx],
                        blockCellsStyle[rowIdx])) {
                    // ????
                    // ?
                    // (??)??????????
                    // ??null???(RowCreate?)???????????
                    row = sheet.createRow(copyToRowIndex);
                }

                if (row != null) {
                    // ?
                    for (int colIdx = 0; colIdx < blockCellsValue[rowIdx].length; colIdx++) {
                        // 
                        int copyToColIndex = blockStartColIndex + colIdx;
                        // ?
                        sheet.setColumnWidth(copyToColIndex, columnWidths[colIdx]);
                        // ?
                        Cell cell = row.getCell(copyToColIndex);
                        // ??
                        CellType cellType = blockCellTypes[rowIdx][colIdx];
                        // ??
                        Object cellValue = blockCellsValue[rowIdx][colIdx];
                        // ??
                        CellStyle cellStyle = blockCellsStyle[rowIdx][colIdx];
                        // ?????????????????
                        if (cell == null && !ReportsUtil.isEmptyCell(cellType, cellValue, cellStyle)) {
                            cell = row.createCell(copyToColIndex);
                        }

                        // 
                        if (cell != null) {
                            // ?
                            cell.setCellType(cellType);
                            // ??
                            PoiUtil.setCellValue(cell, cellValue);
                            // ??
                            if (cellStyle != null) {
                                cell.setCellStyle(cellStyle);
                            }
                            if (log.isDebugEnabled()) {
                                log.debug("row=" + (copyToRowIndex) + " col" + (copyToColIndex) + ">>>>>>"
                                        + blockCellsValue[rowIdx][colIdx]);
                            }
                        }
                    }
                }
            }

            // ?????????
            int plusRowNum = 0;
            // ?????????
            int plusColNum = 0;
            collen = defaultToCellColIndex - defaultFromCellColIndex + 1;

            // 
            for (int targetRow = blockStartRowIndex; targetRow < blockStartRowIndex + rowlen
                    + plusRowNum; targetRow++) {
                if (sheet.getRow(targetRow) == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("row=" + targetRow + " : row is not available. continued...");
                    }
                    continue;
                }

                // ?
                Cell chgTargetCell = null;

                // 
                for (int targetCol = blockStartColIndex; targetCol <= blockStartColIndex + collen + plusColNum
                        - 1; targetCol++) {

                    // ????
                    chgTargetCell = sheet.getRow(targetRow).getCell(targetCol);
                    if (chgTargetCell == null) {
                        if (log.isDebugEnabled()) {
                            log.debug("row=" + targetRow + " col=" + targetCol
                                    + " : cell is not available. continued...");
                        }
                        continue;
                    }

                    parser = info.getMatchTagParser(sheet, chgTargetCell);
                    if (parser == null) {
                        if (log.isDebugEnabled()) {
                            log.debug("row=" + targetRow + " col=" + targetCol
                                    + " parser is not available. continued...");
                        }
                        continue;
                    }

                    String chgTargetCellString = chgTargetCell.getStringCellValue();
                    if (log.isDebugEnabled()) {
                        log.debug("##########  row=" + targetRow + " col=" + targetCol
                                + " =" + chgTargetCellString + " ##########");
                    }

                    // ?
                    result = (ParsedReportInfo) parser.parse(sheet, chgTargetCell,
                            info.createChildParserInfo((ParamInfo) paramInfos[repeatCount]));

                    // ???
                    plusRowNum += result.getRowIndex() - result.getDefaultRowIndex();

                    // ???
                    plusColNum += result.getColumnIndex() - result.getDefaultColumnIndex();

                    // ?single???????????
                    // ???????????
                    if (parser instanceof SingleParamParser
                            && beforeBlockSingleDataMap.containsKey(chgTargetCellString)) {
                        if (beforeBlockSingleDataMap.get(chgTargetCellString)
                                .equals(result.getParsedObject())) {
                            // 
                            PoiUtil.setCellValue(chgTargetCell, "");

                        } else {
                            // ????
                            beforeBlockSingleDataMap.put(chgTargetCellString, result.getParsedObject());
                        }
                    }

                    // ??????????????
                    if (blockStartColIndex != result.getDefaultColumnIndex()
                            && maxblockEndRowIndex <= blockEndRowIndex
                            && result.getRowIndex() > result.getDefaultRowIndex()) {
                        CellRangeAddress preRangeAddress = new CellRangeAddress(blockEndRowIndex + 1,
                                blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()),
                                blockStartColIndex, targetCol - 1);
                        PoiUtil.insertRangeDown(sheet, preRangeAddress);
                        if (log.isDebugEnabled()) {
                            log.debug("******");
                            log.debug("1 : " + (blockEndRowIndex + 1) + ":"
                                    + (blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()))
                                    + ":" + blockStartColIndex + ":" + (targetCol - 1));
                        }
                    }

                    // R??????
                    if (parser instanceof RowRepeatParamParser && maxblockEndRowIndex <= blockEndRowIndex
                            && result.getRowIndex() > result.getDefaultRowIndex()) {
                        CellRangeAddress rearRangeAddress = new CellRangeAddress(blockEndRowIndex + 1,
                                blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()),
                                result.getDefaultColumnIndex() + 1, blockEndColIndex);
                        PoiUtil.insertRangeDown(sheet, rearRangeAddress);
                        if (log.isDebugEnabled()) {
                            log.debug("******");
                            log.debug("2 : " + (blockEndRowIndex + 1) + ":"
                                    + (blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()))
                                    + ":" + (result.getDefaultColumnIndex() + 1) + ":" + blockEndColIndex);
                        }
                    }

                    blockEndRowIndex = defaultToCellRowIndex + plusRowNum;

                    resultList.add(result.getParsedObject());

                    if (parser instanceof BlockColRepeatParamParser
                            || parser instanceof BlockRowRepeatParamParser) {
                        collen += result.getColumnIndex() - result.getDefaultColumnIndex();
                        plusColNum -= result.getColumnIndex() - result.getDefaultColumnIndex();
                    }

                    // ????????
                    if (blockStartColIndex + collen + plusColNum - 1 > blockEndColIndex) {
                        int beforeLastColIndex = blockEndColIndex;
                        blockEndColIndex = blockStartColIndex + collen + plusColNum - 1;

                        // ???????????
                        CellRangeAddress preRangeAddress = new CellRangeAddress(tagCell.getRowIndex(),
                                targetRow - 1, beforeLastColIndex + 1, blockEndColIndex);
                        PoiUtil.insertRangeRight(sheet, preRangeAddress);
                        if (log.isDebugEnabled()) {
                            log.debug("******");
                            log.debug("1 : " + tagCell.getRowIndex() + ":" + (targetRow - 1) + ":"
                                    + (beforeLastColIndex + 1) + ":" + blockEndColIndex);
                        }
                        // ??????
                        int lastRowNum = sheet.getLastRowNum();
                        if ((blockEndRowIndex + 1) <= lastRowNum
                                && (beforeLastColIndex + 1) <= blockEndColIndex) {
                            CellRangeAddress rangeAddress = new CellRangeAddress(blockEndRowIndex + 1,
                                    lastRowNum, beforeLastColIndex + 1, blockEndColIndex);
                            PoiUtil.insertRangeRight(sheet, rangeAddress);
                            if (log.isDebugEnabled()) {
                                log.debug("******");
                                log.debug("3 : " + (blockEndRowIndex + 1) + ":" + lastRowNum + ":"
                                        + (beforeLastColIndex + 1) + ":" + blockEndColIndex);
                            }
                        }
                    }
                    // ?
                }
                // ??????
                if (blockStartColIndex + collen + plusColNum - 1 < blockEndColIndex) {
                    CellRangeAddress rearRangeAddress = new CellRangeAddress(targetRow, targetRow,
                            blockStartColIndex + collen + plusColNum, blockEndColIndex);
                    PoiUtil.insertRangeRight(sheet, rearRangeAddress);
                    if (log.isDebugEnabled()) {
                        log.debug("******");
                        log.debug("2 : " + targetRow + ":" + targetRow + ":"
                                + (blockStartColIndex + collen + plusColNum) + ":" + blockEndColIndex);
                    }
                }

                plusColNum = 0;

                // ?
            }

            // ???????
            if (maxblockEndRowIndex < blockEndRowIndex) {
                if (log.isDebugEnabled()) {
                    log.debug("******");
                }
                if (repeatCount != 0) {
                    CellRangeAddress preRangeAddress = new CellRangeAddress(maxblockEndRowIndex + 1,
                            blockEndRowIndex, defaultFromCellColIndex, blockStartColIndex - 1);
                    PoiUtil.insertRangeDown(sheet, preRangeAddress);
                    if (log.isDebugEnabled()) {
                        log.debug("1 : " + (maxblockEndRowIndex + 1) + ":" + blockEndRowIndex + ":"
                                + defaultFromCellColIndex + ":" + (blockStartColIndex - 1));
                    }
                }

                // ???????
                CellRangeAddress rearRangeAddress = new CellRangeAddress(maxblockEndRowIndex + 1,
                        blockEndRowIndex, blockEndColIndex + 1, PoiUtil.getLastColNum(sheet));
                PoiUtil.insertRangeDown(sheet, rearRangeAddress);
                if (log.isDebugEnabled()) {
                    log.debug("2 : " + (maxblockEndRowIndex + 1) + ":" + blockEndRowIndex + ":"
                            + (blockEndColIndex + 1) + ":" + PoiUtil.getLastColNum(sheet));
                }

                maxblockEndRowIndex = blockEndRowIndex;
            }
        }

        // 
        if (removeTag) {
            tagCell.setCellType(CellType.BLANK);
        }

        // 
        parsedReportInfo.setDefaultRowIndex(defaultToCellRowIndex);
        parsedReportInfo.setDefaultColumnIndex(defaultToCellColIndex);
        parsedReportInfo.setColumnIndex(blockEndColIndex);
        parsedReportInfo.setParsedObject(resultList);
        parsedReportInfo.setRowIndex(maxblockEndRowIndex);

        if (log.isDebugEnabled()) {
            log.debug("finalBlockRowIndex= " + maxblockEndRowIndex);
            log.debug("finalBlockColIndex=" + blockEndColIndex);
        }
        return parsedReportInfo;

    } catch (Exception e) {
        throw new ParseException(tagCell, e);
    }
}

From source file:org.bbreak.excella.reports.tag.BlockRowRepeatParamParser.java

License:Open Source License

@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {
    try {/*from  w ww. ja  va  2 s.c  om*/
        // ??
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        checkParam(sheet, paramDef, tagCell);

        ReportsParserInfo reportsParserInfo = (ReportsParserInfo) data;
        ParamInfo paramInfo = reportsParserInfo.getParamInfo();

        // parse?
        ParsedReportInfo parsedReportInfo = new ParsedReportInfo();
        List<Object> resultList = new ArrayList<Object>();

        // ?
        int finalBlockRowIndex = 0;
        int finalBlockColIndex = 0;

        String brTagName = paramDef.get(PARAM_VALUE);
        if (log.isDebugEnabled()) {
            log.debug("BR??: " + brTagName);
        }

        // ?????
        Object[] paramInfos = getParamData(paramInfo, brTagName);
        if (paramInfos == null) {
            return parsedReportInfo;
        }

        // ????
        List<SingleParamParser> singleParsers = getSingleReplaceParsers(reportsParserInfo);

        // POJOParamInfo???
        List<ParamInfo> paramInfoList = new ArrayList<ParamInfo>();
        for (Object obj : paramInfos) {
            if (obj instanceof ParamInfo) {
                paramInfoList.add((ParamInfo) obj);
                continue;
            }
            ParamInfo childParamInfo = new ParamInfo();
            Map<String, Object> map = PropertyUtils.describe(obj);
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                for (ReportsTagParser<?> parser : singleParsers) {
                    childParamInfo.addParam(parser.getTag(), entry.getKey(), entry.getValue());
                }
            }
            paramInfoList.add(childParamInfo);
        }

        // ??
        if (paramDef.containsKey(PARAM_MIN_REPEAT_NUM)) {
            Integer minRepeatNum = Integer.valueOf(paramDef.get(PARAM_MIN_REPEAT_NUM));
            if (minRepeatNum > paramInfoList.size()) {
                int addEmptyRowNum = minRepeatNum - paramInfoList.size();
                for (int num = 0; num < addEmptyRowNum; num++) {
                    ParamInfo childParamInfo = new ParamInfo();
                    paramInfoList.add(childParamInfo);
                }
            }
        }

        paramInfos = paramInfoList.toArray(new ParamInfo[paramInfoList.size()]);

        // repeatNum
        Integer repeatNum = paramInfos.length;
        if (paramDef.containsKey(PARAM_REPEAT_NUM)) {
            if (Integer.valueOf(paramDef.get(PARAM_REPEAT_NUM)) < repeatNum) {
                repeatNum = Integer.valueOf(paramDef.get(PARAM_REPEAT_NUM));
            }
        }

        // duplicateParams  ????????
        Map<String, Object> unduplicableParamMap = new HashMap<String, Object>();
        if (paramDef.containsKey(PARAM_DUPLICATE)) {
            String[] params = paramDef.get(PARAM_DUPLICATE).split(";");
            for (String param : params) {
                for (ReportsTagParser<?> parser : singleParsers) {
                    param = parser.getTag() + TAG_PARAM_PREFIX + param + TAG_PARAM_SUFFIX;
                    unduplicableParamMap.put(param, "");
                }
            }
        }

        // removeTag
        boolean removeTag = false;
        if (paramDef.containsKey(PARAM_REMOVE_TAG)) {
            removeTag = Boolean.valueOf(paramDef.get(PARAM_REMOVE_TAG));
        }

        // 
        int tagCellRowIndex = tagCell.getRowIndex();
        int tagCellColIndex = tagCell.getColumnIndex();

        // fromCell
        String fromCellParamDef = paramDef.get(PARAM_FROM_CELL);
        int[] fromCellPosition = ReportsUtil.getCellIndex(fromCellParamDef, PARAM_FROM_CELL);
        int defaultFromCellRowIndex = tagCellRowIndex + fromCellPosition[0];
        int defaultFromCellColIndex = tagCellColIndex + fromCellPosition[1];

        // toCell
        String toCellParamDef = paramDef.get(PARAM_TO_CELL);
        int[] toCellPosition = ReportsUtil.getCellIndex(toCellParamDef, PARAM_TO_CELL);
        int defaultToCellRowIndex = tagCellRowIndex + toCellPosition[0];
        int defaultToCellColIndex = tagCellColIndex + toCellPosition[1];

        // 
        Object[][] blockCellValues = ReportsUtil.getBlockCellValue(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        CellStyle[][] blockCellStyles = ReportsUtil.getBlockCellStyle(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        CellType[][] blockCellTypes = ReportsUtil.getBlockCellType(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        float[] rowHeight = ReportsUtil.getRowHeight(sheet, defaultFromCellRowIndex, defaultToCellRowIndex);

        // ???
        CellRangeAddress[] margedCells = ReportsUtil.getMargedCells(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);

        // ?
        final int defaultBlockHeight = defaultToCellRowIndex - defaultFromCellRowIndex + 1;
        final int defaultBlockWidth = defaultToCellColIndex - defaultFromCellColIndex + 1;
        int rowlen = defaultBlockHeight;
        int collen = defaultBlockWidth;

        // ??(fromCell??, 1?????)
        int blockStartRowIndex = defaultFromCellRowIndex;
        int blockStartColIndex = defaultFromCellColIndex;
        int blockEndRowIndex = defaultToCellRowIndex;
        int blockEndColIndex = defaultToCellColIndex;
        int maxblockEndRowIndex = blockEndRowIndex;

        parsedReportInfo.setDefaultRowIndex(defaultToCellRowIndex);
        parsedReportInfo.setDefaultColumnIndex(defaultToCellColIndex);

        for (int repeatCount = 0; repeatCount < repeatNum; repeatCount++) {
            // ????
            if (repeatCount > 0) {
                blockStartRowIndex = blockEndRowIndex + 1;
                blockEndRowIndex = blockStartRowIndex + rowlen - 1;

                CellRangeAddress rangeAddress = new CellRangeAddress(blockStartRowIndex, blockEndRowIndex,
                        blockStartColIndex, PoiUtil.getLastColNum(sheet));
                PoiUtil.insertRangeDown(sheet, rangeAddress);

                if (log.isDebugEnabled()) {
                    log.debug("");
                    log.debug(" : " + blockStartRowIndex + ":" + (blockStartRowIndex + rowlen - 1)
                            + ":" + blockStartColIndex + ":" + PoiUtil.getLastColNum(sheet));
                }

                // ???
                // ???????????
                // ??????
                int targetRowNum = maxblockEndRowIndex - (defaultFromCellRowIndex - 1);
                for (CellRangeAddress address : margedCells) {
                    // ??? + BR?? - BR?
                    // ??? + BR?? - BR?
                    // ???(BR????????)
                    // ???(BR????????)
                    int firstRowNum = address.getFirstRow() + targetRowNum;
                    int lastRowNum = address.getLastRow() + targetRowNum;
                    int firstColumnNum = address.getFirstColumn();
                    int lastColumnNum = address.getLastColumn();

                    CellRangeAddress copyAddress = new CellRangeAddress(firstRowNum, lastRowNum, firstColumnNum,
                            lastColumnNum);
                    sheet.addMergedRegion(copyAddress);
                }

            }

            if (log.isDebugEnabled()) {
                log.debug("repeatCount = " + repeatCount);
                log.debug("blockStartRowIndex = " + blockStartRowIndex);
                log.debug("blockStartColIndex = " + blockStartColIndex);
            }

            // ??
            if (log.isDebugEnabled()) {
                log.debug("?????? =" + repeatCount);
            }

            for (int rowIdx = 0; rowIdx < defaultBlockHeight; rowIdx++) {
                // ?
                Row row = sheet.getRow(blockStartRowIndex + rowIdx);
                // ??null???????????????
                if (row == null && !ReportsUtil.isEmptyRow(blockCellTypes[rowIdx], blockCellValues[rowIdx],
                        blockCellStyles[rowIdx])) {
                    row = sheet.createRow(blockStartRowIndex + rowIdx);
                }

                if (row != null) {
                    // ???
                    row.setHeightInPoints(rowHeight[rowIdx]);

                    // ?
                    for (int colIdx = 0; colIdx < defaultBlockWidth; colIdx++) {
                        // ?
                        Cell cell = row.getCell(blockStartColIndex + colIdx);
                        // ??
                        CellType cellType = blockCellTypes[rowIdx][colIdx];
                        // ??
                        Object cellValue = blockCellValues[rowIdx][colIdx];
                        // ??
                        CellStyle cellStyle = blockCellStyles[rowIdx][colIdx];
                        // ?????????????????
                        if (cell == null && !ReportsUtil.isEmptyCell(cellType, cellValue, cellStyle)) {
                            cell = row.createCell(blockStartColIndex + colIdx);
                        }

                        // 
                        if (cell != null) {
                            // ?
                            cell.setCellType(cellType);
                            // ??
                            PoiUtil.setCellValue(cell, cellValue);
                            // ??
                            if (cellStyle == null) {
                                log.info("Cell Style at [" + rowIdx + "," + colIdx
                                        + "] is not available. Skipping setCellValue()");
                            } else {
                                cell.setCellStyle(cellStyle);
                            }
                            log.debug("row=" + (blockStartRowIndex + rowIdx) + " col"
                                    + (blockStartColIndex + colIdx) + ">>>>>>"
                                    + blockCellValues[rowIdx][colIdx]);
                        }
                    }
                }
            }

            int currentBlockHeight = rowlen;
            int currentBlockWidth = collen;
            // ?????????
            int plusRowNum = 0;
            // ?????????
            int plusColNum = 0;
            collen = defaultBlockWidth;
            // 
            // ???
            for (int targetRow = blockStartRowIndex; targetRow < blockStartRowIndex + rowlen
                    + plusRowNum; targetRow++) {
                if (finalBlockRowIndex < targetRow) {
                    finalBlockRowIndex = targetRow;
                }
                if (sheet.getRow(targetRow) == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("row=" + targetRow + " : row is not available. continued...");
                    }
                    continue;
                }

                for (int targetCol = blockStartColIndex; targetCol <= blockStartColIndex + collen + plusColNum
                        - 1; targetCol++) {
                    if (finalBlockColIndex < targetCol) {
                        finalBlockColIndex = targetCol;
                    }
                    Cell targetCell = sheet.getRow(targetRow).getCell(targetCol);
                    if (targetCell == null) {
                        if (log.isDebugEnabled()) {
                            log.debug("row=" + targetRow + " col=" + targetCol
                                    + " : cell is not available. continued...");
                        }
                        continue;
                    }

                    // ??
                    TagParser<?> parser = reportsParserInfo.getMatchTagParser(sheet, targetCell);
                    if (parser == null) {
                        if (log.isDebugEnabled()) {
                            log.debug("row=" + targetRow + " col=" + targetCol
                                    + " parser is not available. continued...");
                        }
                        continue;
                    }

                    String targetCellTag = targetCell.getStringCellValue();
                    if (log.isDebugEnabled()) {
                        log.debug("##########  row=" + targetRow + " col=" + targetCol
                                + " =" + targetCellTag + " ##########");
                    }

                    // 
                    ParsedReportInfo result = (ParsedReportInfo) parser.parse(sheet, targetCell,
                            reportsParserInfo.createChildParserInfo((ParamInfo) paramInfos[repeatCount]));
                    resultList.add(result.getParsedObject());

                    // ???
                    plusRowNum += result.getRowIndex() - result.getDefaultRowIndex();

                    // ???
                    plusColNum += result.getColumnIndex() - result.getDefaultColumnIndex();

                    int additionalHeight = result.getRowIndex() - result.getDefaultRowIndex();
                    int additionalWidth = result.getColumnIndex() - result.getDefaultColumnIndex();

                    // ??????????
                    currentBlockHeight = currentBlockHeight + additionalHeight;
                    currentBlockWidth = currentBlockWidth + additionalWidth;

                    // ???
                    if (parser instanceof SingleParamParser) {
                        if (unduplicableParamMap.containsKey(targetCellTag)) {
                            if (unduplicableParamMap.get(targetCellTag).equals(result.getParsedObject())) {
                                PoiUtil.setCellValue(targetCell, "");

                            } else {
                                unduplicableParamMap.put(targetCellTag, result.getParsedObject());
                            }
                        }
                    }

                    // ??????????????
                    if (defaultFromCellColIndex != result.getDefaultColumnIndex()
                            && result.getRowIndex() > result.getDefaultRowIndex()
                            && blockStartColIndex < targetCol) {
                        CellRangeAddress preRangeAddress = new CellRangeAddress(blockEndRowIndex + 1,
                                blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()),
                                blockStartColIndex, targetCol - 1);
                        PoiUtil.insertRangeDown(sheet, preRangeAddress);
                        if (log.isDebugEnabled()) {
                            log.debug("******");
                            log.debug("1 : " + (blockEndRowIndex + 1) + ":"
                                    + (blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()))
                                    + ":" + blockStartColIndex + ":" + (targetCol - 1));
                        }
                    }

                    // R??????
                    if (parser instanceof RowRepeatParamParser
                            && result.getRowIndex() > result.getDefaultRowIndex()) {
                        CellRangeAddress rearRangeAddress = new CellRangeAddress(blockEndRowIndex + 1,
                                blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()),
                                result.getDefaultColumnIndex() + 1, PoiUtil.getLastColNum(sheet));
                        PoiUtil.insertRangeDown(sheet, rearRangeAddress);
                        if (log.isDebugEnabled()) {
                            log.debug("******");
                            log.debug("2 : " + (blockEndRowIndex + 1) + ":"
                                    + (blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()))
                                    + ":" + (result.getDefaultColumnIndex() + 1) + ":"
                                    + PoiUtil.getLastColNum(sheet));
                        }
                    }

                    blockEndRowIndex += result.getRowIndex() - result.getDefaultRowIndex();

                    if (parser instanceof BlockColRepeatParamParser
                            || parser instanceof BlockRowRepeatParamParser) {
                        collen += result.getColumnIndex() - result.getDefaultColumnIndex();
                        plusColNum -= result.getColumnIndex() - result.getDefaultColumnIndex();
                    }

                    // ????????
                    if (blockStartColIndex + collen + plusColNum - 1 > blockEndColIndex) {
                        int beforeLastColIndex = blockEndColIndex;
                        blockEndColIndex = blockStartColIndex + collen + plusColNum - 1;

                        // ???????????
                        CellRangeAddress preRangeAddress = new CellRangeAddress(tagCell.getRowIndex(),
                                targetRow - 1, beforeLastColIndex + 1, blockEndColIndex);
                        PoiUtil.insertRangeRight(sheet, preRangeAddress);

                        if (log.isDebugEnabled()) {
                            log.debug("******");
                            log.debug("1 : " + tagCell.getRowIndex() + ":" + (targetRow - 1) + ":"
                                    + (beforeLastColIndex + 1) + ":" + blockEndColIndex);
                        }
                    }

                    // ?
                }
                // ??????
                if (blockStartColIndex + collen + plusColNum - 1 < blockEndColIndex) {
                    CellRangeAddress rearRangeAddress = new CellRangeAddress(targetRow, targetRow,
                            blockStartColIndex + collen + plusColNum, blockEndColIndex);
                    PoiUtil.insertRangeRight(sheet, rearRangeAddress);

                    if (log.isDebugEnabled()) {
                        log.debug("******");
                        log.debug("2 : " + targetRow + ":" + targetRow + ":"
                                + (blockStartColIndex + collen + plusColNum) + ":" + blockEndColIndex);
                    }
                }
                plusColNum = 0;

                // ?
            }
            // ???????
            int lastColNum = PoiUtil.getLastColNum(sheet);
            if ((maxblockEndRowIndex + 1) <= blockEndRowIndex && (blockEndColIndex + 1) <= lastColNum) {
                CellRangeAddress rearRangeAddress = new CellRangeAddress(maxblockEndRowIndex + 1,
                        blockEndRowIndex, blockEndColIndex + 1, lastColNum);
                PoiUtil.insertRangeDown(sheet, rearRangeAddress);
                if (log.isDebugEnabled()) {
                    log.debug("2 : " + (maxblockEndRowIndex + 1) + ":" + blockEndRowIndex + ":"
                            + (blockEndColIndex + 1) + ":" + lastColNum);
                }
            }
            maxblockEndRowIndex = blockEndRowIndex;
        }

        // 
        if (removeTag) {
            tagCell.setCellType(CellType.BLANK);
        }

        // ??
        parsedReportInfo.setColumnIndex(finalBlockColIndex);
        parsedReportInfo.setRowIndex(finalBlockRowIndex);
        parsedReportInfo.setParsedObject(resultList);
        parsedReportInfo.setDefaultRowIndex(defaultToCellRowIndex);
        parsedReportInfo.setDefaultColumnIndex(defaultToCellColIndex);

        if (log.isDebugEnabled()) {
            log.debug("finalBlockRowIndex= " + finalBlockRowIndex);
            log.debug("finalBlockColIndex=" + finalBlockColIndex);
        }

        return parsedReportInfo;

    } catch (Exception e) {
        throw new ParseException(tagCell, e);
    }

}

From source file:org.bbreak.excella.reports.tag.BreakParamParser.java

License:Open Source License

/**
 * @see org.bbreak.excella.reports.tag.ReportsTagParser#parse(org.apache.poi.ss.usermodel.Sheet, org.apache.poi.ss.usermodel.Cell, java.lang.Object)
 *///from   w  w w. j  ava 2  s.c om
@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {
    // ??????
    ParsedReportInfo parsedReportInfo = new ParsedReportInfo();
    parsedReportInfo.setRowIndex(tagCell.getRowIndex());
    parsedReportInfo.setColumnIndex(tagCell.getColumnIndex());
    parsedReportInfo.setDefaultRowIndex(tagCell.getRowIndex());
    parsedReportInfo.setDefaultColumnIndex(tagCell.getColumnIndex());
    return parsedReportInfo;
}

From source file:org.bbreak.excella.reports.tag.ColRepeatParamParser.java

License:Open Source License

@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

    // ?//w  w w  .j a v  a 2s . c om
    checkParam(paramDef, tagCell);

    String tag = tagCell.getStringCellValue();
    ReportsParserInfo info = (ReportsParserInfo) data;
    ParamInfo paramInfo = info.getParamInfo();
    ParsedReportInfo parsedReportInfo = new ParsedReportInfo();

    // ??
    Object[] paramValues = null;
    try {
        // ???
        String replaceParam = paramDef.get(PARAM_VALUE);

        // ?
        Integer repeatNum = null;
        if (paramDef.containsKey(PARAM_REPEAT_NUM)) {
            repeatNum = Integer.valueOf(paramDef.get(PARAM_REPEAT_NUM));
        }
        // ??
        Integer minRepeatNum = null;
        if (paramDef.containsKey(PARAM_MIN_REPEAT_NUM)) {
            minRepeatNum = Integer.valueOf(paramDef.get(PARAM_MIN_REPEAT_NUM));
        }

        // ?
        boolean sheetLink = false;
        if (paramDef.containsKey(PARAM_SHEET_LINK)) {
            sheetLink = Boolean.valueOf(paramDef.get(PARAM_SHEET_LINK));
        }

        // 
        String propertyName = null;
        if (paramDef.containsKey(PARAM_PROPERTY)) {
            propertyName = paramDef.get(PARAM_PROPERTY);
        }

        // ???
        boolean hideDuplicate = false;
        if (paramDef.containsKey(PARAM_DUPLICATE)) {
            hideDuplicate = Boolean.valueOf(paramDef.get(PARAM_DUPLICATE));
        }

        // 
        if (ReportsUtil.VALUE_SHEET_NAMES.equals(replaceParam)) {
            // ??
            paramValues = ReportsUtil.getSheetNames(info.getReportBook()).toArray();
        } else if (ReportsUtil.VALUE_SHEET_VALUES.equals(replaceParam)) {
            // 
            paramValues = ReportsUtil
                    .getSheetValues(info.getReportBook(), propertyName, info.getReportParsers()).toArray();
        } else {
            // ???
            if (paramInfo != null) {
                paramValues = getParamData(paramInfo, replaceParam);
            }
        }

        if (paramValues == null || paramValues.length == 0) {
            // ?
            paramValues = new Object[] { null };
        }

        // ?
        if (hideDuplicate && paramValues.length > 1) {
            List<Object> paramValuesList = new ArrayList<Object>();
            for (int i = 0; i <= paramValues.length - 1; i++) {
                // ?????
                if (!paramValuesList.contains(paramValues[i])) {
                    paramValuesList.add(paramValues[i]);
                } else {
                    paramValuesList.add(null);
                }
            }
            paramValues = paramValuesList.toArray();
        }

        // ?
        int shiftNum = paramValues.length;
        // ?
        int paramLength = paramValues.length;

        // ???????
        if (minRepeatNum != null && shiftNum < minRepeatNum) {
            Object[] tmpValues = new Object[minRepeatNum];
            System.arraycopy(paramValues, 0, tmpValues, 0, paramValues.length);
            paramValues = tmpValues;
            shiftNum = paramValues.length;
            paramLength = paramValues.length;
        }

        // ???
        int defaultFromCellRowIndex = tagCell.getRowIndex();
        // ???
        int defaultFromCellColIndex = tagCell.getColumnIndex();

        // ??
        int unitColSize = 1;

        // ???
        List<CellRangeAddress> maegedAddresses = new ArrayList<CellRangeAddress>();
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress targetAddress = sheet.getMergedRegion(i);
            maegedAddresses.add(targetAddress);
        }

        // ???
        if (maegedAddresses.size() > 0) {
            // ?????????????
            for (CellRangeAddress curMergedAdress : maegedAddresses) {
                if (defaultFromCellColIndex == curMergedAdress.getFirstColumn()
                        && defaultFromCellRowIndex == curMergedAdress.getFirstRow()) {
                    // ????????????
                    // ??????
                    unitColSize = curMergedAdress.getLastColumn() - curMergedAdress.getFirstColumn() + 1;

                    // ??????
                    shiftNum = shiftNum * unitColSize;
                }
            }
        }

        // ?
        if (repeatNum != null && repeatNum < shiftNum) {
            // ??????
            // ????????????????
            // ?(repeatNum)??(unitColSize)??
            shiftNum = repeatNum * unitColSize;

            // ??????
            paramLength = repeatNum;
        }

        // ???
        // ?????
        tagCell = new CellClone(tagCell);
        List<Cell> cellList = new ArrayList<Cell>();
        int defaultToOverCellColIndex = tagCell.getColumnIndex() + unitColSize;
        for (int i = defaultFromCellColIndex; i < defaultToOverCellColIndex; i++) {
            Row targetCellRow = sheet.getRow(tagCell.getRowIndex());
            cellList.add(new CellClone(targetCellRow.getCell(i)));
        }

        // ?            
        if (shiftNum > 1) {
            // ?(????????)
            int shiftColSize = tagCell.getColumnIndex() + shiftNum - unitColSize - 1;
            // ???
            CellRangeAddress rangeAddress = new CellRangeAddress(tagCell.getRowIndex(), tagCell.getRowIndex(),
                    tagCell.getColumnIndex(), shiftColSize);
            PoiUtil.insertRangeRight(sheet, rangeAddress);
            // 
            int tagCellWidth = sheet.getColumnWidth(tagCell.getColumnIndex());
            for (int i = tagCell.getColumnIndex() + 1; i <= shiftColSize; i++) {
                int colWidth = sheet.getColumnWidth(i);
                if (colWidth < tagCellWidth) {
                    // ??  ???????
                    // ??????
                    sheet.setColumnWidth(i, tagCellWidth);
                }
            }
        }

        // ???
        Workbook workbook = sheet.getWorkbook();
        String sheetName = workbook.getSheetName(workbook.getSheetIndex(sheet));
        // ??
        List<String> sheetNames = ReportsUtil.getSheetNames(info.getReportBook());
        // ?
        List<Object> resultValues = new ArrayList<Object>();
        // ??(beforeValue)
        Object beforeValue = null;

        // ?
        int valueIndex = -1;
        // ?????
        for (int colIndex = 0; colIndex < shiftNum; colIndex++) {
            // ??
            Row row = sheet.getRow(tagCell.getRowIndex());
            if (row == null) {
                // ????
                // ?
                // (??)??????????
                // ??null???(RowCreate?)???????????
                row = sheet.createRow(tagCell.getRowIndex());
            }
            // ??
            Cell cell = row.getCell(tagCell.getColumnIndex() + colIndex);
            if (cell == null) {
                cell = row.createCell(tagCell.getColumnIndex() + colIndex);
            }
            // ????(null)
            Object value = null;

            // ??????
            // ??0???(?????????)???????
            int cellIndex = colIndex % unitColSize;

            // ?????????
            boolean skipCol = false;
            if (cellIndex != 0) {
                skipCol = true;
            } else {
                valueIndex++;
            }

            // 
            // ?
            PoiUtil.copyCell(cellList.get(cellIndex), cell);

            // ?
            Object currentValue = paramValues[valueIndex];
            // ??=true???????????
            boolean duplicateValue = false;
            if (beforeValue != null && currentValue != null && beforeValue.equals(currentValue)) {
                // ???
                duplicateValue = true;
            }
            if (!skipCol && !(hideDuplicate && duplicateValue)) {
                // ??=true
                // ??????????????
                value = currentValue;
            }
            if (log.isDebugEnabled()) {
                log.debug("[??=" + sheetName + ",=(" + cell.getRowIndex() + ","
                        + cell.getColumnIndex() + ")]  " + tag + "  " + value);
            }
            PoiUtil.setCellValue(cell, value);
            resultValues.add(value);

            // ?
            if (sheetLink) {
                if (!skipCol && valueIndex < sheetNames.size()) {
                    PoiUtil.setHyperlink(cell, HyperlinkType.DOCUMENT,
                            "'" + sheetNames.get(valueIndex) + "'!A1");
                    if (log.isDebugEnabled()) {
                        log.debug("[??=" + sheetName + ",=(" + cell.getRowIndex() + ","
                                + cell.getColumnIndex() + ")]  Hyperlink  " + "'"
                                + sheetNames.get(valueIndex) + "'!A1");
                    }
                }
            }

            // ??
            // ??????????????????
            if (!skipCol && unitColSize > 1 && paramLength > valueIndex + 1) {
                CellRangeAddress rangeAddress = new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex(),
                        cell.getColumnIndex(), cell.getColumnIndex() + unitColSize - 1);
                sheet.addMergedRegion(rangeAddress);

                // ????????????
                beforeValue = value;
            }

            // ???????
            if (unitColSize == 1) {
                beforeValue = value;
            }

        }

        parsedReportInfo.setDefaultRowIndex(tagCell.getRowIndex());
        // ??
        parsedReportInfo.setDefaultColumnIndex(tagCell.getColumnIndex() + unitColSize - 1);
        parsedReportInfo.setRowIndex(tagCell.getRowIndex());
        parsedReportInfo.setColumnIndex(tagCell.getColumnIndex() + shiftNum - 1);
        parsedReportInfo.setParsedObject(resultValues);
        if (log.isDebugEnabled()) {
            log.debug(parsedReportInfo);
        }
        return parsedReportInfo;

    } catch (Exception e) {
        throw new ParseException(tagCell, e);
    }

}

From source file:org.bbreak.excella.reports.tag.ImageParamParser.java

License:Open Source License

@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

    // ?//from   w  ww. j a  v  a 2s.c  o  m
    if (hasComments(sheet)) {
        throw new ParseException(
                "?[" + sheet.getWorkbook().getSheetName(sheet.getWorkbook().getSheetIndex(sheet))
                        + "]");
    }

    // ?
    checkParam(paramDef, tagCell);

    ReportsParserInfo reportsParserInfo = (ReportsParserInfo) data;

    ParamInfo paramInfo = reportsParserInfo.getParamInfo();

    String paramValue = null;

    if (paramInfo != null) {
        // ?
        String replaceParam = paramDef.get(PARAM_VALUE);

        paramValue = getParamData(paramInfo, replaceParam);

        // ??
        Integer dx1 = null;
        if (paramDef.containsKey(PARAM_WIDTH)) {
            dx1 = Integer.valueOf(paramDef.get(PARAM_WIDTH));
        }

        // ???
        Integer dy1 = null;
        if (paramDef.containsKey(PARAM_HEIGHT)) {
            dy1 = Integer.valueOf(paramDef.get(PARAM_HEIGHT));
        }

        // ???
        Double scale = 1.0;
        if (paramDef.containsKey(PARAM_SCALE)) {
            scale = Double.valueOf(paramDef.get(PARAM_SCALE));
        }

        // ??????
        if (ReportsUtil.getMergedAddress(sheet, tagCell.getRowIndex(), tagCell.getColumnIndex()) != null) {
            CellStyle cellStyle = tagCell.getCellStyle();
            tagCell.setCellType(CellType.BLANK);
            tagCell.setCellStyle(cellStyle);
        } else {
            tagCell = new CellClone(tagCell);
            PoiUtil.clearCell(sheet, new CellRangeAddress(tagCell.getRowIndex(), tagCell.getRowIndex(),
                    tagCell.getColumnIndex(), tagCell.getColumnIndex()));
        }

        if (log.isDebugEnabled()) {
            Workbook workbook = sheet.getWorkbook();
            String sheetName = workbook.getSheetName(workbook.getSheetIndex(sheet));

            log.debug("[??=" + sheetName + ",=(" + tagCell.getRowIndex() + ","
                    + tagCell.getColumnIndex() + ")]  " + tagCell.getStringCellValue() + "  " + paramValue);
        }

        if (paramValue != null) {
            replaceImageValue(sheet, tagCell, paramValue, dx1, dy1, scale);
        }

    }

    // ????
    ParsedReportInfo parsedReportInfo = new ParsedReportInfo();
    parsedReportInfo.setParsedObject(paramValue);
    parsedReportInfo.setRowIndex(tagCell.getRowIndex());
    parsedReportInfo.setColumnIndex(tagCell.getColumnIndex());
    parsedReportInfo.setDefaultRowIndex(tagCell.getRowIndex());
    parsedReportInfo.setDefaultColumnIndex(tagCell.getColumnIndex());

    return parsedReportInfo;
}

From source file:org.bbreak.excella.reports.tag.ImageParamParser.java

License:Open Source License

/**
 * ??/*from w  w  w  .j ava  2 s  . com*/
 * 
 * @param sheet ?
 * @param cell 
 * @param filePath ?
 * @param dx1 ??
 * @param dy1 ???
 * @param scale ???
 * @throws ParseException
 */
public void replaceImageValue(Sheet sheet, Cell cell, String filePath, Integer dx1, Integer dy1, Double scale)
        throws ParseException {

    Workbook workbook = sheet.getWorkbook();

    int format = -1;
    if (filePath.toLowerCase().endsWith(JPEG_SUFFIX) || filePath.toLowerCase().endsWith(JPG_SUFFIX)) {
        format = Workbook.PICTURE_TYPE_JPEG;
    } else if (filePath.toLowerCase().endsWith(PNG_SUFFIX)) {
        format = Workbook.PICTURE_TYPE_PNG;
    }
    if (format == -1) {
        throw new ParseException(cell,
                "????????" + filePath);
    }

    byte[] bytes = null;
    InputStream is = null;
    try {
        is = new FileInputStream(filePath);
        bytes = IOUtils.toByteArray(is);
    } catch (Exception e) {
        throw new ParseException(cell, e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            throw new ParseException(cell, e);
        }
    }

    int pictureIdx = workbook.addPicture(bytes, format);

    CreationHelper helper = workbook.getCreationHelper();

    @SuppressWarnings("rawtypes")
    Drawing drawing = drawingCash.get(sheet);
    if (drawing == null) {
        drawing = sheet.createDrawingPatriarch();
        drawingCash.put(sheet, drawing);
    }

    ClientAnchor anchor = helper.createClientAnchor();

    anchor.setRow1(cell.getRowIndex());
    anchor.setCol1(cell.getColumnIndex());
    anchor.setRow2(cell.getRowIndex() + 1);
    anchor.setCol2(cell.getColumnIndex() + 1);
    if (dx1 != null) {
        anchor.setDx1(dx1);
    }
    if (dy1 != null) {
        anchor.setDy1(dy1);
    }

    Picture picture = drawing.createPicture(anchor, pictureIdx);
    picture.resize(scale);

}