1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * @LastModified: Sep 2017 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.xerces.internal.jaxp.validation; 23 24 import java.util.Locale; 25 import java.util.MissingResourceException; 26 import java.util.ResourceBundle; 27 import jdk.xml.internal.SecuritySupport; 28 29 /** 30 * <p>Used to format JAXP Validation API error messages using a specified locale.</p> 31 * 32 * @author Michael Glavassevich, IBM 33 */ 34 final class JAXPValidationMessageFormatter { 35 36 /** 37 * Formats a message with the specified arguments using the given 38 * locale information. 39 * 40 * @param locale The locale of the message. 41 * @param key The message key. 42 * @param arguments The message replacement text arguments. The order 43 * of the arguments must match that of the placeholders 44 * in the actual message. 45 * 46 * @return the formatted message. 47 * 48 * @throws MissingResourceException Thrown if the message with the 49 * specified key cannot be found. 50 */ 51 public static String formatMessage(Locale locale, 52 String key, Object[] arguments) 53 throws MissingResourceException { 54 55 ResourceBundle resourceBundle = null; 56 if (locale != null) { 57 resourceBundle = 58 SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.JAXPValidationMessages", locale); 59 } 60 else { 61 resourceBundle = 62 SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.JAXPValidationMessages"); 63 } 64 65 // format message 66 String msg; 67 try { 68 msg = resourceBundle.getString(key); 69 if (arguments != null) { 70 try { 71 msg = java.text.MessageFormat.format(msg, arguments); 72 } 73 catch (Exception e) { 74 msg = resourceBundle.getString("FormatFailed"); 75 msg += " " + resourceBundle.getString(key); 76 } 77 } 78 } 79 80 // error 81 catch (MissingResourceException e) { 82 msg = resourceBundle.getString("BadMessageKey"); 83 throw new MissingResourceException(key, msg, key); 84 } 85 86 // no message 87 if (msg == null) { 88 msg = key; 89 if (arguments.length > 0) { 90 StringBuffer str = new StringBuffer(msg); 91 str.append('?'); 92 for (int i = 0; i < arguments.length; i++) { 93 if (i > 0) { 94 str.append('&'); 95 } 96 str.append(String.valueOf(arguments[i])); 97 } 98 } 99 } 100 return msg; 101 } 102 }