1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 
  23 package com.sun.org.apache.xerces.internal.dom;
  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  * Used to format DOM error messages, using the system locale.
  31  *
  32  * @xerces.internal
  33  *
  34  * @author Sandy Gao, IBM
  35  * @version $Id: DOMMessageFormatter.java,v 1.6 2010-11-01 04:39:38 joehw Exp $
  36  */
  37 public class DOMMessageFormatter {
  38     public static final String DOM_DOMAIN = "http://www.w3.org/dom/DOMTR";
  39     public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
  40     public static final String SERIALIZER_DOMAIN = "http://apache.org/xml/serializer";
  41 
  42     private static ResourceBundle domResourceBundle = null;
  43     private static ResourceBundle xmlResourceBundle = null;
  44     private static ResourceBundle serResourceBundle = null;
  45     private static Locale locale = null;
  46 
  47 
  48     DOMMessageFormatter(){
  49         locale = Locale.getDefault();
  50     }
  51     /**
  52      * Formats a message with the specified arguments using the given
  53      * locale information.
  54      *
  55      * @param domain    domain from which error string is to come.
  56      * @param key       The message key.
  57      * @param arguments The message replacement text arguments. The order
  58      *                  of the arguments must match that of the placeholders
  59      *                  in the actual message.
  60      *
  61      * @return          the formatted message.
  62      *
  63      * @throws MissingResourceException Thrown if the message with the
  64      *                                  specified key cannot be found.
  65      */
  66     public static String formatMessage(String domain,
  67     String key, Object[] arguments)
  68     throws MissingResourceException {
  69         ResourceBundle resourceBundle = getResourceBundle(domain);
  70         if(resourceBundle == null){
  71             init();
  72             resourceBundle = getResourceBundle(domain);
  73             if(resourceBundle == null)
  74                 throw new MissingResourceException("Unknown domain" + domain, null, key);
  75         }
  76         // format message
  77         String msg;
  78         try {
  79             msg = key + ": " + resourceBundle.getString(key);
  80             if (arguments != null) {
  81                 try {
  82                     msg = java.text.MessageFormat.format(msg, arguments);
  83                 }
  84                 catch (Exception e) {
  85                     msg = resourceBundle.getString("FormatFailed");
  86                     msg += " " + resourceBundle.getString(key);
  87                 }
  88             }
  89         } // error
  90         catch (MissingResourceException e) {
  91             msg = resourceBundle.getString("BadMessageKey");
  92             throw new MissingResourceException(key, msg, key);
  93         }
  94 
  95         // no message
  96         if (msg == null) {
  97             msg = key;
  98             if (arguments.length > 0) {
  99                 StringBuffer str = new StringBuffer(msg);
 100                 str.append('?');
 101                 for (int i = 0; i < arguments.length; i++) {
 102                     if (i > 0) {
 103                         str.append('&');
 104                     }
 105                     str.append(String.valueOf(arguments[i]));
 106                 }
 107             }
 108         }
 109 
 110         return msg;
 111     }
 112 
 113     static ResourceBundle getResourceBundle(String domain){
 114         if(domain == DOM_DOMAIN || domain.equals(DOM_DOMAIN))
 115             return domResourceBundle;
 116         else if( domain == XML_DOMAIN || domain.equals(XML_DOMAIN))
 117             return xmlResourceBundle;
 118         else if(domain == SERIALIZER_DOMAIN || domain.equals(SERIALIZER_DOMAIN))
 119             return serResourceBundle;
 120         return null;
 121     }
 122     /**
 123      * Initialize Message Formatter.
 124      */
 125     public static void init(){
 126         if (locale != null) {
 127             domResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.DOMMessages", locale);
 128             serResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages", locale);
 129             xmlResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale);
 130         }else{
 131             domResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.DOMMessages");
 132             serResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages");
 133             xmlResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages");
 134         }
 135     }
 136 
 137     /**
 138      * setLocale to be used by the formatter.
 139      * @param locale
 140      */
 141     public static void setLocale(Locale dlocale){
 142         locale = dlocale;
 143     }
 144 }