Bug 61421 - org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Rule M2.4 exception
Summary: org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Rule M2.4 exce...
Status: RESOLVED WONTFIX
Alias: None
Product: POI
Classification: Unclassified
Component: XWPF (show other bugs)
Version: 3.16-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-16 05:39 UTC by Alex
Modified: 2017-09-26 14:43 UTC (History)
0 users



Attachments
The file caused the exeption (27.67 KB, application/vnd.openxmlformats-officedocument.wordprocessingml.document)
2017-08-16 05:39 UTC, Alex
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alex 2017-08-16 05:39:35 UTC
Created attachment 35230 [details]
The file caused the exeption
Comment 1 Alex 2017-08-16 05:43:53 UTC
Code that caused the drop

XWPFDocument docx = new XWPFDocument(new FileInputStream("D:\\curs.docx"));
            ArrayList<Segment> topic = new ArrayList<>();

            int i = 0;
            for(XWPFParagraph paragraph : docx.getParagraphs()){
                byte sid = paragraph.getStyleID() != null ? (byte) paragraph.getStyleID().charAt(0) : 0;
                if (sid >= 49 && sid <=52 ){
                    if (paragraph.getText().length() > 0) {
                        System.out.println(paragraph.getStyleID() + ":" + paragraph.getText());
                        Segment segment = new Segment(paragraph.getText(), Integer.valueOf(paragraph.getStyleID().charAt(0)));
                        segment.doc = new XWPFDocument(docx.getPackage());
                        segment.from = i;
                        if(topic.size() > 0){
                            topic.get(topic.size() - 1).to = i - 1;
                        }
                        topic.add(segment);

                    }
                }
                i++;
            }

            for(Segment segment : topic){
                i = 0;
                for(XWPFParagraph paragraph : docx.getParagraphs()) {
                    if(!(i >= segment.from && i <=segment.to)){
                        int pPos = segment.doc.getPosOfParagraph(paragraph);
                        segment.doc.removeBodyElement(pPos);
                    }
                    i++;
                }
                segment.doc.write(new FileOutputStream(new File("D:\\test\\" + segment.title + ".docx")));
                segment.doc.close();
            }
Comment 2 Dominik Stadler 2017-09-26 14:43:40 UTC
I don't think using the existing parts for creating new XWFPDocument instances like you try to do here will work, probably better to immediately retrieve the necessary information from the part and not create many instances of XWPFDocument here.

If you still cannot get it to work when not doing this copying, then please reopen this issue and attach a real self-sufficient unit-test, your example relies on some classes that are not included, so it cannot be reproduced easily.