<%@ page contentType="text/html; charset=gb2312"%> 将文本格式的文章转换为html/xml格式文本的功能封装到Javabean
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
将文本格式的文章转换为html/xml格式文本的功能封装到Javabean
     发布者: 发布时间:2006-07-26
在将textarea里面的大段文字储存到数据库,然后提取出来以显示时,格式无效,因为此时是html格式,所以需要转换。

看到JK_10000在javascript区提供了此代码,就顺路牵来。
当然,碰到我这个代码格式化狂人,是肯定要面目全非的,啊哈哈哈哈哈哈......

htmlTextEncoder方法将文本格式转换为html格式。
xmlEncoder方法将文本格式转换为xml格式。


/**
* 字符串编码器类,将字符串转换为指定格式.<br>
* <br>
* 参数字典:<br>
* src - source 来源的简写<br>
* dst - destnation 目的的简写<br>
* fnd - find 查找的简写<br>
* rep - replace 替换的简写<br>
* idx - index 索引,下标的简写<br>
* enc - encoding 编码的简写<br>
* <br>
* 例子:<br>
* <%=ArticleFormat.htmlTextEncoder(yourString)%>
*/
public class StringEncoder
{
/**
* 将字符串src中的子字符串fnd全部替换为新子字符串rep.<br>
* 功能相当于java sdk 1.4的String.replaceAll方法.<br>
* 不同之处在于查找时不是使用正则表达式而是普通字符串.
*/
public static String replaceAll(String src, String fnd, String rep) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;

int idx = dst.indexOf(fnd);

while (idx >= 0)
{
dst = dst.substring(0, idx) + rep + dst.substring(idx + fnd.length(), dst.length());
idx = dst.indexOf(fnd, idx + rep.length());
}

return dst;
}
/**
* 转换为HTML编码.<br>
*/
public static String htmlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&rt;");
dst = replaceAll(dst, "\"", "&quot;");
dst = replaceAll(dst, "''", "&#039;");

return dst;
}
/**
* 转换为HTML文字编码.<br>
*/
public static String htmlTextEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&rt;");
dst = replaceAll(dst, "\"", "&quot;");
dst = replaceAll(dst, "''", "&#039;");
dst = replaceAll(dst, " ", "&nbsp;");
dst = replaceAll(dst, "\r\n", "<br>");
dst = replaceAll(dst, "\r", "<br>");
dst = replaceAll(dst, "\n", "<br>");

return dst;
}
/**
* 转换为URL编码.<br>
*/
public static String urlEncoder(String src, String enc) throws Exception
{
return java.net.URLEncoder.encode(src, enc) ;
}
/**
* 转换为XML编码.<br>
*/
public static String xmlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "&", "&amp;");
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&gt;");
dst = replaceAll(dst, "\"", "&quot;");
dst = replaceAll(dst, "\''", "&acute;");

return dst;
}
/**
* 转换为SQL编码.<br>
*/
public static String sqlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

return replaceAll(src, "''", "''''");
}
/**
* 转换为javascript编码.<br>
*/
public static String jsEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "''", "\\''");
dst = replaceAll(dst, "\"", "\\\"");
//dst = replaceAll(dst, "\r\n", "\\\n"); // 和\n转换有冲突
dst = replaceAll(dst, "\n", "\\\n");
dst = replaceAll(dst, "\r", "\\\n");

return dst;
}
}
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
java中,this用途总结。。。
开放源码-SMTP发信客户端 for Java
java JDBC 提高程序可移植性
递归函数之JAVA演绎
我编的JAVA程序,欢迎高手提宝贵意见和建议.
通过java提供的URL类包读取网上的文件
 
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号