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: XMLMessages.java 468653 2006-10-28 07:07:05Z minchau $
20  */
21 package org.apache.xml.res;
22 
23 import java.util.ListResourceBundle;
24 import java.util.Locale;
25 import java.util.MissingResourceException;
26 import java.util.ResourceBundle;
27 
28 /**
29  * A utility class for issuing XML error messages.
30  * @xsl.usage internal
31  */
32 public class XMLMessages
33 {
34 
35   /** The local object to use.  */
36   protected Locale fLocale = Locale.getDefault();
37 
38   /** The language specific resource object for XML messages.  */
39   private static ListResourceBundle XMLBundle = new XMLErrorResources(); // android-changed
40 
41   /** String to use if a bad message code is used. */
42   protected static final String BAD_CODE = "BAD_CODE";
43 
44   /** String to use if the message format operation failed.  */
45   protected static final String FORMAT_FAILED = "FORMAT_FAILED";
46 
47   /**
48    * Set the Locale object to use.
49    *
50    * @param locale non-null reference to Locale object.
51    */
setLocale(Locale locale)52    public void setLocale(Locale locale)
53   {
54     fLocale = locale;
55   }
56 
57   /**
58    * Get the Locale object that is being used.
59    *
60    * @return non-null reference to Locale object.
61    */
getLocale()62   public Locale getLocale()
63   {
64     return fLocale;
65   }
66 
67   /**
68    * Creates a message from the specified key and replacement
69    * arguments, localized to the given locale.
70    *
71    * @param msgKey    The key for the message text.
72    * @param args      The arguments to be used as replacement text
73    *                  in the message created.
74    *
75    * @return The formatted message string.
76    */
createXMLMessage(String msgKey, Object args[])77   public static final String createXMLMessage(String msgKey, Object args[])
78   {
79       // BEGIN android-changed
80       //     don't localize exceptions
81       return createMsg(XMLBundle, msgKey, args);
82       // END android-changed
83   }
84 
85   /**
86    * Creates a message from the specified key and replacement
87    * arguments, localized to the given locale.
88    *
89    * @param fResourceBundle The resource bundle to use.
90    * @param msgKey  The message key to use.
91    * @param args      The arguments to be used as replacement text
92    *                  in the message created.
93    *
94    * @return The formatted message string.
95    */
createMsg(ListResourceBundle fResourceBundle, String msgKey, Object args[])96   public static final String createMsg(ListResourceBundle fResourceBundle,
97 	String msgKey, Object args[])  //throws Exception
98   {
99 
100     String fmsg = null;
101     boolean throwex = false;
102     String msg = null;
103 
104     if (msgKey != null)
105       msg = fResourceBundle.getString(msgKey);
106 
107     if (msg == null)
108     {
109       msg = fResourceBundle.getString(BAD_CODE);
110       throwex = true;
111     }
112 
113     if (args != null)
114     {
115       try
116       {
117 
118         // Do this to keep format from crying.
119         // This is better than making a bunch of conditional
120         // code all over the place.
121         int n = args.length;
122 
123         for (int i = 0; i < n; i++)
124         {
125           if (null == args[i])
126             args[i] = "";
127         }
128 
129         fmsg = java.text.MessageFormat.format(msg, args);
130       }
131       catch (Exception e)
132       {
133         fmsg = fResourceBundle.getString(FORMAT_FAILED);
134         fmsg += " " + msg;
135       }
136     }
137     else
138       fmsg = msg;
139 
140     if (throwex)
141     {
142       throw new RuntimeException(fmsg);
143     }
144 
145     return fmsg;
146   }
147 }
148