]> source.dussan.org Git - poi.git/commitdiff
Make compilation work in Eclipse 2019.03 again by working around a
authorDominik Stadler <centic@apache.org>
Thu, 25 Apr 2019 12:27:04 +0000 (12:27 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 25 Apr 2019 12:27:04 +0000 (12:27 +0000)
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

src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java

index 6ae0663eb2860fbaedf45761d84ecf8ba69355a3..20f43f9403919108fe56f8070133f73cde0359db 100644 (file)
@@ -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<HSLFObjectShape> getOLEShapes() {
                return (List<HSLFObjectShape>)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;
+       }
 }
index 73756e5332833e6969d0cf9f39e3f4469a8d4c2f..4d06249f6a81c1f08f4d21b430268c564ffd08e4 100644 (file)
@@ -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