PageRenderTime 64ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/thoughtworks/studio/tools/cardkit/ExcelIdentifier.java

https://bitbucket.org/sagittatius/cardplayer
Java | 144 lines | 124 code | 20 blank | 0 comment | 16 complexity | 4a4e19c7f14bb6beed88e7bdbd211bfc MD5 | raw file
  1. package com.thoughtworks.studio.tools.cardkit;
  2. import static com.thoughtworks.studio.tools.cardkit.util.ExceptionUtils.bomb;
  3. import com.thoughtworks.studio.tools.cardkit.io.ParseException;
  4. import com.thoughtworks.studio.tools.cardkit.io.RetrieverException;
  5. import com.thoughtworks.studio.tools.cardkit.card.Card;
  6. import com.thoughtworks.studio.tools.cardkit.card.Cards;
  7. import java.util.*;
  8. import java.io.FileNotFoundException;
  9. import java.io.IOException;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;
  14. import org.apache.poi.hssf.usermodel.HSSFRow;
  15. import org.apache.poi.hssf.usermodel.HSSFCell;
  16. import org.apache.log4j.Logger;
  17. public class ExcelIdentifier extends CardWallIdentifier {
  18. private HSSFWorkbook workbook;
  19. private Logger logger = Logger.getLogger(ExcelIdentifier.class);
  20. private List<List<Object>> cardMatrix;
  21. private Map<String,Card> cards = null;
  22. public ExcelIdentifier(String name, String source) {
  23. super(name,source);
  24. }
  25. public Cards importCards() throws RetrieverException, ParseException{
  26. try {
  27. return parse();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. throw new ParseException(e.getMessage());
  31. }
  32. }
  33. public List<List<Object>> getDatasInSheet(int sheetNumber)
  34. throws IOException {
  35. List<List<Object>> result = new ArrayList<List<Object>>();
  36. HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
  37. int rowCount = sheet.getLastRowNum();
  38. if (rowCount < 1) {
  39. return result;
  40. }
  41. for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
  42. HSSFRow row = sheet.getRow(rowIndex);
  43. if (row != null) {
  44. List<Object> rowData = new ArrayList<Object>();
  45. int columnCount = row.getLastCellNum();
  46. logger.debug("excel columnCount= " + columnCount);
  47. for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
  48. HSSFCell cell = row.getCell(columnIndex);
  49. Object cellStr = this.getCellString(cell);
  50. rowData.add(cellStr);
  51. }
  52. result.add(rowData);
  53. }
  54. }
  55. return result;
  56. }
  57. protected Object getCellString(HSSFCell cell) {
  58. Object result = null;
  59. if (cell != null) {
  60. int cellType = cell.getCellType();
  61. switch (cellType) {
  62. case HSSFCell.CELL_TYPE_STRING:
  63. result = cell.getRichStringCellValue().getString();
  64. break;
  65. case HSSFCell.CELL_TYPE_NUMERIC:
  66. result = cell.getNumericCellValue();
  67. break;
  68. case HSSFCell.CELL_TYPE_FORMULA:
  69. result = cell.getNumericCellValue();
  70. break;
  71. case HSSFCell.CELL_TYPE_ERROR:
  72. result = null;
  73. break;
  74. case HSSFCell.CELL_TYPE_BOOLEAN:
  75. result = cell.getBooleanCellValue();
  76. break;
  77. case HSSFCell.CELL_TYPE_BLANK:
  78. result = null;
  79. break;
  80. }
  81. }
  82. return result == null ? "" : result;
  83. }
  84. public Cards parse() throws
  85. IOException {
  86. if (!source.endsWith(".xls")) {
  87. logger.error("the file " + source + "is not a excel fiile.");
  88. throw new FileNotFoundException();
  89. }
  90. File excelfile = new File(source);
  91. if (!excelfile.exists()) {
  92. throw new FileNotFoundException();
  93. }
  94. workbook = new HSSFWorkbook(new FileInputStream(excelfile));
  95. cardMatrix = getDatasInSheet(0);
  96. return convertMatrixToCards();
  97. }
  98. private Cards convertMatrixToCards() {
  99. Cards cards= new Cards();
  100. List<Object> properties = (List<Object>) cardMatrix.get(0);
  101. for (int i = 1; i < cardMatrix.size(); i++) {
  102. List<Object> rowdata = cardMatrix.get(i);
  103. cards.add(extractCard(properties, rowdata));
  104. }
  105. return cards;
  106. }
  107. private Card extractCard(List<Object> prop, List<Object> rowdata) {
  108. Card card=new Card();
  109. for (int j = 0; j < rowdata.size() - 1; j++) {
  110. String eachProp = null;
  111. Object eachValue = null;
  112. eachProp = (String) prop.get(j);
  113. eachValue = rowdata.get(j) == null ? "" : rowdata.get(j);
  114. card.setPorperty(eachProp, eachValue);
  115. String value = String.valueOf(eachValue);
  116. if (eachProp.equals("Number")&&!value.equals("")){
  117. card.setNumber(value.substring(0, value.lastIndexOf(".")));
  118. }
  119. if (eachProp.equals("Name"))
  120. card.setName(String.valueOf(eachValue));
  121. }
  122. return card;
  123. }
  124. }