SourceURL:about:blank
/**
* Created by Administrator on 2016/9/22.
*/
public class HtmlEncode {
public String HtmlEncode(String str) {
int length = str.length();
int newLength = length;
boolean someCharacterEscaped = false;
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
int cint = 0xffff & c;
if (cint < 32)
switch (c) {
case 11:
default:
newLength--;
someCharacterEscaped = true;
break; case '\t':
case '\n':
case '\f':
case '\r':
break;
}
else
switch (c) {
case '"':
newLength += 5;
someCharacterEscaped = true;
break; case '&':
case '\'':
newLength += 4;
someCharacterEscaped = true;
break; case '<':
case '>':
newLength += 3;
someCharacterEscaped = true;
break;
}
}
if (!someCharacterEscaped)
return str; StringBuffer sb = new StringBuffer(newLength);
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
int cint = 0xffff & c;
if (cint < 32)
switch (c) {
case '\t':
case '\n':
case '\f':
case '\r':
sb.append(c);
break;
}
else
switch (c) {
case '"':
sb.append(""");
break; case '\'':
sb.append("'");
break; case '&':
sb.append("&");
break; case '<':
sb.append("<");
break; case '>':
sb.append(">");
break; default:
sb.append(c);
break;
}
}
return sb.toString();
}
}