diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2020-12-26 00:08:19 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2020-12-26 00:08:19 +0000 |
commit | d121353f612c6efbcf156e166158f1d403e8697e (patch) | |
tree | 078baea3d2ad8cb33ae4752e677aa8887372389f /src/ooxml | |
parent | be1afe5ff586cfc01c68ca6dc58e84f951aca2ef (diff) | |
download | poi-d121353f612c6efbcf156e166158f1d403e8697e.tar.gz poi-d121353f612c6efbcf156e166158f1d403e8697e.zip |
Sonar fixes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884807 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml')
10 files changed, 46 insertions, 70 deletions
diff --git a/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java index 2a5a9d86f7..2d90b46f20 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java @@ -124,7 +124,6 @@ public final class POIXMLExtractorFactory implements ExtractorProvider { return ExtractorFactory.createExtractor(f, password); } - OPCPackage pkg = null; try { pkg = OPCPackage.open(f.toString(), PackageAccess.READ); @@ -136,7 +135,9 @@ public final class POIXMLExtractorFactory implements ExtractorProvider { } catch (InvalidFormatException ife) { throw new IOException(ife); } catch (IOException e) { - pkg.revert(); + if (pkg != null) { + pkg.revert(); + } throw e; } } diff --git a/src/ooxml/java/org/apache/poi/ooxml/util/IdentifierManager.java b/src/ooxml/java/org/apache/poi/ooxml/util/IdentifierManager.java index f367473cc3..8634862bb7 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/util/IdentifierManager.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/IdentifierManager.java @@ -19,28 +19,12 @@ package org.apache.poi.ooxml.util; import java.util.LinkedList; import java.util.ListIterator; -/** - * <p> - * 24.08.2009<br> - * </p> - * - * @author Stefan Stern<br> - */ - public class IdentifierManager { public static final long MAX_ID = Long.MAX_VALUE - 1; - public static final long MIN_ID = 0L; - /** - * - */ private final long upperbound; - - /** - * - */ private final long lowerbound; /** @@ -56,7 +40,7 @@ public class IdentifierManager { if (lowerbound > upperbound) { throw new IllegalArgumentException("lowerbound must not be greater than upperbound, had " + lowerbound + " and " + upperbound); } - else if (lowerbound < MIN_ID) { + else if (lowerbound < MIN_ID) { String message = "lowerbound must be greater than or equal to " + Long.toString(MIN_ID); throw new IllegalArgumentException(message); } @@ -78,7 +62,7 @@ public class IdentifierManager { throw new IllegalArgumentException("Value for parameter 'id' was out of bounds, had " + id + ", but should be within [" + lowerbound + ":" + upperbound + "]"); } verifyIdentifiersLeft(); - + if (id == upperbound) { Segment lastSegment = segments.getLast(); if (lastSegment.end == upperbound) { @@ -136,7 +120,7 @@ public class IdentifierManager { } /** - * @return a new identifier. + * @return a new identifier. * @throws IllegalStateException if no more identifiers are available, then an Exception is raised. */ public long reserveNew() { @@ -236,7 +220,7 @@ public class IdentifierManager { } /** - * + * */ private void verifyIdentifiersLeft() { if (segments.isEmpty()) { @@ -245,20 +229,14 @@ public class IdentifierManager { } private static class Segment { + private long start; + private long end; public Segment(long start, long end) { this.start = start; this.end = end; } - public long start; - public long end; - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ public String toString() { return "[" + start + "; " + end + "]"; } diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureLine.java b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureLine.java index c6dcde6d74..06067ea531 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureLine.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/SignatureLine.java @@ -386,7 +386,7 @@ public abstract class SignatureLine { int mpos = measurer.getPosition(); int limit = text.indexOf('\n', mpos); limit = (limit == -1) ? text.length() : limit+1; - TextLayout textLayout = measurer.nextLayout(bi.getWidth()-10, limit, false); + TextLayout textLayout = measurer.nextLayout(bi.getWidth()-10f, limit, false); if (lineNr != 1) { y += textLayout.getAscent(); } diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java index b841da4ada..7dd4641107 100644 --- a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java +++ b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometryRowTypes.java @@ -62,8 +62,8 @@ enum GeometryRowTypes { SPLINE_START("SplineStart", SplineStart::new) ; - public String rowType; - public Function<RowType, ? extends GeometryRow> constructor; + private final String rowType; + private final Function<RowType, ? extends GeometryRow> constructor; GeometryRowTypes(String rowType, Function<RowType, ? extends GeometryRow> constructor) { this.rowType = rowType; diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java index 8aa0bdca4d..35af8d52be 100644 --- a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java +++ b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/XDGFSectionTypes.java @@ -50,8 +50,8 @@ enum XDGFSectionTypes { ANNOTATION("Annotation", GenericSection::new), ACTION_TAG("ActionTag", GenericSection::new); - public String sectionType; - public BiFunction<SectionType, XDGFSheet, ? extends XDGFSection> constructor; + private final String sectionType; + private final BiFunction<SectionType, XDGFSheet, ? extends XDGFSection> constructor; XDGFSectionTypes(String sectionType, BiFunction<SectionType, XDGFSheet, ? extends XDGFSection> constructor) { this.sectionType = sectionType; diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/EllipticalArcTo.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/EllipticalArcTo.java index d29392d6a1..2ec09f6b6b 100644 --- a/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/EllipticalArcTo.java +++ b/src/ooxml/java/org/apache/poi/xdgf/usermodel/section/geometry/EllipticalArcTo.java @@ -126,8 +126,6 @@ public class EllipticalArcTo implements GeometryRow { _master = (EllipticalArcTo) row; } - public static int draw; - @Override public void addToPath(java.awt.geom.Path2D.Double path, XDGFShape parent) { diff --git a/src/ooxml/java/org/apache/poi/xslf/util/DummyGraphics2d.java b/src/ooxml/java/org/apache/poi/xslf/util/DummyGraphics2d.java index 281f4129d5..b3f158a334 100644 --- a/src/ooxml/java/org/apache/poi/xslf/util/DummyGraphics2d.java +++ b/src/ooxml/java/org/apache/poi/xslf/util/DummyGraphics2d.java @@ -809,6 +809,7 @@ public class DummyGraphics2d extends Graphics2D { log.println( "finalize():" ); g2D.dispose(); dispose(); + super.finalize(); } public Shape getClip() { diff --git a/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java b/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java index 089a605c03..6e3d0c0c9f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java +++ b/src/ooxml/java/org/apache/poi/xssf/binary/XSSFBCellHeader.java @@ -30,7 +30,7 @@ import org.apache.poi.util.LittleEndianConsts; */ @Internal class XSSFBCellHeader { - public static int length = 8; + public static final int length = 8; /** * diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java index 64ead9f4b5..79f003eb9c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java @@ -70,7 +70,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork public void clearAllCachedResultValues() { _tableCache = null; } - + private int convertFromExternalSheetIndex(int externSheetIndex) { return externSheetIndex; } @@ -113,16 +113,16 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork List<ExternalLinksTable> tables = _uBook.getExternalLinksTable(); int index = findExternalLinkIndex(bookName, tables); if (index != -1) return index; - + // Is it an absolute file reference? if (bookName.startsWith("'file:///") && bookName.endsWith("'")) { String relBookName = bookName.substring(bookName.lastIndexOf('/')+1); relBookName = relBookName.substring(0, relBookName.length()-1); // Trailing ' - + // Try with this name index = findExternalLinkIndex(relBookName, tables); if (index != -1) return index; - + // If we get here, it's got no associated proper links yet // So, add the missing reference and return // Note - this is really rather nasty... @@ -130,7 +130,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork tables.add(fakeLinkTable); return tables.size(); // 1 based results, 0 = current workbook } - + // Not properly referenced throw new RuntimeException("Book not linked for filename " + bookName); } @@ -162,7 +162,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork * @param sheetIndex index of sheet if named range scope is limited to one sheet * if named range scope is global to the workbook, sheetIndex is -1. * @return If name is a named range in the workbook, returns - * EvaluationName corresponding to that named range + * EvaluationName corresponding to that named range * Returns null if there is no named range with the same name and scope in the workbook */ @Override @@ -171,7 +171,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork XSSFName nm = _uBook.getNameAt(i); String nameText = nm.getNameName(); int nameSheetindex = nm.getSheetIndex(); - if (name.equalsIgnoreCase(nameText) && + if (name.equalsIgnoreCase(nameText) && (nameSheetindex == -1 || nameSheetindex == sheetIndex)) { return new Name(nm, i, this); } @@ -183,7 +183,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork public String getSheetName(int sheetIndex) { return _uBook.getSheetName(sheetIndex); } - + @Override public ExternalName getExternalName(int externSheetIndex, int externNameIndex) { throw new IllegalStateException("HSSF-style external references are not supported for XSSF"); @@ -195,13 +195,13 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork // External reference - reference is 1 based, link table is 0 based int linkNumber = externalWorkbookNumber - 1; ExternalLinksTable linkTable = _uBook.getExternalLinksTable().get(linkNumber); - + for (org.apache.poi.ss.usermodel.Name name : linkTable.getDefinedNames()) { if (name.getNameName().equals(nameName)) { // HSSF returns one sheet higher than normal, and various bits // of the code assume that. So, make us match that behaviour! int nameSheetIndex = name.getSheetIndex() + 1; - + // TODO Return a more specialised form of this, see bug #56752 // Should include the cached values, for in case that book isn't available // Should support XSSF stuff lookups @@ -215,7 +215,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork int nameIdx = _uBook.getNameIndex(nameName); return new ExternalName(nameName, nameIdx, 0); // TODO Is this right? } - + } /** @@ -229,7 +229,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork if (func != null) { return new NameXPxg(null, name); } - + // Otherwise, try it as a named range if (sheet == null) { if (!_uBook.getNames(name).isEmpty()) { @@ -237,17 +237,17 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork } return null; } - if (sheet._sheetIdentifier == null) { + if (sheet.getSheetIdentifier() == null) { // Workbook + Named Range only - int bookIndex = resolveBookIndex(sheet._bookName); + int bookIndex = resolveBookIndex(sheet.getBookName()); return new NameXPxg(bookIndex, null, name); } // Use the sheetname and process - String sheetName = sheet._sheetIdentifier.getName(); - - if (sheet._bookName != null) { - int bookIndex = resolveBookIndex(sheet._bookName); + String sheetName = sheet.getSheetIdentifier().getName(); + + if (sheet.getBookName() != null) { + int bookIndex = resolveBookIndex(sheet.getBookName()); return new NameXPxg(bookIndex, sheetName, name); } else { return new NameXPxg(sheetName, name); @@ -255,8 +255,8 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork } @Override public Ptg get3DReferencePtg(CellReference cell, SheetIdentifier sheet) { - if (sheet._bookName != null) { - int bookIndex = resolveBookIndex(sheet._bookName); + if (sheet.getBookName() != null) { + int bookIndex = resolveBookIndex(sheet.getBookName()); return new Ref3DPxg(bookIndex, sheet, cell); } else { return new Ref3DPxg(sheet, cell); @@ -264,8 +264,8 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork } @Override public Ptg get3DReferencePtg(AreaReference area, SheetIdentifier sheet) { - if (sheet._bookName != null) { - int bookIndex = resolveBookIndex(sheet._bookName); + if (sheet.getBookName() != null) { + int bookIndex = resolveBookIndex(sheet.getBookName()); return new Area3DPxg(bookIndex, sheet, area); } else { return new Area3DPxg(sheet, area); @@ -276,18 +276,18 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork public String resolveNameXText(NameXPtg n) { int idx = n.getNameIndex(); String name = null; - + // First, try to find it as a User Defined Function IndexedUDFFinder udfFinder = (IndexedUDFFinder)getUDFFinder(); name = udfFinder.getFunctionName(idx); if (name != null) return name; - + // Otherwise, try it as a named range XSSFName xname = _uBook.getNameAt(idx); if (xname != null) { name = xname.getNameName(); } - + return name; } @@ -307,7 +307,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork // Internal reference workbookName = null; } - + if (lastSheetName == null || firstSheetName.equals(lastSheetName)) { return new ExternalSheet(workbookName, firstSheetName); } else { @@ -385,7 +385,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork * Tables are cached for performance (formula evaluation looks them up by name repeatedly). * After the first table lookup, adding or removing a table from the document structure will cause trouble. * This is meant to be used on documents whose structure is essentially static at the point formulas are evaluated. - * + * * @param name the data table name (case-insensitive) * @return The Data table in the workbook named <tt>name</tt>, or <tt>null</tt> if no table is named <tt>name</tt>. * @since 3.15 beta 2 @@ -396,7 +396,7 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork String lname = caseInsensitive(name); return getTableCache().get(lname); } - + @Override public UDFFinder getUDFFinder(){ return _uBook.getUDFFinder(); diff --git a/src/ooxml/testcases/org/apache/poi/extractor/ooxml/TestExtractorFactory.java b/src/ooxml/testcases/org/apache/poi/extractor/ooxml/TestExtractorFactory.java index 1ef26afca2..3f827c3797 100644 --- a/src/ooxml/testcases/org/apache/poi/extractor/ooxml/TestExtractorFactory.java +++ b/src/ooxml/testcases/org/apache/poi/extractor/ooxml/TestExtractorFactory.java @@ -165,10 +165,8 @@ public class TestExtractorFactory { @Test public void testPOIFSInvalid() { - IOException ex = assertThrows( - IOException.class, - () -> ExtractorFactory.createExtractor(new POIFSFileSystem(txt)) - ); + // Not really an Extractor test, but we'll leave it to test POIFS reaction anyway ... + IOException ex = assertThrows(IOException.class, () -> new POIFSFileSystem(txt)); assertTrue(ex.getMessage().contains("Invalid header signature; read 0x3D20726F68747541, expected 0xE11AB1A1E011CFD0")); } |