测试

eplang 2008-03-09 11:06:15

package com.eplang;

/**
* Creation Date: 2008-03-09
* @author eplang
*/
public class UbbCode
{

private String source; // 待转化的HTML代码字符串

private String ubbTags[]; // UBB 标记数组

private String htmlTags[]; // HTML 标记数组

// 初始化,分别为 UBB 标记数组和 HTML 标记数组赋值
public UbbCode() {
int size = 74;
source = new String();
ubbTags = new String[size];
htmlTags = new String[size];
ubbTags[0] = "";
htmlTags[0] = "<b>";
ubbTags[1] = "
";
htmlTags[1] = "</b>";
ubbTags[2] = "";
htmlTags[2] = "<em>";
ubbTags[3] = "
";
htmlTags[3] = "</em>";
ubbTags[4] = "
引用
";
htmlTags[4] = "<div style=\"border-style:dashed;background-color:#CCCCCC;border-width:thin;border-color:#999999\"><br><em>";
ubbTags[5] = "
";
htmlTags[5] = "</em><br><br></div>";
ubbTags[6] = "[/size]";
htmlTags[6] = "</font>";
ubbTags[7] = "[size=6]";
htmlTags[7] = "<font style=\"font-size:6px\">";
ubbTags[8] = "[size=8]";
htmlTags[8] = "<font style=\"font-size:8px\">";
ubbTags[9] = "[size=10]";
htmlTags[9] = "<font style=\"font-size:10px\">";
ubbTags[10] = "[size=12]";
htmlTags[10] = "<font style=\"font-size:12px\">";
ubbTags[11] = "[size=14]";
htmlTags[11] = "<font style=\"font-size:14px\">";
ubbTags[12] = "[size=18]";
htmlTags[12] = "<font style=\"font-size:18px\">";
ubbTags[13] = "[size=24]";
htmlTags[13] = "<font style=\"font-size:24px\">";
ubbTags[14] = "[size=36]";
htmlTags[14] = "<font style=\"font-size:36px\">";

// 字体
ubbTags[15] = "[/font]";
htmlTags[15] = "</font>";
ubbTags[16] = "[font=Arial]";
htmlTags[16] = "<font face=\"Arial\">";
ubbTags[17] = "[font=Arial Black]";
htmlTags[17] = "<font face=\"Arial Black\">";
ubbTags[18] = "[font=Verdana]";
htmlTags[18] = "<font face=\"Verdana\">";
ubbTags[19] = "[font=Times New Roman]";
htmlTags[19] = "<font face=\"Times New Roman\">";
ubbTags[20] = "[font=Garamond]";
htmlTags[20] = "<font face=\"Garamond\">";
ubbTags[21] = "[font=Courier New]";
htmlTags[21] = "<font face=\"Courier New\">";
ubbTags[22] = "[font=Webdings]";
htmlTags[22] = "<font face=\"Webdings\">";
ubbTags[23] = "[font=Wingdings]";
htmlTags[23] = "<font face=\"Wingdings\">";
ubbTags[24] = "[font=隶书]";
htmlTags[24] = "<font face=\"隶书\">";
ubbTags[25] = "[font=幼圆]";
htmlTags[25] = "<font face=\"幼圆\">";
ubbTags[26] = "[font=方正舒体]";
htmlTags[26] = "<font face=\"方正舒体\">";
ubbTags[27] = "[font=方正姚体]";
htmlTags[27] = "<font face=\"方正姚体\">";
ubbTags[28] = "[font=仿宋_GB2312]";
htmlTags[28] = "<font face=\"仿宋_GB2312\">";
ubbTags[29] = "[font=黑体]";
htmlTags[29] = "<font face=\"黑体\">";
ubbTags[30] = "[font=华文彩云]";
htmlTags[30] = "<font face=\"华文彩云\">";
ubbTags[31] = "[font=华文细黑]";
htmlTags[31] = "<font face=\"华文细黑\">";
ubbTags[32] = "[font=华文新魏]";
htmlTags[32] = "<font face=\"华文新魏\">";
ubbTags[33] = "[font=华文中宋]";
htmlTags[33] = "<font face=\"华文中宋\">";
ubbTags[34] = "[font=华文行楷]";
htmlTags[34] = "<font face=\"华文行楷\">";
ubbTags[35] = "[font=楷体_GB2312]";
htmlTags[35] = "<font face=\"楷体_GB2312\">";
ubbTags[36] = "[font=隶书]";
htmlTags[36] = "<font face=\"隶书\">";
ubbTags[37] = "[font=华文楷体]";
htmlTags[37] = "<font face=\"华文楷体\">";
ubbTags[38] = "[font=宋体]";
htmlTags[38] = "<font face=\"宋体\">";
ubbTags[39] = "[font=新宋体";
htmlTags[39] = "<font face=\"新宋体\">";
ubbTags[40] = "[font=幼圆";
htmlTags[40] = "<font face=\"幼圆\">";

// 字体颜色
ubbTags[41] = "[red]";
htmlTags[41] = "<font color=\"red\">";
ubbTags[42] = "[/red]";
htmlTags[42] = "</font>";
ubbTags[43] = "[blue]";
htmlTags[43] = "<font color=\"blue\">";
ubbTags[44] = "[/blue]";
htmlTags[44] = "</font>";
ubbTags[45] = "[yellow]";
htmlTags[45] = "<font color=\"yellow\">";
ubbTags[46] = "[/yellow]";
htmlTags[46] = "</font>";
ubbTags[47] = "[green]";
htmlTags[47] = "<font color=\"green\">";
ubbTags[48] = "[/green]";
htmlTags[48] = "</font>";

ubbTags[49] = "[f]";
htmlTags[49] = "<marquee width=\"400\" scrolldelay=\"30\" scrollamount=\"1\" onmouseover=\"this.stop()\" onmouseout=\"this.start()\">";

// 标题
ubbTags[50] = "[h1]";
htmlTags[50] = "<h1>";
ubbTags[51] = "[/h1]";
htmlTags[51] = "</h1>";
ubbTags[52] = "[h2]";
htmlTags[52] = "<h2>";
ubbTags[53] = "[/h2]";
htmlTags[53] = "</h2>";
ubbTags[54] = "[h3]";
htmlTags[54] = "<h3>";
ubbTags[55] = "[/h3]";
htmlTags[55] = "</h3>";
ubbTags[56] = "[h4]";
htmlTags[56] = "<h4>";
ubbTags[57] = "[/h4]";
htmlTags[57] = "</h4>";
ubbTags[58] = "[h5]";
htmlTags[58] = "<h5>";
ubbTags[59] = "[/h5]";
htmlTags[59] = "</h5>";
ubbTags[60] = "[h6]";
htmlTags[60] = "<h6>";
ubbTags[61] = "[/h6]";
htmlTags[61] = "</h6>";
ubbTags[62] = "[hr]";
htmlTags[62] = "<hr>";
ubbTags[63] = "[img]";
htmlTags[63] = "<br><img src=\"";
ubbTags[64] = "[/img]";
htmlTags[64] = "\"><br>";
ubbTags[65] = "
";
htmlTags[65] = "<div align=\"center\">";
ubbTags[66] = "
";
htmlTags[66] = "</div>";

ubbTags[67] = "[/f]";
htmlTags[67] = "</marquee>";
ubbTags[68] = "
";
htmlTags[68] = "<div align=\"left\">";
ubbTags[69] = "
";
htmlTags[69] = "</div>";
ubbTags[70] = "
";
htmlTags[70] = "<div align=\"right\">";
ubbTags[71] = "
";
htmlTags[71] = "</div>";
ubbTags[72] = "";
htmlTags[72] = "<u>";
ubbTags[73] = "
";
htmlTags[73] = "</u>";

}

//替换字符串中的某个 UBB 代码变成相对应 HTML 代码.
private String replace(String source, String ubbTag, String htmlTag) {
/*
* 为了避免 UBB 标签中的 [ 符号被 String.replaceAll() 当成是正则表达式的一部分.
* 所以需要为其加上 \\ 符号让其转义成真正意义上的 [ 符号.
*/
StringBuffer strTemp = new StringBuffer();

for (int i = 0; i < ubbTag.length(); i++) {
char c = ubbTag.charAt(i);
switch (c) {
case 91: // '['
strTemp.append("\\[");
break;
case 93: // ']'
strTemp.append("\\]");
break;

default:
strTemp.append(c);
break;
}
}

//将所有 UBB 代码全替换成 HTML 代码,并返回.
source = source.replaceAll(strTemp.toString(), htmlTag);
return source;
}

//替换字符串中的所有 UBB 代码变成相对应 HTML 代码.
private String replaceNormalUBBCode(String source) {
String newResource = new String(source);

for (int i = 0; i < ubbTags.length; i++) {
//调用替换方法,把单某个 UBB 代码变成相对应的 HTML 代码.
newResource = replace(newResource, ubbTags[i], htmlTags[i]);
}

return newResource;
}

//替换字符串中的 URL 标签.
private String replaceURL(String source) {
StringBuffer newSource = new StringBuffer(source);
int urlFrist1 = source.indexOf(" int urlFrist2 = source.indexOf("" target="_blank">");
int urlLast = source.indexOf("
");
String urlName = null;
String fileName = null;

if (urlFrist1 != -1 && urlLast != -1 && urlFrist2 != -1 && urlFrist1 < urlFrist2 && urlFrist1 < urlLast) {
urlName = source.substring(urlFrist1 + 5,urlFrist2);
fileName = source.substring(urlFrist2 + 1,urlLast);

newSource.replace(urlFrist1, urlLast + 6, "<a href=\"" + urlName + "\">" + fileName + "</a>");
}

return newSource.toString();
}

//替换字符串中的 Email 标签.
private String replaceEmail(String source) {
StringBuffer newSource = new StringBuffer(source);
int urlFrist = source.indexOf(" int urlLast = source.indexOf("">");
int urlLast = source.indexOf("");
if (urlFrist != -1 && urlLast != -1 && urlFrist < urlLast) {
String mailUrl = source.substring(urlFrist + 7, urlLast);
String newMailUrl = "<a href=\"mailto:" + mailUrl + "\">" + mailUrl + "</a>";
newSource.replace(urlFrist, urlLast + 8, newMailUrl);
}
return newSource.toString();
}
}
...全文
60 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yumeiran 2008-05-12
  • 打赏
  • 举报
回复
恩???
eplang 2008-03-09
  • 打赏
  • 举报
回复


private void setSource(String source) {
this.source = source;
}

private String getResult() {
return this.source;
}

private void run() {
source = replaceNormalUBBCode(source);

while (source.indexOf("[url=") != -1 && source.indexOf("[/url]") != -1) {
source = replaceURL(source);
}

while (source.indexOf("") != -1 && source.indexOf("") != -1) {
source = replaceEmail(source);
}
}

//对外公开的方法,可以将传递过来的字符串中的 UBB 标签替换成 HTML 代码,并返回.
public String getHtmlUrl(String source) {
this.setSource(source);
this.run();

return this.getResult();
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧