1

我有一个 DefaultStyledDocument,里面有一个格式化的文本。我还有一个函数,它用 Pattern-Matcher 分割内容(作为纯文本)。

我需要一个函数,它从拆分输出中生成新的完整 DefaultStyledDocuments

DefaultStyledDocument doc = new DefaultStyledDocument();
Functions.loadRtfToDocument(rtfText, doc); //rtfText is a RTF-String

Pattern pattern = Pattern.compile("^((\\s*)•)", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(plainText);
while(matcher.find()){
    int start = matcher.start();
    int end = matcher.end();
    DefaultStyledDocument target = new DefaultStyledDocument();
    //Fill the target with the styled text (from start to end)
}
4

1 回答 1

1

这不是一项微不足道的任务。想象一下,您的起始位置位于嵌套表的中间,而结束位置位于基表之后的有序列表的中间。

对于最简单的 cas,当您在文档中只有段落和文本时,您可以遍历所有段落元素,询问它们的开始和结束偏移量,并与匹配器的偏移量进行比较。如果段落适合范围,则通过段落元素的子元素(文本元素)。对于适合给定范围的每个文本元素,只需在目标文档中调用 insertString(),从文本元素传递文本和属性。

于 2013-09-04T11:56:56.023 回答