对字符串进行验证之前先进行规范化

对字符串进行验证之前先进行规范化

原文来自:http://www.javaarch.net/jiagoushi/1068.htm


应用系统中经常对字符串会进行各种规则的验证,不过由于字符串信息在java6中是基于unicode的4.0版本的,而java7则是unicode的6.0.0版本。


unicode的规范化格式有几种,每种的处理方式有些不一样。


NFC
Unicode 规范化格式 C。如果未指定 normalization-type,那么会执行 Unicode 规范化。
NFD
Unicode 规范化格式 D。
NFKC
Unicode 规范化格式 KC。
NFKD
Unicode 规范化格式 KD。

如果我们对输入字符串先进行验证,再规范化,Normalizer.normalize将unicode的文本转成等价的规范化格式内容,下面这个用Pattern.compile("[<>]")验证不通过,


[java]  view plain  copy
  1. // String s may be user controllable  
  2.     // \uFE64 is normalized to < and \uFE65 is normalized to > using NFKC  
  3.     String s = "\uFE64" + "script" + "\uFE65";  
  4.     // Validate  
  5.     Pattern pattern = Pattern.compile("[<>]"); // Check for angle brackets  
  6.     Matcher matcher = pattern.matcher(s);  
  7.     if (matcher.find()) {    
  8.       // Found black listed tag  
  9.       throw new IllegalStateException();  
  10.     } else {  
  11.       // . . .  
  12.     }  
  13.     // Normalize  
  14.     s = Normalizer.normalize(s, Form.NFKC);  



如果对输入字符串先进行规范化在进行验证,使用Pattern.compile("[<>]")验证就能正确判断出来,抛出IllegalStateException异常,正确过滤有问题的输入文本,

[java]  view plain  copy
  1. String s = "\uFE64" + "script" + "\uFE65";  
  2. // Normalize  
  3. s = Normalizer.normalize(s, Form.NFKC);  
  4. // Validate  
  5. Pattern pattern = Pattern.compile("[<>]");  
  6. Matcher matcher = pattern.matcher(s);  
  7. if (matcher.find()) {  
  8.   // Found black listed tag  
  9.   throw new IllegalStateException();  
  10. else {  
  11.   // . . .  
  12. }  



java中的Normalizer类


[java]  view plain  copy
  1. public final class Normalizer {  
  2.   
  3.   
  4.    private Normalizer() {};  
  5.   
  6.   
  7.     /** 
  8.      * This enum provides constants of the four Unicode normalization forms 
  9.      * that are described in 
  10.      * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html"> 
  11.      * Unicode Standard Annex #15 — Unicode Normalization Forms</a> 
  12.      * and two methods to access them. 
  13.      * 
  14.      * @since 1.6 
  15.      */  
  16.     public static enum Form {  
  17.   
  18.   
  19.         /** 
  20.          * Canonical decomposition. 
  21.          */  
  22.         NFD,  
  23.   
  24.   
  25.         /** 
  26.          * Canonical decomposition, followed by canonical composition. 
  27.          */  
  28.         NFC,  
  29.   
  30.   
  31.         /** 
  32.          * Compatibility decomposition. 
  33.          */  
  34.         NFKD,  
  35.   
  36.   
  37.         /** 
  38.          * Compatibility decomposition, followed by canonical composition. 
  39.          */  
  40.         NFKC  
  41.     }  
  42.   
  43.   
  44.     /** 
  45.      * Normalize a sequence of char values. 
  46.      * The sequence will be normalized according to the specified normalization 
  47.      * from. 
  48.      * @param src        The sequence of char values to normalize. 
  49.      * @param form       The normalization form; one of 
  50.      *                   {@link java.text.Normalizer.Form#NFC}, 
  51.      *                   {@link java.text.Normalizer.Form#NFD}, 
  52.      *                   {@link java.text.Normalizer.Form#NFKC}, 
  53.      *                   {@link java.text.Normalizer.Form#NFKD} 
  54.      * @return The normalized String 
  55.      * @throws NullPointerException If <code>src</code> or <code>form</code> 
  56.      * is null. 
  57.      */  
  58.     public static String normalize(CharSequence src, Form form) {  
  59.         return NormalizerBase.normalize(src.toString(), form);  
  60.     }  
  61.   
  62.   
  63.     /** 
  64.      * Determines if the given sequence of char values is normalized. 
  65.      * @param src        The sequence of char values to be checked. 
  66.      * @param form       The normalization form; one of 
  67.      *                   {@link java.text.Normalizer.Form#NFC}, 
  68.      *                   {@link java.text.Normalizer.Form#NFD}, 
  69.      *                   {@link java.text.Normalizer.Form#NFKC}, 
  70.      *                   {@link java.text.Normalizer.Form#NFKD} 
  71.      * @return true if the sequence of char values is normalized; 
  72.      * false otherwise. 
  73.      * @throws NullPointerException If <code>src</code> or <code>form</code> 
  74.      * is null. 
  75.      */  
  76.     public static boolean isNormalized(CharSequence src, Form form) {  
  77.         return NormalizerBase.isNormalized(src.toString(), form);  
  78.     }  
  79. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值