SonarQube fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1777541 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2017-01-06 09:05:14 +00:00
parent d7da7a0ff4
commit 9ee04feeb9
3 changed files with 59 additions and 71 deletions

View File

@ -18,28 +18,27 @@ package org.apache.poi.ss.examples.formula;
import java.io.File ; import java.io.File ;
import java.io.FileInputStream ; 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.functions.FreeRefFunction ;
import org.apache.poi.ss.formula.udf.DefaultUDFFinder ; import org.apache.poi.ss.formula.udf.DefaultUDFFinder ;
import org.apache.poi.ss.formula.udf.UDFFinder ; 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 ; import org.apache.poi.ss.util.CellReference ;
/** /**
* An example class of how to invoke a User Defined Function for a given * An example class of how to invoke a User Defined Function for a given
* XLS instance using POI's UDFFinder implementation. * 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 class UserDefinedFunctionExample {
public static void main( String[] args ) { public static void main( String[] args ) throws Exception {
if( args.length != 2 ) { if( args.length != 2 ) {
System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ; System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ;
@ -51,7 +50,6 @@ public class UserDefinedFunctionExample {
File workbookFile = new File( args[0] ) ; File workbookFile = new File( args[0] ) ;
try {
FileInputStream fis = new FileInputStream(workbookFile); FileInputStream fis = new FileInputStream(workbookFile);
Workbook workbook = WorkbookFactory.create(fis); Workbook workbook = WorkbookFactory.create(fis);
fis.close(); fis.close();
@ -78,12 +76,6 @@ public class UserDefinedFunctionExample {
System.out.println("returns value: " + value ) ; System.out.println("returns value: " + value ) ;
} catch( FileNotFoundException e ) { workbook.close();
e.printStackTrace();
} catch( InvalidFormatException e ) {
e.printStackTrace();
} catch( IOException e ) {
e.printStackTrace();
}
} }
} }

View File

@ -46,13 +46,12 @@ import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/** /**
* This example shows how to display a spreadsheet in HTML using the classes for * This example shows how to display a spreadsheet in HTML using the classes for
* spreadsheet display. * spreadsheet display.
*
* @author Ken Arnold, Industrious Media LLC
*/ */
public class ToHtml { public class ToHtml {
private final Workbook wb; private final Workbook wb;
@ -154,24 +153,27 @@ public class ToHtml {
} }
private ToHtml(Workbook wb, Appendable output) { private ToHtml(Workbook wb, Appendable output) {
if (wb == null) if (wb == null) {
throw new NullPointerException("wb"); throw new NullPointerException("wb");
if (output == null) }
if (output == null) {
throw new NullPointerException("output"); throw new NullPointerException("output");
}
this.wb = wb; this.wb = wb;
this.output = output; this.output = output;
setupColorMap(); setupColorMap();
} }
private void setupColorMap() { private void setupColorMap() {
if (wb instanceof HSSFWorkbook) if (wb instanceof HSSFWorkbook) {
helper = new HSSFHtmlHelper((HSSFWorkbook) wb); helper = new HSSFHtmlHelper((HSSFWorkbook) wb);
else if (wb instanceof XSSFWorkbook) } else if (wb instanceof XSSFWorkbook) {
helper = new XSSFHtmlHelper(); helper = new XSSFHtmlHelper();
else } else {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"unknown workbook type: " + wb.getClass().getSimpleName()); "unknown workbook type: " + wb.getClass().getSimpleName());
} }
}
/** /**
* Run this class as a program * Run this class as a program
@ -214,11 +216,9 @@ public class ToHtml {
out.format("</html>%n"); out.format("</html>%n");
} }
} finally { } finally {
if (out != null) IOUtils.closeQuietly(out);
out.close();
if (output instanceof Closeable) { if (output instanceof Closeable) {
Closeable closeable = (Closeable) output; IOUtils.closeQuietly((Closeable) output);
closeable.close();
} }
} }
} }
@ -236,9 +236,10 @@ public class ToHtml {
} }
private void ensureOut() { private void ensureOut() {
if (out == null) if (out == null) {
out = new Formatter(output); out = new Formatter(output);
} }
}
public void printStyles() { public void printStyles() {
ensureOut(); ensureOut();
@ -255,14 +256,7 @@ public class ToHtml {
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException("Reading standard css", e); throw new IllegalStateException("Reading standard css", e);
} finally { } finally {
if (in != null) { IOUtils.closeQuietly(in);
try {
in.close();
} catch (IOException e) {
//noinspection ThrowFromFinallyBlock
throw new IllegalStateException("Reading standard css", e);
}
}
} }
// now add css for each used style // now add css for each used style
@ -307,10 +301,12 @@ public class ToHtml {
private void fontStyle(CellStyle style) { private void fontStyle(CellStyle style) {
Font font = wb.getFontAt(style.getFontIndex()); Font font = wb.getFontAt(style.getFontIndex());
if (font.getBold()) if (font.getBold()) {
out.format(" font-weight: bold;%n"); out.format(" font-weight: bold;%n");
if (font.getItalic()) }
if (font.getItalic()) {
out.format(" font-style: italic;%n"); out.format(" font-style: italic;%n");
}
int fontheight = font.getFontHeightInPoints(); int fontheight = font.getFontHeightInPoints();
if (fontheight == 9) { if (fontheight == 9) {
@ -323,8 +319,9 @@ public class ToHtml {
} }
private String styleName(CellStyle style) { private String styleName(CellStyle style) {
if (style == null) if (style == null) {
style = wb.getCellStyleAt((short) 0); style = wb.getCellStyleAt((short) 0);
}
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Formatter fmt = new Formatter(sb); Formatter fmt = new Formatter(sb);
try { try {
@ -344,8 +341,9 @@ public class ToHtml {
private static CellType ultimateCellType(Cell c) { private static CellType ultimateCellType(Cell c) {
CellType type = c.getCellTypeEnum(); CellType type = c.getCellTypeEnum();
if (type == CellType.FORMULA) if (type == CellType.FORMULA) {
type = c.getCachedFormulaResultTypeEnum(); type = c.getCachedFormulaResultTypeEnum();
}
return type; return type;
} }
@ -372,8 +370,9 @@ public class ToHtml {
} }
private void ensureColumnBounds(Sheet sheet) { private void ensureColumnBounds(Sheet sheet) {
if (gotBounds) if (gotBounds) {
return; return;
}
Iterator<Row> iter = sheet.rowIterator(); Iterator<Row> iter = sheet.rowIterator();
firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0); firstColumn = (iter.hasNext() ? Integer.MAX_VALUE : 0);
@ -434,10 +433,11 @@ public class ToHtml {
style.getDataFormatString()); style.getDataFormatString());
CellFormatResult result = cf.apply(cell); CellFormatResult result = cf.apply(cell);
content = result.text; content = result.text;
if (content.equals("")) if (content.equals("")) {
content = "&nbsp;"; content = "&nbsp;";
} }
} }
}
out.format(" <td class=%s %s>%s</td>%n", styleName(style), out.format(" <td class=%s %s>%s</td>%n", styleName(style),
attrs, content); attrs, content);
} }

View File

@ -63,6 +63,7 @@ public class CellNumberStringMod implements Comparable<CellNumberStringMod> {
toAdd = ""; toAdd = "";
} }
@Override
public int compareTo(CellNumberStringMod that) { public int compareTo(CellNumberStringMod that) {
int diff = special.pos - that.special.pos; int diff = special.pos - that.special.pos;
return (diff != 0) ? diff : (op - that.op); return (diff != 0) ? diff : (op - that.op);
@ -70,12 +71,7 @@ public class CellNumberStringMod implements Comparable<CellNumberStringMod> {
@Override @Override
public boolean equals(Object that) { public boolean equals(Object that) {
try { return (that instanceof CellNumberStringMod) && compareTo((CellNumberStringMod) that) == 0;
return compareTo((CellNumberStringMod) that) == 0;
} catch (RuntimeException ignored) {
// NullPointerException or CastException
return false;
}
} }
@Override @Override