精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2006-03-06
我想,不至于要自己照着html spec吭哧吭哧写吧? 有URLEncoder,居然没有HtmlEncoder么?不可思议啊。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2006-03-06
ajoo是什么意思?什么叫HtmlEncode?是指freemarker里“?html”这样的东西?
|
|
返回顶楼 | |
发表时间:2006-03-06
hongliang 写道 ajoo是什么意思?什么叫HtmlEncode?是指freemarker里“?html”这样的东西?
比如说,我要在我的web页面里面产生"<B>"这样一个字符串。我可不想写 & l t ; B & g t ; 这么麻烦,能不能有个函数帮我自动作这个转换? btw,javaeye的帖子好像对& l t;这种东西的处理就有问题呀。 |
|
返回顶楼 | |
发表时间:2006-03-06
以前看mvnForum的时候好像有个类是作这个处理的.
|
|
返回顶楼 | |
发表时间:2006-03-06
大概是这样的
public static String enableTag(String input) throws IOException { if (input == null) { return null; } char[] s = input.toCharArray(); int length = s.length; StringBuffer ret = new StringBuffer(length); for (int i = 0; i < length; i++) { if (s[i] == '&') { if (((i + 3) < length) && (s[i + 1] == 'l') && (s[i + 2] == 't') && (s[i + 3] == ';')) { // & = < ret.append('<'); i += 3; } else if ( ((i + 3) < length) && (s[i + 1] == 'g') && (s[i + 2] == 't') && (s[i + 3] == ';')) { // & = > ret.append('>'); i += 3; } else if ( ((i + 4) < length) && (s[i + 1] == 'a') && (s[i + 2] == 'm') && (s[i + 3] == 'p') && (s[i + 4] == ';')) { // & = & ret.append('&'); i += 4; } else if ( ((i + 5) < length) && (s[i + 1] == 'q') && (s[i + 2] == 'u') && (s[i + 3] == 'o') && (s[i + 4] == 't') && (s[i + 5] == ';')) { // & = " ret.append('"'); i += 5; } else { ret.append('&'); } } else { ret.append(s[i]); } } // for return ret.toString(); } public static String disableTag(String input) throws IOException { if (input == null) { return null; } char[] s = input.toCharArray(); int length = s.length; StringBuffer ret = new StringBuffer(length + 100); // add more room to the result String for (int i = 0; i < length; i++) { if (s[i] == '<') { ret.append("&"); } else if (s[i] == '>') { ret.append("&"); } else if (s[i] == '&') { // this hack the escape for unicode character, eg : &2345; if (((i + 3) < length) && (s[i + 1] == '#') && (s[i + 2] >= '0' && s[i + 1] <= '9') && (s[i + 3] >= '0' && s[i + 2] <= '9')) { ret.append(s[i]); // hack & (dont escape this char more than once) } else if ( ((i + 3) < length) && (s[i + 1] == 'l') && (s[i + 2] == 't') && (s[i + 3] == ';')) { ret.append(s[i]); // hack & (dont escape this char more than once) } else if ( ((i + 3) < length) && (s[i + 1] == 'g') && (s[i + 2] == 't') && (s[i + 3] == ';')) { ret.append(s[i]); // hack & (dont escape this char more than once) } else if ( ((i + 4) < length) && (s[i + 1] == 'a') && (s[i + 2] == 'm') && (s[i + 3] == 'p') && (s[i + 4] == ';')) { ret.append(s[i]); // hack & (dont escape this char more than once) } else if ( ((i + 5) < length) && (s[i + 1] == 'q') && (s[i + 2] == 'u') && (s[i + 3] == 'o') && (s[i + 4] == 't') && (s[i + 5] == ';')) { ret.append(s[i]); } else { ret.append("&"); } } else if (s[i] == '"') { ret.append("&"); } else { ret.append(s[i]); } } // for return ret.toString(); } |
|
返回顶楼 | |
发表时间:2006-03-06
似乎有这个东西,org.apache.commons.lang.StringUtils
|
|
返回顶楼 | |
发表时间:2006-03-06
commons-lang里的StringUtils里有这个方法,另外freemarker下的“?html”也可以。
|
|
返回顶楼 | |
发表时间:2006-03-06
StringUtils好像没有html encoder啊。
自己写,可以,但是还是希望能找一个标准的实现。毕竟这个encode不是两三行代码的事情。 |
|
返回顶楼 | |
发表时间:2006-03-06
找到了。是StringEscapeUtils。exactly what I want. 谢谢。
|
|
返回顶楼 | |
发表时间:2006-03-06
引用 escapeHtml public static String escapeHtml(String str) Escapes the characters in a String using HTML entities. For example: "bread" & "butter" becomes: &quot;bread&quot; &amp; &quot;butter&quot;. Supports all known HTML 4.0 entities, including funky accents. Parameters: str - the String to escape, may be null Returns: a new escaped String, null if null string input See Also: unescapeHtml(String), ISO Entities, HTML 3.2 Character Entities for ISO Latin-1, HTML 4.0 Character entity references, HTML 4.01 Character References, HTML 4.01 Code positions 只支持到html4.0 .不知道对xhtml支持好不好。 |
|
返回顶楼 | |