Save This Page
Home » Xerces-J-src.2.9.1 » org.apache.xerces » util » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.xerces.util;
   18   
   19   import java.util.Locale;
   20   import java.util.MissingResourceException;
   21   import java.util.ResourceBundle;
   22   import java.util.PropertyResourceBundle;
   23   
   24   /**
   25    * Used to format SAX error messages using a specified locale.
   26    * 
   27    * @author Michael Glavassevich, IBM
   28    * 
   29    * @version $Id: SAXMessageFormatter.java 447241 2006-09-18 05:12:57Z mrglavas $
   30    */
   31   public class SAXMessageFormatter {
   32   
   33       /**
   34        * Formats a message with the specified arguments using the given
   35        * locale information.
   36        * 
   37        * @param locale    The locale of the message.
   38        * @param key       The message key.
   39        * @param arguments The message replacement text arguments. The order
   40        *                  of the arguments must match that of the placeholders
   41        *                  in the actual message.
   42        * 
   43        * @return          the formatted message.
   44        *
   45        * @throws MissingResourceException Thrown if the message with the
   46        *                                  specified key cannot be found.
   47        */
   48       public static String formatMessage(Locale locale, 
   49           String key, Object[] arguments)
   50           throws MissingResourceException {
   51   
   52           ResourceBundle resourceBundle = null;
   53           if (locale != null) {
   54               resourceBundle = 
   55                   PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.SAXMessages", locale);
   56           }
   57           else {
   58               resourceBundle = 
   59                   PropertyResourceBundle.getBundle("org.apache.xerces.impl.msg.SAXMessages");
   60           }
   61   
   62           // format message
   63           String msg;
   64           try {
   65               msg = resourceBundle.getString(key);
   66               if (arguments != null) {
   67                   try {
   68                       msg = java.text.MessageFormat.format(msg, arguments);
   69                   } 
   70                   catch (Exception e) {
   71                       msg = resourceBundle.getString("FormatFailed");
   72                       msg += " " + resourceBundle.getString(key);
   73                   }
   74               } 
   75           }
   76           
   77           // error
   78           catch (MissingResourceException e) {
   79               msg = resourceBundle.getString("BadMessageKey");
   80               throw new MissingResourceException(key, msg, key);
   81           }
   82   
   83           // no message
   84           if (msg == null) {
   85               msg = key;
   86               if (arguments.length > 0) {
   87                   StringBuffer str = new StringBuffer(msg);
   88                   str.append('?');
   89                   for (int i = 0; i < arguments.length; i++) {
   90                       if (i > 0) {
   91                           str.append('&');
   92                       }
   93                       str.append(String.valueOf(arguments[i]));
   94                   }
   95               }
   96           }
   97           return msg;
   98       }
   99   }

Save This Page
Home » Xerces-J-src.2.9.1 » org.apache.xerces » util » [javadoc | source]