import
java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author zsy
*/
public final class Ubbcode {
private static final String imagePath = "images/"; // 定义图片路径
private static final String strIcons = "1";
private static final String picUrl = "edit/images/";
private static final String imgName = "edit/images/face/";
private static final String notePath = "http://www.richmap.com.cn/richmap4/api/tools/";
private static String ubbItem(String strContent, String re, String replayStr,
Pattern pattern, Matcher matcher, boolean IgnoreCase) {
pattern = Pattern.compile(re);
matcher = pattern.matcher(strContent);
strContent = matcher.replaceAll(replayStr);
if (IgnoreCase) {
pattern = Pattern.compile(re.toUpperCase());
matcher = pattern.matcher(strContent);
strContent = matcher.replaceAll(replayStr);
}
return strContent;
}
private static String quote(String strContent, Pattern pattern,
Matcher matcher, boolean IgnoreCase) {
String re = "(\[quote\])(.*?)(\[\/quote\])";
int start = strContent.lastIndexOf("[quote]");
int end = strContent.indexOf("[/quote]");
String subStr = null;
String newStr = null;
while (start != -1 && end != -1) {
subStr = strContent.substring(start, end + 8);
pattern = Pattern.compile(re);
matcher = pattern.matcher(subStr);
newStr = matcher.replaceAll("" +
");
strContent = strContent.replace(subStr, newStr);
start = strContent.lastIndexOf("[quote]");
end = strContent.indexOf("[/quote]");
}
if (IgnoreCase) {
re = "(\[QUOTE\])(.*?)(\[\/QUOTE\])";
start = strContent.lastIndexOf("[QUOTE]");
end = strContent.indexOf("[/QUOTE]");
while (start != -1 && end != -1) {
subStr = strContent.substring(start, end + 8);
pattern = Pattern.compile(re);
matcher = pattern.matcher(subStr);
newStr = matcher.replaceAll("" +
");
strContent = strContent.replace(subStr, newStr);
start = strContent.lastIndexOf("[QUOTE]");
end = strContent.indexOf("[/QUOTE]");
}
}
return strContent;
}
/**
* @description 转换代码,使之支持ubbcode.
* @param strContent
* 转换前的代码.
* @return result 转换后的内容.
*/
public static String ubbCode(String strContent) {
strContent = dvHTMLEncode(strContent);
Pattern pattern = null;
Matcher matcher = null;
//随意贴
strContent = ubbItem(strContent,
"\[richmapfrm=(.[^\[]*)\](.+?)\[\/richmapfrm\]",
"",
pattern, matcher, true);
strContent = ubbItem(strContent,
"\[richmap=(.[^\[]*)\](.+?)\[\/richmap\]",
"" + notePath + "myNote.jsp?¬eid=$1 " +
"title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank>" + picUrl +
"button/richwindow.gif border=0>$2",
pattern, matcher, true);
//转换IMG
strContent = ubbItem(strContent,
"\[img\](.+?)\[\/img\]",
"" +
"" +
"onload="javascript:if(this.width>600)this.width=600" " +
"galleryImg="no">",
pattern, matcher, true);
//转换附件[FILE=路径,图片]名称[/FILE]
strContent = ubbItem(strContent,
"(\[file=(.[^\[]*),(.[^\[]*)\])(.*?)(\[\/file\])",
"
$4",
pattern, matcher, true);
//DIR
strContent = ubbItem(strContent,
"\[dir=*([0-9]*),*([0-9]*)\](.*?)\[\/dir]",
"",
pattern, matcher, true);
//QT
strContent = ubbItem(strContent,
"\[qt=*([0-9]*),*([0-9]*)\](.*?)\[\/qt]",
"
import java.util.regex.Pattern;
/**
* @author zsy
*/
public final class Ubbcode {
private static final String imagePath = "images/"; // 定义图片路径
private static final String strIcons = "1";
private static final String picUrl = "edit/images/";
private static final String imgName = "edit/images/face/";
private static final String notePath = "http://www.richmap.com.cn/richmap4/api/tools/";
private static String ubbItem(String strContent, String re, String replayStr,
Pattern pattern, Matcher matcher, boolean IgnoreCase) {
pattern = Pattern.compile(re);
matcher = pattern.matcher(strContent);
strContent = matcher.replaceAll(replayStr);
if (IgnoreCase) {
pattern = Pattern.compile(re.toUpperCase());
matcher = pattern.matcher(strContent);
strContent = matcher.replaceAll(replayStr);
}
return strContent;
}
private static String quote(String strContent, Pattern pattern,
Matcher matcher, boolean IgnoreCase) {
String re = "(\[quote\])(.*?)(\[\/quote\])";
int start = strContent.lastIndexOf("[quote]");
int end = strContent.indexOf("[/quote]");
String subStr = null;
String newStr = null;
while (start != -1 && end != -1) {
subStr = strContent.substring(start, end + 8);
pattern = Pattern.compile(re);
matcher = pattern.matcher(subStr);
newStr = matcher.replaceAll("
" + "style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3>" + "style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;">***以下是引" + "用*** "$2 |
strContent = strContent.replace(subStr, newStr);
start = strContent.lastIndexOf("[quote]");
end = strContent.indexOf("[/quote]");
}
if (IgnoreCase) {
re = "(\[QUOTE\])(.*?)(\[\/QUOTE\])";
start = strContent.lastIndexOf("[QUOTE]");
end = strContent.indexOf("[/QUOTE]");
while (start != -1 && end != -1) {
subStr = strContent.substring(start, end + 8);
pattern = Pattern.compile(re);
matcher = pattern.matcher(subStr);
newStr = matcher.replaceAll("
" + "style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3>" + "style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;">***以下是引" + "用*** "$2 |
strContent = strContent.replace(subStr, newStr);
start = strContent.lastIndexOf("[QUOTE]");
end = strContent.indexOf("[/QUOTE]");
}
}
return strContent;
}
/**
* @description 转换代码,使之支持ubbcode.
* @param strContent
* 转换前的代码.
* @return result 转换后的内容.
*/
public static String ubbCode(String strContent) {
strContent = dvHTMLEncode(strContent);
Pattern pattern = null;
Matcher matcher = null;
//随意贴
strContent = ubbItem(strContent,
"\[richmapfrm=(.[^\[]*)\](.+?)\[\/richmapfrm\]",
"",
pattern, matcher, true);
strContent = ubbItem(strContent,
"\[richmap=(.[^\[]*)\](.+?)\[\/richmap\]",
"" + notePath + "myNote.jsp?¬eid=$1 " +
"title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank>" + picUrl +
"button/richwindow.gif border=0>$2",
pattern, matcher, true);
//转换IMG
strContent = ubbItem(strContent,
"\[img\](.+?)\[\/img\]",
"" +
"" +
"onload="javascript:if(this.width>600)this.width=600" " +
"galleryImg="no">",
pattern, matcher, true);
//转换附件[FILE=路径,图片]名称[/FILE]
strContent = ubbItem(strContent,
"(\[file=(.[^\[]*),(.[^\[]*)\])(.*?)(\[\/file\])",
"
$4",
pattern, matcher, true);
//DIR
strContent = ubbItem(strContent,
"\[dir=*([0-9]*),*([0-9]*)\](.*?)\[\/dir]",
"",
pattern, matcher, true);
//QT
strContent = ubbItem(strContent,
"\[qt=*([0-9]*),*([0-9]*)\](.*?)\[\/qt]",
"