答案:/*
*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 , "<%" ,"<%" ) ;
&nb
上一个:SCJP考题中的陷阱
下一个:基于weblogic 的ejb 学习笔记(二)