From 611c15f412f14244d3132d683c5b6543d26254fe Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sat, 2 Jun 2018 21:28:06 +0000 Subject: [PATCH] sonar fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1832748 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hpsf/Section.java | 4 ++-- .../org/apache/poi/ss/usermodel/DataFormatter.java | 7 ++++++- .../apache/poi/ooxml/extractor/ExtractorFactory.java | 4 ++-- .../java/org/apache/poi/ooxml/util/SAXHelper.java | 2 +- .../org/apache/poi/xssf/streaming/SXSSFPicture.java | 5 ++--- .../org/apache/poi/xssf/usermodel/XSSFPicture.java | 5 ++++- .../org/apache/poi/xwpf/usermodel/XWPFChart.java | 12 +----------- .../org/apache/poi/hslf/record/TextRulerAtom.java | 3 ++- 8 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/java/org/apache/poi/hpsf/Section.java b/src/java/org/apache/poi/hpsf/Section.java index 88677801c..bcacfb334 100644 --- a/src/java/org/apache/poi/hpsf/Section.java +++ b/src/java/org/apache/poi/hpsf/Section.java @@ -844,10 +844,10 @@ public class Section { if (cp == CodePageUtil.CP_UNICODE) { pad = 2+((4 - ((nrBytes+2) & 0x3)) & 0x3); } - leis.skip(pad); + IOUtils.skipFully(leis, pad); dic.put(id, str); - } catch (RuntimeException ex) { + } catch (RuntimeException|IOException ex) { LOG.log(POILogger.WARN, errMsg, ex); isCorrupted = true; break; diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 09e9b8455..b225989c1 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -812,6 +812,9 @@ public class DataFormatter implements Observer { * @return Formatted value */ private String getFormattedDateString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) { + if (cell == null) { + return null; + } Format dateFormat = getFormat(cell, cfEvaluator); if(dateFormat instanceof ExcelStyleDateFormatter) { // Hint about the raw excel value @@ -837,7 +840,9 @@ public class DataFormatter implements Observer { * @return a formatted number string */ private String getFormattedNumberString(Cell cell, ConditionalFormattingEvaluator cfEvaluator) { - + if (cell == null) { + return null; + } Format numberFormat = getFormat(cell, cfEvaluator); double d = cell.getNumericCellValue(); if (numberFormat == null) { diff --git a/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java index 6603f5858..d32180993 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java @@ -146,7 +146,7 @@ public class ExtractorFactory { // ensure file-handle release IOUtils.closeQuietly(fs); throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file"); - } catch (OpenXML4JException | Error | RuntimeException | IOException | XmlException e) { + } catch (OpenXML4JException | Error | RuntimeException | IOException | XmlException e) { // NOSONAR // ensure file-handle release IOUtils.closeQuietly(fs); throw e; @@ -245,7 +245,7 @@ public class ExtractorFactory { throw new IllegalArgumentException("No supported documents found in the OOXML package (found "+contentType+")"); - } catch (IOException | Error | RuntimeException | XmlException | OpenXML4JException e) { + } catch (IOException | Error | RuntimeException | XmlException | OpenXML4JException e) { // NOSONAR // ensure that we close the package again if there is an error opening it, however // we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor! pkg.revert(); diff --git a/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java b/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java index 630e5540a..806aede58 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java @@ -68,7 +68,7 @@ public final class SAXHelper { saxFactory = SAXParserFactory.newInstance(); saxFactory.setValidating(false); saxFactory.setNamespaceAware(true); - } catch (RuntimeException | Error re) { + } catch (RuntimeException | Error re) { // NOSONAR // this also catches NoClassDefFoundError, which may be due to a local class path issue // This may occur if the code is run inside a web container // or a restricted JVM diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java index ad54c3ecc..1d83b7c7d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java @@ -106,13 +106,12 @@ public final class SXSSFPicture implements Picture { @Override public void resize(double scale){ XSSFClientAnchor anchor = getClientAnchor(); - if (anchor == null) { + XSSFClientAnchor pref = getPreferredSize(scale); + if (anchor == null || pref == null) { logger.log(POILogger.WARN, "picture is not anchored via client anchor - ignoring resize call"); return; } - XSSFClientAnchor pref = getPreferredSize(scale); - int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1()); int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1()); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java index b488f1a6b..bd39bc035 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java @@ -176,8 +176,11 @@ public final class XSSFPicture extends XSSFShape implements Picture { */ public void resize(double scaleX, double scaleY){ XSSFClientAnchor anchor = getClientAnchor(); - XSSFClientAnchor pref = getPreferredSize(scaleX,scaleY); + if (anchor == null || pref == null) { + logger.log(POILogger.WARN, "picture is not anchored via client anchor - ignoring resize call"); + return; + } int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1()); int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1()); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java index 368fa3c11..44820a587 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java @@ -92,21 +92,11 @@ public class XWPFChart extends XDDFChart { public Long getChecksum() { if (this.checksum == null) { - InputStream is = null; byte[] data; - try { - is = getPackagePart().getInputStream(); + try (InputStream is = getPackagePart().getInputStream()) { data = IOUtils.toByteArray(is); } catch (IOException e) { throw new POIXMLException(e); - } finally { - try { - if (is != null) { - is.close(); - } - } catch (IOException e) { - throw new POIXMLException(e); - } } this.checksum = IOUtils.calculateChecksum(data); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TextRulerAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/TextRulerAtom.java index e0c45a5da..b8ef54ad1 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TextRulerAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TextRulerAtom.java @@ -29,6 +29,7 @@ import java.util.List; import org.apache.poi.hslf.model.textproperties.HSLFTabStop; import org.apache.poi.hslf.model.textproperties.HSLFTabStopPropCollection; import org.apache.poi.util.BitField; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianByteArrayInputStream; import org.apache.poi.util.LittleEndianOutputStream; @@ -89,7 +90,7 @@ public final class TextRulerAtom extends RecordAtom { try { // Get the header. - leis.read(_header); + IOUtils.readFully(leis, _header); // Get the record data. read(leis);