]> source.dussan.org Git - poi.git/commitdiff
[bug-66080] XWPFNumbering.addAbstractNum uses abstract numberings count as id
authorPJ Fanning <fanningpj@apache.org>
Sun, 22 May 2022 08:28:44 +0000 (08:28 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sun, 22 May 2022 08:28:44 +0000 (08:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901110 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFNumbering.java
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFNumbering.java
test-data/document/NumberingWithOutOfOrderId.docx [new file with mode: 0644]

index 62131cafd556840a9e4448a35689f6e151e767e5..c4317eccc7351e9d3e4b9867cad7e7d1e8181d68 100644 (file)
@@ -223,13 +223,22 @@ public class XWPFNumbering extends POIXMLDocumentPart {
             ctNumbering.addNewAbstractNum().set(abstractNum.getAbstractNum());
         } else {
             abstractNum.setCtAbstractNum(ctNumbering.addNewAbstractNum());
-            abstractNum.getAbstractNum().setAbstractNumId(BigInteger.valueOf(pos));
+            BigInteger id = findNextAbstractNumberingId();
+            abstractNum.getAbstractNum().setAbstractNumId(id);
             ctNumbering.setAbstractNumArray(pos, abstractNum.getAbstractNum());
         }
         abstractNums.add(abstractNum);
         return abstractNum.getCTAbstractNum().getAbstractNumId();
     }
 
+    private BigInteger findNextAbstractNumberingId() {
+        long maxId = 0;
+        for (XWPFAbstractNum num : abstractNums) {
+            maxId = Math.max(maxId, num.getAbstractNum().getAbstractNumId().longValue());
+        }
+        return BigInteger.valueOf(maxId + 1);
+    }
+
     /**
      * remove an existing abstractNum
      *
index dd241c14d3d43d9bff1502cedc92bd9b9416cfa3..9b312603bde5840098e84d858d1bffa0941ba52a 100644 (file)
@@ -196,6 +196,9 @@ class TestXWPFBugs {
             //attempt to remove item with numId 2
             assertTrue(numbering.removeAbstractNum(BigInteger.valueOf(2)));
 
+            //adding one level to numbering with id 1
+            numbering.getAbstractNum(BigInteger.valueOf(1)).getCTAbstractNum().addNewLvl();
+
             XWPFDocument docReloaded = writeOutAndReadBack(doc);
             XWPFNumbering numberingReloaded = docReloaded.getNumbering();
 
@@ -208,6 +211,14 @@ class TestXWPFBugs {
                 } else {
                     assertNotNull(abstractNum, "Failed for " + id);
                     assertEquals(id, abstractNum.getAbstractNum().getAbstractNumId().longValue());
+
+                    // we added one level for numbering with id "1"
+                    if (id == 1) {
+                        //TODO remaining issue from https://bz.apache.org/bugzilla/show_bug.cgi?id=66079
+                        //assertEquals(1, abstractNum.getAbstractNum().getLvlList().size());
+                    } else {
+                        assertEquals(0, abstractNum.getAbstractNum().getLvlList().size());
+                    }
                 }
             }
 
index c1806c946af4c14c97692cb1a1b99b6e790b690a..09d31dbf13f1a6ceb3631283b5076dccf61d8dca 100644 (file)
@@ -153,4 +153,16 @@ class TestXWPFNumbering {
             assertEquals("upperLetter", ctNumLvl.getLvl().getNumFmt().getVal().toString());
         }
     }
+
+    @Test
+    void testAddAbstractNum() throws IOException {
+        try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("NumberingWithOutOfOrderId.docx")) {
+            doc.getNumbering().addAbstractNum(new XWPFAbstractNum());
+            long count = doc.getNumbering()
+                    .getAbstractNums().stream()
+                    .map(e -> e.getCTAbstractNum().getAbstractNumId().intValue())
+                    .distinct().count();
+            assertEquals(doc.getNumbering().getAbstractNums().size(), count);
+        }
+    }
 }
diff --git a/test-data/document/NumberingWithOutOfOrderId.docx b/test-data/document/NumberingWithOutOfOrderId.docx
new file mode 100644 (file)
index 0000000..6c471c4
Binary files /dev/null and b/test-data/document/NumberingWithOutOfOrderId.docx differ