FileDocCategorySizeDatePackage
DOMMessageFormatter.javaAPI DocApache Xerces 3.0.15604Fri Sep 14 20:33:54 BST 2007org.apache.xerces.dom

DOMMessageFormatter

public class DOMMessageFormatter extends Object
Used to format DOM error messages, using the system locale.
xerces.internal
author
Sandy Gao, IBM
version
$Id: DOMMessageFormatter.java 449328 2006-09-23 22:58:23Z mrglavas $

Fields Summary
public static final String
DOM_DOMAIN
public static final String
XML_DOMAIN
public static final String
SERIALIZER_DOMAIN
private static ResourceBundle
domResourceBundle
private static ResourceBundle
xmlResourceBundle
private static ResourceBundle
serResourceBundle
private static Locale
locale
Constructors Summary
DOMMessageFormatter()

    
    
    
        locale = Locale.getDefault();
    
Methods Summary
public static java.lang.StringformatMessage(java.lang.String domain, java.lang.String key, java.lang.Object[] arguments)
Formats a message with the specified arguments using the given locale information.

param
domain domain from which error string is to come.
param
key The message key.
param
arguments The message replacement text arguments. The order of the arguments must match that of the placeholders in the actual message.
return
the formatted message.
throws
MissingResourceException Thrown if the message with the specified key cannot be found.

        ResourceBundle resourceBundle = getResourceBundle(domain);
        if(resourceBundle == null){
            init();
            resourceBundle = getResourceBundle(domain);
            if(resourceBundle == null)
                throw new MissingResourceException("Unknown domain" + domain, null, key);
        }
        // format message
        String msg;
        try {
            msg = key + ": " + resourceBundle.getString(key);
            if (arguments != null) {
                try {
                    msg = java.text.MessageFormat.format(msg, arguments);
                }
                catch (Exception e) {
                    msg = resourceBundle.getString("FormatFailed");
                    msg += " " + resourceBundle.getString(key);
                }
            }
        } // error
        catch (MissingResourceException e) {
            msg = resourceBundle.getString("BadMessageKey");
            throw new MissingResourceException(key, msg, key);
        }
        
        // no message
        if (msg == null) {
            msg = key;
            if (arguments.length > 0) {
                StringBuffer str = new StringBuffer(msg);
                str.append('?");
                for (int i = 0; i < arguments.length; i++) {
                    if (i > 0) {
                        str.append('&");
                    }
                    str.append(String.valueOf(arguments[i]));
                }
            }
        }
        
        return msg;
    
static java.util.ResourceBundlegetResourceBundle(java.lang.String domain)

        if(domain == DOM_DOMAIN || domain.equals(DOM_DOMAIN))
            return domResourceBundle;
        else if( domain == XML_DOMAIN || domain.equals(XML_DOMAIN))
            return xmlResourceBundle;
        else if(domain == SERIALIZER_DOMAIN || domain.equals(SERIALIZER_DOMAIN))
            return serResourceBundle;
        return null;
    
public static voidinit()
Initialize Message Formatter.

        if (locale != null) {
            domResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.DOMMessages", locale);
            serResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLSerializerMessages", locale);
            xmlResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLMessages", locale);
        }else{
            domResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.DOMMessages");
            serResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLSerializerMessages");
            xmlResourceBundle = PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.XMLMessages");
        }
    
public static voidsetLocale(java.util.Locale dlocale)
Set Locale to be used by the formatter.

param
dlocale

        locale = dlocale;