From abc7359c8fdbcdde69777624889569f1114a0f82 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 25 Apr 2019 12:27:04 +0000 Subject: [PATCH] Make compilation work in Eclipse 2019.03 again by working around a limitation of the Eclipse Java Compiler regarding casting/generics git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1858117 13f79535-47bb-0310-9956-ffa450edef68 --- .../hslf/extractor/PowerPointExtractor.java | 69 ++++++++++++++++++- .../apache/poi/hslf/usermodel/TestBugs.java | 4 +- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java b/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java index 6ae0663eb2..20f43f9403 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java +++ b/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; +import org.apache.poi.EncryptedDocumentException; import org.apache.poi.extractor.POIOLE2TextExtractor; import org.apache.poi.hslf.usermodel.HSLFObjectShape; import org.apache.poi.hslf.usermodel.HSLFShape; @@ -32,6 +33,7 @@ import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.sl.extractor.SlideShowExtractor; +import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.util.Removal; @@ -94,7 +96,7 @@ public final class PowerPointExtractor extends POIOLE2TextExtractor { * @param fileName The name of the file to extract from */ public PowerPointExtractor(String fileName) throws IOException { - this((HSLFSlideShow)SlideShowFactory.create(new File(fileName), Biff8EncryptionKey.getCurrentUserPassword(), true)); + this(createHSLF(new File(fileName), Biff8EncryptionKey.getCurrentUserPassword(), true)); } /** @@ -103,7 +105,7 @@ public final class PowerPointExtractor extends POIOLE2TextExtractor { * @param iStream The input stream containing the PowerPoint document */ public PowerPointExtractor(InputStream iStream) throws IOException { - this((HSLFSlideShow)SlideShowFactory.create(iStream, Biff8EncryptionKey.getCurrentUserPassword())); + this(createHSLF(iStream, Biff8EncryptionKey.getCurrentUserPassword())); } /** @@ -112,7 +114,7 @@ public final class PowerPointExtractor extends POIOLE2TextExtractor { * @param fs the POIFSFileSystem containing the PowerPoint document */ public PowerPointExtractor(POIFSFileSystem fs) throws IOException { - this((HSLFSlideShow)SlideShowFactory.create(fs, Biff8EncryptionKey.getCurrentUserPassword())); + this(createHSLF(fs, Biff8EncryptionKey.getCurrentUserPassword())); } /** @@ -213,4 +215,65 @@ public final class PowerPointExtractor extends POIOLE2TextExtractor { public List getOLEShapes() { return (List)delegate.getOLEShapes(); } + + /** + * Helper method to avoid problems with compiling code in Eclipse + * + * Eclipse javac has some bugs with complex casts, this method tries + * to work around this. + * + * @param fs The {@link POIFSFileSystem} to read the document from + * @param password The password that should be used or null if no password is necessary. + * + * @return The created SlideShow + * + * @throws IOException if an error occurs while reading the data + */ + private static HSLFSlideShow createHSLF(POIFSFileSystem fs, String password) throws IOException, EncryptedDocumentException { + // Note: don't change the code here, it is required for Eclipse to compile the code + SlideShow slideShowOrig = SlideShowFactory.create(fs, password); + return (HSLFSlideShow)slideShowOrig; + } + + /** + * Helper method to avoid problems with compiling code in Eclipse + * + * Eclipse javac has some bugs with complex casts, this method tries + * to work around this. + * + * @param inp The {@link InputStream} to read data from. + * @param password The password that should be used or null if no password is necessary. + * + * @return The created SlideShow + * + * @throws IOException if an error occurs while reading the data + * @throws EncryptedDocumentException If the wrong password is given for a protected file + */ + private static HSLFSlideShow createHSLF(InputStream inp, String password) throws IOException, EncryptedDocumentException { + // Note: don't change the code here, it is required for Eclipse to compile the code + SlideShow slideShowOrig = SlideShowFactory.create(inp, password); + return (HSLFSlideShow)slideShowOrig; + } + + /** + * Helper method to avoid problems with compiling code in Eclipse + * + * Eclipse javac has some bugs with complex casts, this method tries + * to work around this. + * + * @param file The file to read data from. + * @param password The password that should be used or null if no password is necessary. + * @param readOnly If the SlideShow should be opened in read-only mode to avoid writing back + * changes when the document is closed. + * + * @return The created SlideShow + * + * @throws IOException if an error occurs while reading the data + * @throws EncryptedDocumentException If the wrong password is given for a protected file + */ + private static HSLFSlideShow createHSLF(File file, String password, boolean readOnly) throws IOException, EncryptedDocumentException { + // Note: don't change the code here, it is required for Eclipse to compile the code + SlideShow slideShowOrig = SlideShowFactory.create(file, password, readOnly); + return (HSLFSlideShow)slideShowOrig; + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java index 73756e5332..4d06249f6a 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java @@ -934,7 +934,9 @@ public final class TestBugs { private static HSLFSlideShow open(String fileName) throws IOException { File sample = HSLFTestDataSamples.getSampleFile(fileName); - return (HSLFSlideShow)SlideShowFactory.create(sample); + // Note: don't change the code here, it is required for Eclipse to compile the code + SlideShow slideShowOrig = SlideShowFactory.create(sample, null, false); + return (HSLFSlideShow)slideShowOrig; } @Test