1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2003-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.xinclude;
  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 // TODO: fix error messages in XIncludeMessages.properties
  30 /**
  31  * XIncludeMessageFormatter provides error messages for the XInclude 1.0 Candidate Recommendation
  32  *
  33  * @author Peter McCracken, IBM
  34  *
  35  */
  36 public class XIncludeMessageFormatter implements MessageFormatter {
  37 
  38     public static final String XINCLUDE_DOMAIN = "http://www.w3.org/TR/xinclude";
  39 
  40      // private objects to cache the locale and resource bundle
  41     private Locale fLocale = null;
  42     private ResourceBundle fResourceBundle = null;
  43 
  44     /**
  45      * Formats a message with the specified arguments using the given
  46      * locale information.
  47      *
  48      * @param locale    The locale of the message.
  49      * @param key       The message key.
  50      * @param arguments The message replacement text arguments. The order
  51      *                  of the arguments must match that of the placeholders
  52      *                  in the actual message.
  53      *
  54      * @return Returns the formatted message.
  55      *
  56      * @throws MissingResourceException Thrown if the message with the
  57      *                                  specified key cannot be found.
  58      */
  59      public String formatMessage(Locale locale, String key, Object[] arguments)
  60         throws MissingResourceException {
  61 
  62         if (fResourceBundle == null || locale != fLocale) {
  63             if (locale != null) {
  64                 fResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XIncludeMessages", locale);
  65                 // memorize the most-recent locale
  66                 fLocale = locale;
  67             }
  68             if (fResourceBundle == null)
  69                 fResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XIncludeMessages");
  70         }
  71 
  72         String msg = fResourceBundle.getString(key);
  73         if (arguments != null) {
  74             try {
  75                 msg = java.text.MessageFormat.format(msg, arguments);
  76             } catch (Exception e) {
  77                 msg = fResourceBundle.getString("FormatFailed");
  78                 msg += " " + fResourceBundle.getString(key);
  79             }
  80         }
  81 
  82         if (msg == null) {
  83             msg = fResourceBundle.getString("BadMessageKey");
  84             throw new MissingResourceException(msg, "com.sun.org.apache.xerces.internal.impl.msg.XIncludeMessages", key);
  85         }
  86 
  87         return msg;
  88     }
  89 }