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
*
//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();
} 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());
+ }
}
}
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);
+ }
+ }
}