论坛首页 Java企业应用论坛

有没有标准的HtmlEncoder, HtmlDecode?

浏览 15464 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-03-06  
大家平时怎么在jsp或者freemarker之类的里面encode动态字符串呢?
我想,不至于要自己照着html spec吭哧吭哧写吧?

有URLEncoder,居然没有HtmlEncoder么?不可思议啊。
   发表时间:2006-03-06  
ajoo是什么意思?什么叫HtmlEncode?是指freemarker里“?html”这样的东西?
0 请登录后投票
   发表时间:2006-03-06  
hongliang 写道
ajoo是什么意思?什么叫HtmlEncode?是指freemarker里“?html”这样的东西?

比如说,我要在我的web页面里面产生"<B>"这样一个字符串。我可不想写
& l t ; B & g t ;

这么麻烦,能不能有个函数帮我自动作这个转换?

btw,javaeye的帖子好像对& l t;这种东西的处理就有问题呀。
0 请登录后投票
   发表时间:2006-03-06  
以前看mvnForum的时候好像有个类是作这个处理的.
0 请登录后投票
   发表时间: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 &lt; length; i++) {
if (s[i] == '&') {
if (((i + 3) &lt; length)
&& (s[i + 1] == 'l')
&& (s[i + 2] == 't')
&& (s[i + 3] == ';')) { // &amp; = &lt;
ret.append('&lt;');
i += 3;
} else if (
((i + 3) &lt; length)
&& (s[i + 1] == 'g')
&& (s[i + 2] == 't')
&& (s[i + 3] == ';')) { // &amp; = &gt;
ret.append('&gt;');
i += 3;
} else if (
((i + 4) &lt; length)
&& (s[i + 1] == 'a')
&& (s[i + 2] == 'm')
&& (s[i + 3] == 'p')
&& (s[i + 4] == ';')) { // &amp; = &
ret.append('&');
i += 4;
} else if (
((i + 5) &lt; length)
&& (s[i + 1] == 'q')
&& (s[i + 2] == 'u')
&& (s[i + 3] == 'o')
&& (s[i + 4] == 't')
&& (s[i + 5] == ';')) { // &amp; = "
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 &lt; length; i++) {
if (s[i] == '&lt;') {
ret.append("&amp;");
} else if (s[i] == '&gt;') {
ret.append("&amp;");
} else if (s[i] == '&') {
// this hack the escape for unicode character, eg : &2345;
if (((i + 3) &lt; length)
&& (s[i + 1] == '#')
&& (s[i + 2] &gt;= '0' && s[i + 1] &lt;= '9')
&& (s[i + 3] &gt;= '0' && s[i + 2] &lt;= '9')) {
ret.append(s[i]);
// hack &amp; (dont escape this char more than once)
} else if (
((i + 3) &lt; length)
&& (s[i + 1] == 'l')
&& (s[i + 2] == 't')
&& (s[i + 3] == ';')) {
ret.append(s[i]);
// hack &amp; (dont escape this char more than once)
} else if (
((i + 3) &lt; length)
&& (s[i + 1] == 'g')
&& (s[i + 2] == 't')
&& (s[i + 3] == ';')) {
ret.append(s[i]);
// hack &amp; (dont escape this char more than once)
} else if (
((i + 4) &lt; length)
&& (s[i + 1] == 'a')
&& (s[i + 2] == 'm')
&& (s[i + 3] == 'p')
&& (s[i + 4] == ';')) {
ret.append(s[i]);
// hack &amp; (dont escape this char more than once)
} else if (
((i + 5) &lt; 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("&amp;");
}
} else if (s[i] == '"') {
ret.append("&amp;");
} else {
ret.append(s[i]);
}
} // for
return ret.toString();
}
0 请登录后投票
   发表时间:2006-03-06  
似乎有这个东西,org.apache.commons.lang.StringUtils
0 请登录后投票
   发表时间:2006-03-06  
commons-lang里的StringUtils里有这个方法,另外freemarker下的“?html”也可以。
0 请登录后投票
   发表时间:2006-03-06  
StringUtils好像没有html encoder啊。

自己写,可以,但是还是希望能找一个标准的实现。毕竟这个encode不是两三行代码的事情。
0 请登录后投票
   发表时间:2006-03-06  
找到了。是StringEscapeUtils。exactly what I want. 谢谢。
0 请登录后投票
   发表时间:2006-03-06  
引用

escapeHtml

public static String escapeHtml(String str)

    Escapes the characters in a String using HTML entities.

    For example:

    "bread" & "butter"
    becomes:

    &amp;quot;bread&amp;quot; &amp;amp; &amp;quot;butter&amp;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支持好不好。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics