分享
 
 
 

JAVA写的简单UBB类

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

最近做论坛碰到了UBB的问题,这个很大众化的东西,怎么没有JAVA写的解释器呢,实在是该写一个。下面的这个不全,但是可以应付一下。

import java.util.*;

public class HTMLString{

public HTMLString(){}

public static String htmlEntities(String str){

return UBB(str);

}

public static boolean hasSpecialCharacter(String str){

/*用于合法性检查

if(str.indexOf("\")!=-1){

return false;

}

if(str.indexOf("'")!=-1){

return false;

}

if(str.indexOf('"')!=-1){

return false;

}

if(str.indexOf("<")!=-1){

return false;

}

if(str.indexOf(">")!=-1){

return false;

}

*/

return true;

}

public static String UBB(String fString){

return (ChkString(FormatStr(fString)));

}

//屏蔽HTML代码,并处理回车和换行

public static String FormatStr(String fString){

/*

fString = repstr(fString, "‘","'");

fString = repstr(fString, "\"","“");

fString = repstr(fString, "<","<");

fString = repstr(fString, "<","<");

fString = repstr(fString, ">",">");

fString = repstr(fString, " ","\r");

*/

fString = repstr(fString, "&acute;","'");

fString = repstr(fString, "&quot;","\"");

fString = repstr(fString, "&lt;","<");

fString = repstr(fString, "&gt;",">");

fString = repstr(fString, "&nbsp;"," ");

fString = repstr(fString, " ","\r");

return (fString);

}

//把UBB代码转换成HTML代码以便正常显示

public static String ChkString(String fString){

fString = fString.trim();

if (fString.equals(""))

fString = " ";

else{

fString = doCode(fString, "<B>", "</B>","<b>","</b>");

fString = doCode(fString, "<u>", "</u>", "<u>", "</u>");

fString = doCode(fString, "<I>", "</I>", "<i>", "</i>");

fString = doCode(fString, "[red]", "[/red]", "<font color=red>", "</font id=red>");

fString = doCode(fString, "[green]", "[/green]", "<font color=green>", "</font id=green>");

fString = doCode(fString, "[blue]", "[/blue]", "<font color=blue>", "</font id=blue>");

fString = doCode(fString, "[white]", "[/white]", "<font color=white>", "</font id=white>");

fString = doCode(fString, "[purple]", "[/purple]", "<font color=purple>", "</font id=purple>");

fString = doCode(fString, "[yellow]", "[/yellow]", "<font color=yellow>", "</font id=yellow>");

fString = doCode(fString, "[violet]", "[/violet]", "<font color=violet>", "</font id=violet>");

fString = doCode(fString, "[brown]", "[/brown]", "<font color=brown>", "</font id=brown>");

fString = doCode(fString, "[black]", "[/black]", "<font color=black>", "</font id=black>");

fString = doCode(fString, "[pink]", "[/pink]", "<font color=pink>", "</font id=pink>");

fString = doCode(fString, "[orange]", "[/orange]", "<font color=orange>", "</font id=orange>");

fString = doCode(fString, "[gold]", "[/gold]", "<font color=gold>", "</font id=gold>");

fString = doCode(fString, "<h1>", "</h1>", "<h1>", "</h1>");

fString = doCode(fString, "<h2>", "</h2>", "<h2>", "</h2>");

fString = doCode(fString, "<h3>", "</h3>", "<h3>", "</h3>");

fString = doCode(fString, "<h4>", "</h4>", "<h4>", "</h4>");

fString = doCode(fString, "<h5>", "</h5>", "<h5>", "</h5>");

fString = doCode(fString, "<h6>", "</h6>", "<h6>", "</h6>");

fString = doCode(fString, "[size=1]", "[/size=1]", "<font size=1>", "</font id=size1>");

fString = doCode(fString, "[size=2]", "[/size=2]", "<font size=2>", "</font id=size2>");

fString = doCode(fString, "[size=3]", "[/size=3]", "<font size=3>", "</font id=size3>");

fString = doCode(fString, "[size=4]", "[/size=4]", "<font size=4>", "</font id=size4>");

fString = doCode(fString, "[size=5]", "[/size=5]", "<font size=5>", "</font id=size5>");

fString = doCode(fString, "[size=6]", "[/size=6]", "<font size=6>", "</font id=size6>");

fString = doCode(fString, "[left]", "[/left]", "<div align=left>", "</div id=left>");

fString = doCode(fString, "<center>", "</center>", "<center>", "</center>");

fString = doCode(fString, "<div align=right>", "</div>", "<div align=right>", "</div id=right>");

fString = doCode(fString, "<pre class=CodeSamp>", "</pre>", "<pre id=code><font face=courier size=3 id=code>", "</font id=code></pre id=code>");

fString = doCode(fString, "", "", "<a>", "</a>");

fString = addlink(fString);

fString = addBr(fString);

}

return (fString);

}

//方便的替换函数

public static String doCode(String fString,String fOTag, String fCTag, String fROTag, String fRCTag){

fString = repstr(fString, fROTag, fOTag);

fString = repstr(fString, fRCTag, fCTag);

return (fString);

}

//处理链接

public static String addlink(String str){

String hrefTagPre = "<a>";

String hrefTagLast = "</a>";

String hrefTxt = "";

String strReturn = "";

int iSearchedFrom = 0 ;

int first = 0;

int last = 0;

while (( iSearchedFrom=str.indexOf( hrefTagPre , iSearchedFrom) ) != -1 ){

first = iSearchedFrom + 3;

if (( iSearchedFrom=str.indexOf( hrefTagLast , iSearchedFrom) ) != -1 ) {

last = iSearchedFrom;

iSearchedFrom += 4;

strReturn = str.substring( first, last);

if (strReturn.indexOf( "<a>", 0) != -1){

strReturn = repstr ( strReturn , "", "<a>");

}

hrefTxt = "<a href =" +strReturn + " target=\"_blank\">" + strReturn + "</a>";

strReturn = str.substring( first - 3 , iSearchedFrom);

str = repstr(str, hrefTxt ,strReturn );

iSearchedFrom =0;

}else

break;

}

return (str);

}

public static String fd(String str){

String fd=repstr(str,"''","'");

return fd;

}

public static String repstr(String str, String strDen, String strSrc){

int iLen = str.length();

int iDenLen = strDen.length();

int iSrcLen = strSrc.length();

String strReturn = "";

int iSearchedFrom = 0 ;

int iPrevSearchedFrom = 0;

while ( (iSearchedFrom = str.indexOf(strSrc , iSearchedFrom)) != -1 ){

strReturn += str.substring(iPrevSearchedFrom, iSearchedFrom);

strReturn += strDen;

iSearchedFrom += iSrcLen;

iPrevSearchedFrom = iSearchedFrom;

}

if ( iPrevSearchedFrom !=0 )

strReturn += str.substring( str.lastIndexOf(strSrc) + iSrcLen , str.length());

else

strReturn = str;

return(strReturn);

}

public static String RemoveComment(String Content) {

String makeContent=new String();

StringTokenizer strToken=new StringTokenizer(Content,"\n");

String tempToken=null;

while(strToken.hasMoreTokens()) {

tempToken=strToken.nextToken();

if(tempToken.indexOf(":")!=0)

makeContent=makeContent+tempToken+"\n";

}

return makeContent;

}

public static String addBr(String Content) {

String makeContent=new String();

StringTokenizer strToken=new StringTokenizer(Content,"\n");

while(strToken.hasMoreTokens()){

String sNext = strToken.nextToken();

makeContent=makeContent + sNext.substring(0,sNext.length()-1) + "<br>";

}

return makeContent;

}

public static String delBr(String Content) {

String makeContent=new String();

StringTokenizer strToken=new StringTokenizer(Content,"<br>");

while(strToken.hasMoreTokens()) {

String sNext = strToken.nextToken();

makeContent=makeContent + sNext.substring(0,sNext.length()-4) + "\n";

}

return makeContent;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有