1

The web-application I am currently working on, validates user-supplied xml files against xsd stored on a server. The problem is that if xml fails validation, error messages should be in Russian. I have my parser working - it gives error messages but only in English

    String parserClass = "org.apache.xerces.parsers.SAXParser";
    String validationFeature = "http://xml.org/sax/features/validation";
    String schemaFeature = "http://apache.org/xml/features/validation/schema";
    XMLReader reader = null;
    reader = XMLReaderFactory.createXMLReader(parserClass);

    reader.setFeature(validationFeature,true);
    reader.setFeature(schemaFeature,true);

    BatchContentHandler contentHandler = new BatchContentHandler(reader);
    reader.setContentHandler(contentHandler);

    BatchErrorHandler errorHandler = new BatchErrorHandler(reader);
    reader.setErrorHandler(errorHandler);

    reader.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
    reader.parse(new InputSource(new ByteArrayInputStream(streamedXML)));

It works fine - error messages are in English. Reading this post Locale specific messages in Xerces 2.11.0 (Java) and also this post https://www.java.net//node/699069 I added these lines

Locale l = new Locale("ru", "RU");
reader.setProperty("http://apache.org/xml/properties/locale", l);

I also added XMLSchemaMessages_RU.properties file to the jar. Now I get NULL pointer exception. Any ideas or hints? Thanks in advance!

Community
  • 1
  • 1
Russell'sTeapot
  • 373
  • 2
  • 11
  • 21

1 Answers1

1

I found here this about http://apache.org/xml/properties/locale:

  • Desc:The locale to use for reporting errors and warnings. When the value of this property is null the platform default returned from java.util.Locale.getDefault() will be used.
  • Type: java.util.Locale
  • Access: read-write
  • Since: Xerces-J 2.10.0
  • Note: If no messages are available for the specified locale the platform default will be used. If the platform default is not English and no messages are available for this locale then messages will be reported in English.

Also I found here an example where in order to create a Locale object for the Russian language this code is provided:

Locale dLocale = new Locale.Builder().setLanguage("ru").setScript("Cyrl").build();

I don't know if this could be useful. Just have a try and give me feedback about it!

Paolo
  • 1,641
  • 11
  • 15
  • still getting java.lang.NullPointerException :( – Russell'sTeapot Oct 16 '13 at 11:16
  • Could you edit your post adding the entire stacktrace of the error? Maybe it will be useful. – Paolo Oct 16 '13 at 12:48
  • Have a look at this [stackoverflow post](http://stackoverflow.com/questions/6848106/java-jre-how-to-add-localized-resources-to-stantard-jre-resources) too. – Paolo Oct 16 '13 at 13:06
  • 1
    There it is suggested to call the file **XMLSchemaMessages_ru.properties** – Paolo Oct 16 '13 at 13:12
  • 1
    Hi everyone! Thanks a lot. All of your suggestions were correct. It's just that "http://apache.org/xml/properties/locale" is supported from xercesImpl-2.10.0 and I had older version. I updated the dependency, created file with "_ru" part and it works! – Russell'sTeapot Oct 17 '13 at 03:28
  • @Russell'sTeapot Thanks to you to have added the solution of your problem. It will be useful for somebody else too! Cheers! – Paolo Oct 17 '13 at 07:54