pjfanning commented on code in PR #1599:
URL: https://github.com/apache/poi/pull/1599#discussion_r4726479799


##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java:
##########
@@ -431,18 +431,29 @@ void copy(XSLFShape sh) {
             nvPr.unsetCustDataLst();
         }
         if (blip.isSetExtLst()) {
-            // TODO: check for SVG copying
             CTOfficeArtExtensionList extLst = blip.getExtLst();
             for(CTOfficeArtExtension ext : extLst.getExtArray()){
                 String xpath = "declare namespace a14='"+ MS_DML_NS +"' 
$this//a14:imgProps/a14:imgLayer";
-                XmlObject[] obj = ext.selectPath(xpath);
-                if(obj != null && obj.length == 1) {
-                    try (XmlCursor c = obj[0].newCursor()) {
-                        String id = c.getAttributeText(EMBED_TAG);
-                        String newId = getSheet().importBlip(id, p.getSheet());
-                        c.setAttributeText(EMBED_TAG, newId);
-                    }
-                }
+                importEmbeddedBlip(ext.selectPath(xpath), p);
+                xpath = "declare namespace asvg='"+ MS_SVG_NS +"' 
$this/asvg:svgBlip";
+                importEmbeddedBlip(ext.selectPath(xpath), p);
+            }
+        }
+    }
+
+    /**
+     * Imports the picture referenced by the r:embed attribute of a blip 
extension element
+     * (e.g. the image layer or the SVG image) and points the attribute to the 
new relation.
+     */
+    private void importEmbeddedBlip(XmlObject[] obj, XSLFPictureShape source) {
+        if (obj == null || obj.length != 1) {
+            return;
+        }
+        try (XmlCursor c = obj[0].newCursor()) {
+            String id = c.getAttributeText(EMBED_TAG);
+            if (id != null) {
+                String newId = getSheet().importBlip(id, source.getSheet());

Review Comment:
   `XMLSlideShow.importBlip` does 
`parent.getRelationPartById(blipId).getDocumentPart()` without a null check, 
and throws `IllegalStateException` when the document part is not an 
`XSLFPictureData`. For the main blip that was already the case, but for the 
`svgBlip` this is new:
   
   - a file where `asvg:svgBlip` has an `r:embed` with no matching relationship 
(`getSvgImage()` tolerates that and returns null) would now fail the whole 
`importContent`/copy with a `NullPointerException`;
   - one where the relationship resolves to a part that POI did not load as 
`XSLFPictureData` would now fail with an `IllegalStateException`.
   
   Before this change both copied fine, with the raster picture intact. Could 
the SVG import be skipped (with a log message, or by dropping the SVG ext) when 
the source relation is missing or is not picture data? A test for a dangling 
`svgBlip` id would be good.



##########
poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java:
##########
@@ -271,6 +271,38 @@ void testMerge() throws Exception {
         }
     }
 
+    @Test
+    void testImportSvgImage() throws Exception {
+        try (XMLSlideShow ppt1 = new XMLSlideShow()) {
+            byte[] pngData = new byte[]{1, 2, 3};
+            byte[] svgData = IOUtils.toByteArray(
+                
POIDataSamples.getDocumentInstance().openResourceAsStream("../project-header.svg"));

Review Comment:
   Nit: `IOUtils.toByteArray` does not close the stream, so this one is left 
open. Please use try-with-resources.



##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java:
##########
@@ -431,18 +431,29 @@ void copy(XSLFShape sh) {
             nvPr.unsetCustDataLst();
         }
         if (blip.isSetExtLst()) {
-            // TODO: check for SVG copying
             CTOfficeArtExtensionList extLst = blip.getExtLst();
             for(CTOfficeArtExtension ext : extLst.getExtArray()){
                 String xpath = "declare namespace a14='"+ MS_DML_NS +"' 
$this//a14:imgProps/a14:imgLayer";
-                XmlObject[] obj = ext.selectPath(xpath);
-                if(obj != null && obj.length == 1) {
-                    try (XmlCursor c = obj[0].newCursor()) {
-                        String id = c.getAttributeText(EMBED_TAG);
-                        String newId = getSheet().importBlip(id, p.getSheet());
-                        c.setAttributeText(EMBED_TAG, newId);
-                    }
-                }
+                importEmbeddedBlip(ext.selectPath(xpath), p);
+                xpath = "declare namespace asvg='"+ MS_SVG_NS +"' 
$this/asvg:svgBlip";
+                importEmbeddedBlip(ext.selectPath(xpath), p);
+            }
+        }
+    }
+
+    /**
+     * Imports the picture referenced by the r:embed attribute of a blip 
extension element
+     * (e.g. the image layer or the SVG image) and points the attribute to the 
new relation.
+     */
+    private void importEmbeddedBlip(XmlObject[] obj, XSLFPictureShape source) {
+        if (obj == null || obj.length != 1) {

Review Comment:
   This keeps the old `length == 1` check, so if there is more than one match 
(e.g. two `a14:imgLayer` elements) nothing is imported and the copy keeps the 
`r:embed` ids of the source slide. Since this is now a helper, would it be 
simple to loop over all the matches instead?



##########
poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java:
##########
@@ -271,6 +271,38 @@ void testMerge() throws Exception {
         }
     }
 
+    @Test
+    void testImportSvgImage() throws Exception {
+        try (XMLSlideShow ppt1 = new XMLSlideShow()) {
+            byte[] pngData = new byte[]{1, 2, 3};
+            byte[] svgData = IOUtils.toByteArray(
+                
POIDataSamples.getDocumentInstance().openResourceAsStream("../project-header.svg"));
+
+            XSLFSlide slide1 = ppt1.createSlide();
+            XSLFPictureShape shape1 = 
slide1.createPicture(ppt1.addPicture(pngData, PictureType.PNG));
+            shape1.setSvgImage(ppt1.addPicture(svgData, PictureType.SVG));
+            assertArrayEquals(svgData, shape1.getSvgImage().getData());
+
+            // copy into another slideshow and into the same one
+            try (XMLSlideShow ppt2 = new XMLSlideShow()) {
+                for (XMLSlideShow target : new XMLSlideShow[]{ppt2, ppt1}) {
+                    XSLFSlide slideCopy = 
target.createSlide().importContent(slide1);
+                    XSLFPictureShape shapeCopy = (XSLFPictureShape) 
slideCopy.getShapes().get(0);
+
+                    assertArrayEquals(pngData, 
shapeCopy.getPictureData().getData());
+                    XSLFPictureData svgCopy = shapeCopy.getSvgImage();
+                    assertNotNull(svgCopy, "the svgBlip of the copy must point 
to a relation of the new slide");
+                    assertArrayEquals(svgData, svgCopy.getData());
+                }
+
+                try (XMLSlideShow ppt3 = writeOutAndReadBack(ppt2)) {

Review Comment:
   Only the copy into `ppt2` is written out and read back. Could `ppt1` (the 
same-slideshow copy) be round-tripped too, and could the test check that the 
copied SVG is still an SVG after the read (e.g. `getType() == PictureType.SVG` 
/ the content type)? `importBlip` adds the relation with `XSLFRelation.IMAGES`, 
so it would be good to have that pinned down.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to