1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 1999-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.msg;
  22 
  23 import java.util.Locale;
  24 import java.util.MissingResourceException;
  25 import java.util.ResourceBundle;
  26 import java.util.PropertyResourceBundle;
  27 
  28 import com.sun.org.apache.xerces.internal.util.MessageFormatter;
  29 import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
  30 
  31 /**
  32  * XMLMessageFormatter provides error messages for the XML 1.0 Recommendation and for
  33  * the Namespaces Recommendation
  34  *
  35  * @xerces.internal
  36  *
  37  * @author Eric Ye, IBM
  38  * @version $Id: XMLMessageFormatter_ja.java 3094 2012-03-21 05:50:01Z joehw $
  39  *
  40  */
  41 public class XMLMessageFormatter_ja implements MessageFormatter {
  42     /**
  43      * The domain of messages concerning the XML 1.0 specification.
  44      */
  45     public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
  46     public static final String XMLNS_DOMAIN = "http://www.w3.org/TR/1999/REC-xml-names-19990114";
  47 
  48     // private objects to cache the locale and resource bundle
  49     private Locale fLocale = null;
  50     private ResourceBundle fResourceBundle = null;
  51 
  52     //
  53     // MessageFormatter methods
  54     //
  55 
  56     /**
  57      * Formats a message with the specified arguments using the given
  58      * locale information.
  59      *
  60      * @param locale    The locale of the message.
  61      * @param key       The message key.
  62      * @param arguments The message replacement text arguments. The order
  63      *                  of the arguments must match that of the placeholders
  64      *                  in the actual message.
  65      *
  66      * @return Returns the formatted message.
  67      *
  68      * @throws MissingResourceException Thrown if the message with the
  69      *                                  specified key cannot be found.
  70      */
  71     public String formatMessage(Locale locale, String key, Object[] arguments)
  72         throws MissingResourceException {
  73 
  74         if (fResourceBundle == null || locale != fLocale) {
  75             if (locale != null) {
  76                 fResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale);
  77                 // memorize the most-recent locale
  78                 fLocale = locale;
  79             }
  80             if (fResourceBundle == null)
  81                 fResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages");
  82         }
  83 
  84         // format message
  85         String msg;
  86         try {
  87             msg = fResourceBundle.getString(key);
  88             if (arguments != null) {
  89                 try {
  90                     msg = java.text.MessageFormat.format(msg, arguments);
  91                 }
  92                 catch (Exception e) {
  93                     msg = fResourceBundle.getString("FormatFailed");
  94                     msg += " " + fResourceBundle.getString(key);
  95                 }
  96             }
  97         }
  98 
  99         // error
 100         catch (MissingResourceException e) {
 101             msg = fResourceBundle.getString("BadMessageKey");
 102             throw new MissingResourceException(key, msg, key);
 103         }
 104 
 105         // no message
 106         if (msg == null) {
 107             msg = key;
 108             if (arguments.length > 0) {
 109                 StringBuffer str = new StringBuffer(msg);
 110                 str.append('?');
 111                 for (int i = 0; i < arguments.length; i++) {
 112                     if (i > 0) {
 113                         str.append('&');
 114                     }
 115                     str.append(String.valueOf(arguments[i]));
 116                 }
 117             }
 118         }
 119 
 120         return msg;
 121     }
 122 
 123 }