]> source.dussan.org Git - poi.git/commitdiff
Fix bug #56812 - In XSLF provide a way to get the URI of externally linked pictures
authorNick Burch <nick@apache.org>
Mon, 4 Aug 2014 22:00:47 +0000 (22:00 +0000)
committerNick Burch <nick@apache.org>
Mon, 4 Aug 2014 22:00:47 +0000 (22:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1615812 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java
src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java

index 8663b897fc4bbdc8cb83bfe4fb13f32b73e021e5..6d6e631b523188b4631cb2b214596f8b37c20e65 100644 (file)
@@ -23,6 +23,7 @@ import java.awt.Graphics2D;
 import java.awt.geom.Rectangle2D;\r
 import java.awt.image.BufferedImage;\r
 import java.io.ByteArrayInputStream;\r
+import java.net.URI;\r
 \r
 import javax.imageio.ImageIO;\r
 import javax.xml.namespace.QName;\r
@@ -99,10 +100,27 @@ public class XSLFPictureShape extends XSLFSimpleShape {
             setAnchor(new java.awt.Rectangle(50, 50, 200, 200));\r
         }\r
     }\r
+    \r
+    /**\r
+     * Is this an internal picture (image data included within\r
+     *  the PowerPoint file), or an external linked picture\r
+     *  (image lives outside)?\r
+     */\r
+    public boolean isExternalLinkedPicture() {\r
+        if (getBlipId() == null && getBlipLink() != null) {\r
+            return true;\r
+        }\r
+        return false;\r
+    }\r
 \r
+    /**\r
+     * Return the data on the (internal) picture.\r
+     * For an external linked picture, will return null\r
+     */\r
     public XSLFPictureData getPictureData() {\r
         if(_data == null){\r
             String blipId = getBlipId();\r
+            if (blipId == null) return null;\r
 \r
             PackagePart p = getSheet().getPackagePart();\r
             PackageRelationship rel = p.getRelationship(blipId);\r
@@ -118,10 +136,45 @@ public class XSLFPictureShape extends XSLFSimpleShape {
         }\r
         return _data;\r
     }\r
+    \r
+    /**\r
+     * For an external linked picture, return the last-seen\r
+     *  path to the picture.\r
+     * For an internal picture, returns null.\r
+     */\r
+    public URI getPictureLink() {\r
+        if (getBlipId() != null) {\r
+            // Internal picture, nothing to return\r
+            return null;\r
+        }\r
+        \r
+        String rId = getBlipLink();\r
+        if (rId == null) {\r
+            // No link recorded, nothing we can do\r
+            return null;\r
+        }\r
+        \r
+        PackagePart p = getSheet().getPackagePart();\r
+        PackageRelationship rel = p.getRelationship(rId);\r
+        if (rel != null) {\r
+            return rel.getTargetURI();\r
+        }\r
+        return null;\r
+    }\r
 \r
-    private String getBlipId(){\r
+    private CTBlip getBlip(){\r
         CTPicture ct = (CTPicture)getXmlObject();\r
-        return ct.getBlipFill().getBlip().getEmbed();\r
+        return ct.getBlipFill().getBlip();\r
+    }\r
+    private String getBlipLink(){\r
+        String link = getBlip().getLink();\r
+        if (link.isEmpty()) return null;\r
+        return link;\r
+    }\r
+    private String getBlipId(){\r
+        String id = getBlip().getEmbed();\r
+        if (id.isEmpty()) return null;\r
+        return id;\r
     }\r
 \r
     @Override\r
index fc422e778dfa6b2b121532da9ed2e03debc2250d..0e787ba569798f46414d16acbf77de6f21c24928 100644 (file)
@@ -144,24 +144,34 @@ public class TestXSLFBugs extends POITestCase {
      * there is no data available and XSLFPictureShape.getPictureData()
      * gives a NPE, see bug #56812
      */
-    public void DISABLEDtest56812() throws Exception {
+    public void test56812() throws Exception {
         XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("56812.pptx");
         
-        int pictures = 0;
+        int internalPictures = 0;
+        int externalPictures = 0;
         for (XSLFSlide slide : ppt.getSlides()){
             for (XSLFShape shape : slide.getShapes()){
                 assertNotNull(shape);
                 
                 if (shape instanceof XSLFPictureShape) {
-                    XSLFPictureData data = ((XSLFPictureShape) shape).getPictureData();
-                    assertNotNull(data);
-                    assertNotNull(data.getFileName());
-                    pictures++;
+                    XSLFPictureShape picture = (XSLFPictureShape)shape;
+                    if (picture.isExternalLinkedPicture()) {
+                        externalPictures++;
+                        
+                        assertNotNull(picture.getPictureLink());
+                    } else {
+                        internalPictures++;
+                        
+                        XSLFPictureData data = picture.getPictureData();
+                        assertNotNull(data);
+                        assertNotNull(data.getFileName());
+                    }
                 }
             }
         }
         
-        assertEquals(3, pictures);
+        assertEquals(2, internalPictures);
+        assertEquals(1, externalPictures);
     }
     
     protected String getSlideText(XSLFSlide slide) {