私のアプリケーションは、XSD ファイルに基づいて XML を検証します。この検証の結果はエラー メッセージです。これをユーザーに表示するために、java.text.MessageFormat を使用してこのエラー メッセージを解析します。問題は、メッセージが英語の場合は正常に解析できますが、メッセージがポルトガル語 (ブラジル) の場合は次のエラーが返されることです。
java.text.ParseException: MessageFormat parse error!
at java.text.MessageFormat.parse(MessageFormat.java:1035)
at com.local.messageformat.MessageFormatMain.splitMessage(MessageFormatMain.java:34)
at com.local.messageformat.MessageFormatMain.start(MessageFormatMain.java:20)
at com.local.messageformat.MessageFormatMain.main(MessageFormatMain.java:14)
これが私のコードです:
public class MessageFormatMain {
public static void main(String[] args) {
final MessageFormatMain main = new MessageFormatMain();
main.start();
}
private void start() {
final String errorMessageKey = "cvc-complex-type.2.4.a";
//Portuguese message (shows error)
final String errorMessage = "cvc-complex-type.2.4.a: Conteúdo inválido encontrado a partir do elemento 'inscricaomunicipaltomador'. Um de '{razaosocialtomador, estrangeirotomador, tipologradourotomador, logradourotomador, numeroenderecotomador, bairrotomador, complementotomador, cidadetomadordescricao, estadotomadordescricao, fonetomador, ceptomador, emailtomador, tomadorpais, tomadorresptribut, codigoatividade, aliquotaatividade, tiporecolhimento, valortotalrps, valorservicosrps, valoriss, valorpis, valorcofins, valorinss, valorir, valorcsll, aliquotapis, aliquotacofins, aliquotainss, aliquotair, aliquotacsll, descricaorps, tributacaorps, localservico, itens}' é esperado.";
// English message (works fine)
// final String errorMessage = "cvc-complex-type.2.4.a: Invalid content was found starting with element 'inscricaomunicipaltomador'. One of '{razaosocialtomador, estrangeirotomador, tipologradourotomador, logradourotomador, numeroenderecotomador, bairrotomador, complementotomador, cidadetomadordescricao, estadotomadordescricao, fonetomador, ceptomador, emailtomador, tomadorpais, tomadorresptribut, codigoatividade, aliquotaatividade, tiporecolhimento, valortotalrps, valorservicosrps, valoriss, valorpis, valorcofins, valorinss, valorir, valorcsll, aliquotapis, aliquotacofins, aliquotainss, aliquotair, aliquotacsll, descricaorps, tributacaorps, localservico, itens}' is expected.";
Object[] ret = splitMessage(errorMessage, errorMessageKey);
if (ret.length > 0) {
System.out.println(ret[0]);
System.out.println(ret[1]);
}
}
private Object[] splitMessage(final String errorMessage, final String errorMessageKey) {
final ResourceBundle resourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages", Locale.ROOT);
if (errorMessageKey != null && !errorMessageKey.isEmpty() && resourceBundle.containsKey(errorMessageKey)) {
final String errorMessagePattern = resourceBundle.getString(errorMessageKey);
final MessageFormat messageFormat = new MessageFormat(errorMessagePattern);
Object arguments[];
try {
arguments = messageFormat.parse(errorMessage);
return arguments;
} catch (final ParseException e) {
e.printStackTrace();
}
return new Object[0];
}
return new Object[0];
}
}
別の場所を指定して ResourceBundle を取得し、MessageFormat をインスタンス化しようとしましたが、いずれも機能しませんでした。
前もって感謝します。
ギエルメ