想让JSF生成的html源代码是中文的处理方法
2006-05-09 10:49
393 查看
本文件在tomcat测试通过。
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.myfaces.renderkit.html.util;
/**
* Converts Strings so that they can be used within HTML-Code.
*/
public abstract class HTMLEncoder
{
/**
* Variant of {@link #encode} where encodeNewline is false and encodeNbsp is true.
*/
public static String encode (String string)
{
return encode(string, false, true);
}
/**
* Variant of {@link #encode} where encodeNbsp is true.
*/
public static String encode (String string, boolean encodeNewline)
{
return encode(string, encodeNewline, true);
}
/**
* Encodes the given string, so that it can be used within a html page.
* @param string the string to convert
* @param encodeNewline if true newline characters are converted to <br>'s
* @param encodeSubsequentBlanksToNbsp if true subsequent blanks are converted to 's
*/
public static String encode (String string,
boolean encodeNewline,
boolean encodeSubsequentBlanksToNbsp)
{
if (string == null)
{
return "";
}
StringBuffer sb = null; //create later on demand
String app;
char c;
for (int i = 0; i < string.length (); ++i)
{
app = null;
c = string.charAt(i);
switch (c)
{
case '"': app = """; break; //"
case '&': app = "&"; break; //&
case '<': app = "<"; break; //<
case '>': app = ">"; break; //>
case ' ':
if (encodeSubsequentBlanksToNbsp &&
(i == 0 || (i - 1 >= 0 && string.charAt(i - 1) == ' ')))
{
//Space at beginning or after another space
app = " ";
}
break;
case '/n':
if (encodeNewline)
{
app = "<br/>";
}
break;
//german umlauts
case '/u00E4' : app = "ä"; break;
case '/u00C4' : app = "Ä"; break;
case '/u00F6' : app = "ö"; break;
case '/u00D6' : app = "Ö"; break;
case '/u00FC' : app = "ü"; break;
case '/u00DC' : app = "Ü"; break;
case '/u00DF' : app = "ß"; break;
//misc
//case 0x80: app = "€"; break; sometimes euro symbol is ascii 128, should we suport it?
case '/u20AC': app = "€"; break;
case '/u00AB': app = "«"; break;
case '/u00BB': app = "»"; break;
case '/u00A0': app = " "; break;
default:
if (((int)c) >= 0x80)
{
//encode all non basic latin characters
//更改的就是这句话
//app = "" + ((int)c) + ";";
char[] charbuf = new char[1];
charbuf[0]=c;
app=new String(charbuf);
}
break;
}
if (app != null)
{
if (sb == null)
{
sb = new StringBuffer(string.substring(0, i));
}
sb.append(app);
} else {
if (sb != null)
{
sb.append(c);
}
}
}
if (sb == null)
{
return string;
}
else
{
return sb.toString();
}
}
}
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.myfaces.renderkit.html.util;
/**
* Converts Strings so that they can be used within HTML-Code.
*/
public abstract class HTMLEncoder
{
/**
* Variant of {@link #encode} where encodeNewline is false and encodeNbsp is true.
*/
public static String encode (String string)
{
return encode(string, false, true);
}
/**
* Variant of {@link #encode} where encodeNbsp is true.
*/
public static String encode (String string, boolean encodeNewline)
{
return encode(string, encodeNewline, true);
}
/**
* Encodes the given string, so that it can be used within a html page.
* @param string the string to convert
* @param encodeNewline if true newline characters are converted to <br>'s
* @param encodeSubsequentBlanksToNbsp if true subsequent blanks are converted to 's
*/
public static String encode (String string,
boolean encodeNewline,
boolean encodeSubsequentBlanksToNbsp)
{
if (string == null)
{
return "";
}
StringBuffer sb = null; //create later on demand
String app;
char c;
for (int i = 0; i < string.length (); ++i)
{
app = null;
c = string.charAt(i);
switch (c)
{
case '"': app = """; break; //"
case '&': app = "&"; break; //&
case '<': app = "<"; break; //<
case '>': app = ">"; break; //>
case ' ':
if (encodeSubsequentBlanksToNbsp &&
(i == 0 || (i - 1 >= 0 && string.charAt(i - 1) == ' ')))
{
//Space at beginning or after another space
app = " ";
}
break;
case '/n':
if (encodeNewline)
{
app = "<br/>";
}
break;
//german umlauts
case '/u00E4' : app = "ä"; break;
case '/u00C4' : app = "Ä"; break;
case '/u00F6' : app = "ö"; break;
case '/u00D6' : app = "Ö"; break;
case '/u00FC' : app = "ü"; break;
case '/u00DC' : app = "Ü"; break;
case '/u00DF' : app = "ß"; break;
//misc
//case 0x80: app = "€"; break; sometimes euro symbol is ascii 128, should we suport it?
case '/u20AC': app = "€"; break;
case '/u00AB': app = "«"; break;
case '/u00BB': app = "»"; break;
case '/u00A0': app = " "; break;
default:
if (((int)c) >= 0x80)
{
//encode all non basic latin characters
//更改的就是这句话
//app = "" + ((int)c) + ";";
char[] charbuf = new char[1];
charbuf[0]=c;
app=new String(charbuf);
}
break;
}
if (app != null)
{
if (sb == null)
{
sb = new StringBuffer(string.substring(0, i));
}
sb.append(app);
} else {
if (sb != null)
{
sb.append(c);
}
}
}
if (sb == null)
{
return string;
}
else
{
return sb.toString();
}
}
}
相关文章推荐
- html中文显示乱码的处理方法
- maven下载源代码,中文注释乱码的处理方法
- Jquery 中为后生成或插入的 Html 元素先设定响应事件处理方法
- Html字符串生成pdf的方法 支持中文及中文换行 可行demo和jar
- 动态生成html添加响应事件和css样式时处理方法
- javadoc生成方法及中文乱码处理
- itext应用HTML模版生成PDF并加水印,处理了中文问题
- 【smarty模板引擎技术】smarty模板引擎技术生成html静态网页的方法
- JSP HTML 各种 乱码 解决方法|jsp include html乱码|include 乱码|MyEclipse 中文乱码
- log4j日志文件中文乱码处理方法
- 67 插件生成html语句 操作伪标签方法
- html 图片超出父容器,文字超出边界需要自动换行处理方法
- DedeCms生成html加速的终极优化方法
- 动态ASP网站生成HTM、HTML静态网站方法
- Qt中中文处理的简单方法
- asp获取网页源代码并处理乱码的方法
- C# substring处理中文,让汉字二个字符的处理方法
- 中文与韩、日文混排出现在Gb2312编码的Aspx的处理方法
- 解析Java中文乱码的处理方法
- Yii——使用CHtml::link()等方法时,设置htmlOptions属性可快速生成js代码和ajax请求