Chinaunix首页 | 论坛 | 博客
  • 博客访问: 64261
  • 博文数量: 12
  • 博客积分: 2070
  • 博客等级: 大尉
  • 技术积分: 157
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-31 11:07
文章分类
文章存档

2011年(1)

2009年(4)

2008年(7)

分类: Java

2009-02-26 13:26:29

以前用java做WEB开发编写的转换代码,希望对大家有所帮助。

主要包括如下函数:

1. escapeJSValue: 转换为可以对Javascript变量赋值的文本
2. escapeFormValue:转换成可以对表单元素的value赋值的字符串
3. escapeHtmlValue:转换成HTML中显示的文本
4. escapeTextAreaValue:转换成textarea中的文本

    //Javascript字符串转换,转换成Javascript中的字符串,用于对JS变量赋值
    public static String escapeJSValue(String src)
    {
        if (src == null)
        {
            return null;
        }

        String szResult;

        szResult = src;

        szResult = szResult.replaceAll("\\\\", "\\\\\\\\");
        szResult = szResult.replaceAll("\\r", "\\\\r");
        szResult = szResult.replaceAll("\\n", "\\\\n");
        szResult = szResult.replaceAll("'", "\\\\'");
        szResult = szResult.replaceAll("<", "\\\\x3C");
        szResult = szResult.replaceAll("\"", "\\\\\"");

        return szResult;
    }

    //转换成HTML中可以对value赋值的字符串,用于对form中的元素赋值
    public static String escapeFormValue(String src)
    {
        if (src == null)
        {
            return null;
        }

        int len = src.length();
        StringBuffer sbResult = new StringBuffer(len);
        char ch;

        try
        {
            for (int i=0; i            {
                ch = src.charAt(i);
                switch(ch)
                {
                    case '\"':
                        sbResult.append(""");
                        break;
                    case '&':
                        sbResult.append("&");
                        break;
                    case '>':
                        sbResult.append(">");
                        break;
                    case '<':
                        sbResult.append("<");
                        break;
                    case '\n':
                        sbResult.append(" ");
                        break;
                    case '\r':
                        sbResult.append(" ");
                        break;
                    default:
                        sbResult.append(ch);
                    break;
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return sbResult.toString();
    }

   //转换成HTML中显示的文字
    public static String escapeHtmlValue(String src)
    {
        if (src == null)
        {
            return null;
        }

        int len = src.length();
        StringBuffer sbResult = new StringBuffer(len);
        char ch;
        boolean bPreviousIsSpace = false;

        try
        {
            for (int i=0; i            {
                ch = src.charAt(i);
                switch(ch)
                {
                    case '&':
                        sbResult.append("&");
                        bPreviousIsSpace = false;
                        break;
                    case '>':
                        sbResult.append(">");
                        bPreviousIsSpace = false;
                        break;
                    case '<':
                        sbResult.append("<");
                        bPreviousIsSpace = false;
                        break;
                    case ' ':
                        if (!bPreviousIsSpace)
                        {
                            sbResult.append(ch);
                            bPreviousIsSpace = true;
                        }
                        else
                        {
                            sbResult.append(" ");
                        }
                        break;
                    default:
                        sbResult.append(ch);
                    bPreviousIsSpace = false;
                    break;
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return sbResult.toString();
    }

    public static String escapeHtmlValue2(String src)
    {
        if (src == null)
        {
            return null;
        }

        String szResult = escapeHtmlValue(src);
        szResult = szResult.replaceAll("\\r\\n", "
");
        szResult = szResult.replaceAll("\\n", "
");
        return szResult.replaceAll("\\t", "    ");
    }

    //转换成Textarea中的文字
    public static String escapeTextAreaValue(String src)
    {
        if (src == null)
        {
            return null;
        }

        int len = src.length();
        StringBuffer sbResult = new StringBuffer(len);
        char ch;

        try
        {
        for (int i=0; i        {
            ch = src.charAt(i);
            switch(ch)
            {
                case '&':
                    sbResult.append("&");
                    break;
                case '>':
                    sbResult.append(">");
                    break;
                case '<':
                    sbResult.append("<");
                    break;
                default:
                    sbResult.append(ch);
                    break;
            }
        }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return sbResult.toString();
    }

 

阅读(1293) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册