java版的正规UBB JavaQtJSPAppleJavaScript
程序员文章站
2022-06-01 17:58:16
...
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("<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; " +
"TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: " +
"#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 " +
"cellPadding=6 width=95% align=center border=0><TBODY><TR><TD " +
"style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3><FONT " +
"style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;">***以下是引" +
"用***</FONT><HR width=95% style="border:1px dotted #cccccc "><center>" +
"$2</center></TD></TR></TBODY></TABLE>");
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("<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; " +
"TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: " +
"#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 " +
"cellPadding=6 width=95% align=center border=0><TBODY><TR><TD " +
"style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3><FONT " +
"style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;">***以下是引" +
"用***</FONT><HR width=95% style="border:1px dotted #cccccc "><center>" +
"$2</center></TD></TR></TBODY></TABLE>");
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\]",
"<iframe src=" + notePath + "singleNote.jsp?w=600&h=480" +
"¬eid=$1 frameborder=0 marginheight=0 marginwidth=0 scrolling=no height=400 " +
"width=600 title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank><img src=" +
picUrl + "button/richiframe.gif border=0> $2</iframe>",
pattern, matcher, true);
strContent = ubbItem(strContent,
"\[richmap=(.[^\[]*)\](.+?)\[\/richmap\]",
"<a href=" + notePath + "myNote.jsp?¬eid=$1 " +
"title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank><img src=" + picUrl +
"button/richwindow.gif border=0>$2</a>",
pattern, matcher, true);
//转换IMG
strContent = ubbItem(strContent,
"\[img\](.+?)\[\/img\]",
"<a href="$1" target="_blank">" +
"<IMG SRC="$1" border="0" alt="按此在新窗口浏览图片" " +
"onload="javascript:if(this.width>600)this.width=600" " +
"galleryImg="no"></a>",
pattern, matcher, true);
//转换附件[FILE=路径,图片]名称[/FILE]
strContent = ubbItem(strContent,
"(\[file=(.[^\[]*),(.[^\[]*)\])(.*?)(\[\/file\])",
"<br><IMG SRC=$3 border=0><br> <a href=$2>$4</a>",
pattern, matcher, true);
//DIR
strContent = ubbItem(strContent,
"\[dir=*([0-9]*),*([0-9]*)\](.*?)\[\/dir]",
"<object classid=clsid:" +
"166B1BCA-3F9C-11CF-8075-444553540000 codebase=" +
"http://download.macromedia.com/pub/shockwave/cabs/director/" +
"sw.cab#version=7,0,2,0 width=$1 height=$2><param name=src " +
"value=$3><embed src=$3 pluginspage=http://www.macromedia.com/" +
"shockwave/download/ width=$1 height=$2></embed></object>",
pattern, matcher, true);
//QT
strContent = ubbItem(strContent,
"\[qt=*([0-9]*),*([0-9]*)\](.*?)\[\/qt]",
"<embed src=$3 width=$1 height=$2 " +
"autoplay=true loop=false controller=true playeveryframe=false " +
"cache=false scale=TOFIT bgcolor=#000000 kioskmode=false " +
"targetcache=false pluginspage=http://www.apple.com/quicktime/>",
pattern, matcher, true);
//MP
strContent = ubbItem(strContent,
"\[mp=*([0-9]*),*([0-9]*)\](.*?)\[\/mp]",
"<object align=middle classid=CLSID:" +
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("<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; " +
"TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: " +
"#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 " +
"cellPadding=6 width=95% align=center border=0><TBODY><TR><TD " +
"style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3><FONT " +
"style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;">***以下是引" +
"用***</FONT><HR width=95% style="border:1px dotted #cccccc "><center>" +
"$2</center></TD></TR></TBODY></TABLE>");
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("<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; " +
"TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: " +
"#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 " +
"cellPadding=6 width=95% align=center border=0><TBODY><TR><TD " +
"style="WORD-WRAP: break-word;font-size:12px;" bgColor=#f3f3f3><FONT " +
"style="FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;">***以下是引" +
"用***</FONT><HR width=95% style="border:1px dotted #cccccc "><center>" +
"$2</center></TD></TR></TBODY></TABLE>");
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\]",
"<iframe src=" + notePath + "singleNote.jsp?w=600&h=480" +
"¬eid=$1 frameborder=0 marginheight=0 marginwidth=0 scrolling=no height=400 " +
"width=600 title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank><img src=" +
picUrl + "button/richiframe.gif border=0> $2</iframe>",
pattern, matcher, true);
strContent = ubbItem(strContent,
"\[richmap=(.[^\[]*)\](.+?)\[\/richmap\]",
"<a href=" + notePath + "myNote.jsp?¬eid=$1 " +
"title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank><img src=" + picUrl +
"button/richwindow.gif border=0>$2</a>",
pattern, matcher, true);
//转换IMG
strContent = ubbItem(strContent,
"\[img\](.+?)\[\/img\]",
"<a href="$1" target="_blank">" +
"<IMG SRC="$1" border="0" alt="按此在新窗口浏览图片" " +
"onload="javascript:if(this.width>600)this.width=600" " +
"galleryImg="no"></a>",
pattern, matcher, true);
//转换附件[FILE=路径,图片]名称[/FILE]
strContent = ubbItem(strContent,
"(\[file=(.[^\[]*),(.[^\[]*)\])(.*?)(\[\/file\])",
"<br><IMG SRC=$3 border=0><br> <a href=$2>$4</a>",
pattern, matcher, true);
//DIR
strContent = ubbItem(strContent,
"\[dir=*([0-9]*),*([0-9]*)\](.*?)\[\/dir]",
"<object classid=clsid:" +
"166B1BCA-3F9C-11CF-8075-444553540000 codebase=" +
"http://download.macromedia.com/pub/shockwave/cabs/director/" +
"sw.cab#version=7,0,2,0 width=$1 height=$2><param name=src " +
"value=$3><embed src=$3 pluginspage=http://www.macromedia.com/" +
"shockwave/download/ width=$1 height=$2></embed></object>",
pattern, matcher, true);
//QT
strContent = ubbItem(strContent,
"\[qt=*([0-9]*),*([0-9]*)\](.*?)\[\/qt]",
"<embed src=$3 width=$1 height=$2 " +
"autoplay=true loop=false controller=true playeveryframe=false " +
"cache=false scale=TOFIT bgcolor=#000000 kioskmode=false " +
"targetcache=false pluginspage=http://www.apple.com/quicktime/>",
pattern, matcher, true);
//MP
strContent = ubbItem(strContent,
"\[mp=*([0-9]*),*([0-9]*)\](.*?)\[\/mp]",
"<object align=middle classid=CLSID:" +