Search code examples
javaxmlexceptionjaxbjaxb2

How to internationalization SAXParseException while parsing XML file?


I've got a problem similar to this question: SAXParseException localized

I'm trying to parse a XML file and get a list of parser errors (SAXParseException) in a several languages for example:

XmlImporter.importFile(params, "en") should return a list of errors in English, XmlImporter.importFile(params, "fr") should return a list of errors in French, XmlImporter.importFile(params, "pl") should return a list of errors in Polish language.

Every call of XmlImporter.importFile(params, "...") may be with a different locale.

This is my validation method:

private void validate(String xmlFilePath, String schemaFilePath) throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(new File(schemaFilePath));
    Validator validator = schema.newValidator();

    XmlErrorHandler errorHandler = new XmlErrorHandler();
    validator.setErrorHandler(errorHandler);

    try (InputStream stream = new FileInputStream(new File(xmlFilePath))) {
        validator.validate(new StreamSource(stream));
    }

XmlErrorHandler:

public class XmlErrorHandler implements ErrorHandler {

    private List<String> errorsList = new ArrayList<>();

    public List<String> getErrorsList() {
        return errorsList;
    }

    @Override
    public void warning(SAXParseException exception) throws SAXException {
        errorsList.add(prepareExceptionDescription(exception));
    }

    @Override
    public void error(SAXParseException exception) throws SAXException {
        errorsList.add(prepareExceptionDescription(exception));
    }

    @Override
    public void fatalError(SAXParseException exception) throws SAXException {
        errorsList.add(prepareExceptionDescription(exception));
    }

    private String prepareExceptionDescription(SAXParseException exception) {
        return "Error: " +
                "colNumber: " + exception.getColumnNumber() +
                " line number: " + exception.getLineNumber() +
                " message: " + exception.getLocalizedMessage();
    }
}

I assume, that I need to pass somehow/somewhere java.util.Locale/String to get in exception.getLocalizedMessage() custom message (in en, fr or pl)?


Solution

  • By the default Xerces (Java Parser which is used to convert XML file to Java object) could provide internationalization for given languages:

    • XMLSchemaMessages_de.properties XMLSchemaMessages_es.properties
    • XMLSchemaMessages_fr.properties XMLSchemaMessages_it.properties
    • XMLSchemaMessages_ja.properties XMLSchemaMessages_ko.properties
    • XMLSchemaMessages_pt_BR.properties XMLSchemaMessages_sv.properties
    • XMLSchemaMessages_zh_CN.properties XMLSchemaMessages_zh_TW.properties

    To provide internationalization in other language:

    1. Get XMLSchemaMessages.properties file from Apache Xerces and rename file to a new file XMLSchemaMessages_LANG.properties, where LANG needs to be changed to a new language.

    2. Update file's messages to a new language and place this file in a classpath (You can add this file to src\main\resources\com\sun\org\apache\xerces\internal\impl\msg)

    3. Exceptions will be visible in a new language (messages will be taken from XMLSchemaMessages_LANG.properties file)