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.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();
}
}

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.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("</html>%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<Row> 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 = "&nbsp;";
}
}
}
out.format(" <td class=%s %s>%s</td>%n", styleName(style),

View File

@ -63,6 +63,7 @@ public class CellNumberStringMod implements Comparable<CellNumberStringMod> {
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<CellNumberStringMod> {
@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