OpenOffice xSentenceCursor卡在段落

时间:2018-02-16 10:07:51

标签: java openoffice.org

我正在使用此例程迭代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);
    }

这似乎有效,但我不确定它是否会引入未来的问题。我宁愿选择“优雅”的解决方案。

1 个答案:

答案 0 :(得分:0)

根据gotoNextSentence(),它应该只在光标移动时返回true,所以这是一个bug。考虑filing a report

问题似乎发生在isEndOfSentence()但不是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