最近做论坛碰到了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, "´","'");
fString = repstr(fString, ""","\"");
fString = repstr(fString, "<","<");
fString = repstr(fString, ">",">");
fString = repstr(fString, " "," ");
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, "[url]", "[/url]", "<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;
}
}
本文地址:http://com.8s8s.com/it/it9858.htm