插入数据库的文章的处理!

/**
* @(#) CharacterUtil.java
* CopyLeft www.dameng.cn
*/
package com.dm.bugtrack.util;

import java.io.*;

/**
* CharacterUtil is a tool handling different characters related.
* @author quickpoint
* @version 1.0 07/05/2005
*/
public class CharacterUtil {

/**
* Convert string without escaping html.
* @param str string to be converted
* @return string being converted from input string
*/
public static String convertWithoutEscapeHtml(String str) {
return convert( str, false);
}

/**
* Convert string with escaping html.
* @param str string to be converted
* @return string being converted from input string
*/
public static String convertWithEscapeHtml(String str) {
return convert( str, true );
}

/**
* Convert string
* @param str string to be converted
* @param escape true if escape html, false if not
* @return string beging converted from input string
*/
public static String convert( String str, boolean escape ) {
if ( null == str ) {
return "";
}
try {
byte tempByte[] = str.trim().getBytes("ISO-8859-1");
String tempStr = new String( tempByte);
if( escape ) {
return escapeHtml( tempStr, true );
} else {
return tempStr;
}
} catch ( UnsupportedEncodingException ex ) {
return "";
}
}

/**
* Escape sub part of html
* @param str string to be converted
* @return string being converted from input string
*/
private static String escapeSubHtml( String str ) {
return escapeHtml( str, false);
}

/**
* Convert string with just escaping a sub ste of html.
* @param str string to be converted
* @return string being conveted from input string
*/
public static String convertWithEscapeSubHtml( String str ) {
if (null == str) {
return "";
}
try {
byte tempByte[] = str.trim().getBytes("ISO-8859-1");
String tempStr = new String(tempByte);
return escapeSubHtml(tempStr);
} catch(UnsupportedEncodingException ex) {
return "";
}
}




/**
* DeEscape html character from str
* Note, here we make use of DFA:
*
* => (0) & -> (1) a -> (2) c -> (3) u ->(4) t ->(5) e ->(6) ; ->(7) (final)
* l -> (8) t -> (9) ; ->(10) (final)
* g -> (11) t -> (12) ; -> (13) (final)
* n -> (14) b -> (15) s -> (16) p -> (17) ; -> 18(final)
* < -> (19) b -> (20) r -> (21) > -> (22)(final)
*
* @return str after being deescaped html characters
*/
public static String deEscapeHtml( String str ) {
if( null == str || str.length() == 0 ) {
return str;
}

StringBuffer buf = new StringBuffer();
int i = 0;
int state = 0;
while( i < str.length()) {
char ch = str.charAt(i);
switch( state ) {
case 0:
if( ch == '&') {
state = 1;
} else if ( ch == '<') {
state = 19;
} else {
buf.append(ch);
}
break;
case 1:
if( ch == 'a') {
state = 2;
} else if( ch == 'l') {
state = 8;
} else if( ch == 'g') {
state = 11;
} else if( ch == 'n') {
state = 14;
} else {
buf.append("&");
state = 0;
continue;
}
break;
case 2:
if( ch == 'c') {
state = 3;
} else {
buf.append("&a");
state = 0;
continue;
}
break;
case 3:
if( ch == 'u' ) {
state = 4;
} else {
buf.append("&ac");
state = 0;
continue;
}
break;
case 4:
if( ch == 't') {
state = 5;
} else {
buf.append("&acu");
state = 0;
continue;
}
break;
case 5:
if( ch == 'e') {
state = 6;
} else {
buf.append("&acut");
state = 0;
continue;
}
break;
case 6:
if( ch == ';') {
state = 7;
} else {
buf.append("&acute");
state = 0;
continue;
}
break;
case 7:
buf.append("/'");
state = 0;
continue;

case 8:
if( ch == 't') {
state = 9;
} else {
buf.append("&l");
state = 0;
continue;
}
break;
case 9:
if( ch == ';') {
state = 10;
} else {
buf.append("&lt");
state = 0;
continue;
}
break;
case 10:
buf.append("<");
state = 0;
continue;

case 11:
if( ch == 't') {
state = 12;
} else {
buf.append("&g");
state = 0;
continue;
}
break;
case 12:
if( ch == ';') {
state = 13;
} else {
buf.append("&gt");
state = 0;
continue;
}
break;
case 13:
buf.append(">");
state = 0;
continue;
case 14:
if( ch == 'b') {
state = 15;
} else {
buf.append("&n");
state = 0;
continue;
}
break;
case 15:
if( ch == 's') {
state = 16;
} else {
buf.append("&nb");
state = 0;
continue;
}
break;
case 16:
if( ch == 'p') {
state = 17;
} else {
buf.append("&nbs");
state = 0;
continue;
}
break;
case 17:
if( ch == ';') {
state = 18;
} else {
buf.append("&nbsp");
state = 0;
continue;
}
break;
case 18:
buf.append(" ");
state = 0;
continue;
case 19:
if( ch == 'b') {
state = 20;
} else {
buf.append("<");
state = 0;
continue;
}
break;
case 20:
if( ch == 'r') {
state = 21;
} else {
buf.append("<b");
state = 0;
continue;
}
break;
case 21:
if( ch == '>') {
state = 22;
} else {
buf.append("<br");
state = 0;
continue;
}
break;
case 22:
buf.append("/n");
state = 0;
continue;
default :
state = 0;
break;
}

i++;
}

return buf.toString();
}


/**
* Escape html character from input str.
* @param str string to be converted
* @param wholeEscape true if just escape whole of html, false if escape a part.
* @return string beging escaped html characters from input string
*/
public static String escapeHtml(String str, boolean wholeEscape) {
if (null == str || str.length() == 0) {
return str;
}

StringBuffer buf = new StringBuffer();
char ch;
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if( wholeEscape ) {
if (ch == '<') {
buf.append("&lt;");
}
else if (ch == '>') {
buf.append("&gt;");
}
else if (ch == '/n') {
buf.append("<br>");
}
else if( ch == '/'') {
buf.append("&acute;");
} else if( ch == ' ') {
buf.append("&nbsp;");
}
else {
buf.append(ch);
}
} else {
if (ch == '/n') {
buf.append("<br>");
}
else if( ch == '/'') {
buf.append("&acute;");
} else {
buf.append(ch);
}
}
}
return buf.toString();
}

// main, just for test
public static void main( String[] args ) {
String pageInputStr = "test<br> 'html' <br> is ok?";
String toPutIntoDbStr = convertWithEscapeHtml( pageInputStr );
String restoreToPageStr = deEscapeHtml( toPutIntoDbStr );
System.out.println( pageInputStr );
System.out.println( toPutIntoDbStr);
System.out.println( restoreToPageStr);

String pageInputWithURLStr = "<a href=/'http://www.dameng.cn/'></a>";
String toPutIntoDbWithURLStr = convertWithEscapeSubHtml( pageInputWithURLStr);
String restoreToPageWithURLStr = deEscapeHtml( toPutIntoDbWithURLStr);
System.out.println( pageInputWithURLStr);
System.out.println( toPutIntoDbWithURLStr);
System.out.println( restoreToPageWithURLStr);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值