给大家一个操作String的javabean,哈哈~~爽呆辣!
发表于:2007-07-04来源:作者:点击数:
标签:
/* *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( St
/*
*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 , "
" ,"<li>" ) ;
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>"
) ;
}
}
}
原文转自:http://www.ltesting.net