diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java index bca850186d..06bc962d5d 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java @@ -34,6 +34,8 @@ import java.util.Random; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.poi.openxml4j.opc.internal.ZipHelper; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.FillPatternType; @@ -73,6 +75,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; * See SXSSF (Streaming Usermodel API). */ public final class BigGridDemo { + private static final Logger LOG = LogManager.getLogger(BigGridDemo.class); private static final String XML_ENCODING = "UTF-8"; private static final Random rnd = new Random(); @@ -111,7 +114,11 @@ public final class BigGridDemo { substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out); } } finally { - if (tmp != null) tmp.delete(); + if (tmp != null && tmp.exists()) { + if (!tmp.delete()) { + LOG.atInfo().log("failed to delete temp file"); + } + } } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfPicture.java b/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfPicture.java index cd5f61fdaf..899b053ddc 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfPicture.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfPicture.java @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Spliterator; import java.util.function.Consumer; import java.util.function.Function; @@ -205,6 +206,12 @@ public class HemfPicture implements Iterable, GenericRecord { final Rectangle2D b = getBoundsInPoints(); return new Dimension2DDouble(abs(b.getWidth()), abs(b.getHeight())); } + + /** + * @param ctx + * @param graphicsBounds + * @throws IllegalStateException if the draw fails + */ public void draw(Graphics2D ctx, Rectangle2D graphicsBounds) { final Shape clip = ctx.getClip(); final AffineTransform at = ctx.getTransform(); @@ -232,8 +239,13 @@ public class HemfPicture implements Iterable, GenericRecord { ? winBounds : emfBounds; } else { - b = Stream.of(emfBounds, winBounds, viewBounds). - min(comparingDouble(r -> diff(r, recBounds))).get(); + Optional result = Stream.of(emfBounds, winBounds, viewBounds). + min(comparingDouble(r -> diff(r, recBounds))); + if (result.isPresent()) { + b = result.get(); + } else { + throw new IllegalStateException("Failed to create Rectangle2D for drawing"); + } } ctx.translate(graphicsBounds.getCenterX(), graphicsBounds.getCenterY()); @@ -245,7 +257,7 @@ public class HemfPicture implements Iterable, GenericRecord { HemfGraphics g = new HemfGraphics(ctx, b); - int idx=0; + int idx = 0; for (HemfRecord r : getRecords()) { try { g.draw(r); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java index d627b1287f..f29a393f5f 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java @@ -94,7 +94,7 @@ final class XMatchFunction implements FreeRefFunction { vector = LookupUtils.createRowVector(tableArray, 0); } int matchedIdx = LookupUtils.xlookupIndexOfValue(lookupValue, vector, matchMode, searchMode); - return new NumberEval(matchedIdx + 1); + return new NumberEval((double)matchedIdx + 1); } catch (EvaluationException e) { return e.getErrorEval(); } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java index a393e13b65..a74901ab20 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Spliterator; import java.util.Spliterators; import java.util.regex.Matcher; @@ -142,7 +143,9 @@ public final class LookupUtils { @Override public Integer next() { - return pos--; + pos--; + if (pos < 0) throw new NoSuchElementException(); + return pos; } }; } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java index 1ad1c052fd..1ba040e4da 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/NumericFunction.java @@ -177,7 +177,7 @@ public abstract class NumericFunction implements Function { } double dpm = Math.abs(d)+1; long x = ((long) dpm) & PARITY_MASK; - return MathX.sign(d) * ((Double.compare(x, dpm) == 0) ? x-1 : x+1); + return (double) MathX.sign(d) * ((Double.compare(x, dpm) == 0) ? x-1 : x+1); } @@ -190,7 +190,7 @@ public abstract class NumericFunction implements Function { double dpm = Math.abs(d); long x = ((long) dpm) & PARITY_MASK; - return MathX.sign(d) * ((Double.compare(x, dpm) == 0) ? x : (x + 2)); + return (double) MathX.sign(d) * ((Double.compare(x, dpm) == 0) ? x : (x + 2)); } diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java index ed468e1b84..76d272c495 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java @@ -243,7 +243,8 @@ public final class CellUtil { // Copy CellStyle if (policy.isCopyCellStyle()) { - if (destCell.getSheet().getWorkbook() == srcCell.getSheet().getWorkbook()) { + if (srcCell.getSheet() != null && destCell.getSheet() != null && + destCell.getSheet().getWorkbook() == srcCell.getSheet().getWorkbook()) { destCell.setCellStyle(srcCell.getCellStyle()); } else { CellStyle srcStyle = srcCell.getCellStyle();