com.cybermkd.validation.ChineseValidator Maven / Gradle / Ivy
package com.cybermkd.validation;
import com.cybermkd.constraints.Chinese;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* 创建人:T-baby
* 创建日期: 16/8/3
* 文件描述:
*/
public class ChineseValidator implements ConstraintValidator {
private boolean value = false;
@Override
public void initialize(Chinese chinese) {
value = chinese.value();
}
@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
if (value) {
return isChinese(s);
} else {
return !isChinese(s);
}
}
// 根据Unicode编码判断中文汉字和符号
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
}
return false;
}
// 判断中文汉字和符号
public static boolean isChinese(String strName) {
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
return true;
}
}
return false;
}
}