]> source.dussan.org Git - poi.git/commitdiff
[bug-66081] fix editing of added abstract numberings
authorPJ Fanning <fanningpj@apache.org>
Sun, 22 May 2022 10:23:59 +0000 (10:23 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sun, 22 May 2022 10:23:59 +0000 (10:23 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901120 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

index c4317eccc7351e9d3e4b9867cad7e7d1e8181d68..b752f98ba17b2d7159c741bf07caaef10c148f85 100644 (file)
@@ -220,12 +220,15 @@ public class XWPFNumbering extends POIXMLDocumentPart {
     public BigInteger addAbstractNum(XWPFAbstractNum abstractNum) {
         int pos = abstractNums.size();
         if (abstractNum.getAbstractNum() != null) { // Use the current CTAbstractNum if it exists
-            ctNumbering.addNewAbstractNum().set(abstractNum.getAbstractNum());
+            CTAbstractNum ctAbstractNum = ctNumbering.addNewAbstractNum();
+            ctAbstractNum.set(abstractNum.getAbstractNum());
+            abstractNum.setCtAbstractNum(ctAbstractNum);
         } else {
             abstractNum.setCtAbstractNum(ctNumbering.addNewAbstractNum());
             BigInteger id = findNextAbstractNumberingId();
             abstractNum.getAbstractNum().setAbstractNumId(id);
             ctNumbering.setAbstractNumArray(pos, abstractNum.getAbstractNum());
+            abstractNum.setCtAbstractNum(ctNumbering.getAbstractNumArray(pos));
         }
         abstractNums.add(abstractNum);
         return abstractNum.getCTAbstractNum().getAbstractNumId();
index edfb986e99fbc8130a31f086ec9ea346c1cc603a..8afaaecb2d88a3b730718df1d041a69291c8aaca 100644 (file)
@@ -214,8 +214,7 @@ class TestXWPFBugs {
 
                     // 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());
+                        assertEquals(1, abstractNum.getAbstractNum().getLvlList().size());
                     } else {
                         assertEquals(0, abstractNum.getAbstractNum().getLvlList().size());
                     }
@@ -288,6 +287,48 @@ class TestXWPFBugs {
         }
     }
 
+    @Test
+    void testEditNumberings() throws IOException {
+        try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("NumberingWithOutOfOrderId.docx")) {
+            XWPFNumbering numbering = doc.createNumbering();
+
+            // Abstract numbering with id = 1 already exists in the file, and has 9 levels
+            XWPFAbstractNum abstractNum1 = numbering.getAbstractNum(BigInteger.ONE);
+            assertEquals(9, abstractNum1.getAbstractNum().getLvlList().size());
+            // Remove all levels from this numbering
+            while (abstractNum1.getAbstractNum().getLvlList().size() > 0) {
+                abstractNum1.getAbstractNum().removeLvl(0);
+            }
+
+            // Add abstract numbering with id = 0
+            addNumberingWithAbstractId(numbering, 0);
+
+            // Add abstract numbering with auto-generated id, this should be 2
+            numbering.addAbstractNum(new XWPFAbstractNum());
+
+            // Add (id + 1) levels to each abstract numbering
+            for (int id = 0; id < 3; id++) {
+                XWPFAbstractNum num = numbering.getAbstractNum(BigInteger.valueOf(id));
+                for (int j = 0; j < id + 1; j++) {
+                    num.getAbstractNum().addNewLvl();
+                }
+            }
+
+            // Check that all levels added successfully
+            for (int id = 0; id < 3; id++) {
+                assertEquals(id + 1, numbering.getAbstractNum(BigInteger.valueOf(id)).getAbstractNum().getLvlList().size());
+            }
+
+            XWPFDocument docReloaded = writeOutAndReadBack(doc);
+            XWPFNumbering numberingReloaded = docReloaded.getNumbering();
+
+            // Check that all added levels persisted after document reload
+            for (int id = 0; id < 3; id++) {
+                assertEquals(id + 1, numberingReloaded.getAbstractNum(BigInteger.valueOf(id)).getAbstractNum().getLvlList().size());
+            }
+        }
+    }
+
     private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id){
         // create a numbering scheme
         CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();