/*
*author:tyfun
*datetime:2002.12.19
*package:com.westarsoft.function
*/
package com.westarsoft.function;
public class htmlfilter {
/*use:
*string escapehtmltags( string input , boolean htmlflag )
*/
public static string escapehtmltags( string input , boolean htmlflag ) {
if( input == null || input.length() == 0 || htmlflag ) {
return input;
}
stringbuffer buf = new stringbuffer(input.length()+6);
char ch = ;
for( int i=0; i<input.length(); i++ ) {
ch = input.charat(i);
if( ch == < ) {
buf.append( "<" );
}
else if( ch == > ) {
buf.append( ">" );
}
else {
buf.append( ch );
}
}
return buf.tostring();
}
/*use:
*string replace( string line, string oldstring, string newstring )
*/
public static string replace( string line, string oldstring, string newstring ) {
int i=0;
if ( ( line == null || oldstring == null ) ) {
return null;
}
if ( ( i=line.indexof( oldstring, i ) ) >= 0 ) {
int olength = oldstring.length();
int nlength = newstring.length();
stringbuffer buf = new stringbuffer();
buf.append(line.substring(0,i)).append(newstring);
i += olength;
int j = i;
while( (i=line.indexof(oldstring,i)) > 0 ) {
buf.append(line.substring(j,i)).append(newstring);
i += olength;
j = i;
}
buf.append(line.substring(j));
return buf.tostring();
}
return line;
}
/*use:
*string replaceignorecase( string line, string oldstring, string newstring )
*/
public static string replaceignorecase( string line, string oldstring, string newstring ) {
if (line == null) {
return null;
}
string lcline = line.tolowercase();
string lcoldstring = oldstring.tolowercase();
int i=0;
if ( ( i=lcline.indexof( lcoldstring, i ) ) >= 0 ) {
char [] line2 = line.tochararray();
char [] newstring2 = newstring.tochararray();
int olength = oldstring.length();
stringbuffer buf = new stringbuffer(line2.length);
buf.append(line2, 0, i).append(newstring2);
i += olength;
int j = i;
while( ( i=lcline.indexof( lcoldstring, i ) ) > 0 ) {
buf.append(line2, j, i-j).append(newstring2);
i += olength;
j = i;
}
buf.append(line2, j, line2.length – j);
return buf.tostring();
}
return line;
}
/*use:
*string convertubb( string input )
*/
public static string convertubb ( string input ) {
if( input == null || input.length() == 0 ) {
return input;
}
else {
input = replaceignorecase( input , "<" ,"<" ) ;
input = replaceignorecase( input , "<%" ,"<%" ) ;
input = replaceignorecase( input , "%>" ,"%>" ) ;
input = replaceignorecase( input , ">" ,">" ) ;
input = replaceignorecase( input , "" ,"<b>" ) ;
input = replaceignorecase( input , "" ,"</b>" ) ;
input = replaceignorecase( input , "" ,"<i>" ) ;
input = replaceignorecase( input , "" ,"</i>" ) ;
input = replaceignorecase( input , "" ,"<u>" ) ;
input = replaceignorecase( input , "" ,"</u>" ) ;
input = replaceignorecase( input , "[red]" ,"<font color=red>" ) ;
input = replaceignorecase( input , "[/red]" ,"</font>" ) ;
input = replaceignorecase( input , "
- " ,"<ul>" ) ;
input = replaceignorecase( input , "
" ,"</ul>" ) ;
input = replaceignorecase( input , "
input = replaceignorecase( input , "
" ,"</li>" ) ;
input = replaceignorecase ( input , "
" , "<blockquote><hr><b>" ) ;
input = replaceignorecase ( input , "
" , "</b><hr></blockquote>" ) ;
input = replace ( input , " " ," " ) ;
input = replace ( input , "\n" ,"<br>" ) ;
if ( input.indexof("<") >= 0 ) {
int oldpre = input.indexof( "<" , 0 ) ;
int oldend = input.indexof( ">" , oldpre ) ;
int tempint = 0 ;
stringbuffer setblank = new stringbuffer () ;
while ( oldpre >= 0 && oldend > 0 ) {
setblank = setblank.append( input.substring( tempint , oldpre) ) ;
setblank = setblank.append ( replace ( input.substring( oldpre ,oldend ) , " " , " " ) ) ;
tempint = oldend ;
oldpre = input.indexof( "<" , oldend ) ;
oldend = input.indexof( ">" , oldpre ) ;
}
setblank = setblank.append ( input.substring( tempint ) ) ;
input = setblank.tostring() ;
}
}
return input;
}
/*use:
*string converturl( string input , boolean urlflag )
*/
public static string converturl( string input , boolean urlflag ) {
if( input == null || input.length() == 0 || urlflag || input.indexof("[url") != -1 ) {
return input;
}
else {
stringbuffer buf = new stringbuffer();
int i = 0, j = 0, oldend = 0;
int len = input.length();
char cur;
while ( ( i=input.indexof( "http://", oldend) ) >= 0 ) {
j=i+7;
cur = input.charat(j);
while (j < len) {
if (cur == ) break;
if (cur == <) break;
if (cur == \n) break;
if (cur == \r && j<len-1 && input.charat(j+1) == \n) break;
j++;
if (j<len) {
cur = input.charat(j);
}
}
buf.append(input.substring(oldend,i));
buf.append("]
buf.append(input.substring(i,j));
buf.append("” target=_blank>");
buf.append(input.substring(i,j));
buf.append("");
oldend = j;
}
buf.append(input.substring(j,len));
return buf.tostring();
}
}
//
public static string convertspecialtage (
string input ,
string tagnamepre ,
string tagnameend ,
string tagcontentpre ,
string tagcontentend ,
string tagattach ) {
if( input == null || input.length() == 0 || input.indexof( tagnamepre ) < 0 ) {
return input;
}
else {
try {
stringbuffer buf = new stringbuffer();
int i = 0, j = 0, oldend = 0;
int len = input.length();
int tagprelength = tagnamepre.length() ;
int tagendlength = tagnameend.length() ;
boolean errorflag = false ;
char cur;
while ( ( i=input.indexof( tagnamepre , oldend) ) >= 0 ) {
j = i + tagprelength ;
j = input.indexof( tagnameend , j ) ;
if ( j < 0 ) {
buf.append ( input.substring( oldend , len ) ) ;
errorflag = true ;
break;
}
else {
buf.append( input.substring(oldend,i));
buf.append( tagcontentpre );
buf.append( input.substring(i + tagprelength ,j));
buf.append( tagcontentend );
buf.append( input.substring(i + tagprelength ,j)).append( tagattach ) ;
}
oldend = j + tagendlength ;
}
if ( !errorflag ) {
buf.append(input.substring(j + tagendlength,len));
}
return buf.tostring();
}
catch ( indexoutofboundsexception iobe ) {
return input;
}
catch ( exception e ) {
return input;
}
}
}
//
public static string convertspecialtage (
string input ,
string tagnamepre ,
string tagnameend ,
string tagcontentpre ,
string tagcontentend ) {
if( input == null || input.length() == 0 || input.indexof( tagnamepre ) < 0 ) {
return input;
}
else {
try {
stringbuffer buf = new stringbuffer();
int i = 0, j = 0, oldend = 0;
int len = input.length();
int tagprelength = tagnamepre.length() ;
int tagendlength = tagnameend.length() ;
boolean errorflag = false ;
char cur;
while ( ( i=input.indexof( tagnamepre , oldend) ) >= 0 ) {
j = i + tagprelength ;
j = input.indexof( tagnameend , j ) ;
if ( j < 0 ) {
buf.append ( input.substring( oldend , len ) ) ;
errorflag = true ;
break;
}
else {
buf.append( input.substring(oldend,i));
buf.append( tagcontentpre );
buf.append( input.substring(i + tagprelength ,j));
buf.append( tagcontentend );
}
oldend = j + tagendlength ;
}
if ( !errorflag ) {
buf.append( input.substring(j + tagendlength,len));
}
return buf.tostring();
}
catch ( indexoutofboundsexception iobe ) {
return input;
}
catch ( exception e ) {
return input;
}
}
}
//
public static string convertspecialtage (
string input ,
string tagnamepre ,
string tagnameend ,
string tagcontentpre ,
string tagcontentend ,
string tagattach ,
string interval ) {
if( input == null || input.length() == 0 || input.indexof( tagnamepre ) < 0 ) {
return input;
}
else {
try {
stringbuffer buf = new stringbuffer();
int i = 0, j = 0, oldend = 0;
int len = input.length();
int tagprelength = tagnamepre.length() ;
int tagendlength = tagnameend.length() ;
int intervallength = interval.length() ;
boolean errorflag = false ;
string tempstring = null ;
char cur;
while ( ( i=input.indexof( tagnamepre , oldend) ) >= 0 ) {
j = i + tagprelength ;
j = input.indexof( tagnameend , j ) ;
if ( j < 0 ) {
buf.append ( input.substring( oldend , len ) ) ;
errorflag = true ;
break;
}
else {
int intervalpostion = input.indexof( interval , i + tagprelength ) ;
if ( intervalpostion + intervallength >= j ) {
buf.append ( input.substring( oldend , len ) ) ;
errorflag = true ;
break;
}
buf.append( input.substring(oldend,i));
buf.append( tagcontentpre );
tempstring = input.substring(i + tagprelength ,intervalpostion ).trim() ;
if ( tempstring.indexof("\"") == 0 ) {
tempstring = tempstring.substring ( 1 , tempstring.indexof( "\"" , 1 ) ) ;
}
buf.append( tempstring );
buf.append( tagcontentend );
buf.append( input.substring( intervalpostion + intervallength ,j ) ).append( tagattach ) ;
}
oldend = j + tagendlength ;
}
if ( !errorflag ) {
buf.append(input.substring(j + tagendlength,len));
}
return buf.tostring();
}
catch ( indexoutofboundsexception iobe ) {
return input;
}
catch ( exception e ) {
return input;
}
}
}
/*use:
*string convertimg( string input )
*/
public static string convertimg ( string input ) {
if( input == null || input.length() == 0 ) {
return input;
}
else {
return convertspecialtage ( input , "" , "<img src=\"" , "\">" ) ;
}
}
/*use:
*string convertsupperubb( string input , int level )
*/
public static string convertsupperubb ( string input , int level ) {
if( input == null || input.length() == 0 ) {
return input;
}
else {
level++ ;
if ( level– > 0 ) input = convertspecialtage ( input , "" , "" , "<a target=\"_blank\" href=\"" , "\">" , "</a>" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[url=" , "][/url]" , "<a target=\"_blank\" href=\"" , "\">" , "</a>" , "]" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[color=" ,"][/color]" , "<font color=\"" , "\">" , "</font>" , "]" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[size=" ,"][/size]" , "<font size=\"" , "\">" , "</font>" , "]" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "" , "" , "<a href=\"mailto:" , "\">" , "</a>" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "" , "<img src=\"" , "\">" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[swf]" , "[/swf]" ,
"<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0\"><param name=movie value=\"" ,
"\"><param name=quality value=high><embed src=\"" ,
"\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash\" type=\"application/x-shockwave-flash\"></embed></object>"
) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[url2]" , "[/url2]" , "<a " + "href=\"" , "\">" , "</a>" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[url2=" , "][/url2]" , "<a " + "href=\"" , "\">" , "</a>" , "]" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[a]" ,"[/a]" , "<a name=\"" , "\"></a>" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[div=" ,"][/div]" , "<div align=\"" , "\">" , "</div>" , "]" ) ;
if ( level– > 0 ) input = convertspecialtage ( input , "[jsurl=" ,"][/jsurl]" , "<a href=\"#\" onclick=\"" ,"\">" ,"</a>" ,"]") ;
if ( level– > 0 ) input = convertspecialtage ( input , "[class=" ,"][/class]" , "<span class=\"" , "\">" , "</span>" , "]" ) ;
input = convertubb ( input ) ;
return input ;
}
}
/*use:
*string convertmail( string input )
*/
public static string convertmail ( string input ) {
if( input == null || input.length() == 0 ) {
return input;
}
else {
return convertspecialtage ( input , "" , "" , "<a href=\"mailto:" , "\">" , "</a>" ) ;
}
}
/*use:
*string convertswf( string input )
*/
public static string convertswf ( string input ) {
if( input == null || input.length() == 0 ) {
return input;
}
else {
return convertspecialtage ( input , "[swf]" , "[/swf]" ,
"<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0\"><param name=movie value=\"" ,
"\"><param name=quality value=high><embed src=\"" ,
"\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash\" type=\"application/x-shockwave-flash\"></embed></object>"
) ;
}
}
}
