ConcurrentModificationException when trying to replace XWPFHyperlink for XWPFRun

651 Views Asked by At

I am trying to replace a string pattern for another one with hyperlink, but I am getting java.util.ConcurrentModificationException. The lines of code which the error is pointing don't make sense, so I wasn't able to find out what happened.

    // Replace occurrences in all paragraphs
    for (XWPFParagraph p : doc_buffer.getParagraphs()) {
        List<XWPFRun> p_runs = p.getRuns();
        if (p_runs != null) {
            for (XWPFRun r : p_runs) {
                String text = r.getText(0);
                if ((text != null) && (text.contains(pattern))) {
                    if (pattern.equals("LINK_TO_DOCS")) {
                        //TODO
                        String h_url = "http://example.com/linktodocs/";
                        String h_text = replacement;

                        // Creates the link as an external relationship
                        XWPFParagraph temp_p = doc_buffer.createParagraph();
                        String id = temp_p.getDocument().getPackagePart().addExternalRelationship(h_url, XWPFRelation.HYPERLINK.getRelation()).getId();

                        // Binds the link to the relationship
                        CTHyperlink link = temp_p.getCTP().addNewHyperlink();
                        link.setId(id);

                        // Creates the linked text
                        CTText linked_text = CTText.Factory.newInstance();
                        linked_text.setStringValue(h_text);

                        // Creates a wordprocessing Run wrapper
                        CTR ctr = CTR.Factory.newInstance();
                        ctr.setTArray(new CTText[] {linked_text});
                        link.setRArray(new CTR[] {ctr});

                        r = new XWPFHyperlinkRun(link, r.getCTR(), r.getParent());
                    }
                    else {
                        text = text.replaceAll(pattern, replacement);
                        r.setText(text, 0);
                    }

                }
            }
        }
    }

Console error:

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1042)
at releasenotes.ReleaseNotesUpdater.replaceAllOccurrences(ReleaseNotesUpdater.java:263)
at releasenotes.ReleaseNotesUpdater.main(ReleaseNotesUpdater.java:85)

Also, besides this error, I also would like some advice about how can I replace a string pattern for another one with hyperlink. I have searched but I am a bit confused about how it works.


Edit.: at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1042)

        public Iterator<E> iterator() {
        return new Iterator<E>() {
            private final Iterator<? extends E> i = c.iterator();

            public boolean hasNext() {return i.hasNext();}
            public E next()          {return i.next();}
            public void remove() {
                throw new UnsupportedOperationException();
            }
            @Override
            public void forEachRemaining(Consumer<? super E> action) {
                // Use backing collection version
                i.forEachRemaining(action);
            }
        };
    }

at java.util.ArrayList$Itr.next(ArrayList.java:859)

        @SuppressWarnings("unchecked")
    public E next() {
        checkForComodification();
        int i = cursor;
        if (i >= size)
            throw new NoSuchElementException();
        Object[] elementData = ArrayList.this.elementData;
        if (i >= elementData.length)
            throw new ConcurrentModificationException();
        cursor = i + 1;
        return (E) elementData[lastRet = i];
    }

at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)

        final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
    }
1

There are 1 best solutions below

0
On BEST ANSWER

I have found the solution so I am sharing if anyone has the same trouble.

To replace a common run with a Hyperlink run, simply do the following:

                        String h_url = "http://example.com/index.html";
                        String h_text = replacement;

                        // Creates the link as an external relationship
                        String id = r.getDocument().getPackagePart()
                                .addExternalRelationship(h_url, XWPFRelation.HYPERLINK.getRelation()).getId();

                        // Binds the link to the relationship
                        CTHyperlink link = r.getParagraph().getCTP().addNewHyperlink();
                        link.setId(id);

                        // Creates the linked text
                        CTText linked_text = CTText.Factory.newInstance();
                        linked_text.setStringValue(h_text);

                        // Creates a XML wordprocessing wrapper for Run
                        // The magic is here
                        CTR ctr = r.getCTR();
                        ctr.setTArray(new CTText[] { linked_text });

                        // Stylizing
                        CTRPr rpr_c = ctr.addNewRPr();
                        CTColor color = CTColor.Factory.newInstance();
                        color.setVal("0000FF");
                        rpr_c.setColor(color);

                        CTRPr rpr_u = ctr.addNewRPr();
                        rpr_u.addNewU().setVal(STUnderline.SINGLE);

The code above is inside a loop which is iterating over all runs in a paragraph (r is the current run). So you just have to call r.getCTR() to be able to edit the run.

The reason why the exception was happening, was because I was trying to modify the document structure while going through it in this line:

 XWPFParagraph temp_p = doc_buffer.createParagraph();

If anyone has questions, feel free to ask in the comments.