OpenOffice xSentenceCursor 停留在段落末尾

OpenOffice xSentenceCursor stuck at end of paragraph

我正在使用此例程迭代 OpenOffice 文档中的句子:

    while (moreParagraphsOO) {
        while (moreSentencesOO) {
            xSentenceCursor.gotoEndOfSentence(true);
            textSentence = xSentenceCursor.getString();
            xTextViewCursor.gotoRange(xSentenceCursor.getStart(), false);
            xTextViewCursor.gotoRange(xSentenceCursor.getEnd(), true);
            if (!textSentence.equals("")) {
                return textSentence;
            }
            moreSentencesOO = xSentenceCursor.gotoNextSentence(false);

        }

        moreParagraphsOO = xParagraphCursor.gotoNextParagraph(false);
        moreSentencesOO = xSentenceCursor.gotoStartOfSentence(false);
    }

它工作正常,除非它找到一个以“.”结尾的段落,这是一个句号和一个或几个空格。在这种情况下,它会进入并执行

的无限循环
 while (moreSentencesOO)
    ...
    moreSentencesOO = xSentenceCursor.gotoNextSentence(false);

没完没了。我对 OpenOffice API 不是很精通,我被困在这里了。有什么想法吗?

谢谢。


编辑:我带来了一个有点笨拙的补丁,包括检查光标的当前位置,如果它在两次迭代之间没有前进,则跳转到下一段:

    while (moreParagraphsOO) {
        while (moreSentencesOO) {

            /**********************************/
            int previousPosX = xTextViewCursor.getPosition().X;
            int previousPosY = xTextViewCursor.getPosition().Y;
            /*********************************/

            xSentenceCursor.gotoEndOfSentence(true);
            textSentence = xSentenceCursor.getString();
            xTextViewCursor.gotoRange(xSentenceCursor.getStart(), false);
            xTextViewCursor.gotoRange(xSentenceCursor.getEnd(), true);
            if (!textSentence.equals("")) {
                return textSentence;
            }            

            moreSentencesOO = xSentenceCursor.gotoNextSentence(false);

            /**********************************/
            if (previousPosX == xTextViewCursor.getPosition().X &&
                previousPosY == xTextViewCursor.getPosition().Y){
                    xParagraphCursor.gotoNextParagraph(false);
            }
            /**********************************/
        }
        moreParagraphsOO = xParagraphCursor.gotoNextParagraph(false);
        moreSentencesOO = xSentenceCursor.gotoStartOfSentence(false);
    }

它似乎有效,但我不确定它是否会引入未来的问题。我更喜欢 "elegant" 解决方案。

根据gotoNextSentence(), it should only return true if the cursor was moved, so this is a bug. Consider filing a report

问题似乎发生在 isEndOfSentence() but not isStartOfSentence() 时。所以测试那个而不是 getPosition().

这是 Andrew Pitonyak 的 Basic 宏,我对其进行了修改以包含此修复程序。

Sub CountSentences
    oCursor = ThisComponent.Text.createTextCursor()
    oCursor.gotoStart(False)
    Do
        nSentences = nSentences + 1
        If oCursor.isEndOfSentence() And Not oCursor.isStartOfSentence() Then
            oCursor.goRight(1, False)
        End If
    Loop While oCursor.gotoNextSentence(False)
    MsgBox nSentences & " sentences."
End Sub