1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 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.util; 23 24 import com.sun.org.apache.xerces.internal.utils.SecuritySupport; 25 import java.util.Locale; 26 import java.util.MissingResourceException; 27 import java.util.ResourceBundle; 28 29 /** 30 * <p>Used to format JAXP 1.3 Datatype API error messages using a specified locale.</p> 31 * 32 * @author Neeraj Bajaj, Sun Microsystems 33 * @version $Id: DatatypeMessageFormatter.java,v 1.6 2010-11-01 04:40:14 joehw Exp $ 34 */ 35 public class DatatypeMessageFormatter { 36 37 private static final String BASE_NAME = "com.sun.org.apache.xerces.internal.impl.msg.DatatypeMessages"; 38 39 /** 40 * Formats a message with the specified arguments using the given 41 * locale information. 42 * 43 * @param locale The locale of the message. 44 * @param key The message key. 45 * @param arguments The message replacement text arguments. The order 46 * of the arguments must match that of the placeholders 47 * in the actual message. 48 * 49 * @return the formatted message. 50 * 51 * @throws MissingResourceException Thrown if the message with the 52 * specified key cannot be found. 53 */ 54 public static String formatMessage(Locale locale, 55 String key, Object[] arguments) 56 throws MissingResourceException { 57 58 ResourceBundle resourceBundle = null; 59 if (locale != null) { 60 resourceBundle = 61 SecuritySupport.getResourceBundle(BASE_NAME, locale); 62 } 63 else { 64 resourceBundle = 65 SecuritySupport.getResourceBundle(BASE_NAME); 66 } 67 68 // format message 69 String msg; 70 try { 71 msg = resourceBundle.getString(key); 72 if (arguments != null) { 73 try { 74 msg = java.text.MessageFormat.format(msg, arguments); 75 } 76 catch (Exception e) { 77 msg = resourceBundle.getString("FormatFailed"); 78 msg += " " + resourceBundle.getString(key); 79 } 80 } 81 } 82 83 // error 84 catch (MissingResourceException e) { 85 msg = resourceBundle.getString("BadMessageKey"); 86 throw new MissingResourceException(key, msg, key); 87 } 88 89 // no message 90 if (msg == null) { 91 msg = key; 92 if (arguments.length > 0) { 93 StringBuffer str = new StringBuffer(msg); 94 str.append('?'); 95 for (int i = 0; i < arguments.length; i++) { 96 if (i > 0) { 97 str.append('&'); 98 } 99 str.append(String.valueOf(arguments[i])); 100 } 101 } 102 } 103 return msg; 104 } 105 }