Bug 57829

Summary: XWPFParagraph.removeRun(.) don't remove XWPFRun from irun list and can lead to XmlValueDisconnectedException
Product: POI Reporter: Bruno <brunohpg>
Component: XWPFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: brunohpg
Priority: P2    
Version: 3.11-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description Bruno 2015-04-17 21:47:26 UTC
The method XWPFParagraph.removeRun(.) don't remove parameter XWPFRun from internal 'irun' list of XWPFParagraph.

Insert methods puts XWPFRun in two lists: 'run' and 'irun'. Remove method must remove XWPFRun from both lists.

After use "XWPFParagraph.removeRun(.)" method, methods like XWPFParagraph.getText() throws XmlValueDisconnectedException.

----------------------------------------------
Simple code to generate an error:

        XWPFDocument doc = ...;

        for (XWPFParagraph paragraph : doc.getParagraphs()) {
            paragraph.removeRun(0);
            System.out.println(paragraph.getText());
        }

----------------------------------------------
The patch (NOT TESTED!) can be:
From:

    public boolean removeRun(int pos){
        if (pos >= 0 && pos < paragraph.sizeOfRArray()) {
            getCTP().removeR(pos);
            runs.remove(pos);
            return true;
        }
        return false;
    }

To:

    public boolean removeRun(int pos){
        if (pos >= 0 && pos < paragraph.sizeOfRArray()) {
            getCTP().removeR(pos);
            iruns.remove(runs.get(pos));
            runs.remove(pos);
            return true;
        }
        return false;
    }
----------------------------------------------

You can also check out if "pos" always corresponds to CTP.RArray index and 'run' list index.
Comment 1 Nick Burch 2015-04-24 01:29:38 UTC
Thanks, fixed with unit test (based on your examples!) in r1675738.