aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-05-22 10:23:59 +0000
committerPJ Fanning <fanningpj@apache.org>2022-05-22 10:23:59 +0000
commit435dc86896dde5f757303f57f3be2cadfa288229 (patch)
tree8abdd5f070c51ffcd71759c6c24ea343fb64ec58
parentb9d8bb01ec9bd27f24ddd973f596057ed0c6ba6d (diff)
downloadpoi-435dc86896dde5f757303f57f3be2cadfa288229.tar.gz
poi-435dc86896dde5f757303f57f3be2cadfa288229.zip
[bug-66081] fix editing of added abstract numberings
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901120 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFNumbering.java5
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java45
2 files changed, 47 insertions, 3 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFNumbering.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFNumbering.java
index c4317eccc7..b752f98ba1 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFNumbering.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFNumbering.java
@@ -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();
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java
index edfb986e99..8afaaecb2d 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java
@@ -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();