From 9ee04feeb9dd01bba271f5f68d4faf48032180da Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Fri, 6 Jan 2017 09:05:14 +0000 Subject: [PATCH] SonarQube fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1777541 13f79535-47bb-0310-9956-ffa450edef68 --- .../formula/UserDefinedFunctionExample.java | 70 ++++++++----------- .../apache/poi/ss/examples/html/ToHtml.java | 52 +++++++------- .../poi/ss/format/CellNumberStringMod.java | 8 +-- 3 files changed, 59 insertions(+), 71 deletions(-) diff --git a/src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java b/src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java index 74dca7d1b..394026cdc 100644 --- a/src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java +++ b/src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java @@ -18,28 +18,27 @@ package org.apache.poi.ss.examples.formula; import java.io.File ; import java.io.FileInputStream ; -import java.io.FileNotFoundException ; -import java.io.IOException ; -import org.apache.poi.openxml4j.exceptions.InvalidFormatException ; import org.apache.poi.ss.formula.functions.FreeRefFunction ; import org.apache.poi.ss.formula.udf.DefaultUDFFinder ; import org.apache.poi.ss.formula.udf.UDFFinder ; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellValue; +import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.util.CellReference ; /** * An example class of how to invoke a User Defined Function for a given * XLS instance using POI's UDFFinder implementation. - * - * @author Jon Svede ( jon [at] loquatic [dot] com ) - * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov ) - * */ public class UserDefinedFunctionExample { - public static void main( String[] args ) { + public static void main( String[] args ) throws Exception { if( args.length != 2 ) { System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ; @@ -51,39 +50,32 @@ public class UserDefinedFunctionExample { File workbookFile = new File( args[0] ) ; - try { - FileInputStream fis = new FileInputStream(workbookFile); - Workbook workbook = WorkbookFactory.create(fis); - fis.close(); + FileInputStream fis = new FileInputStream(workbookFile); + Workbook workbook = WorkbookFactory.create(fis); + fis.close(); - String[] functionNames = { "calculatePayment" } ; - FreeRefFunction[] functionImpls = { new CalculateMortgage() } ; - - UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ; + String[] functionNames = { "calculatePayment" } ; + FreeRefFunction[] functionImpls = { new CalculateMortgage() } ; + + UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ; - // register the user-defined function in the workbook - workbook.addToolPack(udfToolpack); + // register the user-defined function in the workbook + workbook.addToolPack(udfToolpack); - FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); + FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); - CellReference cr = new CellReference( args[1] ) ; - String sheetName = cr.getSheetName() ; - Sheet sheet = workbook.getSheet( sheetName ) ; - int rowIdx = cr.getRow() ; - int colIdx = cr.getCol() ; - Row row = sheet.getRow( rowIdx ) ; - Cell cell = row.getCell( colIdx ) ; - - CellValue value = evaluator.evaluate( cell ) ; - - System.out.println("returns value: " + value ) ; - - } catch( FileNotFoundException e ) { - e.printStackTrace(); - } catch( InvalidFormatException e ) { - e.printStackTrace(); - } catch( IOException e ) { - e.printStackTrace(); - } + CellReference cr = new CellReference( args[1] ) ; + String sheetName = cr.getSheetName() ; + Sheet sheet = workbook.getSheet( sheetName ) ; + int rowIdx = cr.getRow() ; + int colIdx = cr.getCol() ; + Row row = sheet.getRow( rowIdx ) ; + Cell cell = row.getCell( colIdx ) ; + + CellValue value = evaluator.evaluate( cell ) ; + + System.out.println("returns value: " + value ) ; + + workbook.close(); } } diff --git a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java index f5d2ecba8..2a7fe69f2 100644 --- a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java +++ b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java @@ -46,13 +46,12 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * This example shows how to display a spreadsheet in HTML using the classes for * spreadsheet display. - * - * @author Ken Arnold, Industrious Media LLC */ public class ToHtml { private final Workbook wb; @@ -154,23 +153,26 @@ public class ToHtml { } private ToHtml(Workbook wb, Appendable output) { - if (wb == null) + if (wb == null) { throw new NullPointerException("wb"); - if (output == null) + } + if (output == null) { throw new NullPointerException("output"); + } this.wb = wb; this.output = output; setupColorMap(); } private void setupColorMap() { - if (wb instanceof HSSFWorkbook) + if (wb instanceof HSSFWorkbook) { helper = new HSSFHtmlHelper((HSSFWorkbook) wb); - else if (wb instanceof XSSFWorkbook) + } else if (wb instanceof XSSFWorkbook) { helper = new XSSFHtmlHelper(); - else + } else { throw new IllegalArgumentException( "unknown workbook type: " + wb.getClass().getSimpleName()); + } } /** @@ -214,11 +216,9 @@ public class ToHtml { out.format("%n"); } } finally { - if (out != null) - out.close(); + IOUtils.closeQuietly(out); if (output instanceof Closeable) { - Closeable closeable = (Closeable) output; - closeable.close(); + IOUtils.closeQuietly((Closeable) output); } } } @@ -236,8 +236,9 @@ public class ToHtml { } private void ensureOut() { - if (out == null) + if (out == null) { out = new Formatter(output); + } } public void printStyles() { @@ -255,14 +256,7 @@ public class ToHtml { } catch (IOException e) { throw new IllegalStateException("Reading standard css", e); } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - //noinspection ThrowFromFinallyBlock - throw new IllegalStateException("Reading standard css", e); - } - } + IOUtils.closeQuietly(in); } // now add css for each used style @@ -307,10 +301,12 @@ public class ToHtml { private void fontStyle(CellStyle style) { Font font = wb.getFontAt(style.getFontIndex()); - if (font.getBold()) + if (font.getBold()) { out.format(" font-weight: bold;%n"); - if (font.getItalic()) + } + if (font.getItalic()) { out.format(" font-style: italic;%n"); + } int fontheight = font.getFontHeightInPoints(); if (fontheight == 9) { @@ -323,8 +319,9 @@ public class ToHtml { } private String styleName(CellStyle style) { - if (style == null) + if (style == null) { style = wb.getCellStyleAt((short) 0); + } StringBuilder sb = new StringBuilder(); Formatter fmt = new Formatter(sb); try { @@ -344,8 +341,9 @@ public class ToHtml { private static CellType ultimateCellType(Cell c) { CellType type = c.getCellTypeEnum(); - if (type == CellType.FORMULA) + if (type == CellType.FORMULA) { type = c.getCachedFormulaResultTypeEnum(); + } return type; } @@ -372,8 +370,9 @@ public class ToHtml { } private void ensureColumnBounds(Sheet sheet) { - if (gotBounds) + if (gotBounds) { return; + } Iterator iter = sheet.rowIterator(); firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0); @@ -434,8 +433,9 @@ public class ToHtml { style.getDataFormatString()); CellFormatResult result = cf.apply(cell); content = result.text; - if (content.equals("")) + if (content.equals("")) { content = " "; + } } } out.format(" %s%n", styleName(style), diff --git a/src/java/org/apache/poi/ss/format/CellNumberStringMod.java b/src/java/org/apache/poi/ss/format/CellNumberStringMod.java index a053206a8..050a1512c 100644 --- a/src/java/org/apache/poi/ss/format/CellNumberStringMod.java +++ b/src/java/org/apache/poi/ss/format/CellNumberStringMod.java @@ -63,6 +63,7 @@ public class CellNumberStringMod implements Comparable { toAdd = ""; } + @Override public int compareTo(CellNumberStringMod that) { int diff = special.pos - that.special.pos; return (diff != 0) ? diff : (op - that.op); @@ -70,12 +71,7 @@ public class CellNumberStringMod implements Comparable { @Override public boolean equals(Object that) { - try { - return compareTo((CellNumberStringMod) that) == 0; - } catch (RuntimeException ignored) { - // NullPointerException or CastException - return false; - } + return (that instanceof CellNumberStringMod) && compareTo((CellNumberStringMod) that) == 0; } @Override