xml校验,验证报告国际化
java schema校验xml,获取国际化的验证结果
代码如下:
public static List<String> validateXml(byte[] xsdBytes, byte[] xmlBytes, Locale locale) {
List<String> errorMsg = new ArrayList<>();
try {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
ByteArrayInputStream xsdBais = new ByteArrayInputStream(xsdBytes);
Source xsdSource = new StreamSource(xsdBais);
Schema schema = schemaFactory.newSchema(xsdSource);
// 通过Schema产生针对于此Schema的验证器,利用schenaFile进行验证
Validator validator = schema.newValidator();
if (null != locale) {
if (locale.getLanguage().equals("en")) {
validator.setProperty("http://apache.org/xml/properties/locale", new Locale(""));
} else {
validator.setProperty("http://apache.org/xml/properties/locale", locale);
}
}
validator.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
errorMsg.add(exception.getMessage());
}
@Override
public void error(SAXParseException exception) throws SAXException {
errorMsg.add(exception.getMessage());
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
errorMsg.add(exception.getMessage());
}
});
ByteArrayInputStream xmlBais = new ByteArrayInputStream(xmlBytes);
// 得到验证的数据源
Source source = new StreamSource(xmlBais);
// 开始验证,成功输出success!!!,失败输出fail
validator.validate(source);
} catch (Exception e) {
errorMsg.add(e.getMessage());
}
return errorMsg;
}
public static void main(String[] args) {
String xsdFileName = "aaaxsd.xsd";
String xmlFileName = "bbbb.xml";
String a1 = FileUtil.readString(xsdFileName, Charset.forName("utf-8"));
String a2 = FileUtil.readString(xmlFileName, Charset.forName("utf-8"));
System.out.println(validateXml(a1.getBytes(), a2.getBytes(), new Locale("en")));
}
validator.setProperty(“http://apache.org/xml/properties/locale”,locale);
设置语言代码简单,但是有个坑的地方是,如果启动环境的默认语言环境非英文,而想获取英文的报告,设置语言需要设置为 new Locale("") ,否则在源码中会java.util.ResourceBundle.getBundleImpl()方法中,获取到英文的国际化信息后, boolean isBaseBundle = Locale.ROOT.equals(bundle.locale); 验证不通过,又调用了control.getFallbackLocale(baseName, targetLocale)),拿到了默认环境的语言信息。 为啥不通过呢? 因为获去到的英文环境的 ResourceBundle 对象的locale属性是空字符串。
在此处配置生效,根据配置语言获取ResourceBundle。获取ResourceBundle对象内容为位置,1379英文国际化内容判断失败,又调用了getFallbackLocale方法locale为en时PropertyResourceBundle的locale属性值为空字符串为啥不自己做验证报告的国际化,xml验证报告稿结果内容太多,269种,自己真玩不转
国际化报告样例:
英文:
cvc-pattern-valid: Value '4545' is not facet-valid with respect to pattern '#[\dA-F]{6}([\dA-F][\dA-F])?' for type 'color'.
cvc-attribute.3: The value '4545' of attribute 'color' on element 'text' is not valid with respect to its type, 'color'.
cvc-datatype-valid.1.2.1: 'aa' is not a valid value for 'integer'.
cvc-attribute.3: The value 'aa' of attribute 'width' on element 'image' is not valid with respect to its type, 'int'.
中文:
cvc-pattern-valid: 对于类型为 'color' 的模式 '#[\dA-F]{6}([\dA-F][\dA-F])?', 值 '4545' 不具有面有效性。
cvc-attribute.3: 在元素 'text' 中, 属性 'color' 的值 '4545' 与其类型 'color' 不匹配。
cvc-datatype-valid.1.2.1: 'aa' 不是 'integer' 的有效值。
cvc-attribute.3: 在元素 'image' 中, 属性 'width' 的值 'aa' 与其类型 'int' 不匹配。
日文:
cvc-pattern-valid: 値'4545'は、タイプ'color'のパターン'#[\dA-F]{6}([\dA-F][\dA-F])?'に対してファセットが有効ではありません。
cvc-attribute.3: 要素'text'の属性'color'の値'4545'は、そのタイプ'color'に対して無効です。
cvc-datatype-valid.1.2.1: 'aa'は'integer'の有効な値ではありません。
cvc-attribute.3: 要素'image'の属性'width'の値'aa'は、そのタイプ'int'に対して無効です。
参考文章:
http://xerces.apache.org/xerces2-j/properties.html