]> source.dussan.org Git - poi.git/commitdiff
Fix some Sonar issues in sample "HSSFReadWrite"
authorDominik Stadler <centic@apache.org>
Mon, 13 Mar 2017 14:09:27 +0000 (14:09 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 13 Mar 2017 14:09:27 +0000 (14:09 +0000)
Fix some IntelliJ warnings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1786695 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java
src/java/org/apache/poi/hssf/record/WSBoolRecord.java
src/java/org/apache/poi/hssf/usermodel/HSSFCell.java

index 632cd5585faf0cd6402bdbc4279833c9e7c4866b..1f56f7ec5e67fcc8f5b2b713aad19767a20c4b74 100644 (file)
@@ -44,12 +44,12 @@ import org.apache.poi.ss.util.CellRangeAddress;
 public final class HSSFReadWrite {
 
        /**
-        * creates an {@link HSSFWorkbook} the specified OS filename.
+        * creates an {@link HSSFWorkbook} with the specified OS filename.
         */
        private static HSSFWorkbook readFile(String filename) throws IOException {
            FileInputStream fis = new FileInputStream(filename);
            try {
-               return new HSSFWorkbook(fis);
+               return new HSSFWorkbook(fis);           // NOSONAR - should not be closed here
            } finally {
                fis.close();
            }
@@ -60,76 +60,79 @@ public final class HSSFReadWrite {
         * rows/cells.
         */
        private static void testCreateSampleSheet(String outputFilename) throws IOException {
-               int rownum;
                HSSFWorkbook wb = new HSSFWorkbook();
-               HSSFSheet s = wb.createSheet();
-               HSSFCellStyle cs = wb.createCellStyle();
-               HSSFCellStyle cs2 = wb.createCellStyle();
-               HSSFCellStyle cs3 = wb.createCellStyle();
-               HSSFFont f = wb.createFont();
-               HSSFFont f2 = wb.createFont();
-
-               f.setFontHeightInPoints((short) 12);
-               f.setColor((short) 0xA);
-               f.setBold(true);
-               f2.setFontHeightInPoints((short) 10);
-               f2.setColor((short) 0xf);
-               f2.setBold(true);
-               cs.setFont(f);
-               cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
-               cs2.setBorderBottom(BorderStyle.THIN);
-               cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-               cs2.setFillForegroundColor((short) 0xA);
-               cs2.setFont(f2);
-               wb.setSheetName(0, "HSSF Test");
-               for (rownum = 0; rownum < 300; rownum++) {
-                       HSSFRow r = s.createRow(rownum);
-                       if ((rownum % 2) == 0) {
-                               r.setHeight((short) 0x249);
-                       }
+               try {
+                       HSSFSheet s = wb.createSheet();
+                       HSSFCellStyle cs = wb.createCellStyle();
+                       HSSFCellStyle cs2 = wb.createCellStyle();
+                       HSSFCellStyle cs3 = wb.createCellStyle();
+                       HSSFFont f = wb.createFont();
+                       HSSFFont f2 = wb.createFont();
 
-                       for (int cellnum = 0; cellnum < 50; cellnum += 2) {
-                               HSSFCell c = r.createCell(cellnum);
-                               c.setCellValue(rownum * 10000 + cellnum
-                                               + (((double) rownum / 1000) + ((double) cellnum / 10000)));
+                       f.setFontHeightInPoints((short) 12);
+                       f.setColor((short) 0xA);
+                       f.setBold(true);
+                       f2.setFontHeightInPoints((short) 10);
+                       f2.setColor((short) 0xf);
+                       f2.setBold(true);
+                       cs.setFont(f);
+                       cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
+                       cs2.setBorderBottom(BorderStyle.THIN);
+                       cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+                       cs2.setFillForegroundColor((short) 0xA);
+                       cs2.setFont(f2);
+                       wb.setSheetName(0, "HSSF Test");
+                       int rownum;
+                       for (rownum = 0; rownum < 300; rownum++) {
+                               HSSFRow r = s.createRow(rownum);
                                if ((rownum % 2) == 0) {
-                                       c.setCellStyle(cs);
+                                       r.setHeight((short) 0x249);
                                }
-                               c = r.createCell(cellnum + 1);
-                               c.setCellValue(new HSSFRichTextString("TEST"));
-                               // 50 characters divided by 1/20th of a point
-                               s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
-                               if ((rownum % 2) == 0) {
-                                       c.setCellStyle(cs2);
+
+                               for (int cellnum = 0; cellnum < 50; cellnum += 2) {
+                                       HSSFCell c = r.createCell(cellnum);
+                                       c.setCellValue(rownum * 10000 + cellnum
+                                                       + (((double) rownum / 1000) + ((double) cellnum / 10000)));
+                                       if ((rownum % 2) == 0) {
+                                               c.setCellStyle(cs);
+                                       }
+                                       c = r.createCell(cellnum + 1);
+                                       c.setCellValue(new HSSFRichTextString("TEST"));
+                                       // 50 characters divided by 1/20th of a point
+                                       s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
+                                       if ((rownum % 2) == 0) {
+                                               c.setCellStyle(cs2);
+                                       }
                                }
                        }
-               }
 
-               // draw a thick black border on the row at the bottom using BLANKS
-               rownum++;
-               rownum++;
-               HSSFRow r = s.createRow(rownum);
-               cs3.setBorderBottom(BorderStyle.THICK);
-               for (int cellnum = 0; cellnum < 50; cellnum++) {
-                       HSSFCell c = r.createCell(cellnum);
-                       c.setCellStyle(cs3);
-               }
-               s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
-               s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));
+                       // draw a thick black border on the row at the bottom using BLANKS
+                       rownum++;
+                       rownum++;
+                       HSSFRow r = s.createRow(rownum);
+                       cs3.setBorderBottom(BorderStyle.THICK);
+                       for (int cellnum = 0; cellnum < 50; cellnum++) {
+                               HSSFCell c = r.createCell(cellnum);
+                               c.setCellStyle(cs3);
+                       }
+                       s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
+                       s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));
 
-               // end draw thick black border
-               // create a sheet, set its title then delete it
-               wb.createSheet();
-               wb.setSheetName(1, "DeletedSheet");
-               wb.removeSheetAt(1);
+                       // end draw thick black border
+                       // create a sheet, set its title then delete it
+                       wb.createSheet();
+                       wb.setSheetName(1, "DeletedSheet");
+                       wb.removeSheetAt(1);
 
-               // end deleted sheet
-               FileOutputStream out = new FileOutputStream(outputFilename);
-               try {
-                   wb.write(out);
+                       // end deleted sheet
+                       FileOutputStream out = new FileOutputStream(outputFilename);
+                       try {
+                               wb.write(out);
+                       } finally {
+                               out.close();
+                       }
                } finally {
-                   out.close();
-               wb.close();
+                       wb.close();
                }
        }
 
@@ -165,48 +168,64 @@ public final class HSSFReadWrite {
 
                                HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
 
-                               System.out.println("Data dump:\n");
-
-                               for (int k = 0; k < wb.getNumberOfSheets(); k++) {
-                                       HSSFSheet sheet = wb.getSheetAt(k);
-                                       int rows = sheet.getPhysicalNumberOfRows();
-                                       System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows
-                                                       + " row(s).");
-                                       for (int r = 0; r < rows; r++) {
-                                               HSSFRow row = sheet.getRow(r);
-                                               if (row == null) {
-                                                       continue;
-                                               }
+                               try {
+                                       System.out.println("Data dump:\n");
+
+                                       for (int k = 0; k < wb.getNumberOfSheets(); k++) {
+                                               HSSFSheet sheet = wb.getSheetAt(k);
+                                               int rows = sheet.getPhysicalNumberOfRows();
+                                               System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows
+                                                               + " row(s).");
+                                               for (int r = 0; r < rows; r++) {
+                                                       HSSFRow row = sheet.getRow(r);
+                                                       if (row == null) {
+                                                               continue;
+                                                       }
 
-                                               int cells = row.getPhysicalNumberOfCells();
-                                               System.out.println("\nROW " + row.getRowNum() + " has " + cells
-                                                               + " cell(s).");
-                                               for (int c = 0; c < cells; c++) {
-                                                       HSSFCell cell = row.getCell(c);
-                                                       String value = null;
+                                                       System.out.println("\nROW " + row.getRowNum() + " has " + row.getPhysicalNumberOfCells() + " cell(s).");
+                                                       for (int c = 0; c < row.getLastCellNum(); c++) {
+                                                               HSSFCell cell = row.getCell(c);
+                                                               String value;
 
-                                                       switch (cell.getCellTypeEnum()) {
+                                                               if(cell != null) {
+                                                                       switch (cell.getCellTypeEnum()) {
 
-                                                               case FORMULA:
-                                                                       value = "FORMULA value=" + cell.getCellFormula();
-                                                                       break;
+                                                                               case FORMULA:
+                                                                                       value = "FORMULA value=" + cell.getCellFormula();
+                                                                                       break;
 
-                                                               case NUMERIC:
-                                                                       value = "NUMERIC value=" + cell.getNumericCellValue();
-                                                                       break;
+                                                                               case NUMERIC:
+                                                                                       value = "NUMERIC value=" + cell.getNumericCellValue();
+                                                                                       break;
 
-                                                               case STRING:
-                                                                       value = "STRING value=" + cell.getStringCellValue();
-                                                                       break;
+                                                                               case STRING:
+                                                                                       value = "STRING value=" + cell.getStringCellValue();
+                                                                                       break;
 
-                                                               default:
+                                                                               case BLANK:
+                                                                                       value = "<BLANK>";
+                                                                                       break;
+
+                                                                               case BOOLEAN:
+                                                                                       value = "BOOLEAN value-" + cell.getBooleanCellValue();
+                                                                                       break;
+
+                                                                               case ERROR:
+                                                                                       value = "ERROR value=" + cell.getErrorCellValue();
+                                                                                       break;
+
+                                                                               default:
+                                                                                       value = "UNKNOWN value of type " + cell.getCellTypeEnum();
+                                                                       }
+                                                                       System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE="
+                                                                                       + value);
+                                                               }
                                                        }
-                                                       System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE="
-                                                                       + value);
                                                }
                                        }
+                               } finally {
+                                       wb.close();
                                }
-                               wb.close();
                        } else if (args.length == 2) {
                                if (args[1].toLowerCase(Locale.ROOT).equals("write")) {
                                        System.out.println("Write mode");
@@ -218,36 +237,47 @@ public final class HSSFReadWrite {
                                } else {
                                        System.out.println("readwrite test");
                                        HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
-                                       FileOutputStream stream = new FileOutputStream(args[1]);
-
-                                       wb.write(stream);
-                                       stream.close();
-                                       wb.close();
+                                       try {
+                                               FileOutputStream stream = new FileOutputStream(args[1]);
+                                               try {
+                                                       wb.write(stream);
+                                               } finally {
+                                                       stream.close();
+                                               }
+                                       } finally {
+                                               wb.close();
+                                       }
                                }
-                       } else if (args.length == 3 && args[2].toLowerCase(Locale.ROOT).equals("modify1")) {
+                       } else if (args.length == 3 && args[2].equalsIgnoreCase("modify1")) {
                                // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
 
                                HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
-                               FileOutputStream stream = new FileOutputStream(args[1]);
-                               HSSFSheet sheet = wb.getSheetAt(0);
+                               try {
+                                       HSSFSheet sheet = wb.getSheetAt(0);
 
-                               for (int k = 0; k < 25; k++) {
-                                       HSSFRow row = sheet.getRow(k);
+                                       for (int k = 0; k < 25; k++) {
+                                               HSSFRow row = sheet.getRow(k);
 
-                                       sheet.removeRow(row);
-                               }
-                               for (int k = 74; k < 100; k++) {
-                                       HSSFRow row = sheet.getRow(k);
+                                               sheet.removeRow(row);
+                                       }
+                                       for (int k = 74; k < 100; k++) {
+                                               HSSFRow row = sheet.getRow(k);
 
-                                       sheet.removeRow(row);
-                               }
-                               HSSFRow row = sheet.getRow(39);
-                               HSSFCell cell = row.getCell(3);
-                               cell.setCellValue("MODIFIED CELL!!!!!");
+                                               sheet.removeRow(row);
+                                       }
+                                       HSSFRow row = sheet.getRow(39);
+                                       HSSFCell cell = row.getCell(3);
+                                       cell.setCellValue("MODIFIED CELL!!!!!");
 
-                               wb.write(stream);
-                               stream.close();
-                               wb.close();
+                                       FileOutputStream stream = new FileOutputStream(args[1]);
+                                       try {
+                                               wb.write(stream);
+                                       } finally {
+                                               stream.close();
+                                       }
+                               } finally {
+                                       wb.close();
+                               }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
index 78185c1df8ed25f01fd9848db9c4eeba3865836e..b3659db994feb7c9d10f2a97e5e43c9eb9abc20b 100644 (file)
@@ -283,33 +283,19 @@ public final class WSBoolRecord extends StandardRecord {
     }
 
     // end bitfields
-    public String toString()
-    {
-        StringBuffer buffer = new StringBuffer();
-
-        buffer.append("[WSBOOL]\n");
-        buffer.append("    .wsbool1        = ")
-            .append(Integer.toHexString(getWSBool1())).append("\n");
-        buffer.append("        .autobreaks = ").append(getAutobreaks())
-            .append("\n");
-        buffer.append("        .dialog     = ").append(getDialog())
-            .append("\n");
-        buffer.append("        .rowsumsbelw= ").append(getRowSumsBelow())
-            .append("\n");
-        buffer.append("        .rowsumsrigt= ").append(getRowSumsRight())
-            .append("\n");
-        buffer.append("    .wsbool2        = ")
-            .append(Integer.toHexString(getWSBool2())).append("\n");
-        buffer.append("        .fittopage  = ").append(getFitToPage())
-            .append("\n");
-        buffer.append("        .displayguts= ").append(getDisplayGuts())
-            .append("\n");
-        buffer.append("        .alternateex= ")
-            .append(getAlternateExpression()).append("\n");
-        buffer.append("        .alternatefo= ").append(getAlternateFormula())
-            .append("\n");
-        buffer.append("[/WSBOOL]\n");
-        return buffer.toString();
+    public String toString() {
+        return "[WSBOOL]\n" +
+                "    .wsbool1        = " + Integer.toHexString(getWSBool1()) + "\n" +
+                "        .autobreaks = " + getAutobreaks() + "\n" +
+                "        .dialog     = " + getDialog() + "\n" +
+                "        .rowsumsbelw= " + getRowSumsBelow() + "\n" +
+                "        .rowsumsrigt= " + getRowSumsRight() + "\n" +
+                "    .wsbool2        = " + Integer.toHexString(getWSBool2()) + "\n" +
+                "        .fittopage  = " + getFitToPage() + "\n" +
+                "        .displayguts= " + getDisplayGuts() + "\n" +
+                "        .alternateex= " + getAlternateExpression() + "\n" +
+                "        .alternatefo= " + getAlternateFormula() + "\n" +
+                "[/WSBOOL]\n";
     }
 
     public void serialize(LittleEndianOutput out) {
index 981cbd4829b4a78c06e584dc6c917e18f2a104aa..a95ea239a26a793870f9dd39d378df5bd939eccb 100644 (file)
@@ -908,13 +908,12 @@ public class HSSFCell implements Cell {
             case ERROR:
                 return (( BoolErrRecord ) _record).getErrorValue();
             case FORMULA:
-                break;
+                FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
+                checkFormulaCachedValueType(CellType.ERROR, fr);
+                return (byte) fr.getCachedErrorValue();
             default:
                 throw typeMismatch(CellType.ERROR, _cellType, false);
         }
-        FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
-        checkFormulaCachedValueType(CellType.ERROR, fr);
-        return (byte) fr.getCachedErrorValue();
     }
 
     /**