1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 package com.sun.org.apache.xerces.internal.util;
  21 
  22 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
  23 import java.util.Locale;
  24 import java.util.MissingResourceException;
  25 import java.util.ResourceBundle;
  26 
  27 /**
  28  * Used to format SAX error messages using a specified locale.
  29  *
  30  * @author Michael Glavassevich, IBM
  31  *
  32  * @version $Id: SAXMessageFormatter.java,v 1.6 2010-11-01 04:40:14 joehw Exp $
  33  */
  34 public class SAXMessageFormatter {
  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.SAXMessages", locale);
  59         }
  60         else {
  61             resourceBundle =
  62                 SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.SAXMessages");
  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 }