Russell'sTeapot
Russell'sTeapot

Reputation: 383

SAX XML parser - localized error messages

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!

Upvotes: 1

Views: 3416

Answers (1)

Paolo
Paolo

Reputation: 1691

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!

Upvotes: 1

Related Questions