poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r1241896 - in /poi/trunk/src/ooxml: java/org/apache/poi/xslf/usermodel/XMLSlideShow.java java/org/apache/poi/xslf/usermodel/XSLFPictureData.java testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
Date Wed, 08 Feb 2012 13:44:12 GMT
Author: yegor
Date: Wed Feb  8 13:44:12 2012
New Revision: 1241896

URL: http://svn.apache.org/viewvc?rev=1241896&view=rev
Log:
support caching images in xslf

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
    poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java?rev=1241896&r1=1241895&r2=1241896&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java Wed Feb  8 13:44:12
2012
@@ -19,17 +19,10 @@ package org.apache.poi.xslf.usermodel;
 import org.apache.poi.POIXMLDocument;
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
+import org.apache.poi.POIXMLRelation;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
-import org.apache.poi.openxml4j.opc.OPCPackage;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.openxml4j.opc.PackagePartName;
-import org.apache.poi.openxml4j.opc.TargetMode;
-import org.apache.poi.util.Beta;
-import org.apache.poi.util.Internal;
-import org.apache.poi.util.POILogFactory;
-import org.apache.poi.util.POILogger;
-import org.apache.poi.util.PackageHelper;
-import org.apache.poi.util.Units;
+import org.apache.poi.openxml4j.opc.*;
+import org.apache.poi.util.*;
 import org.apache.poi.xslf.XSLFSlideShow;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
@@ -338,21 +331,40 @@ public class XMLSlideShow  extends POIXM
      * @see XSLFPictureData#PICTURE_TYPE_DIB
      */
     public int addPicture(byte[] pictureData, int format) {
-        getAllPictures();
-        
-        int imageNumber = _pictures.size() + 1;
-        XSLFPictureData img = (XSLFPictureData) createRelationship(
-                XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber,
true);
-        _pictures.add(img);
-        try {
-            OutputStream out = img.getPackagePart().getOutputStream();
-            out.write(pictureData);
-            out.close();
-        } catch (IOException e) {
-            throw new POIXMLException(e);
+        XSLFPictureData img = findPictureData(pictureData);
+        POIXMLRelation relDesc = XSLFPictureData.RELATIONS[format];
+
+        if(img == null) {
+            int imageNumber = _pictures.size();
+            img = (XSLFPictureData) createRelationship(
+                    XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber
+ 1, true);
+            _pictures.add(img);
+            try {
+                OutputStream out = img.getPackagePart().getOutputStream();
+                out.write(pictureData);
+                out.close();
+            } catch (IOException e) {
+                throw new POIXMLException(e);
+            }
+            return _pictures.size() - 1;
+        } else {
+            return _pictures.indexOf(img);
+        }
+    }
+
+    /**
+     * check if a picture with this picture data already exists in this presentation
+     */
+    XSLFPictureData findPictureData(byte[] pictureData){
+        long checksum = IOUtils.calculateChecksum(pictureData);
+        for(XSLFPictureData pic : getAllPictures()){
+            if(pic.getChecksum() == checksum) {
+                return pic;
+            }
         }
-        return imageNumber - 1;
+        return null;
     }
+
     public XSLFTableStyles getTableStyles(){
         return _tableStyles;
     }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java?rev=1241896&r1=1241895&r2=1241896&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java Wed Feb  8
13:44:12 2012
@@ -168,4 +168,11 @@ public final class XSLFPictureData exten
         return 0;
     }
 
+    long getChecksum(){
+        if(checksum == null){
+            byte[] pictureData = getData();
+            checksum = IOUtils.calculateChecksum(pictureData);
+        }
+        return checksum;
+    }
 }
\ No newline at end of file

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java?rev=1241896&r1=1241895&r2=1241896&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
(original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
Wed Feb  8 13:44:12 2012
@@ -113,4 +113,21 @@ public class TestXSLFPictureShape extend
             assertTrue(Arrays.equals(data1, data.getData()));
         }
     }
+
+    public void testImageCaching() {
+        XMLSlideShow ppt = new XMLSlideShow();
+        byte[] img1 = new byte[]{1,2,3};
+        byte[] img2 = new byte[]{3,4,5};
+        int idx1 = ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG);
+        assertEquals(0, idx1);
+        assertEquals(0, ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG));
+
+        int idx2 = ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG);
+        assertEquals(1, idx2);
+        assertEquals(1, ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG));
+
+        XSLFSlide slide1 = ppt.createSlide();
+        XSLFSlide slide2 = ppt.createSlide();
+
+    }
 }
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message