Bug 57829 - XWPFParagraph.removeRun(.) don't remove XWPFRun from irun list and can lead to XmlValueDisconnectedException
Summary: XWPFParagraph.removeRun(.) don't remove XWPFRun from irun list and can lead t...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XWPF (show other bugs)
Version: 3.11-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-04-17 21:47 UTC by Bruno
Modified: 2015-04-24 01:29 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.