Bug 62138

Summary: Undefined results for insertNewRun() method when XWPFParagraph contains SDT runs
Product: POI Reporter: radu.c.stefanescu <radu.c.stefanescu>
Component: XWPFAssignee: POI Developers List <dev>
Status: NEW ---    
Severity: major    
Priority: P2    
Version: 3.14-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description radu.c.stefanescu@gmail.com 2018-02-27 11:22:18 UTC
I have a paragraph that contains one SDT element (Checkbox) on position 0 and some text on the next runs. I want to insert a text run BEFORE the SDT element in the paragraph, but calling the following method does not yield the expected results:


The insertNewRun method, when doing the synchronization between the Run list and the IRun list, will make no assumptions about the ordering of XWPFSDT and XWPFRun types of elements in the IRun list, which in this case is not yielding the desired result, as a client of the API will not be able to order the sequence of IRunElements as desired.

In the example before, the new text run (XWPFRun) is inserted on position 1, rather then before the XWPFSDT element.
This is happening due to this logic:

// To update the iruns, find where we're going
            // in the normal runs, and go in there
            int iPos = iruns.size();
            if (pos < runs.size()) {
                XWPFRun oldAtPos = runs.get(pos);
                int oldAt = iruns.indexOf(oldAtPos);
                if (oldAt != -1) {
                    iPos = oldAt;

Possible solution: enrich the API for XWPFParagraph with a method that inserts directly in the IRun list in order to be able to control the correct order of runs.