KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > res > XPATHMessages


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: XPATHMessages.java,v 1.5 2004/02/17 04:36:46 minchau Exp $
18  */

19 package org.apache.xpath.res;
20
21 import java.util.ListResourceBundle JavaDoc;
22
23 import org.apache.xml.res.XMLMessages;
24
25 /**
26  * A utility class for issuing XPath error messages.
27  * @xsl.usage internal
28  */

29 public class XPATHMessages extends XMLMessages
30 {
31   /** The language specific resource object for XPath messages. */
32   private static ListResourceBundle JavaDoc XPATHBundle = null;
33
34   /** The class name of the XPath error message string table. */
35   private static final String JavaDoc XPATH_ERROR_RESOURCES =
36     "org.apache.xpath.res.XPATHErrorResources";
37   
38   /**
39    * Creates a message from the specified key and replacement
40    * arguments, localized to the given locale.
41    *
42    * @param errorCode The key for the message text.
43    * @param args The arguments to be used as replacement text
44    * in the message created.
45    *
46    * @return The formatted message string.
47    */

48   public static final String JavaDoc createXPATHMessage(String JavaDoc msgKey, Object JavaDoc args[]) //throws Exception
49
{
50     if (XPATHBundle == null)
51       XPATHBundle = loadResourceBundle(XPATH_ERROR_RESOURCES);
52     
53     if (XPATHBundle != null)
54     {
55       return createXPATHMsg(XPATHBundle, msgKey, args);
56     }
57     else
58       return "Could not load any resource bundles.";
59   }
60
61   /**
62    * Creates a message from the specified key and replacement
63    * arguments, localized to the given locale.
64    *
65    * @param msgKey The key for the message text.
66    * @param args The arguments to be used as replacement text
67    * in the message created.
68    *
69    * @return The formatted warning string.
70    */

71   public static final String JavaDoc createXPATHWarning(String JavaDoc msgKey, Object JavaDoc args[]) //throws Exception
72
{
73     if (XPATHBundle == null)
74       XPATHBundle = loadResourceBundle(XPATH_ERROR_RESOURCES);
75
76     if (XPATHBundle != null)
77     {
78       return createXPATHMsg(XPATHBundle, msgKey, args);
79     }
80     else
81       return "Could not load any resource bundles.";
82   }
83
84   /**
85    * Creates a message from the specified key and replacement
86    * arguments, localized to the given locale.
87    *
88    * @param errorCode The key for the message text.
89    *
90    * @param fResourceBundle The resource bundle to use.
91    * @param msgKey The message key to use.
92    * @param args The arguments to be used as replacement text
93    * in the message created.
94    *
95    * @return The formatted message string.
96    */

97   public static final String JavaDoc createXPATHMsg(ListResourceBundle JavaDoc fResourceBundle,
98                                             String JavaDoc msgKey, Object JavaDoc args[]) //throws Exception
99
{
100
101     String JavaDoc fmsg = null;
102     boolean throwex = false;
103     String JavaDoc msg = null;
104
105     if (msgKey != null)
106       msg = fResourceBundle.getString(msgKey);
107
108     if (msg == null)
109     {
110       msg = fResourceBundle.getString(XPATHErrorResources.BAD_CODE);
111       throwex = true;
112     }
113
114     if (args != null)
115     {
116       try
117       {
118
119         // Do this to keep format from crying.
120
// This is better than making a bunch of conditional
121
// code all over the place.
122
int n = args.length;
123
124         for (int i = 0; i < n; i++)
125         {
126           if (null == args[i])
127             args[i] = "";
128         }
129
130         fmsg = java.text.MessageFormat.format(msg, args);
131       }
132       catch (Exception JavaDoc e)
133       {
134         fmsg = fResourceBundle.getString(XPATHErrorResources.FORMAT_FAILED);
135         fmsg += " " + msg;
136       }
137     }
138     else
139       fmsg = msg;
140
141     if (throwex)
142     {
143       throw new RuntimeException JavaDoc(fmsg);
144     }
145
146     return fmsg;
147   }
148
149 }
150
Popular Tags