我有一个里面有格式化文本的DefaultStyledDocument。我还有一个函数,它用模式匹配器拆分内容(作为plainText)。
我需要一个函数,它可以从拆分的输出中生成新的完整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)
}发布于 2013-09-04 19:56:56
这不是一项微不足道的任务。想象一下,你的起始位置在嵌套表的中间,结束位置在有序列表的中间,放在基表之后。
对于最简单的cas,当文档中只有副图和文本时,可以遍历所有段落元素,询问它们的开始和结束偏移量,并与匹配器的偏移量进行比较。如果段落适合范围,则查看段落元素的子元素(文本元素)。对于每个适合给定范围的文本元素,只需在目标文档中调用insertString(),从文本元素传递文本和属性即可。
https://stackoverflow.com/questions/18612309
复制相似问题