分享

HTMLEscape JavaScriptEscape

 燮羽 2010-11-27
ava代码 
  1. /** 
  2.      * Returns a string that is equivalent to the input string, but with 
  3.      * special characters converted to HTML escape sequences. 
  4.      * 
  5.      * @param input the string to escape (<code>null</code> not permitted). 
  6.      * 
  7.      * @return A string with characters escaped. 
  8.      * 
  9.      * @since 1.0.9 
  10.      */  
  11.     public static String htmlEscape(String input) {  
  12.         if (input == null) {  
  13.             throw new IllegalArgumentException("Null 'input' argument.");  
  14.         }  
  15.         StringBuffer result = new StringBuffer();  
  16.         int length = input.length();  
  17.         for (int i = 0; i < length; i++) {  
  18.             char c = input.charAt(i);  
  19.             if (c == '&') {  
  20.                 result.append("&");  
  21.             }  
  22.             else if (c == '\"') {  
  23.                 result.append(""");  
  24.             }  
  25.             else if (c == '<') {  
  26.                 result.append("<");  
  27.             }  
  28.             else if (c == '>') {  
  29.                 result.append(">");  
  30.             }  
  31.             else if (c == '\'') {  
  32.                 result.append("'");  
  33.             }  
  34.             else if (c == '\\') {  
  35.                 result.append("\");  
  36.             }  
  37.             else {  
  38.                 result.append(c);  
  39.             }  
  40.         }  
  41.         return result.toString();  
  42.     }  
  43.   
  44.     /** 
  45.      * Returns a string that is equivalent to the input string, but with 
  46.      * special characters converted to JavaScript escape sequences. 
  47.      * 
  48.      * @param input the string to escape (<code>null</code> not permitted). 
  49.      * 
  50.      * @return A string with characters escaped. 
  51.      * 
  52.      * @since 1.0.13 
  53.      */  
  54.     public static String javascriptEscape(String input) {  
  55.         if (input == null) {  
  56.             throw new IllegalArgumentException("Null 'input' argument.");  
  57.         }  
  58.         StringBuffer result = new StringBuffer();  
  59.         int length = input.length();  
  60.         for (int i = 0; i < length; i++) {  
  61.             char c = input.charAt(i);  
  62.             if (c == '\"') {  
  63.                 result.append("\\\"");  
  64.             }  
  65.             else if (c == '\'') {  
  66.                 result.append("\\'");  
  67.             }  
  68.             else if (c == '\\') {  
  69.                 result.append("\\\\");  
  70.             }  
  71.             else {  
  72.                 result.append(c);  
  73.             }  
  74.         }  
  75.         return result.toString();  
  76.     }  
  77. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多