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

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

Introduction

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

Prototype

int getColumnIndex();

Source Link

Document

Returns column index of this cell

Usage

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

License:Open Source License

@Test
public final void testArraysParser() throws ParseException {
    Workbook wk = getWorkbook();//from  w w  w. j a  va 2s  .  c o  m
    Sheet sheet1 = wk.getSheetAt(0);
    Sheet sheet2 = wk.getSheetAt(1);
    Sheet sheet3 = wk.getSheetAt(2);
    Sheet sheet4 = wk.getSheetAt(3);
    ArraysParser arraysParser = new ArraysParser("@Arrays");
    String tag = arraysParser.getTag();
    arraysParser.setTag(tag);
    Cell tagCell = null;
    Object data = null;
    List<Object[]> list = null;

    // No.1 ?
    tagCell = sheet1.getRow(5).getCell(0);
    list = arraysParser.parse(sheet1, tagCell, data);
    Object[] arrays = list.get(0);
    assertEquals("value1-1", arrays[0]);
    assertEquals("value2-1", arrays[1]);
    assertEquals("value3-1", arrays[2]);
    assertEquals(3, arrays.length);
    arrays = list.get(1);
    assertEquals("value1-2", arrays[0]);
    assertEquals("value2-2", arrays[1]);
    assertEquals("value3-2", arrays[2]);
    assertEquals(3, arrays.length);
    arrays = list.get(2);
    assertEquals("value1-3", arrays[0]);
    assertEquals("value2-3", arrays[1]);
    assertEquals("value3-3", arrays[2]);
    assertEquals(3, arrays.length);
    arrays = list.get(3);
    assertEquals("value1-4", arrays[0]);
    assertEquals("value2-4", arrays[1]);
    assertEquals("value3-4", arrays[2]);
    assertEquals("value4-1", arrays[3]);
    assertEquals(4, arrays.length);
    arrays = list.get(4);
    assertEquals("value1-5", arrays[0]);
    assertEquals(null, arrays[1]);
    assertEquals("value3-5", arrays[2]);
    assertEquals("value4-2", arrays[3]);
    assertEquals(4, arrays.length);
    arrays = list.get(5);
    assertEquals("value1-6", arrays[0]);
    assertEquals("value2-5", arrays[1]);
    assertEquals(2, arrays.length);
    assertEquals(6, list.size());

    // No.2 
    tagCell = sheet2.getRow(5).getCell(0);
    list.clear();
    list = arraysParser.parse(sheet2, tagCell, data);
    arrays = list.get(0);
    assertEquals("value1-1", arrays[0]);
    assertEquals("value2-1", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(1);
    assertEquals("value1-2", arrays[0]);
    assertEquals("value2-2", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(2);
    assertEquals("value1-3", arrays[0]);
    assertEquals("value2-3", arrays[1]);
    assertEquals(2, arrays.length);
    assertEquals(3, list.size());

    // No.3 ?
    tagCell = sheet2.getRow(13).getCell(0);
    list.clear();
    list = arraysParser.parse(sheet2, tagCell, data);
    arrays = list.get(0);
    assertEquals("value1-4", arrays[0]);
    assertEquals("value2-4", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(1);
    assertEquals("value1-5", arrays[0]);
    assertEquals("value2-5", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(2);
    assertEquals("value1-6", arrays[0]);
    assertEquals("value2-6", arrays[1]);
    assertEquals(2, arrays.length);
    assertEquals(3, list.size());

    // No.4 ?null
    tagCell = sheet2.getRow(22).getCell(0);
    list.clear();
    list = arraysParser.parse(sheet2, tagCell, data);
    arrays = list.get(0);
    assertEquals("value1-7", arrays[0]);
    assertEquals("value2-7", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(1);
    assertEquals("value1-8", arrays[0]);
    assertEquals("value2-8", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(2);
    assertEquals("value1-9", arrays[0]);
    assertEquals("value2-9", arrays[1]);
    assertEquals(2, arrays.length);
    assertEquals(3, list.size());

    // No.5 ?null
    tagCell = sheet2.getRow(31).getCell(0);
    list.clear();
    list = arraysParser.parse(sheet2, tagCell, data);
    arrays = list.get(0);
    assertEquals(null, arrays[0]);
    assertEquals("value2-10", arrays[1]);
    assertEquals("value3-1", arrays[2]);
    assertEquals(3, arrays.length);
    arrays = list.get(1);
    assertEquals("value1-10", arrays[0]);
    assertEquals(null, arrays[1]);
    assertEquals("value3-2", arrays[2]);
    assertEquals(3, arrays.length);
    arrays = list.get(2);
    assertEquals("value1-11", arrays[0]);
    assertEquals("value2-11", arrays[1]);
    assertEquals(2, arrays.length);
    assertEquals(3, list.size());

    // No.6 
    tagCell = sheet2.getRow(39).getCell(0);
    list.clear();
    list = arraysParser.parse(sheet2, tagCell, data);
    arrays = list.get(0);
    assertEquals("value2-12", arrays[0]);
    assertEquals("value3-3", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(1);
    assertEquals("value2-13", arrays[0]);
    assertEquals("value3-4", arrays[1]);
    assertEquals(2, arrays.length);
    assertEquals(2, list.size());

    // No.7 
    tagCell = sheet2.getRow(46).getCell(0);
    list.clear();
    list = arraysParser.parse(sheet2, tagCell, data);
    arrays = list.get(0);
    assertEquals("value1-14", arrays[0]);
    assertEquals("value2-14", arrays[1]);
    assertEquals(null, arrays[2]);
    assertEquals(null, arrays[3]);
    assertEquals(null, arrays[4]);
    assertEquals(5, arrays.length);
    arrays = list.get(1);
    assertEquals("value1-15", arrays[0]);
    assertEquals("value2-15", arrays[1]);
    assertEquals("value3-5", arrays[2]);
    assertEquals(null, arrays[3]);
    assertEquals(null, arrays[4]);
    assertEquals(5, arrays.length);
    assertEquals(2, list.size());

    // No.8 
    tagCell = sheet2.getRow(53).getCell(1);
    list.clear();
    list = arraysParser.parse(sheet2, tagCell, data);
    arrays = list.get(0);
    assertEquals("value3-6", arrays[0]);
    assertEquals("value4-1", arrays[1]);
    assertEquals(null, arrays[2]);
    assertEquals(null, arrays[3]);
    assertEquals(4, arrays.length);
    arrays = list.get(1);
    assertEquals(null, arrays[0]);
    assertEquals("value4-2", arrays[1]);
    assertEquals("value5-1", arrays[2]);
    assertEquals(null, arrays[3]);
    assertEquals(4, arrays.length);
    arrays = list.get(2);
    assertEquals(null, arrays[0]);
    assertEquals(null, arrays[1]);
    assertEquals(null, arrays[2]);
    assertEquals(null, arrays[3]);
    assertEquals(4, arrays.length);
    assertEquals(3, list.size());

    // No.9 
    tagCell = sheet3.getRow(7).getCell(0);
    list.clear();
    list = arraysParser.parse(sheet3, tagCell, data);
    arrays = list.get(0);
    assertEquals("value1-1", arrays[0]);
    assertEquals("value2-1", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(1);
    assertEquals("value1-2", arrays[0]);
    assertEquals("value2-2", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(2);
    assertEquals("value1-3", arrays[0]);
    assertEquals("value2-3", arrays[1]);
    assertEquals(2, arrays.length);
    arrays = list.get(3);
    assertEquals("value1-4", arrays[0]);
    assertEquals("value2-4", arrays[1]);
    assertEquals(2, arrays.length);
    assertEquals(4, list.size());

    // No.10 
    tagCell = sheet3.getRow(12).getCell(2);
    list.clear();
    list = arraysParser.parse(sheet3, tagCell, data);
    arrays = list.get(0);
    assertEquals("value2-6", arrays[0]);
    assertEquals("value3-1", arrays[1]);
    assertEquals("value4-1", arrays[2]);
    assertEquals("value5-1", arrays[3]);
    assertEquals(4, arrays.length);
    arrays = list.get(1);
    assertEquals("value2-7", arrays[0]);
    assertEquals("value3-2", arrays[1]);
    assertEquals("value4-2", arrays[2]);
    assertEquals("value5-2", arrays[3]);
    assertEquals(4, arrays.length);
    assertEquals(2, list.size());

    // No.11 
    tagCell = sheet3.getRow(21).getCell(2);
    list.clear();
    list = arraysParser.parse(sheet3, tagCell, data);
    arrays = list.get(0);
    assertEquals("value2-8", arrays[0]);
    assertEquals("value3-3", arrays[1]);
    assertEquals("value4-3", arrays[2]);
    assertEquals("value5-3", arrays[3]);
    assertEquals(4, arrays.length);
    arrays = list.get(1);
    assertEquals("value2-9", arrays[0]);
    assertEquals("value3-4", arrays[1]);
    assertEquals("value4-4", arrays[2]);
    assertEquals("value5-4", arrays[3]);
    assertEquals(4, arrays.length);
    assertEquals(2, list.size());

    // No.12 DataRowFrom > DataRowTo
    tagCell = sheet3.getRow(30).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(30, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.12:" + pe);
    }

    // No.13 DataColumnFrom > DataColumnTo
    tagCell = sheet3.getRow(40).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(40, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.13:" + pe);
    }

    // No.14 DataRowFrom??
    tagCell = sheet3.getRow(48).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(48, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.14:" + pe);
    }

    // No.15 DataRowTo??
    tagCell = sheet3.getRow(51).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(51, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.15:" + pe);
    }

    // No.16 DataColumnFrom??
    tagCell = sheet3.getRow(54).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(54, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.16:" + pe);
    }

    // No.17 DataColumnTo??
    tagCell = sheet3.getRow(57).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(57, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.17:" + pe);
    }

    // No.18 DataRowFrom??
    tagCell = sheet3.getRow(60).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(60, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.18:" + pe);
    }

    // No.19 DataRowTo??
    tagCell = sheet3.getRow(63).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(63, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.19:" + pe);
    }

    // No.20 DataColumnFrom??
    tagCell = sheet3.getRow(66).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(66, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.20:" + pe);
    }

    // No.21 DataColumnTo??
    tagCell = sheet3.getRow(69).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(69, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.21:" + pe);
    }

    // No.22 DataColumnFrom?A??
    tagCell = sheet4.getRow(2).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(2, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.22:" + pe);
    }

    // No.23 DataColumnTo?A??
    tagCell = sheet4.getRow(11).getCell(0);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(11, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.23:" + pe);
    }

    // No.24 DataRowFrom?1??
    tagCell = sheet4.getRow(0).getCell(3);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(3, cell.getColumnIndex());
        System.out.println("No.24:" + pe);
    }

    // No.25 DataRowTo?1??
    tagCell = sheet4.getRow(0).getCell(6);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(6, cell.getColumnIndex());
        System.out.println("No.25:" + pe);
    }

    // No.26 DataColumnFrom???
    tagCell = sheet4.getRow(2).getCell(11);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(2, cell.getRow().getRowNum());
        assertEquals(11, cell.getColumnIndex());
        System.out.println("No.26:" + pe);
    }

    // No.27 DataColumnTo???
    tagCell = sheet4.getRow(11).getCell(11);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(11, cell.getRow().getRowNum());
        assertEquals(11, cell.getColumnIndex());
        System.out.println("No.27:" + pe);
    }

    // No.28 DataRowFrom???
    tagCell = sheet4.getRow(18).getCell(3);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(3, cell.getColumnIndex());
        System.out.println("No.28:" + pe);
    }

    // No.29 DataRowTo???
    tagCell = sheet4.getRow(18).getCell(6);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(6, cell.getColumnIndex());
        System.out.println("No.29:" + pe);
    }

    // No.30 ?
    tagCell = sheet4.getRow(18).getCell(9);
    list.clear();
    try {
        list = arraysParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(9, cell.getColumnIndex());
        System.out.println("No.30:" + pe);
    }
}

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

License:Open Source License

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

    // ??
    List<Object> results = new ArrayList<Object>();

    try {
        // 
        int tagRowIdx = tagCell.getRowIndex();
        int valueRowFromIdx;
        int valueRowToIdx = sheet.getLastRowNum();
        int valueColIdx = tagCell.getColumnIndex();

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

        // ?
        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);
        }

        // Value??
        valueColIdx = TagUtil.adjustValue(valueColIdx, paramDef, PARAM_VALUE_COLUMN,
                DEFAULT_VALUE_COLUMN_ADJUST);
        if (valueColIdx > PoiUtil.getLastColNum(sheet) || valueColIdx < 0) {
            throw new ParseException(tagCell, "?" + PARAM_VALUE_COLUMN);
        }

        // ?
        for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {
            Row row = sheet.getRow(rowCnt);
            if (row != null) {
                Object value = PoiUtil.getCellValue(row.getCell(valueColIdx));
                results.add(value);
            }
        }

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

    return results;
}

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

License:Open Source License

@Test
public final void testListParser() throws ParseException {
    Workbook wk = getWorkbook();/*from w ww  .j a  v a2 s  .c  o  m*/
    Sheet sheet = wk.getSheetAt(0);
    ListParser listParser = new ListParser("@List");
    Cell tagCell = null;
    Object data = null;
    List<?> list = null;

    // No.1 @List
    tagCell = sheet.getRow(2).getCell(0);
    list = listParser.parse(sheet, tagCell, data);
    assertEquals("?1", list.get(0));
    assertEquals("?2", list.get(1));
    assertEquals("@List{DataRowFrom=2,DataRowTo=5}", list.get(2));
    assertEquals("", list.get(3));
    assertEquals("?3", list.get(4));
    assertEquals("?4", list.get(5));
    assertEquals("?5", list.get(6));
    assertEquals("@List{DataRowFrom=2,DataRowTo=3,ValueColumn=1}", list.get(7));
    assertEquals(null, list.get(8));
    assertEquals(null, list.get(9));
    assertEquals(null, list.get(10));
    assertEquals("@List{ValueColumn=-1}", list.get(11));
    assertEquals("?8", list.get(12));
    assertEquals(13, list.size());

    // No.2 @List{DataRowFrom=2,DataRowTo=5}
    tagCell = sheet.getRow(6).getCell(0);
    list.clear();
    list = listParser.parse(sheet, tagCell, data);
    assertEquals("?3", list.get(0));
    assertEquals("?4", list.get(1));
    assertEquals("?5", list.get(2));
    assertEquals(3, list.size());

    // No.3 @List{DataRowFrom=2,DataRowTo=3,ValueColumn=1}
    tagCell = sheet.getRow(14).getCell(0);
    list.clear();
    list = listParser.parse(sheet, tagCell, data);
    assertEquals("?6", list.get(0));
    assertEquals("?7", list.get(1));
    assertEquals(2, list.size());

    // No.4 @List{ValueColumn=-1}(1??????)
    tagCell = sheet.getRow(24).getCell(0);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(24, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.4:" + pe);
    }

    // No.5 @List{DataRowFrom=-1}(1?????)
    tagCell = sheet.getRow(0).getCell(4);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(4, cell.getColumnIndex());
        System.out.println("No.5:" + pe);
    }

    // No.6 @List{DataRowFrom=1}(???????)
    tagCell = sheet.getRow(25).getCell(4);
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(25, cell.getRow().getRowNum());
        assertEquals(4, cell.getColumnIndex());
        System.out.println("No.6:" + pe);
    }

    // No.7 @List{DataRowTo=-1}(1?????)
    tagCell = sheet.getRow(0).getCell(7);
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(7, cell.getColumnIndex());
        System.out.println("No.7:" + pe);
    }

    // No.8 @List{DataRowTo=1}(???????)
    tagCell = sheet.getRow(25).getCell(7);
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(25, cell.getRow().getRowNum());
        assertEquals(7, cell.getColumnIndex());
        System.out.println("No.8:" + pe);
    }

    // No.9 @List{ValueColumn=1}(??????)
    tagCell = sheet.getRow(2).getCell(10);
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(2, cell.getRow().getRowNum());
        assertEquals(10, cell.getColumnIndex());
        System.out.println("No.9:" + pe);
    }

    // 
    sheet = wk.getSheetAt(1);

    // No.10 @List{DataRowFrom=3,DataRowTo=1}(DataRowFrom > DataRowTo ??)
    tagCell = sheet.getRow(0).getCell(0);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.10:" + pe);
    }

    // No.11 @List{DataRowFrom=-1,DataRowTo=-3}(?)
    tagCell = sheet.getRow(13).getCell(0);
    list.clear();
    list = listParser.parse(sheet, tagCell, data);
    assertEquals("?2-4", list.get(0));
    assertEquals("?2-5", list.get(1));
    assertEquals("?2-6", list.get(2));
    assertEquals(3, list.size());

    // No.12 @List{ValueColumn=-1}(?)
    tagCell = sheet.getRow(29).getCell(1);
    list.clear();
    list = listParser.parse(sheet, tagCell, data);
    assertEquals("?2-7", list.get(0));
    assertEquals("?2-8", list.get(1));
    assertEquals("?2-9", list.get(2));
    assertEquals(3, list.size());

    // No.13 @List{DataRowFrom=1}(DataRowFrom??)
    tagCell = sheet.getRow(27).getCell(4);
    list.clear();
    list = listParser.parse(sheet, tagCell, data);
    assertEquals("?2-10", list.get(0));
    assertEquals("?2-11", list.get(1));
    assertEquals("?2-12", list.get(2));
    assertEquals(null, list.get(3));
    assertEquals("?2-13", list.get(4));
    assertEquals(5, list.size());

    // No.14 @List{DataRowTo=2}(DataRowTo??)
    tagCell = sheet.getRow(27).getCell(8);
    list.clear();
    list = listParser.parse(sheet, tagCell, data);
    assertEquals("?2-14", list.get(0));
    assertEquals("?2-15", list.get(1));
    assertEquals(2, list.size());

    // No.15 @List{DataRowFrom=}(DataRowFrom)
    tagCell = sheet.getRow(0).getCell(4);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(4, cell.getColumnIndex());
        System.out.println("No.15:" + pe);
    }

    // No.16 @List{DataRowTo=}(DataRowTo)
    tagCell = sheet.getRow(9).getCell(4);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(9, cell.getRow().getRowNum());
        assertEquals(4, cell.getColumnIndex());
        System.out.println("No.16:" + pe);
    }

    // No.17 @List{ValueColumn=}(ValueColumn)
    tagCell = sheet.getRow(18).getCell(4);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(4, cell.getColumnIndex());
        System.out.println("No.17:" + pe);
    }

    // No.18 @List{DataRowFrom=a}(DataRowFrom)
    tagCell = sheet.getRow(0).getCell(7);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(7, cell.getColumnIndex());
        System.out.println("No.18:" + pe);
    }

    // No.19 @List{DataRowTo=a}(DataRowTo)
    tagCell = sheet.getRow(9).getCell(7);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(9, cell.getRow().getRowNum());
        assertEquals(7, cell.getColumnIndex());
        System.out.println("No.19:" + pe);
    }

    // No.20 @List{ValueColumn=a}(ValueColumn)
    tagCell = sheet.getRow(18).getCell(7);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(7, cell.getColumnIndex());
        System.out.println("No.20:" + pe);
    }

    // 
    sheet = wk.getSheetAt(1);

    // No.21 @List(???)
    tagCell = sheet.getRow(0).getCell(0);
    list.clear();
    try {
        list = listParser.parse(sheet, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.21:" + pe);
    }
}

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

License:Open Source License

/**
 * ?//from w w w  . ja  v  a 2  s  .c  o  m
 * 
 * @param sheet 
 * @param tagCell ???
 * @param data BookController?parseBook(), parseSheet()?<BR>
 *              SheetParser?parseSheet?????<BR>
 *              TagParser??????<BR>
 * @return ?
 * @throws ParseException 
 */
@Override
public Map<?, ?> parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

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

    // 
    int keyColIdx = 0;
    // 
    int valueColIdx = 0;

    // 
    boolean keyTagFlag;
    // 
    boolean valueTagFlag;

    // 
    String defKey = null;
    // 
    String defValue = null;

    // 
    boolean keyCellTagFlag;
    // 
    boolean valueCellTagFlag;

    // 
    int keyRowIdx = 0;
    // 
    int valueRowIdx = 0;

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

        // ?
        checkParam(paramDef, tagCell);

        // ?
        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);
        }

        keyTagFlag = paramDef.containsKey(PARAM_KEY);
        keyCellTagFlag = paramDef.containsKey(PARAM_KEY_CELL);

        if (keyTagFlag) {
            // ???
            defKey = paramDef.get(PARAM_KEY);

        } else if (keyCellTagFlag) {
            // ???
            String value = paramDef.get(PARAM_KEY_CELL);

            // ??
            keyRowIdx = tagRowIdx + Integer.valueOf(value.split(PARAM_CELL_DELIM)[SPLIT_FIRST_INDEX]);
            if (keyRowIdx < 0 || keyRowIdx > sheet.getLastRowNum()) {
                throw new ParseException(tagCell, "?" + PARAM_KEY_CELL);
            }
            // ??
            keyColIdx = tagColIdx + Integer.valueOf(value.split(PARAM_CELL_DELIM)[SPLIT_LAST_INDEX]);
            if (keyColIdx > PoiUtil.getLastColNum(sheet) || keyColIdx < 0) {
                throw new ParseException(tagCell, "?" + PARAM_KEY_CELL);
            }

        } else {
            // ????
            // ??
            keyColIdx = TagUtil.adjustValue(tagColIdx, paramDef, PARAM_KEY_COLUMN, DEFAULT_KEY_COLUMN_ADJUST);
            if (keyColIdx > PoiUtil.getLastColNum(sheet) || keyColIdx < 0) {
                throw new ParseException(tagCell, "?" + PARAM_KEY_COLUMN);
            }
        }

        valueTagFlag = paramDef.containsKey(PARAM_VALUE);
        valueCellTagFlag = paramDef.containsKey(PARAM_VALUE_CELL);

        if (valueTagFlag) {
            // ???
            defValue = paramDef.get(PARAM_VALUE);

        } else if (valueCellTagFlag) {
            // ???
            String value = paramDef.get(PARAM_VALUE_CELL);

            // ??
            valueRowIdx = tagRowIdx + Integer.valueOf(value.split(PARAM_CELL_DELIM)[SPLIT_FIRST_INDEX]);
            if (valueRowIdx < 0 || valueRowIdx > sheet.getLastRowNum()) {
                throw new ParseException(tagCell, "?" + PARAM_VALUE_CELL);
            }

            // ??
            valueColIdx = tagColIdx + Integer.valueOf(value.split(PARAM_CELL_DELIM)[SPLIT_LAST_INDEX]);
            if (valueColIdx > PoiUtil.getLastColNum(sheet) || valueColIdx < 0) {
                throw new ParseException(tagCell, "?" + PARAM_VALUE_CELL);
            }

        } else {
            // ????
            // ??
            valueColIdx = TagUtil.adjustValue(tagColIdx, paramDef, PARAM_VALUE_COLUMN,
                    DEFAULT_VALUE_COLUMN_ADJUST);
            if (valueColIdx > PoiUtil.getLastColNum(sheet) || valueColIdx < 0) {
                throw new ParseException(tagCell, "?" + PARAM_VALUE_COLUMN);
            }
        }

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

    // ?
    Map<Object, Object> results = new LinkedHashMap<Object, Object>();
    Row keyRow = null;
    Row valueRow = null;
    for (int rowCnt = valueRowFromIdx; rowCnt <= valueRowToIdx; rowCnt++) {

        Object key;
        Object value;

        if (keyTagFlag) {
            // ???
            key = defKey;

        } else {
            // ????

            // ??
            if (keyCellTagFlag) {
                // ???
                keyRow = sheet.getRow(keyRowIdx);
            } else {
                // ????
                keyRow = sheet.getRow(rowCnt);
            }

            if (keyRow == null) {
                continue;
            }
            key = PoiUtil.getCellValue(keyRow.getCell(keyColIdx));
        }

        if (valueTagFlag) {
            // ???
            value = defValue;

        } else {
            // ????

            // ??
            if (valueCellTagFlag) {
                // ???
                valueRow = sheet.getRow(valueRowIdx);
            } else {
                // ????
                valueRow = sheet.getRow(rowCnt);
            }
            if (valueRow != null) {
                value = PoiUtil.getCellValue(valueRow.getCell(valueColIdx));
            } else {
                value = null;
            }
        }
        results.put(key, value);
    }
    return results;
}

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

License:Open Source License

@Test
public final void testMapParser() throws ParseException {
    Workbook wk = getWorkbook();/*ww w . jav  a  2  s . c o  m*/
    Sheet sheet1 = wk.getSheetAt(0);
    Sheet sheet2 = wk.getSheetAt(1);
    Sheet sheet3 = wk.getSheetAt(2);
    Sheet sheet4 = wk.getSheetAt(3);
    MapParser mapParser = new MapParser("@Map");
    Cell tagCell = null;
    Object data = null;
    Map<?, ?> map = null;

    // No.1 ?
    tagCell = sheet1.getRow(3).getCell(0);
    map = mapParser.parse(sheet1, tagCell, data);
    Set<?> keySet = map.keySet();
    assertEquals(25, keySet.size());
    assertEquals("1", map.get("1"));
    assertEquals("2", map.get("2"));
    assertTrue(keySet.contains("@Map{DataRowFrom=2,DataRowTo=5}"));
    assertEquals(null, map.get("@Map{DataRowFrom=2,DataRowTo=5}"));
    assertEquals("3", map.get("3"));
    assertEquals("4", map.get("4"));
    assertEquals("5", map.get("5"));
    assertEquals("6", map.get("6"));
    assertTrue(keySet.contains("@Map{DataRowFrom=2,DataRowTo=3,Key=}"));
    assertEquals(null, map.get("@Map{DataRowFrom=2,DataRowTo=3,Key=}"));
    assertEquals("7", map.get("7"));
    assertEquals("8", map.get("8"));
    assertTrue(keySet.contains("@Map{DataRowFrom=2,DataRowTo=3,Value=}"));
    assertEquals(null, map.get("@Map{DataRowFrom=2,DataRowTo=3,Value=}"));
    assertEquals("9", map.get("9"));
    assertEquals("10", map.get("10"));
    assertTrue(keySet.contains("@Map{DataRowFrom=2,DataRowTo=3,KeyColumn=2}"));
    assertEquals(null, map.get("@Map{DataRowFrom=2,DataRowTo=3,KeyColumn=2}"));
    assertTrue(keySet.contains("@Map{DataRowFrom=2,DataRowTo=3,ValueColumn=2}"));
    assertEquals(null, map.get("@Map{DataRowFrom=2,DataRowTo=3,ValueColumn=2}"));
    assertEquals("", map.get("13"));
    assertEquals("", map.get("14"));
    assertTrue(keySet.contains("@Map{DataRowFrom=2,DataRowTo=4,KeyCell=2:2}"));
    assertEquals(null, map.get("@Map{DataRowFrom=2,DataRowTo=4,KeyCell=2:2}"));
    assertEquals("17", map.get(""));
    assertTrue(keySet.contains("@Map{DataRowFrom=2,DataRowTo=4,ValueCell=4:3}"));
    assertEquals(null, map.get("@Map{DataRowFrom=2,DataRowTo=4,ValueCell=4:3}"));
    assertEquals("", map.get("18"));
    assertEquals("", map.get("19"));
    assertEquals("", map.get("20"));

    // No.2 
    tagCell = sheet1.getRow(7).getCell(0);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(4, keySet.size());
    assertEquals("3", map.get("3"));
    assertEquals("4", map.get("4"));
    assertEquals("5", map.get("5"));
    assertEquals("6", map.get("6"));

    // No.3 
    tagCell = sheet1.getRow(15).getCell(0);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(1, keySet.size());
    assertEquals("8", map.get(""));

    // No.4 
    tagCell = sheet1.getRow(24).getCell(0);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(2, keySet.size());
    assertEquals("", map.get("9"));
    assertEquals("", map.get("10"));

    // No.5 
    tagCell = sheet1.getRow(32).getCell(0);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(2, keySet.size());
    assertEquals("11", map.get("11"));
    assertEquals("12", map.get("12"));

    // No.6 
    tagCell = sheet1.getRow(41).getCell(0);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(2, keySet.size());
    assertEquals("13", map.get("13"));
    assertEquals("14", map.get("14"));

    // No.7 
    tagCell = sheet1.getRow(49).getCell(0);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(1, keySet.size());
    assertEquals("17", map.get("15"));

    // No.8 
    tagCell = sheet1.getRow(58).getCell(0);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(3, keySet.size());
    assertEquals("20", map.get("18"));
    assertEquals("20", map.get("19"));
    assertEquals("20", map.get("20"));

    // No.9 DataRowFrom > DataRowTo
    tagCell = sheet1.getRow(3).getCell(7);
    map.clear();
    try {
        map = mapParser.parse(sheet1, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(3, cell.getRow().getRowNum());
        assertEquals(7, cell.getColumnIndex());
        System.out.println("No.9:" + pe);
    }

    // No.10 
    tagCell = sheet1.getRow(18).getCell(7);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(4, keySet.size());
    assertEquals("5", map.get("5"));
    assertEquals("6", map.get("6"));
    assertEquals("7", map.get("7"));
    assertEquals("8", map.get("8"));

    // No.11 
    tagCell = sheet1.getRow(23).getCell(8);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(4, keySet.size());
    assertEquals("9", map.get("9"));
    assertEquals("10", map.get("10"));
    assertEquals("11", map.get("11"));
    assertEquals("12", map.get("12"));

    // No.12 
    tagCell = sheet1.getRow(33).getCell(8);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(4, keySet.size());
    assertEquals("13", map.get("13"));
    assertEquals("14", map.get("14"));
    assertEquals("15", map.get("15"));
    assertEquals("16", map.get("16"));

    // No.13 
    tagCell = sheet1.getRow(43).getCell(8);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(1, keySet.size());
    assertEquals("20", map.get(""));

    // No.14 
    tagCell = sheet1.getRow(53).getCell(8);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(4, keySet.size());
    assertEquals("", map.get("21"));
    assertEquals("", map.get("22"));
    assertEquals("", map.get("23"));
    assertEquals("", map.get("24"));

    // No.15 ??
    tagCell = sheet1.getRow(63).getCell(7);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(2, keySet.size());
    assertEquals("25", map.get("25"));
    assertEquals("26", map.get("26"));

    // No.16 ????
    tagCell = sheet1.getRow(71).getCell(7);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(1, keySet.size());
    assertEquals("@Map{DataRowFrom=0,DataRowTo=0,ValueColumn=0}",
            map.get("@Map{DataRowFrom=0,DataRowTo=0,ValueColumn=0}"));

    // No.17 ?
    tagCell = sheet1.getRow(76).getCell(7);
    map.clear();
    map = mapParser.parse(sheet1, tagCell, data);
    keySet = map.keySet();
    assertEquals(1, keySet.size());
    assertEquals("@Map{DataRowFrom=0,DataRowTo=0,KeyCell=0:0,ValueCell=0:0}",
            map.get("@Map{DataRowFrom=0,DataRowTo=0,KeyCell=0:0,ValueCell=0:0}"));

    // No.18 ?null
    tagCell = sheet2.getRow(3).getCell(0);
    map.clear();
    map = mapParser.parse(sheet2, tagCell, data);
    keySet = map.keySet();
    assertEquals(3, keySet.size());
    assertEquals("1", map.get("1"));
    assertTrue(keySet.contains(null));
    assertEquals("2", map.get(null));
    assertEquals("3", map.get("2"));

    // No.19 ?null
    tagCell = sheet2.getRow(11).getCell(0);
    map.clear();
    map = mapParser.parse(sheet2, tagCell, data);
    keySet = map.keySet();
    assertEquals(3, keySet.size());
    assertEquals("4", map.get("3"));
    assertEquals(null, map.get("4"));
    assertEquals("5", map.get("5"));

    // No.20 ?null
    tagCell = sheet2.getRow(19).getCell(0);
    map.clear();
    map = mapParser.parse(sheet2, tagCell, data);
    keySet = map.keySet();
    assertEquals(2, keySet.size());
    assertEquals("6", map.get("6"));
    assertEquals("7", map.get("7"));

    // No.21 ?null
    tagCell = sheet2.getRow(27).getCell(0);
    map.clear();
    map = mapParser.parse(sheet2, tagCell, data);
    keySet = map.keySet();
    assertEquals(3, keySet.size());
    assertEquals("8", map.get("8"));
    assertEquals("9", map.get(null));
    assertEquals("10", map.get("9"));

    // No.22 ??null
    tagCell = sheet2.getRow(35).getCell(0);
    map.clear();
    map = mapParser.parse(sheet2, tagCell, data);
    keySet = map.keySet();
    assertEquals(3, keySet.size());
    assertEquals("11", map.get("10"));
    assertTrue(keySet.contains("11"));
    assertEquals(null, map.get("11"));
    assertEquals("12", map.get("12"));

    // No.23 ???null
    tagCell = sheet2.getRow(43).getCell(0);
    map.clear();
    map = mapParser.parse(sheet2, tagCell, data);
    keySet = map.keySet();
    assertEquals(3, keySet.size());
    assertEquals("13", map.get("13"));
    assertTrue(keySet.contains(null));
    assertEquals(null, map.get(null));
    assertEquals("14", map.get("14"));

    // No.24 ??null
    tagCell = sheet2.getRow(51).getCell(0);
    map.clear();
    map = mapParser.parse(sheet2, tagCell, data);
    keySet = map.keySet();
    assertEquals(2, keySet.size());
    assertEquals(null, map.get("15"));
    assertEquals(null, map.get("16"));

    // No.25 ?Key, KeyColumn, KeyCell
    tagCell = sheet3.getRow(4).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(4, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.25:" + pe);
    }

    // No.26 ?Key, KeyColumn
    tagCell = sheet3.getRow(8).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(8, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.26:" + pe);
    }

    // No.27 ?Key, KeyCell
    tagCell = sheet3.getRow(12).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(12, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.27:" + pe);
    }

    // No.28 ?KeyColumn, KeyCell
    tagCell = sheet3.getRow(16).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet1, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(16, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.28:" + pe);
    }

    // No.29 ?Value, ValueColumn, ValueCell
    tagCell = sheet3.getRow(20).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(20, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.29:" + pe);
    }

    // No.30 ?Value, ValueColumn
    tagCell = sheet3.getRow(24).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(24, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.30:" + pe);
    }

    // No.31 ?Value, ValueCell
    tagCell = sheet3.getRow(28).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(28, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.31:" + pe);
    }

    // No.32 ?ValueColumn, ValueCell
    tagCell = sheet3.getRow(32).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(32, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.32:" + pe);
    }

    // No.33 ?
    tagCell = sheet3.getRow(38).getCell(1);
    map.clear();
    map = mapParser.parse(sheet3, tagCell, data);

    // No.34 DataRowFrom??
    tagCell = sheet3.getRow(41).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(41, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.33:" + pe);
    }

    // No.35 DataRowTo??
    tagCell = sheet3.getRow(44).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(44, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.35:" + pe);
    }

    // No.36 Key??
    tagCell = sheet3.getRow(47).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(47, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.36:" + pe);
    }

    // No.37 Value??
    tagCell = sheet3.getRow(50).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(50, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.37:" + pe);
    }

    // No.38 KeyColumn??
    tagCell = sheet3.getRow(53).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(53, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.38:" + pe);
    }

    // No.39 ValueColumn??
    tagCell = sheet3.getRow(56).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(56, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.39:" + pe);
    }

    // No.40 KeyCell??
    tagCell = sheet3.getRow(59).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(59, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.40:" + pe);
    }

    // No.41 ValueCell??
    tagCell = sheet3.getRow(62).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(62, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.41:" + pe);
    }

    // No.42 DataRowFrom??
    tagCell = sheet3.getRow(65).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(65, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.42:" + pe);
    }

    // No.43 DataRowTo??
    tagCell = sheet3.getRow(68).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(68, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.43:" + pe);
    }

    // No.44 KeyColumen??
    tagCell = sheet3.getRow(71).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(71, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.44:" + pe);
    }

    // No.45 ValueColumn??
    tagCell = sheet3.getRow(74).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(74, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.45:" + pe);
    }

    // No.46 KeyCell??
    tagCell = sheet3.getRow(77).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(77, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.46:" + pe);
    }

    // No.47 ValueCell??
    tagCell = sheet3.getRow(80).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(80, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.47:" + pe);
    }

    // No.48 KeyColumn?A??
    tagCell = sheet4.getRow(2).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(2, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.48:" + pe);
    }

    // No.49 ValueColumn?A??
    tagCell = sheet4.getRow(11).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(11, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.49:" + pe);
    }

    // No.50 KeyCell?A??
    tagCell = sheet4.getRow(20).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(20, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.50:" + pe);
    }

    // No.51 ValueCell?A??
    tagCell = sheet4.getRow(29).getCell(0);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(29, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.51:" + pe);
    }

    // No.52 DataRowFrom?1??
    tagCell = sheet4.getRow(0).getCell(3);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(3, cell.getColumnIndex());
        System.out.println("No.52:" + pe);
    }

    // No.53 DataRowTo?1??
    tagCell = sheet4.getRow(0).getCell(6);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(6, cell.getColumnIndex());
        System.out.println("No.53:" + pe);
    }

    // No.54 KeyCell?1??
    tagCell = sheet4.getRow(0).getCell(9);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(9, cell.getColumnIndex());
        System.out.println("No.54:" + pe);
    }

    // No.55 ValueCell?1??
    tagCell = sheet4.getRow(0).getCell(12);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(12, cell.getColumnIndex());
        System.out.println("No.55:" + pe);
    }

    // No.56 KeyColumn???
    tagCell = sheet4.getRow(3).getCell(16);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(3, cell.getRow().getRowNum());
        assertEquals(16, cell.getColumnIndex());
        System.out.println("No.56:" + pe);
    }

    // No.57 ValueColumn???
    tagCell = sheet4.getRow(12).getCell(16);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(12, cell.getRow().getRowNum());
        assertEquals(16, cell.getColumnIndex());
        System.out.println("No.57:" + pe);
    }

    // No.58 KeyCell???
    tagCell = sheet4.getRow(21).getCell(16);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(21, cell.getRow().getRowNum());
        assertEquals(16, cell.getColumnIndex());
        System.out.println("No.58:" + pe);
    }

    // No.59 ValueCell???
    tagCell = sheet4.getRow(30).getCell(16);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(30, cell.getRow().getRowNum());
        assertEquals(16, cell.getColumnIndex());
        System.out.println("No.59:" + pe);
    }

    // No.60 DataRowFrom???
    tagCell = sheet4.getRow(34).getCell(3);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(34, cell.getRow().getRowNum());
        assertEquals(3, cell.getColumnIndex());
        System.out.println("No.60:" + pe);
    }

    // No.61 DataRowTo???
    tagCell = sheet4.getRow(34).getCell(6);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(34, cell.getRow().getRowNum());
        assertEquals(6, cell.getColumnIndex());
        System.out.println("No.61:" + pe);
    }

    // No.62 KeyCell???
    tagCell = sheet4.getRow(34).getCell(9);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(34, cell.getRow().getRowNum());
        assertEquals(9, cell.getColumnIndex());
        System.out.println("No.62:" + pe);
    }

    // No.63 ValueCell???
    tagCell = sheet4.getRow(34).getCell(12);
    map.clear();
    try {
        map = mapParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(34, cell.getRow().getRowNum());
        assertEquals(12, cell.getColumnIndex());
        System.out.println("No.63:" + pe);
    }
}

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

License:Open Source License

@Test
public final void testMapsParser() throws ParseException {
    Workbook wk = getWorkbook();/*  w w  w  .j  a  v a 2  s .c o m*/
    Sheet sheet1 = wk.getSheetAt(0);
    Sheet sheet2 = wk.getSheetAt(1);
    Sheet sheet3 = wk.getSheetAt(2);
    Sheet sheet4 = wk.getSheetAt(3);
    Sheet sheet5 = wk.getSheetAt(4);
    MapsParser mapsParser = new MapsParser("@Maps");
    Cell tagCell = null;
    Object data = null;
    List<Map<?, ?>> maps = null;

    // No.1 ?
    tagCell = sheet1.getRow(5).getCell(0);
    maps = mapsParser.parse(sheet1, tagCell, data);
    assertEquals("value1-1", maps.get(0).get("key1"));
    assertEquals("value2-1", maps.get(0).get("key2"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals("value1-2", maps.get(1).get("key1"));
    assertEquals("value2-2", maps.get(1).get("key2"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals("value1-3", maps.get(2).get("key1"));
    assertEquals("value2-3", maps.get(2).get("key2"));
    assertEquals(2, maps.get(2).keySet().size());
    assertEquals(3, maps.size());

    // No.2 ?
    tagCell = sheet2.getRow(5).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet2, tagCell, data);
    assertEquals(0, maps.size());

    // No.3 
    tagCell = sheet3.getRow(5).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value1-1", maps.get(0).get("key1"));
    assertEquals("value2-1", maps.get(0).get("key2"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals("value1-2", maps.get(1).get("key1"));
    assertEquals("value2-2", maps.get(1).get("key2"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals(2, maps.size());

    // No.4 ??
    tagCell = sheet3.getRow(13).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value3-1", maps.get(0).get("key3"));
    assertEquals("value4-1", maps.get(0).get("key4"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals("value3-2", maps.get(1).get("key3"));
    assertEquals("value4-2", maps.get(1).get("key4"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals(2, maps.size());

    // No.5 
    tagCell = sheet3.getRow(23).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value5-1", maps.get(0).get("key5"));
    assertEquals("value6-1", maps.get(0).get("key6"));
    assertEquals(null, maps.get(2).get("key7"));
    assertEquals("value8-1", maps.get(0).get("key8"));
    assertEquals(3, maps.get(0).keySet().size());
    assertEquals("value5-2", maps.get(1).get("key5"));
    assertEquals("value6-2", maps.get(1).get("key6"));
    assertEquals(null, maps.get(2).get("key7"));
    assertEquals("value8-2", maps.get(1).get("key8"));
    assertEquals(3, maps.get(1).keySet().size());
    assertEquals("value5-3", maps.get(2).get("key5"));
    assertEquals("value6-3", maps.get(2).get("key6"));
    assertEquals(null, maps.get(2).get("key7"));
    assertEquals("value8-3", maps.get(2).get("key8"));
    assertEquals(3, maps.get(2).keySet().size());
    assertEquals(3, maps.size());

    // No.6 ?null
    tagCell = sheet3.getRow(31).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals(0, maps.size());

    // No.7 ?null
    tagCell = sheet3.getRow(39).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value9-1", maps.get(0).get("key9"));
    assertEquals("value10-1", maps.get(0).get("key10"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals("value9-2", maps.get(1).get("key9"));
    assertEquals("value10-2", maps.get(1).get("key10"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals("value9-3", maps.get(2).get("key9"));
    assertEquals("value10-3", maps.get(2).get("key10"));
    assertEquals(2, maps.get(2).keySet().size());
    assertEquals(3, maps.size());

    // No.8 ?null
    tagCell = sheet3.getRow(47).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value11-1", maps.get(0).get("key11"));
    assertEquals("value12-1", maps.get(0).get("key12"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals("value11-2", maps.get(1).get("key11"));
    assertEquals("value12-2", maps.get(1).get("key12"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals("value11-3", maps.get(2).get("key11"));
    assertEquals("value12-3", maps.get(2).get("key12"));
    assertEquals(2, maps.get(2).keySet().size());
    assertEquals(3, maps.size());

    // No.9 ?null
    tagCell = sheet3.getRow(55).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value13-1", maps.get(0).get("key13"));
    assertEquals("value14-1", maps.get(0).get("key14"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals("value13-2", maps.get(1).get("key13"));
    assertEquals("value14-2", maps.get(1).get("key14"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals(2, maps.size());

    // No.10 ?null
    tagCell = sheet3.getRow(63).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value15-1", maps.get(0).get("key15"));
    assertEquals("value16-1", maps.get(0).get("key16"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals(null, maps.get(1).get("key15"));
    assertEquals(null, maps.get(1).get("key16"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals("value15-2", maps.get(2).get("key15"));
    assertEquals("value16-2", maps.get(2).get("key16"));
    assertEquals(2, maps.get(2).keySet().size());
    assertEquals(3, maps.size());

    // No.11 ?null
    tagCell = sheet3.getRow(71).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value17-1", maps.get(0).get("key17"));
    assertEquals("value18-1", maps.get(0).get("key18"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals(null, maps.get(1).get("key17"));
    assertEquals(null, maps.get(1).get("key18"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals("value17-2", maps.get(2).get("key17"));
    assertEquals("value18-2", maps.get(2).get("key18"));
    assertEquals(2, maps.get(2).keySet().size());
    assertEquals(3, maps.size());

    // No.12 ?
    tagCell = sheet3.getRow(79).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet3, tagCell, data);
    assertEquals("value19-4", maps.get(0).get("key19"));
    assertEquals(1, maps.get(0).keySet().size());
    assertEquals(null, maps.get(1).get("key19"));
    assertEquals(1, maps.get(1).keySet().size());
    assertEquals("value19-5", maps.get(2).get("key19"));
    assertEquals(1, maps.get(2).keySet().size());
    assertEquals(3, maps.size());

    // No.13 
    tagCell = sheet4.getRow(7).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet4, tagCell, data);
    assertEquals("value1-1", maps.get(0).get("key1"));
    assertEquals("value2-1", maps.get(0).get("key2"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals("value1-2", maps.get(1).get("key1"));
    assertEquals("value2-2", maps.get(1).get("key2"));
    assertEquals(2, maps.get(1).keySet().size());
    assertEquals("value1-3", maps.get(2).get("key1"));
    assertEquals("value2-3", maps.get(2).get("key2"));
    assertEquals(2, maps.get(2).keySet().size());
    assertEquals("value1-4", maps.get(3).get("key1"));
    assertEquals("value2-4", maps.get(3).get("key2"));
    assertEquals(2, maps.get(3).keySet().size());
    assertEquals(4, maps.size());

    // No.14 ?
    tagCell = sheet4.getRow(12).getCell(0);
    maps.clear();
    maps = mapsParser.parse(sheet4, tagCell, data);
    assertEquals("1", maps.get(0).get("1"));
    assertEquals("2", maps.get(0).get("2"));
    assertEquals(2, maps.get(0).keySet().size());
    assertEquals(1, maps.size());

    // No.15 DataRowFrom > DataRowTo
    tagCell = sheet4.getRow(21).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(21, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.15:" + pe);
    }

    // No.16 KeyRow??
    tagCell = sheet4.getRow(27).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(27, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.16:" + pe);
    }

    // No.17 DataRowFrom??
    tagCell = sheet4.getRow(30).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(30, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.17:" + pe);
    }

    // No.18 DataRowTo??
    tagCell = sheet4.getRow(33).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(33, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.18:" + pe);
    }

    // No.19 KeyRow??
    tagCell = sheet4.getRow(36).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(36, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.19:" + pe);
    }

    // No.20 DataRowFrom??
    tagCell = sheet4.getRow(39).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(39, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.20:" + pe);
    }

    // No.21 DataRowFrom??
    tagCell = sheet4.getRow(42).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet4, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(42, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.21:" + pe);
    }

    // No.22 KeyRow?1??
    tagCell = sheet5.getRow(0).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.22:" + pe);
    }

    // No.23 DataRowFrom?1??
    tagCell = sheet5.getRow(0).getCell(3);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(3, cell.getColumnIndex());
        System.out.println("No.23:" + pe);
    }

    // No.24 DataRowTo?1??
    tagCell = sheet5.getRow(0).getCell(6);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(6, cell.getColumnIndex());
        System.out.println("No.24:" + pe);
    }

    // No.25 KeyRow???
    tagCell = sheet5.getRow(18).getCell(0);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.25:" + pe);
    }

    // No.26 DataRowFrom???
    tagCell = sheet5.getRow(18).getCell(3);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(3, cell.getColumnIndex());
        System.out.println("No.26:" + pe);
    }

    // No.27 DataRowTo???
    tagCell = sheet5.getRow(18).getCell(6);
    maps.clear();
    try {
        maps = mapsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(6, cell.getColumnIndex());
        System.out.println("No.27:" + pe);
    }
}

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

License:Open Source License

@Test
public final void testObjectsParser() throws ParseException, java.text.ParseException {
    Workbook wk = getWorkbook();/*from   w ww  . j a  va2 s . com*/
    Sheet sheet1 = wk.getSheetAt(0);
    Sheet sheet2 = wk.getSheetAt(1);
    Sheet sheet3 = wk.getSheetAt(2);
    Sheet sheet4 = wk.getSheetAt(3);
    Sheet sheet5 = wk.getSheetAt(4);
    ObjectsParser objectsParser = new ObjectsParser("@Objects");
    Cell tagCell = null;
    Object data = null;
    List<Object> list = null;

    // No.1 ?
    tagCell = sheet1.getRow(5).getCell(0);
    try {
        list = objectsParser.parse(sheet1, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(5, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.1:" + pe);
    }

    // No.2 
    tagCell = sheet1.getRow(13).getCell(0);
    list = objectsParser.parse(sheet1, tagCell, data);
    TargetEntity targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Integer("1"), targetEntity.getNumberInteger());
    assertEquals(2, targetEntity.getNumberInt());
    assertEquals(new Long("3"), targetEntity.getNumberLong());
    assertEquals(4, targetEntity.getNumberlong());
    assertEquals(new Float("5.5"), targetEntity.getNumberFloat());
    assertEquals("6.6", String.valueOf(targetEntity.getNumberfloat()));
    assertEquals(new Double("8.8"), targetEntity.getNumberDouble());
    assertEquals("9.9", String.valueOf(targetEntity.getNumberdouble()));
    assertEquals(new BigDecimal(10.1), targetEntity.getNumberDecimal());
    assertEquals(new Byte("11"), targetEntity.getNumberByte());
    assertEquals(12, targetEntity.getNumberbyte());
    assertEquals(Boolean.TRUE, targetEntity.getValueBoolean());
    assertEquals(Boolean.FALSE, targetEntity.isValueboolean());
    assertEquals(DateFormat.getDateInstance().parse("2009/4/13"), targetEntity.getDate());
    assertEquals("", targetEntity.getString());
    assertEquals(1, list.size());

    // No.3 ??
    tagCell = sheet2.getRow(4).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Integer("1"), targetEntity.getNumberInteger());
    assertEquals(3, targetEntity.getNumberInt());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(new Integer("2"), targetEntity.getNumberInteger());
    assertEquals(4, targetEntity.getNumberInt());
    assertEquals(2, list.size());

    // No.4 
    tagCell = sheet2.getRow(13).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Integer("5"), targetEntity.getNumberInteger());
    assertEquals(9, targetEntity.getNumberInt());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(new Integer("6"), targetEntity.getNumberInteger());
    assertEquals(10, targetEntity.getNumberInt());
    assertEquals(2, list.size());

    // No.5 ?null
    tagCell = sheet2.getRow(20).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    assertEquals(0, list.size());

    // No.6 ?null
    tagCell = sheet2.getRow(28).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(20000000, targetEntity.getNumberlong());
    assertEquals(new Long("26000000"), targetEntity.getNumberLong());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(21000000, targetEntity.getNumberlong());
    assertEquals(new Long("27000000"), targetEntity.getNumberLong());
    targetEntity = (TargetEntity) list.get(2);
    assertEquals(22000000, targetEntity.getNumberlong());
    assertEquals(new Long("28000000"), targetEntity.getNumberLong());
    assertEquals(3, list.size());

    // No.7 ?null
    tagCell = sheet2.getRow(36).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(29000000, targetEntity.getNumberlong());
    assertEquals(new Long("35000000"), targetEntity.getNumberLong());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(30000000, targetEntity.getNumberlong());
    assertEquals(new Long("36000000"), targetEntity.getNumberLong());
    targetEntity = (TargetEntity) list.get(2);
    assertEquals(31000000, targetEntity.getNumberlong());
    assertEquals(new Long("37000000"), targetEntity.getNumberLong());
    assertEquals(3, list.size());

    // No.8 ?null
    tagCell = sheet2.getRow(44).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Float("38000000"), targetEntity.getNumberFloat());
    assertEquals(40000000, targetEntity.getNumberfloat(), 0.01);
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(new Float("39000000"), targetEntity.getNumberFloat());
    assertEquals(41000000, targetEntity.getNumberfloat(), 0.01);
    assertEquals(2, list.size());

    // No.9 ?null
    tagCell = sheet2.getRow(52).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Float("42000000"), targetEntity.getNumberFloat());
    assertEquals(new Double("44000000"), targetEntity.getNumberDouble());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(null, targetEntity.getNumberFloat());
    assertEquals(null, targetEntity.getNumberDouble());
    targetEntity = (TargetEntity) list.get(2);
    assertEquals(new Float("43000000"), targetEntity.getNumberFloat());
    assertEquals(new Double("45000000"), targetEntity.getNumberDouble());
    assertEquals(3, list.size());

    // No.10 ?null
    tagCell = sheet2.getRow(60).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Float("46000000"), targetEntity.getNumberFloat());
    assertEquals(new Double("48000000"), targetEntity.getNumberDouble());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(null, targetEntity.getNumberFloat());
    assertEquals(null, targetEntity.getNumberDouble());
    targetEntity = (TargetEntity) list.get(2);
    assertEquals(new Float("47000000"), targetEntity.getNumberFloat());
    assertEquals(new Double("49000000"), targetEntity.getNumberDouble());
    assertEquals(3, list.size());

    // No.11 ?
    tagCell = sheet2.getRow(68).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet2, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Double("8.8888888"), targetEntity.getNumberDouble());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(null, targetEntity.getNumberDouble());
    targetEntity = (TargetEntity) list.get(2);
    assertEquals(new Double("9.9999999"), targetEntity.getNumberDouble());
    assertEquals(3, list.size());

    // No.12 
    tagCell = sheet3.getRow(7).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet3, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(Boolean.TRUE, targetEntity.getValueBoolean());
    assertEquals("1", targetEntity.getString());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(Boolean.TRUE, targetEntity.getValueBoolean());
    assertEquals("2", targetEntity.getString());
    targetEntity = (TargetEntity) list.get(2);
    assertEquals(Boolean.FALSE, targetEntity.getValueBoolean());
    assertEquals("3", targetEntity.getString());
    targetEntity = (TargetEntity) list.get(3);
    assertEquals(Boolean.FALSE, targetEntity.getValueBoolean());
    assertEquals("4", targetEntity.getString());
    assertEquals(4, list.size());

    // No.13 ????
    tagCell = sheet3.getRow(12).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet3, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals("string", targetEntity.getString());
    assertEquals(1, list.size());

    // No.14 DataRowFrom > DataRowTo
    tagCell = sheet3.getRow(21).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(21, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.14:" + pe);
    }

    // No.15 PropertyRow??
    tagCell = sheet3.getRow(27).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(27, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.15:" + pe);
    }

    // No.16 DataRowFrom??
    tagCell = sheet3.getRow(30).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(30, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.16:" + pe);
    }

    // No.17 DataRowTo??
    tagCell = sheet3.getRow(33).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(33, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.17:" + pe);
    }

    // No.18 PropertyRow??
    tagCell = sheet3.getRow(36).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(36, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.18:" + pe);
    }

    // No.19 DataRowFrom??
    tagCell = sheet3.getRow(39).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(39, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.19:" + pe);
    }

    // No.20 DataRowTo??
    tagCell = sheet3.getRow(42).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(42, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.20:" + pe);
    }

    // No.21 Class????
    tagCell = sheet3.getRow(46).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(46, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.21:" + pe);
    }

    // No.22 ???
    tagCell = sheet3.getRow(50).getCell(0);
    list.clear();
    list = objectsParser.parse(sheet3, tagCell, data);
    assertEquals(0, list.size());

    // No.23 ?
    tagCell = sheet3.getRow(55).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(57, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.23:" + pe);
    }

    // No.24 ?
    tagCell = sheet3.getRow(61).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(62, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.24:" + pe);
    }

    // No.25 ??private
    tagCell = sheet3.getRow(67).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(68, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.25:" + pe);
    }

    // No.26 ??
    tagCell = sheet3.getRow(73).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet3, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(74, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.26:" + pe);
    }

    // No.27 
    tagCell = sheet4.getRow(1).getCell(0);
    ChildNameParser childNameParser = new ChildNameParser("");
    childNameParser.setTag("@childName");
    assertEquals("@childName", childNameParser.getTag());
    assertEquals(Boolean.FALSE, childNameParser.isParse(sheet4, null));

    objectsParser.addPropertyParser(childNameParser);
    list.clear();
    list = objectsParser.parse(sheet4, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals("childName1", targetEntity.getChildEntity().getChildName());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals("childName2", targetEntity.getChildEntity().getChildName());
    assertEquals(2, list.size());

    // No.28 ?
    tagCell = sheet4.getRow(8).getCell(0);
    list.clear();
    objectsParser.removePropertyParser(childNameParser);
    list = objectsParser.parse(sheet4, tagCell, data);
    assertEquals(0, list.size());

    // No.29 ?
    tagCell = sheet4.getRow(15).getCell(0);
    list.clear();
    ChildNoParser childNoParser = new ChildNoParser("@childNo");
    objectsParser.addPropertyParser(childNameParser);
    objectsParser.addPropertyParser(childNoParser);
    list = objectsParser.parse(sheet4, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Integer("55"), targetEntity.getNumberInteger());
    assertEquals("childName5", targetEntity.getChildEntity().getChildName());
    assertEquals(5, targetEntity.getChildEntity().getChildNo());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(new Integer("66"), targetEntity.getNumberInteger());
    assertEquals("childName6", targetEntity.getChildEntity().getChildName());
    assertEquals(6, targetEntity.getChildEntity().getChildNo());
    assertEquals(2, list.size());

    // No.30 
    tagCell = sheet4.getRow(22).getCell(0);
    list.clear();
    objectsParser.clearPropertyParsers();
    list = objectsParser.parse(sheet4, tagCell, data);
    targetEntity = (TargetEntity) list.get(0);
    assertEquals(new Integer("77"), targetEntity.getNumberInteger());
    assertEquals(null, targetEntity.getChildEntity());
    targetEntity = (TargetEntity) list.get(1);
    assertEquals(new Integer("88"), targetEntity.getNumberInteger());
    assertEquals(null, targetEntity.getChildEntity());
    assertEquals(2, list.size());

    // No.31 PropertyRow?1??
    tagCell = sheet5.getRow(0).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.31:" + pe);
    }

    // No.32 DataRowFrom?1??
    tagCell = sheet5.getRow(0).getCell(8);
    list.clear();
    try {
        list = objectsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(8, cell.getColumnIndex());
        System.out.println("No.32:" + pe);
    }

    // No.33 DataRowTo?1??
    tagCell = sheet5.getRow(0).getCell(16);
    list.clear();
    try {
        list = objectsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(0, cell.getRow().getRowNum());
        assertEquals(16, cell.getColumnIndex());
        System.out.println("No.33:" + pe);
    }

    // No.34 PropertyRow???
    tagCell = sheet5.getRow(18).getCell(0);
    list.clear();
    try {
        list = objectsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(0, cell.getColumnIndex());
        System.out.println("No.34:" + pe);
    }

    // No.35 DataRowFrom???
    tagCell = sheet5.getRow(18).getCell(8);
    list.clear();
    try {
        list = objectsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(8, cell.getColumnIndex());
        System.out.println("No.35:" + pe);
    }

    // No.36 DataRowTo???
    tagCell = sheet5.getRow(18).getCell(16);
    list.clear();
    try {
        list = objectsParser.parse(sheet5, tagCell, data);
        fail();
    } catch (ParseException pe) {
        Cell cell = pe.getCell();
        assertEquals(18, cell.getRow().getRowNum());
        assertEquals(16, cell.getColumnIndex());
        System.out.println("No.36:" + pe);
    }
}

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 w  w  .j  av  a 2s  . c om*/
    // ----------------------

    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.core.util.PoiUtil.java

License:Open Source License

/**
 * ?/*from  www.  j a  v  a  2s .  c  om*/
 * 
 * @param fromSheet 
 * @param rangeAddress 
 * @param toSheet 
 * @param toRowNum 
 * @param toColumnNum 
 * @param clearFromRange 
 */
public static void copyRange(Sheet fromSheet, CellRangeAddress rangeAddress, Sheet toSheet, int toRowNum,
        int toColumnNum, boolean clearFromRange) {

    if (fromSheet == null || rangeAddress == null || toSheet == null) {
        return;
    }

    int fromRowIndex = rangeAddress.getFirstRow();
    int fromColumnIndex = rangeAddress.getFirstColumn();

    int rowNumOffset = toRowNum - fromRowIndex;
    int columnNumOffset = toColumnNum - fromColumnIndex;

    // 
    CellRangeAddress toAddress = new CellRangeAddress(rangeAddress.getFirstRow() + rowNumOffset,
            rangeAddress.getLastRow() + rowNumOffset, rangeAddress.getFirstColumn() + columnNumOffset,
            rangeAddress.getLastColumn() + columnNumOffset);

    Workbook fromWorkbook = fromSheet.getWorkbook();
    Sheet baseSheet = fromSheet;

    Sheet tmpSheet = null;
    // ?????
    if (fromSheet.equals(toSheet) && crossRangeAddress(rangeAddress, toAddress)) {
        // ?
        tmpSheet = fromWorkbook.getSheet(TMP_SHEET_NAME);
        if (tmpSheet == null) {
            tmpSheet = fromWorkbook.createSheet(TMP_SHEET_NAME);
        }
        baseSheet = tmpSheet;

        int lastColNum = getLastColNum(fromSheet);
        for (int i = 0; i <= lastColNum; i++) {
            tmpSheet.setColumnWidth(i, fromSheet.getColumnWidth(i));
        }

        copyRange(fromSheet, rangeAddress, tmpSheet, rangeAddress.getFirstRow(), rangeAddress.getFirstColumn(),
                false);

        // ?
        if (clearFromRange) {
            clearRange(fromSheet, rangeAddress);
        }
    }

    // ????
    Set<CellRangeAddress> targetCellSet = getMergedAddress(baseSheet, rangeAddress);
    // ???
    clearRange(toSheet, toAddress);

    // ???
    for (CellRangeAddress mergeAddress : targetCellSet) {

        toSheet.addMergedRegion(new CellRangeAddress(mergeAddress.getFirstRow() + rowNumOffset,
                mergeAddress.getLastRow() + rowNumOffset, mergeAddress.getFirstColumn() + columnNumOffset,
                mergeAddress.getLastColumn() + columnNumOffset));

    }

    for (int i = rangeAddress.getFirstRow(); i <= rangeAddress.getLastRow(); i++) {
        // 
        Row fromRow = baseSheet.getRow(i);
        if (fromRow == null) {
            continue;
        }
        Row row = toSheet.getRow(i + rowNumOffset);
        if (row == null) {
            row = toSheet.createRow(i + rowNumOffset);
            row.setHeight((short) 0);
        }

        // ??????
        int fromRowHeight = fromRow.getHeight();
        int toRowHeight = row.getHeight();
        if (toRowHeight < fromRowHeight) {
            row.setHeight(fromRow.getHeight());
        }

        ColumnHelper columnHelper = null;
        if (toSheet instanceof XSSFSheet) {
            XSSFSheet xssfSheet = (XSSFSheet) toSheet.getWorkbook()
                    .getSheetAt(toSheet.getWorkbook().getSheetIndex(toSheet));
            CTWorksheet ctWorksheet = xssfSheet.getCTWorksheet();
            columnHelper = new ColumnHelper(ctWorksheet);
        }

        for (int j = rangeAddress.getFirstColumn(); j <= rangeAddress.getLastColumn(); j++) {
            Cell fromCell = fromRow.getCell(j);
            if (fromCell == null) {
                continue;
            }
            int maxColumn = SpreadsheetVersion.EXCEL97.getMaxColumns();
            if (toSheet instanceof XSSFSheet) {
                maxColumn = SpreadsheetVersion.EXCEL2007.getMaxColumns();
            }
            if (j + columnNumOffset >= maxColumn) {
                break;
            }
            Cell cell = row.getCell(j + columnNumOffset);
            if (cell == null) {
                cell = row.createCell(j + columnNumOffset);
                if (toSheet instanceof XSSFSheet) {
                    // XSSF??????????
                    CTCol col = columnHelper.getColumn(cell.getColumnIndex(), false);
                    if (col == null || !col.isSetWidth()) {
                        toSheet.setColumnWidth(cell.getColumnIndex(), baseSheet.getColumnWidth(j));
                    }
                }
            }

            // ?
            copyCell(fromCell, cell);

            // ??????
            int fromColumnWidth = baseSheet.getColumnWidth(j);
            int toColumnWidth = toSheet.getColumnWidth(j + columnNumOffset);

            if (toColumnWidth < fromColumnWidth) {
                toSheet.setColumnWidth(j + columnNumOffset, baseSheet.getColumnWidth(j));
            }
        }
    }

    if (tmpSheet != null) {
        // 
        fromWorkbook.removeSheetAt(fromWorkbook.getSheetIndex(tmpSheet));
    } else if (clearFromRange) {
        // ????
        clearRange(fromSheet, rangeAddress);
    }

}

From source file:org.bbreak.excella.core.util.PoiUtil.java

License:Open Source License

/**
 * ??????/*from   www  .  ja  v a2s  .  c  o  m*/
 * 
 * @param sheet 
 * @param firstColumnIndex 
 * @param lastColmunIndex 
 * @return ?
 */
public static int getLastRowNum(Sheet sheet, int firstColumnIndex, int lastColmunIndex) {
    // ??
    int sheetLastRowNum = sheet.getLastRowNum();

    int rangeLastRowNum = -1;
    // ???
    for (int i = sheetLastRowNum; 0 <= i; i--) {
        Row row = sheet.getRow(i);
        if (row == null) {
            continue;
        }
        Iterator<Cell> rowIterator = row.iterator();
        while (rowIterator.hasNext()) {
            Cell cell = rowIterator.next();
            if (cell != null) {
                if (firstColumnIndex <= cell.getColumnIndex() && cell.getColumnIndex() <= lastColmunIndex) {
                    rangeLastRowNum = i;
                    break;
                }
            }
        }
        if (rangeLastRowNum != -1) {
            break;
        }
    }
    return rangeLastRowNum;
}