尝试为 XWPFRun 替换 XWPFHyperlink 时出现 ConcurrentModificationException

ConcurrentModificationException when trying to replace XWPFHyperlink for XWPFRun

我正在尝试用超链接替换另一个字符串模式,但我得到 java.util.ConcurrentModificationException。错误指向的代码行没有意义,所以我无法找出发生了什么。

    // 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);
                    }

                }
            }
        }
    }

控制台错误:

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.next(Collections.java:1042)
at releasenotes.ReleaseNotesUpdater.replaceAllOccurrences(ReleaseNotesUpdater.java:263)
at releasenotes.ReleaseNotesUpdater.main(ReleaseNotesUpdater.java:85)

另外,除了这个错误,我还想得到一些关于如何用超链接替换字符串模式的建议。我已经搜索过了,但我对它的工作原理有点困惑。


编辑: 在 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);
            }
        };
    }

在 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];
    }

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

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

我已经找到了解决方案,所以如果有人遇到同样的问题,我会分享。

要用超链接 运行 替换普通 运行,只需执行以下操作:

                        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);

上面的代码在一个循环内,该循环遍历段落中的所有 运行(r 是当前的 运行)。所以你只需要调用 r.getCTR() 就可以编辑 运行.

发生异常的原因是因为我在这一行中尝试修改文档结构:

 XWPFParagraph temp_p = doc_buffer.createParagraph();

如有任何疑问,请随时在评论中提问。