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