Use KefirBB to Extract Text from BBCode String

之前写了一篇帖子介绍如何从字符串中去除 BBCode 标签、提取纯文本信息,使用的是 JBoss Portal format 包,虽然可以解决问题,但非常不完美,比如,无法支持大写标签、无法识别自定义标签等。春节前发现了一个专门用于处理 BBCode 的 Java 库 KefirBB,当时的版本是 0.5,只支持 Java 6,我修改了些源码以支持 Java 5,并发了 patch 给其作者 Kefir。很快 Kefir 发布了新版本 0.6,同时支持 Java 5 & 6.

使用 KefirBB 提取纯文本信息非常简单。首先新建配置文件 kefirbb.xml 并放置到 CLASSPATH 中,其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
 
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://kefir-bb.sourceforge.net/schemas"
xsi:schemaLocation="http://kefir-bb.sourceforge.net/schemas http://kefir-bb.sourceforge.net/schemas/kefir-bb-0.5.xsd">
 
 <code>
 <pattern>[<var/>]</pattern>
 <template/>
 </code>
 
</configuration>

以上配置表示将所有用 中括号 [] 包含起来的部分去除。

然后就可以在 Java 代码中进行操作了:

TextProcessor processor = BBProcessorFactory.getInstance().create();
String txt = processor.process(bbString);

就是这么简单。

其实 KefirBB 的功能远不止如此,通过在配置文件中指明转换规则,KefirBB 可以进行任何文本间的转换。

This entry was posted in Development and tagged . Bookmark the permalink.

2 Responses to Use KefirBB to Extract Text from BBCode String

  1. Pingback: Extract Text from BBCode String | 细柳营

  2. maihill says:

    能否给一些实例给我参考一下,实践使用KefirBB 将bbcode转成HTML

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">