KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > common > MessageLibrary


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.common;
24
25
26 import java.text.MessageFormat JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30
31 /**
32  * This class holds a hash table of error messages. The hash key is the
33  * error code and the value is the message.
34  */

35 public class MessageLibrary
36 {
37
38     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40
41     private static MessageFormat JavaDoc _formatter = null ;
42     private static ResourceBundle JavaDoc _bundle;
43
44     public static void setup (String JavaDoc messageFileName,Locale JavaDoc locale )
45     {
46         try
47         {
48             _bundle = ResourceBundle.getBundle(messageFileName,locale);
49         }
50         catch(Exception JavaDoc ex)
51         {
52             throw new SqlWrapperException ( "Can not load message library file:"+messageFileName, ex);
53         }
54         _formatter = new MessageFormat JavaDoc("");
55         _formatter.setLocale(locale);
56
57     }
58
59     public static String JavaDoc getMessage(String JavaDoc key)
60     {
61         if(_bundle==null)
62             return "No message library available.";
63         try
64         {
65             return key + ": " + _bundle.getString(key);
66         }
67         catch(Exception JavaDoc ex)
68         {
69             return key + ": " + "Message [" + key + "] not found.";
70         }
71     }
72
73     public static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc [] arguments)
74     {
75         String JavaDoc retVal = getMessage ( key );
76
77          _formatter.applyPattern(retVal);
78         retVal = _formatter.format(arguments);
79
80         return retVal ;
81     }
82
83     public static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc argument )
84     {
85         Object JavaDoc [] args = {argument};
86         String JavaDoc retVal = getMessage (key, args);
87         return retVal ;
88     }
89
90     public static String JavaDoc getMessage(String JavaDoc key, Object JavaDoc argument1, Object JavaDoc argument2)
91     {
92         Object JavaDoc [] args = {argument1, argument2};
93         String JavaDoc retVal = getMessage (key, args);
94         return retVal ;
95     }
96
97     public static void main ( String JavaDoc [] args) {
98         MessageLibrary.setup( "org.xquark.extractor.common.resources.MessageLibrary" , new Locale JavaDoc("en","US"));
99         String JavaDoc message ;
100         Object JavaDoc [] as = {"=="} ;
101         message = MessageLibrary.getMessage( "T_N_SUP_OPTR",as);
102         System.out.println(message);
103
104         message = MessageLibrary.getMessage( "T_N_SUP_FUN","contains");
105         System.out.println(message);
106
107         message = MessageLibrary.getMessage( "T_N_SUP_FUN","contains","==");
108         System.out.println(message);
109     }
110
111 }
112
Popular Tags