]> source.dussan.org Git - poi.git/commitdiff
Add missing close and handle theme-pptx in ExtractorFactory. Add creating slide-bitma...
authorDominik Stadler <centic@apache.org>
Sun, 1 Mar 2015 17:50:16 +0000 (17:50 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 1 Mar 2015 17:50:16 +0000 (17:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1663137 13f79535-47bb-0310-9956-ffa450edef68

src/integrationtest/org/apache/poi/stress/XSLFFileHandler.java
src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java

index e6cbb184b207eff7d27e2bf75e4a3de562ec9bab..b734c4e4bc1975aad9e5988d8ee1f17cca9d5bf1 100644 (file)
@@ -18,35 +18,93 @@ package org.apache.poi.stress;
 
 import static org.junit.Assert.assertNotNull;
 
+import java.awt.Dimension;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.xslf.XSLFSlideShow;
+import org.apache.poi.xslf.usermodel.XMLSlideShow;
+import org.apache.poi.xslf.usermodel.XSLFNotes;
+import org.apache.poi.xslf.usermodel.XSLFShape;
+import org.apache.poi.xslf.usermodel.XSLFSlide;
+import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
+import org.apache.poi.xslf.usermodel.XSLFTextShape;
 import org.junit.Test;
 
 public class XSLFFileHandler extends AbstractFileHandler {
        @Override
     public void handleFile(InputStream stream) throws Exception {
-        // ignore password protected files
-        if (POIXMLDocumentHandler.isEncrypted(stream)) return;
-
         XSLFSlideShow slide = new XSLFSlideShow(OPCPackage.open(stream));
                assertNotNull(slide.getPresentation());
                assertNotNull(slide.getSlideMasterReferences());
                assertNotNull(slide.getSlideReferences());
                
                new POIXMLDocumentHandler().handlePOIXMLDocument(slide);
+
+               ByteArrayOutputStream out = new ByteArrayOutputStream();
+               try {
+                   slide.write(out);
+               } finally {
+                   out.close();
+               }
+               
+        createBitmaps(out);            
        }
 
+    private void createBitmaps(ByteArrayOutputStream out) throws IOException {
+        XMLSlideShow ppt = new XMLSlideShow(new ByteArrayInputStream(out.toByteArray()));
+        Dimension pgsize = ppt.getPageSize();
+        XSLFSlide[] xmlSlide = ppt.getSlides();
+        int slideSize = xmlSlide.length;
+        for (int i = 0; i < slideSize; i++) {
+//            System.out.println("slide-" + (i + 1));
+//            System.out.println("" + xmlSlide[i].getTitle());
+
+            BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
+            Graphics2D graphics = img.createGraphics();
+
+            // draw stuff
+            xmlSlide[i].draw(graphics);
+
+            // Also try to read notes
+            XSLFNotes notes = xmlSlide[i].getNotes();
+            if(notes != null) {
+                for (XSLFShape note : notes) {
+                    note.draw(graphics);
+                    
+                    if (note instanceof XSLFTextShape) {
+                        XSLFTextShape txShape = (XSLFTextShape) note;
+                        for (XSLFTextParagraph xslfParagraph : txShape.getTextParagraphs()) {
+                            xslfParagraph.getText();
+                        }
+                    }
+                }
+            }
+        }
+    }
+
        // a test-case to test this locally without executing the full TestAllFiles
        @Test
        public void test() throws Exception {
-               InputStream stream = new FileInputStream("test-data/slideshow/testPPT.pptx");
+               InputStream stream = new FileInputStream("test-data/slideshow/pptx2svg.pptx");
                try {
                        handleFile(stream);
                } finally {
                        stream.close();
                }
        }
+
+
+    // a test-case to test this locally without executing the full TestAllFiles
+    @Test
+    public void testExtractor() throws Exception {
+        handleExtracting(new File("test-data/slideshow/testPPT.thmx"));
+    }
 }
\ No newline at end of file
index 60a0f518109a7e52a8410f1d026e7e8d963287d4..46cd2cd386d0075b2c433c84b01e91ecf048c383 100644 (file)
@@ -51,6 +51,7 @@ import org.apache.poi.poifs.filesystem.DirectoryEntry;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.Entry;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.xslf.XSLFSlideShow;
 import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
 import org.apache.poi.xslf.usermodel.XSLFRelation;
 import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
@@ -190,6 +191,14 @@ public class ExtractorFactory {
           }
        }
 
+       // special handling for SlideShow-Theme-files, 
+       if(XSLFRelation.THEME_MANAGER.getContentType().equals(corePart.getContentType())) {
+           return new XSLFPowerPointExtractor(new XSLFSlideShow(pkg));
+       }
+
+       // 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();
        throw new IllegalArgumentException("No supported documents found in the OOXML package (found "+corePart.getContentType()+")");
        }