4

我想在我的 JAXP 和 Xerces2 中使用特定于语言环境的错误消息。默认情况下,只有英文消息可用。

第一步是检索消息文件并将它们放入包“org/apache/xerces/impl/msg/” - 完成。通过使用Locale.setDefault (Locale.GERMANY)德语消息显示,因此这是有效的。

但我希望消息在每个实例的基础上进行不同的本地化。所以一个解析器应该返回英文消息,另一个解析器应该返回德语消息。

我用来创建 SAX2 解析器的代码是:

org.xml.sax.XMLReader ret = XMLReaderFactory.createXMLReader ();

对于 DOM,我使用 DocumentBuilder 喜欢这样(非常简化):

    final DocumentBuilderFactory aDocumentBuilderFactory = DocumentBuilderFactory.newInstance ();
    final DocumentBuilder aDocBuilder = aDocBuilderFactory.newDocumentBuilder ();
    final Document doc = aDocumentBuilder.parse (aInputSource);

我找到了类似org.apache.xerces.impl.XMLErrorReporter具有setLocale(Locale)方法的类的东西,但我没有找到获取/设置它的方法。

顺便说一句,切换到 SAX1 不是一种选择。

任何帮助表示赞赏!

4

3 回答 3

6

不是最大的可移植性,但它的工作原理是在 99% 的情况下,解析器都是 apache 解析器。

final DocumentBuilderFactory aDocBuilderFactory = DocumentBuilderFactory.newInstance();
aDocBuilderFactory.setAttribute("http://apache.org/xml/properties/locale", Locale.FRANCE);
final DocumentBuilder aDocBuilder = aDocBuilderFactory.newDocumentBuilder();
final Document doc = aDocBuilder.parse (aInputSource);

一个SAXParser saxParser简单的电话saxParser.setProperty("http://apache.org/xml/properties/locale", Locale.FRANCE);

哦,忘了官方出处:http: //xerces.apache.org/xerces2-j/properties.html

于 2013-09-11T15:57:18.023 回答
4

设置/获取 MessageFormatter 的可能性:

Validator validator = schema.newValidator();      
XMLErrorReporter property = (XMLErrorReporter) validator.getProperty("http://apache.org/xml/properties/internal/error-reporter");
MessageFormatter messageFormatter = property.getMessageFormatter("http://www.w3.org/TR/xml-schema-1");
property.putMessageFormatter(MyMessageFormatter.SCHEMA_DOMAIN, new MyMessageFormatter());


public class MyMessageFormatter implements MessageFormatter {
    public static final String SCHEMA_DOMAIN = "http://www.w3.org/TR/xml-schema-1";
    //...
    public String formatMessage(Locale locale, String key, Object[] arguments)
            throws MissingResourceException {...}
    //...

}
于 2015-04-09T20:15:31.783 回答
0

I think you should try using

com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter 

If you are writing a custom validation, try calling its formatMessage(...) method, where you could provide the locale name as parameter.

An example of the same is provided in the apache library itself. See it http://cr.openjdk.java.net/~coffeys/openJDK.7u21.sync/webrev/jaxp/src/com/sun/org/apache/xerces/internal/impl/msg/XMLMessageFormatter_zh_CN.java-.html

or

http://www.docjar.com/html/api/com/sun/org/apache/xerces/internal/impl/msg/XMLMessageFormatter.java.html

Another approach could be that you may override the formatMessage() method to implement it in your own way. See the below implemented code of this method:

 public String More ...formatMessage(Locale locale, String key, Object[] arguments)
         throws MissingResourceException {
          if (fResourceBundle == null || locale != fLocale) {
             if (locale != null) {
                 fResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale);
                 // memorize the most-recent locale
                 fLocale = locale;
             }
             if (fResourceBundle == null)
                 fResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages");
         }

This indicate that, if a resource bundle file is declared according to locale, the control should be able to pick a different resource file having error message in different language.

于 2013-09-08T12:00:03.197 回答