286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2002,2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.dom;
524N/Aimport com.sun.org.apache.xerces.internal.utils.SecuritySupport;
286N/Aimport java.util.Locale;
286N/Aimport java.util.MissingResourceException;
286N/Aimport java.util.ResourceBundle;
286N/A
286N/A/**
286N/A * Used to format DOM error messages, using the system locale.
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Sandy Gao, IBM
524N/A * @version $Id: DOMMessageFormatter.java,v 1.6 2010-11-01 04:39:38 joehw Exp $
286N/A */
286N/Apublic class DOMMessageFormatter {
286N/A public static final String DOM_DOMAIN = "http://www.w3.org/dom/DOMTR";
286N/A public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
286N/A public static final String SERIALIZER_DOMAIN = "http://apache.org/xml/serializer";
286N/A
286N/A private static ResourceBundle domResourceBundle = null;
286N/A private static ResourceBundle xmlResourceBundle = null;
286N/A private static ResourceBundle serResourceBundle = null;
286N/A private static Locale locale = null;
286N/A
286N/A
286N/A DOMMessageFormatter(){
286N/A locale = Locale.getDefault();
286N/A }
286N/A /**
286N/A * Formats a message with the specified arguments using the given
286N/A * locale information.
286N/A *
286N/A * @param domain domain from which error string is to come.
286N/A * @param key The message key.
286N/A * @param arguments The message replacement text arguments. The order
286N/A * of the arguments must match that of the placeholders
286N/A * in the actual message.
286N/A *
286N/A * @return the formatted message.
286N/A *
286N/A * @throws MissingResourceException Thrown if the message with the
286N/A * specified key cannot be found.
286N/A */
286N/A public static String formatMessage(String domain,
286N/A String key, Object[] arguments)
286N/A throws MissingResourceException {
286N/A ResourceBundle resourceBundle = getResourceBundle(domain);
286N/A if(resourceBundle == null){
286N/A init();
286N/A resourceBundle = getResourceBundle(domain);
286N/A if(resourceBundle == null)
286N/A throw new MissingResourceException("Unknown domain" + domain, null, key);
286N/A }
286N/A // format message
286N/A String msg;
286N/A try {
286N/A msg = key + ": " + resourceBundle.getString(key);
286N/A if (arguments != null) {
286N/A try {
286N/A msg = java.text.MessageFormat.format(msg, arguments);
286N/A }
286N/A catch (Exception e) {
286N/A msg = resourceBundle.getString("FormatFailed");
286N/A msg += " " + resourceBundle.getString(key);
286N/A }
286N/A }
286N/A } // error
286N/A catch (MissingResourceException e) {
286N/A msg = resourceBundle.getString("BadMessageKey");
286N/A throw new MissingResourceException(key, msg, key);
286N/A }
286N/A
286N/A // no message
286N/A if (msg == null) {
286N/A msg = key;
286N/A if (arguments.length > 0) {
286N/A StringBuffer str = new StringBuffer(msg);
286N/A str.append('?');
286N/A for (int i = 0; i < arguments.length; i++) {
286N/A if (i > 0) {
286N/A str.append('&');
286N/A }
286N/A str.append(String.valueOf(arguments[i]));
286N/A }
286N/A }
286N/A }
286N/A
286N/A return msg;
286N/A }
286N/A
286N/A static ResourceBundle getResourceBundle(String domain){
286N/A if(domain == DOM_DOMAIN || domain.equals(DOM_DOMAIN))
286N/A return domResourceBundle;
286N/A else if( domain == XML_DOMAIN || domain.equals(XML_DOMAIN))
286N/A return xmlResourceBundle;
286N/A else if(domain == SERIALIZER_DOMAIN || domain.equals(SERIALIZER_DOMAIN))
286N/A return serResourceBundle;
286N/A return null;
286N/A }
286N/A /**
286N/A * Initialize Message Formatter.
286N/A */
286N/A public static void init(){
286N/A if (locale != null) {
524N/A domResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.DOMMessages", locale);
524N/A serResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages", locale);
524N/A xmlResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale);
286N/A }else{
524N/A domResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.DOMMessages");
524N/A serResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages");
524N/A xmlResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages");
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * setLocale to be used by the formatter.
286N/A * @param locale
286N/A */
286N/A public static void setLocale(Locale dlocale){
286N/A locale = dlocale;
286N/A }
286N/A}