This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 368f94a42b Adjusting XSLFDrawing's slide shape tree scan in 
constructor to prevent shape ID warnings. (#1187)
368f94a42b is described below

commit 368f94a42bb6c15e3a0a12dadbbe43e7f96a50c8
Author: David Bunner <[email protected]>
AuthorDate: Tue Jul 28 10:10:47 2026 -0400

    Adjusting XSLFDrawing's slide shape tree scan in constructor to prevent 
shape ID warnings. (#1187)
    
    * Updating the slide shape tree scan in XSLFDrawing's constructor to 
prevent it from reregistering every shape id it found.  This will restrict the 
scan to only run when construting the sheet's own top-level drawing.  Fixes 
issue #924.
    
    * Applying recomendations to open PR
    
    ---------
    
    Co-authored-by: David M. Bunner <[email protected]>
---
 .../org/apache/poi/xslf/usermodel/XSLFDrawing.java | 28 +++++++++++++---------
 .../poi/xslf/usermodel/TestXSLFGroupShape.java     | 23 ++++++++++++++++++
 2 files changed, 40 insertions(+), 11 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFDrawing.java 
b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFDrawing.java
index b9d954353f..ff216db9b3 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFDrawing.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFDrawing.java
@@ -38,17 +38,23 @@ public class XSLFDrawing {
     /*package*/ XSLFDrawing(XSLFSheet sheet, CTGroupShape spTree){
         _sheet = sheet;
         _spTree = spTree;
-        XmlObject[] cNvPr = sheet.getSpTree().selectPath(
-                "declare namespace p='" + NS_PRESENTATIONML + "' 
.//*/p:cNvPr");
-        for(XmlObject o : cNvPr) {
-            // powerpoint generates AlternateContent elements which cNvPr 
elements aren't recognized
-            // ignore them for now
-            if (o instanceof CTNonVisualDrawingProps) {
-                CTNonVisualDrawingProps p = (CTNonVisualDrawingProps)o;
-                try {
-                    sheet.registerShapeId(Math.toIntExact(p.getId()));
-                } catch (ArithmeticException e) {
-                    throw new IllegalStateException("Int overflow with drawing 
id " + p.getId());
+
+        // Only performs the shape id scan when this is the sheet's own 
top-level
+        // drawing, since that scan is already recursive and covers every 
shape in
+        // the sheet. This avoids unnecessary re-scanning and the annoying
+        // "shape id _ has been already used" warning. See 
https://github.com/apache/poi/issues/924.
+        if (spTree == sheet.getSpTree()) {
+            XmlObject[] cNvPr = _spTree.selectPath(
+                    "declare namespace p='" + NS_PRESENTATIONML + "' 
.//*/p:cNvPr");
+            for(XmlObject o : cNvPr) {
+                // powerpoint generates AlternateContent elements which cNvPr 
elements aren't recognized
+                // ignore them for now
+                if (o instanceof CTNonVisualDrawingProps p) {
+                    try {
+                        sheet.registerShapeId(Math.toIntExact(p.getId()));
+                    } catch (ArithmeticException e) {
+                        throw new IllegalStateException("Int overflow with 
drawing id " + p.getId());
+                    }
                 }
             }
         }
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFGroupShape.java 
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFGroupShape.java
index 547da5f71c..48bacd3397 100644
--- 
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFGroupShape.java
+++ 
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFGroupShape.java
@@ -19,6 +19,11 @@ package org.apache.poi.xslf.usermodel;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
 
 import java.awt.Dimension;
 import java.awt.geom.Rectangle2D;
@@ -86,6 +91,24 @@ class TestXSLFGroupShape {
         }
     }
 
+    // https://github.com/apache/poi/issues/924
+    @Test
+    void testCreatingShapeInGroupDoesNotReregisterShapeIds() throws Exception {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFSlide slide = spy(ppt.createSlide());
+            slide.createTextBox();
+            slide.createTextBox();
+            XSLFGroupShape group = slide.createGroup();
+
+            clearInvocations(slide);
+
+            // This is the trigger: the first shape created directly on the 
group.
+            group.createTextBox();
+
+            verify(slide, never()).registerShapeId(anyInt());
+        }
+    }
+
     @Test
     void testRemoveShapes() throws Exception {
         try (XMLSlideShow ppt = new XMLSlideShow()) {


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

Reply via email to