1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001, 2002,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 
  21 package com.sun.org.apache.xerces.internal.impl.xs;
  22 
  23 import com.sun.org.apache.xerces.internal.util.MessageFormatter;
  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 /**
  31  * SchemaMessageProvider implements an XMLMessageProvider that
  32  * provides localizable error messages for the W3C XML Schema Language
  33  *
  34  * @xerces.internal
  35  *
  36  * @author Elena Litani, IBM
  37  */
  38 public class XSMessageFormatter implements MessageFormatter {
  39     /**
  40      * The domain of messages concerning the XML Schema: Structures specification.
  41      */
  42     public static final String SCHEMA_DOMAIN = "http://www.w3.org/TR/xml-schema-1";
  43 
  44 
  45     // private objects to cache the locale and resource bundle
  46     private Locale fLocale = null;
  47     private ResourceBundle fResourceBundle = null;
  48 
  49     /**
  50      * Formats a message with the specified arguments using the given
  51      * locale information.
  52      *
  53      * @param locale    The locale of the message.
  54      * @param key       The message key.
  55      * @param arguments The message replacement text arguments. The order
  56      *                  of the arguments must match that of the placeholders
  57      *                  in the actual message.
  58      *
  59      * @return Returns the formatted message.
  60      *
  61      * @throws MissingResourceException Thrown if the message with the
  62      *                                  specified key cannot be found.
  63      */
  64      public String formatMessage(Locale locale, String key, Object[] arguments)
  65         throws MissingResourceException {
  66 
  67         if (fResourceBundle == null || locale != fLocale) {
  68             if (locale != null) {
  69                 fResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages", locale);
  70                 // memorize the most-recent locale
  71                 fLocale = locale;
  72             }
  73             if (fResourceBundle == null)
  74                 fResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages");
  75         }
  76 
  77         String msg = fResourceBundle.getString(key);
  78         if (arguments != null) {
  79             try {
  80                 msg = java.text.MessageFormat.format(msg, arguments);
  81             } catch (Exception e) {
  82                 msg = fResourceBundle.getString("FormatFailed");
  83                 msg += " " + fResourceBundle.getString(key);
  84             }
  85         }
  86 
  87         if (msg == null) {
  88             msg = fResourceBundle.getString("BadMessageKey");
  89             throw new MissingResourceException(msg, "com.sun.org.apache.xerces.internal.impl.msg.SchemaMessages", key);
  90         }
  91 
  92         return msg;
  93     }
  94 }