From 611f5273fdc6a2105a3239fc337b6500af6026ba Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Mon, 18 Oct 2021 17:40:47 +0000 Subject: [PATCH] sonar fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894360 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/examples/ss/CellStyleDetails.java | 4 ++ .../formula/UserDefinedFunctionExample.java | 4 ++ .../poi/stress/AbstractFileHandler.java | 24 +++++------ .../apache/poi/xssf/model/StylesTable.java | 41 ++++++++++++++----- .../poi/hslf/dev/BaseTestPPTIterating.java | 31 +++++++------- .../org/apache/poi/sl/usermodel/Sheet.java | 1 + .../org/apache/poi/sl/usermodel/Slide.java | 7 ++-- .../apache/poi/sl/usermodel/SlideShow.java | 11 ++--- .../poi/sl/usermodel/SlideShowFactory.java | 1 + .../poi/sl/usermodel/TextParagraph.java | 1 + .../org/apache/poi/sl/usermodel/TextRun.java | 40 +++++++++--------- .../apache/poi/util/GenericRecordUtil.java | 1 + .../poi/util/GenericRecordXmlWriter.java | 1 + 13 files changed, 99 insertions(+), 68 deletions(-) diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/CellStyleDetails.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/CellStyleDetails.java index 6997a9e8fa..ce69271805 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/CellStyleDetails.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/CellStyleDetails.java @@ -47,6 +47,10 @@ public final class CellStyleDetails { } try (Workbook wb = WorkbookFactory.create(new File(args[0]))) { + if (wb == null) { + System.out.println("Workbook "+args[0]+" can't be loaded."); + return; + } DataFormatter formatter = new DataFormatter(); for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) { diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/formula/UserDefinedFunctionExample.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/formula/UserDefinedFunctionExample.java index 939fd9ab9d..be03faebbb 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/formula/UserDefinedFunctionExample.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/formula/UserDefinedFunctionExample.java @@ -54,6 +54,10 @@ public final class UserDefinedFunctionExample { File workbookFile = new File( args[0] ) ; try (Workbook workbook = WorkbookFactory.create(workbookFile, null, true)) { + if (workbook == null) { + System.out.println("Workbook "+workbookFile+" can't be loaded."); + return; + } String[] functionNames = {"calculatePayment"}; FreeRefFunction[] functionImpls = {new CalculateMortgage()}; diff --git a/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java index 2177af2b6f..fec396281d 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/AbstractFileHandler.java @@ -25,6 +25,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -43,23 +44,22 @@ import org.apache.poi.util.IOUtils; * in the integration tests, mostly text-extraction related at the moment. */ public abstract class AbstractFileHandler implements FileHandler { - public static final Set EXPECTED_EXTRACTOR_FAILURES = new HashSet<>(); - static { + public static final Set EXPECTED_EXTRACTOR_FAILURES = new HashSet<>(Arrays.asList( // password protected files without password // ... currently none ... // unsupported file-types, no supported OLE2 parts - EXPECTED_EXTRACTOR_FAILURES.add("hmef/quick-winmail.dat"); - EXPECTED_EXTRACTOR_FAILURES.add("hmef/winmail-sample1.dat"); - EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug52400-winmail-simple.dat"); - EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug52400-winmail-with-attachments.dat"); - EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug63955-winmail.dat"); - EXPECTED_EXTRACTOR_FAILURES.add("hpsf/Test0313rur.adm"); - EXPECTED_EXTRACTOR_FAILURES.add("poifs/Notes.ole2"); - EXPECTED_EXTRACTOR_FAILURES.add("poifs/64322.ole2"); + "hmef/quick-winmail.dat", + "hmef/winmail-sample1.dat", + "hmef/bug52400-winmail-simple.dat", + "hmef/bug52400-winmail-with-attachments.dat", + "hmef/bug63955-winmail.dat", + "hpsf/Test0313rur.adm", + "poifs/Notes.ole2", + "poifs/64322.ole2", //commons-compress 1.21 no longer supports truncated files - EXPECTED_EXTRACTOR_FAILURES.add("document/truncated62886.docx"); - } + "document/truncated62886.docx" + )); @Override public void handleExtracting(File file) throws Exception { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java index 20e2e4d990..60b1a9ce42 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java @@ -36,14 +36,17 @@ import java.util.TreeMap; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.ss.SpreadsheetVersion; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.BuiltinFormats; +import org.apache.poi.ss.usermodel.Color; +import org.apache.poi.ss.usermodel.FontFamily; +import org.apache.poi.ss.usermodel.FontScheme; +import org.apache.poi.ss.usermodel.TableStyle; import org.apache.poi.util.Internal; import org.apache.poi.xssf.usermodel.CustomIndexedColorMap; import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap; import org.apache.poi.xssf.usermodel.IndexedColorMap; import org.apache.poi.xssf.usermodel.XSSFBuiltinTableStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle; -import org.apache.poi.xssf.usermodel.XSSFFactory; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.poi.xssf.usermodel.XSSFTableStyle; @@ -51,7 +54,24 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.apache.xmlbeans.XmlException; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorders; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellStyleXfs; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxfs; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFills; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFonts; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmt; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmts; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyle; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyles; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument; /** * Table of styles shared across all sheets in a workbook. @@ -173,10 +193,9 @@ public class StylesTable extends POIXMLDocumentPart implements Styles { * After this, calls to {@link #getTheme()} won't give null */ public void ensureThemesTable() { - if (theme != null) return; - - XSSFFactory factory = workbook == null ? XSSFFactory.getInstance() : workbook.getXssfFactory(); - setTheme((ThemesTable)workbook.createRelationship(XSSFRelation.THEME, factory)); + if (theme == null && workbook != null) { + setTheme((ThemesTable) workbook.createRelationship(XSSFRelation.THEME, workbook.getXssfFactory())); + } } /** @@ -278,12 +297,12 @@ public class StylesTable extends POIXMLDocumentPart implements Styles { } /** - * Puts fmt in the numberFormats map if the format is not + * Puts {@code fmt} in the numberFormats map if the format is not * already in the the number format style table. - * Does nothing if fmt is already in number format style table. + * Does nothing if {@code fmt} is already in number format style table. * * @param fmt the number format to add to number format style table - * @return the index of fmt in the number format style table + * @return the index of {@code fmt} in the number format style table * @throws IllegalStateException if adding the number format to the styles table * would exceed the {@link #MAXIMUM_NUMBER_OF_DATA_FORMATS} allowed. */ @@ -331,7 +350,7 @@ public class StylesTable extends POIXMLDocumentPart implements Styles { /** * Add a number format with a specific ID into the numberFormats map. * If a format with the same ID already exists, overwrite the format code - * with fmt + * with {@code fmt} * This may be used to override built-in number formats. * * @param index the number format ID diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hslf/dev/BaseTestPPTIterating.java b/poi-scratchpad/src/test/java/org/apache/poi/hslf/dev/BaseTestPPTIterating.java index 9797cb74c8..4a0036b010 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hslf/dev/BaseTestPPTIterating.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hslf/dev/BaseTestPPTIterating.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.File; import java.io.PrintStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -44,23 +45,19 @@ import org.junit.jupiter.params.provider.MethodSource; @Isolated // this test changes global static BYTE_ARRAY_MAX_OVERRIDE public abstract class BaseTestPPTIterating { - protected static final Set OLD_FILES = new HashSet<>(); - static { - OLD_FILES.add("PPT95.ppt"); - OLD_FILES.add("pp40only.ppt"); - } - - protected static final Set ENCRYPTED_FILES = new HashSet<>(); - static { - ENCRYPTED_FILES.add("cryptoapi-proc2356.ppt"); - ENCRYPTED_FILES.add("Password_Protected-np-hello.ppt"); - ENCRYPTED_FILES.add("Password_Protected-56-hello.ppt"); - ENCRYPTED_FILES.add("Password_Protected-hello.ppt"); - ENCRYPTED_FILES.add("ppt_with_png_encrypted.ppt"); - } - - protected static final Map> EXCLUDED = - new HashMap<>(); + static final Set OLD_FILES = new HashSet<>(Arrays.asList( + "PPT95.ppt", "pp40only.ppt" + )); + + static final Set ENCRYPTED_FILES = new HashSet<>(Arrays.asList( + "cryptoapi-proc2356.ppt", + "Password_Protected-np-hello.ppt", + "Password_Protected-56-hello.ppt", + "Password_Protected-hello.ppt", + "ppt_with_png_encrypted.ppt" + )); + + static final Map> EXCLUDED = new HashMap<>(); public static Stream files() { String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY); diff --git a/poi/src/main/java/org/apache/poi/sl/usermodel/Sheet.java b/poi/src/main/java/org/apache/poi/sl/usermodel/Sheet.java index ab187cbc5d..8be7ee46d5 100644 --- a/poi/src/main/java/org/apache/poi/sl/usermodel/Sheet.java +++ b/poi/src/main/java/org/apache/poi/sl/usermodel/Sheet.java @@ -23,6 +23,7 @@ import java.awt.Graphics2D; /** * Common parent of Slides, Notes and Masters */ +@SuppressWarnings("java:S1452") public interface Sheet< S extends Shape, P extends TextParagraph diff --git a/poi/src/main/java/org/apache/poi/sl/usermodel/Slide.java b/poi/src/main/java/org/apache/poi/sl/usermodel/Slide.java index 7c0d566138..ade5b08ed6 100644 --- a/poi/src/main/java/org/apache/poi/sl/usermodel/Slide.java +++ b/poi/src/main/java/org/apache/poi/sl/usermodel/Slide.java @@ -58,17 +58,17 @@ public interface Slide< boolean getDisplayPlaceholder(Placeholder placeholder); /** - * Sets the slide visibility + * Sets the slide visibility * * @param hidden slide visibility, if {@code true} the slide is hidden, {@code false} shows the slide - * + * * @since POI 4.0.0 */ void setHidden(boolean hidden); /** * @return the slide visibility, the slide is hidden when {@code true} - or shown when {@code false} - * + * * @since POI 4.0.0 */ boolean isHidden(); @@ -76,6 +76,7 @@ public interface Slide< /** * @return the comment(s) for this slide */ + @SuppressWarnings("java:S1452") List getComments(); /** diff --git a/poi/src/main/java/org/apache/poi/sl/usermodel/SlideShow.java b/poi/src/main/java/org/apache/poi/sl/usermodel/SlideShow.java index eaca055dfe..1ad08c2c88 100644 --- a/poi/src/main/java/org/apache/poi/sl/usermodel/SlideShow.java +++ b/poi/src/main/java/org/apache/poi/sl/usermodel/SlideShow.java @@ -29,6 +29,7 @@ import org.apache.poi.common.usermodel.fonts.FontInfo; import org.apache.poi.extractor.POITextExtractor; import org.apache.poi.sl.usermodel.PictureData.PictureType; +@SuppressWarnings("java:S1452") public interface SlideShow< S extends Shape, P extends TextParagraph @@ -58,10 +59,10 @@ public interface SlideShow< * @param pgsize page size (in points) */ void setPageSize(Dimension pgsize); - + /** * Returns all Pictures of this slideshow. - * The returned {@link List} is unmodifiable. + * The returned {@link List} is unmodifiable. * @return a {@link List} of {@link PictureData}. */ List getPictureData(); @@ -97,10 +98,10 @@ public interface SlideShow< * @since 3.15 beta 1 */ PictureData addPicture(File pict, PictureType format) throws IOException; - + /** * check if a picture with this picture data already exists in this presentation - * + * * @param pictureData The picture data to find in the SlideShow * @return {@code null} if picture data is not found in this slideshow * @since 3.15 beta 3 @@ -121,7 +122,7 @@ public interface SlideShow< /** * @return an extractor for the slideshow metadata - * + * * @since POI 4.0.0 */ POITextExtractor getMetadataTextExtractor(); diff --git a/poi/src/main/java/org/apache/poi/sl/usermodel/SlideShowFactory.java b/poi/src/main/java/org/apache/poi/sl/usermodel/SlideShowFactory.java index 465c6f978b..cb4c81ca77 100644 --- a/poi/src/main/java/org/apache/poi/sl/usermodel/SlideShowFactory.java +++ b/poi/src/main/java/org/apache/poi/sl/usermodel/SlideShowFactory.java @@ -35,6 +35,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.FileMagic; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +@SuppressWarnings("java:S1452") public final class SlideShowFactory { private static class Singleton { diff --git a/poi/src/main/java/org/apache/poi/sl/usermodel/TextParagraph.java b/poi/src/main/java/org/apache/poi/sl/usermodel/TextParagraph.java index a6c4394278..c0d0c3be3a 100644 --- a/poi/src/main/java/org/apache/poi/sl/usermodel/TextParagraph.java +++ b/poi/src/main/java/org/apache/poi/sl/usermodel/TextParagraph.java @@ -384,6 +384,7 @@ public interface TextParagraph< * * @since POI 4.0.0 */ + @SuppressWarnings("java:S1452") List getTabStops(); /** diff --git a/poi/src/main/java/org/apache/poi/sl/usermodel/TextRun.java b/poi/src/main/java/org/apache/poi/sl/usermodel/TextRun.java index 7dfd4933d8..11bc79c8ec 100644 --- a/poi/src/main/java/org/apache/poi/sl/usermodel/TextRun.java +++ b/poi/src/main/java/org/apache/poi/sl/usermodel/TextRun.java @@ -27,7 +27,7 @@ import org.apache.poi.util.Internal; /** * Some text. */ -@SuppressWarnings("unused") +@SuppressWarnings({"unused","java:S1452"}) public interface TextRun { /** * Type of text capitals @@ -37,7 +37,7 @@ public interface TextRun { SMALL, ALL } - + /** * Type of placeholder fields */ @@ -96,18 +96,18 @@ public interface TextRun { void setFontSize(Double fontSize); /** - * Get the font family - convenience method for {@link #getFontInfo(FontGroup)} - * + * Get the font family - convenience method for {@link #getFontInfo(FontGroup)} + * * @return font family or null if not set */ String getFontFamily(); /** * Get the font family - convenience method for {@link #getFontInfo(FontGroup)} - * + * * @param fontGroup the font group, i.e. the range of glpyhs to be covered. - * if {@code null}, the font group matching the first character will be returned - * + * if {@code null}, the font group matching the first character will be returned + * * @return font family or null if not set */ String getFontFamily(FontGroup fontGroup); @@ -128,17 +128,17 @@ public interface TextRun { * @param typeface the font to apply to this text run. * The value of {@code null} removes the run specific font setting, so the default setting is activated again. * @param fontGroup the font group, i.e. the range of glpyhs to be covered. - * if {@code null}, the font group matching the first character will be returned + * if {@code null}, the font group matching the first character will be returned */ void setFontFamily(String typeface, FontGroup fontGroup); /** * Get the font info for the given font group - * + * * @param fontGroup the font group, i.e. the range of glpyhs to be covered. - * if {@code null}, the font group matching the first character will be returned + * if {@code null}, the font group matching the first character will be returned * @return font info or {@code null} if not set - * + * * @since POI 3.17-beta2 */ FontInfo getFontInfo(FontGroup fontGroup); @@ -149,11 +149,11 @@ public interface TextRun { * @param fontInfo the font to apply to this text run. * The value of {@code null} removes the run specific font setting, so the default setting is activated again. * @param fontGroup the font group, i.e. the range of glpyhs to be covered. defaults to latin, if {@code null}. - * + * * @since POI 3.17-beta2 */ void setFontInfo(FontInfo fontInfo, FontGroup fontGroup); - + /** * @return true, if text is bold */ @@ -165,7 +165,7 @@ public interface TextRun { * @param bold set to true for bold text, false for normal weight */ void setBold(boolean bold); - + /** * @return true, if text is italic */ @@ -219,24 +219,24 @@ public interface TextRun { /** * Return the associated hyperlink - * + * * @return the associated hyperlink or null if no hyperlink was set - * + * * @since POI 3.14-Beta2 */ Hyperlink getHyperlink(); - - + + /** * Creates a new hyperlink and assigns it to this text run. * If the text run has already a hyperlink assigned, return it instead * * @return the associated hyperlink - * + * * @since POI 3.14-Beta2 */ Hyperlink createHyperlink(); - + /** * Experimental method to determine the field type, e.g. slide number * diff --git a/poi/src/main/java/org/apache/poi/util/GenericRecordUtil.java b/poi/src/main/java/org/apache/poi/util/GenericRecordUtil.java index f788ec8667..a341b060c3 100644 --- a/poi/src/main/java/org/apache/poi/util/GenericRecordUtil.java +++ b/poi/src/main/java/org/apache/poi/util/GenericRecordUtil.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.function.Supplier; import java.util.stream.Collectors; +@SuppressWarnings("java:S1452") @Internal public final class GenericRecordUtil { private GenericRecordUtil() {} diff --git a/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java b/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java index a90f9c94fb..793c1e845f 100644 --- a/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java +++ b/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java @@ -234,6 +234,7 @@ public class GenericRecordXmlWriter implements Closeable { printObject("error", errorMsg); } + @SuppressWarnings("java:S1452") protected Stream>> writeProp(Map.Entry> me) { Object obj = me.getValue().get(); if (obj == null) { -- 2.39.5