Home | History | Annotate | Download | only in res
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: XPATHMessages.java 468655 2006-10-28 07:12:06Z minchau $
     20  */
     21 package org.apache.xpath.res;
     22 
     23 import java.util.ListResourceBundle;
     24 
     25 import org.apache.xml.res.XMLMessages;
     26 
     27 /**
     28  * A utility class for issuing XPath error messages.
     29  * @xsl.usage internal
     30  */
     31 public class XPATHMessages extends XMLMessages
     32 {
     33   /** The language specific resource object for XPath messages.  */
     34   private static ListResourceBundle XPATHBundle = new XPATHErrorResources();
     35 
     36   /** The class name of the XPath error message string table.     */
     37   private static final String XPATH_ERROR_RESOURCES =
     38     "org.apache.xpath.res.XPATHErrorResources";
     39 
     40   /**
     41    * Creates a message from the specified key and replacement
     42    * arguments, localized to the given locale.
     43    *
     44    * @param msgKey    The key for the message text.
     45    * @param args      The arguments to be used as replacement text
     46    *                  in the message created.
     47    *
     48    * @return The formatted message string.
     49    */
     50   public static final String createXPATHMessage(String msgKey, Object args[])  //throws Exception
     51   {
     52       // BEGIN android-changed
     53       //     don't localize exception messages
     54       return createXPATHMsg(XPATHBundle, msgKey, args);
     55       // END android-changed
     56   }
     57 
     58   /**
     59    * Creates a message from the specified key and replacement
     60    * arguments, localized to the given locale.
     61    *
     62    * @param msgKey The key for the message text.
     63    * @param args      The arguments to be used as replacement text
     64    *                  in the message created.
     65    *
     66    * @return The formatted warning string.
     67    */
     68   public static final String createXPATHWarning(String msgKey, Object args[])  //throws Exception
     69   {
     70       // BEGIN android-changed
     71       //     don't localize exception messages
     72       return createXPATHMsg(XPATHBundle, msgKey, args);
     73       // END android-changed
     74   }
     75 
     76   /**
     77    * Creates a message from the specified key and replacement
     78    * arguments, localized to the given locale.
     79    *
     80    * @param fResourceBundle The resource bundle to use.
     81    * @param msgKey  The message key to use.
     82    * @param args      The arguments to be used as replacement text
     83    *                  in the message created.
     84    *
     85    * @return The formatted message string.
     86    */
     87   public static final String createXPATHMsg(ListResourceBundle fResourceBundle,
     88                                             String msgKey, Object args[])  //throws Exception
     89   {
     90 
     91     String fmsg = null;
     92     boolean throwex = false;
     93     String msg = null;
     94 
     95     if (msgKey != null)
     96       msg = fResourceBundle.getString(msgKey);
     97 
     98     if (msg == null)
     99     {
    100       msg = fResourceBundle.getString(XPATHErrorResources.BAD_CODE);
    101       throwex = true;
    102     }
    103 
    104     if (args != null)
    105     {
    106       try
    107       {
    108 
    109         // Do this to keep format from crying.
    110         // This is better than making a bunch of conditional
    111         // code all over the place.
    112         int n = args.length;
    113 
    114         for (int i = 0; i < n; i++)
    115         {
    116           if (null == args[i])
    117             args[i] = "";
    118         }
    119 
    120         fmsg = java.text.MessageFormat.format(msg, args);
    121       }
    122       catch (Exception e)
    123       {
    124         fmsg = fResourceBundle.getString(XPATHErrorResources.FORMAT_FAILED);
    125         fmsg += " " + msg;
    126       }
    127     }
    128     else
    129       fmsg = msg;
    130 
    131     if (throwex)
    132     {
    133       throw new RuntimeException(fmsg);
    134     }
    135 
    136     return fmsg;
    137   }
    138 
    139 }
    140