繁体中文
设为首页
加入收藏
当前位置:JSP技术首页 >> 资料/其它 >> 给大家一个操作String的javabean,哈哈~~爽呆辣!

给大家一个操作String的javabean,哈哈~~爽呆辣!

2004-12-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:/* *Author:tyfun *DateTime:2002.12.19 *Package:com.westarsoft.function */ package com.westarsoft.function; public class HtmlFilter { /*USE: *String escapeHTMLTags( String input , boolean htmlFlag ...

/*

*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

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 , "" ,"" ) ;

input = replaceIgnoreCase( input , "" ,"" ) ;

input = replaceIgnoreCase( input , "" ,"" ) ;

input = replaceIgnoreCase( input , "" ,"" ) ;

input = replaceIgnoreCase( input , "" ,"" ) ;

input = replaceIgnoreCase( input , "" ,"" ) ;

input = replaceIgnoreCase( input , "[red]" ,"" ) ;

input = replaceIgnoreCase( input , "[/red]" ,"" ) ;

input = replaceIgnoreCase( input , "" ,"

    " ) ;

    input = replaceIgnoreCase( input , "" ,"

" ) ;

input = replaceIgnoreCase( input , "" ,"

  • " ) ;

    input = replaceIgnoreCase( input , "" ,"

  • " ) ;

    input = replaceIgnoreCase ( input , "" , "


    " ) ;

    input = replaceIgnoreCase ( input , "" , "


    " ) ;

    input = replace ( input , " " ," " ) ;

    input = replace ( input , "\n" ,"
    " ) ;

    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

    j++;

    if (j

    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 , "" , "" ) ;

    }

    }

    /*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 , "" , "" , "" , "" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[url=" , "][/url]" , "" , "" , "]" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[color=" ,"][/color]" , "" , "" , "]" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[size=" ,"][/size]" , "" , "" , "]" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "" , "" , "" , "" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "" , "" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[swf]" , "[/swf]" ,

    "

    "\">

    "\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\">

    "

    ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[url2]" , "[/url2]" , "" , "" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[url2=" , "][/url2]" , "" , "" , "]" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[a]" ,"[/a]" , "" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[div=" ,"][/div]" , "

    " , "
    " , "]" ) ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[jsurl=" ,"][/jsurl]" , "" ,"" ,"]") ;

    if ( level-- > 0 ) input = convertSpecialTage ( input , "[class=" ,"][/class]" , "" , "" , "]" ) ;

    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 , "" , "" , "" , "" ) ;

    }

    }

    /*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]" ,

    "

    "\">

    "\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\">

    "

    ) ;

    }

    }

    }

    责任编辑:admin
    相关文章