Fix some Sonar issues in sample "HSSFReadWrite"

Fix some IntelliJ warnings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1786695 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-03-13 14:09:27 +00:00
parent 699ddabcdf
commit e13f7dc8af
3 changed files with 175 additions and 160 deletions

View File

@ -44,12 +44,12 @@ import org.apache.poi.ss.util.CellRangeAddress;
public final class HSSFReadWrite { 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 { private static HSSFWorkbook readFile(String filename) throws IOException {
FileInputStream fis = new FileInputStream(filename); FileInputStream fis = new FileInputStream(filename);
try { try {
return new HSSFWorkbook(fis); return new HSSFWorkbook(fis); // NOSONAR - should not be closed here
} finally { } finally {
fis.close(); fis.close();
} }
@ -60,8 +60,8 @@ public final class HSSFReadWrite {
* rows/cells. * rows/cells.
*/ */
private static void testCreateSampleSheet(String outputFilename) throws IOException { private static void testCreateSampleSheet(String outputFilename) throws IOException {
int rownum;
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
try {
HSSFSheet s = wb.createSheet(); HSSFSheet s = wb.createSheet();
HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle();
@ -82,6 +82,7 @@ public final class HSSFReadWrite {
cs2.setFillForegroundColor((short) 0xA); cs2.setFillForegroundColor((short) 0xA);
cs2.setFont(f2); cs2.setFont(f2);
wb.setSheetName(0, "HSSF Test"); wb.setSheetName(0, "HSSF Test");
int rownum;
for (rownum = 0; rownum < 300; rownum++) { for (rownum = 0; rownum < 300; rownum++) {
HSSFRow r = s.createRow(rownum); HSSFRow r = s.createRow(rownum);
if ((rownum % 2) == 0) { if ((rownum % 2) == 0) {
@ -129,6 +130,8 @@ public final class HSSFReadWrite {
wb.write(out); wb.write(out);
} finally { } finally {
out.close(); out.close();
}
} finally {
wb.close(); wb.close();
} }
} }
@ -165,6 +168,7 @@ public final class HSSFReadWrite {
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName); HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
try {
System.out.println("Data dump:\n"); System.out.println("Data dump:\n");
for (int k = 0; k < wb.getNumberOfSheets(); k++) { for (int k = 0; k < wb.getNumberOfSheets(); k++) {
@ -178,13 +182,12 @@ public final class HSSFReadWrite {
continue; continue;
} }
int cells = row.getPhysicalNumberOfCells(); System.out.println("\nROW " + row.getRowNum() + " has " + row.getPhysicalNumberOfCells() + " cell(s).");
System.out.println("\nROW " + row.getRowNum() + " has " + cells for (int c = 0; c < row.getLastCellNum(); c++) {
+ " cell(s).");
for (int c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c); HSSFCell cell = row.getCell(c);
String value = null; String value;
if(cell != null) {
switch (cell.getCellTypeEnum()) { switch (cell.getCellTypeEnum()) {
case FORMULA: case FORMULA:
@ -199,14 +202,30 @@ public final class HSSFReadWrite {
value = "STRING value=" + cell.getStringCellValue(); value = "STRING value=" + cell.getStringCellValue();
break; break;
case BLANK:
value = "<BLANK>";
break;
case BOOLEAN:
value = "BOOLEAN value-" + cell.getBooleanCellValue();
break;
case ERROR:
value = "ERROR value=" + cell.getErrorCellValue();
break;
default: default:
value = "UNKNOWN value of type " + cell.getCellTypeEnum();
} }
System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE="
+ value); + value);
} }
} }
} }
}
} finally {
wb.close(); wb.close();
}
} else if (args.length == 2) { } else if (args.length == 2) {
if (args[1].toLowerCase(Locale.ROOT).equals("write")) { if (args[1].toLowerCase(Locale.ROOT).equals("write")) {
System.out.println("Write mode"); System.out.println("Write mode");
@ -218,17 +237,22 @@ public final class HSSFReadWrite {
} else { } else {
System.out.println("readwrite test"); System.out.println("readwrite test");
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName); HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
try {
FileOutputStream stream = new FileOutputStream(args[1]); FileOutputStream stream = new FileOutputStream(args[1]);
try {
wb.write(stream); wb.write(stream);
} finally {
stream.close(); stream.close();
}
} finally {
wb.close(); 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!!" // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName); HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
FileOutputStream stream = new FileOutputStream(args[1]); try {
HSSFSheet sheet = wb.getSheetAt(0); HSSFSheet sheet = wb.getSheetAt(0);
for (int k = 0; k < 25; k++) { for (int k = 0; k < 25; k++) {
@ -245,10 +269,16 @@ public final class HSSFReadWrite {
HSSFCell cell = row.getCell(3); HSSFCell cell = row.getCell(3);
cell.setCellValue("MODIFIED CELL!!!!!"); cell.setCellValue("MODIFIED CELL!!!!!");
FileOutputStream stream = new FileOutputStream(args[1]);
try {
wb.write(stream); wb.write(stream);
} finally {
stream.close(); stream.close();
}
} finally {
wb.close(); wb.close();
} }
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -283,33 +283,19 @@ public final class WSBoolRecord extends StandardRecord {
} }
// end bitfields // end bitfields
public String toString() public String toString() {
{ return "[WSBOOL]\n" +
StringBuffer buffer = new StringBuffer(); " .wsbool1 = " + Integer.toHexString(getWSBool1()) + "\n" +
" .autobreaks = " + getAutobreaks() + "\n" +
buffer.append("[WSBOOL]\n"); " .dialog = " + getDialog() + "\n" +
buffer.append(" .wsbool1 = ") " .rowsumsbelw= " + getRowSumsBelow() + "\n" +
.append(Integer.toHexString(getWSBool1())).append("\n"); " .rowsumsrigt= " + getRowSumsRight() + "\n" +
buffer.append(" .autobreaks = ").append(getAutobreaks()) " .wsbool2 = " + Integer.toHexString(getWSBool2()) + "\n" +
.append("\n"); " .fittopage = " + getFitToPage() + "\n" +
buffer.append(" .dialog = ").append(getDialog()) " .displayguts= " + getDisplayGuts() + "\n" +
.append("\n"); " .alternateex= " + getAlternateExpression() + "\n" +
buffer.append(" .rowsumsbelw= ").append(getRowSumsBelow()) " .alternatefo= " + getAlternateFormula() + "\n" +
.append("\n"); "[/WSBOOL]\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 void serialize(LittleEndianOutput out) { public void serialize(LittleEndianOutput out) {

View File

@ -908,13 +908,12 @@ public class HSSFCell implements Cell {
case ERROR: case ERROR:
return (( BoolErrRecord ) _record).getErrorValue(); return (( BoolErrRecord ) _record).getErrorValue();
case FORMULA: case FORMULA:
break;
default:
throw typeMismatch(CellType.ERROR, _cellType, false);
}
FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord(); FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
checkFormulaCachedValueType(CellType.ERROR, fr); checkFormulaCachedValueType(CellType.ERROR, fr);
return (byte) fr.getCachedErrorValue(); return (byte) fr.getCachedErrorValue();
default:
throw typeMismatch(CellType.ERROR, _cellType, false);
}
} }
/** /**