Bug 66312 - Inserting paragraph into table from cursor
Summary: Inserting paragraph into table from cursor
Alias: None
Product: POI
Classification: Unclassified
Component: XWPF (show other bugs)
Version: 5.2.2-FINAL
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2022-10-13 07:58 UTC by aoellerer
Modified: 2022-11-23 10:41 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description aoellerer 2022-10-13 07:58:05 UTC
I am trying to insert a paragraph next to another paragraph (`xwpfParagraph`) in a table in the following way:

1. `XmlCursor cursor = xwpfParagraph.getCTP().newCursor()`
2. `XWPFParagraph paragraph = document.insertNewParagraph(cursor)`

`paragraph` contains a valid `XWPFParagraph`

`paragraph` is `null`

This happens because the `XWPFDocument#insertNewParagraph` function checks the validity of the cursor (in the `XWPFDocument#isCursorInBody` function) by asserting that the parent of the `cursor` is the document`s body.
This is not the case in the example above, as the parent is the table, and only the parent of the table is the document body.
Comment 1 PJ Fanning 2022-10-13 08:00:25 UTC
could you write a full reproducible test case?
Comment 2 aoellerer 2022-10-13 09:22:16 UTC
Sorry for the delay!
Comment 3 PJ Fanning 2022-10-13 10:40:33 UTC
I added r1904563 - the test results are not as expected - I had to modify the broken test to get it to pass.

Anton - could you debug the new code to see why it replaces the paragraph instead of adding one?
Comment 4 aoellerer 2022-10-13 11:35:52 UTC
From what I've seen in the patched code, the old paragraph doesn't get replaced, `document.get....getParagraph(0)` always has the same memory adress
Comment 5 aoellerer 2022-10-13 11:53:15 UTC
XWPFDocument:716 the element is unconditionally added to the paragraphs array.
If it should be supported by apache poi, one would probably need to do a check on the parent type of the cursor to do the insertion correctly e.g. with `tables.get(x).getRow(x).getCell(x).addParagraph();`
But I think this would probably lead to a stark increase in the search space and resulting runtime of the algorithm?
Maybe it would be best to just specify in the documentation that only insertion to a `top-level` paragraph is possible?
Comment 6 PJ Fanning 2022-10-13 12:05:30 UTC
Honestly, POI XWPF performance is poor due to the overheads in XMLBeans. I'd prefer to get the code working than to worry about performance. In theory, the code can be tuned later - or overloaded methods added (again later) that skip the search if users feel it is not needed in their use case.

I haven't coded much with the XWPF code base and have little enthusiasm to get more involved.

If you feel like you can modify the insert to do what you need it to do without breaking existing tests, then I'd be happy to merge whatever you come up with.
Comment 7 aoellerer 2022-10-13 12:23:32 UTC
Ok, I will see what I can do, or whether I can find a viable path around it, thank you!
Comment 8 PJ Fanning 2022-11-23 10:41:08 UTC
I think this is complete