[bug-66080] XWPFNumbering.addAbstractNum uses abstract numberings count as id

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901110 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-05-22 08:28:44 +00:00
parent dae226aecf
commit d178bb9dc1
4 changed files with 33 additions and 1 deletions

View 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
*

View 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());
}
}
}

View 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);
}
}
}

Binary file not shown.