KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > Config


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch;
25
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import org.hibernate.cfg.*;
30 import org.apache.commons.logging.*;
31
32 /**
33  * Instances of this class maintain general parameters like configuration properties.
34  * @author Alexandre Fenyo
35  * @version $Id: Config.java,v 1.7 2007/03/03 00:38:19 fenyo Exp $
36  */

37
38 public class Config {
39   private static Log log = LogFactory.getLog(Config.class);
40
41   // Configuration properties.
42
private final Properties properties;
43
44   // Localization state variables.
45
private Locale locale = null;
46   private ResourceBundle bundle = null;
47
48   private boolean needEnd = false;
49
50   /**
51    * Constructor.
52    * Reads the configuration properties from the initialization file.
53    * main thread
54    * @param none.
55    * @throws IOException file not found.
56    */

57   public Config() throws IOException {
58     properties = new Properties();
59     properties.loadFromXML(new FileInputStream("config.xml"));
60
61     locale = new Locale(getProperty("language"), getProperty("country"));
62     bundle = ResourceBundle.getBundle("i18n", locale);
63   }
64
65   /**
66    * Declare that the application will exit soon.
67    * main thread
68    * @param none.
69    * @return void.
70    */

71   // main thread
72
public void setEnd() {
73     needEnd = true;
74   }
75
76   /**
77    * Checks the application state.
78    * @param none.
79    * @return boolean application state.
80    */

81   // Background, Capture, Queue, AwtGUI & main threads
82
public boolean isEnd() {
83     return needEnd;
84   }
85
86   /**
87    * Gets a property value.
88    * @param key.
89    * @return String property value.
90    */

91   public String JavaDoc getProperty(final String JavaDoc key) {
92     return properties.getProperty("net.fenyo." + key);
93   }
94
95   /**
96    * Gets a property value.
97    * @param key key.
98    * @param dflt default value.
99    * @return String property value.
100    */

101   public String JavaDoc getProperty(final String JavaDoc key, final String JavaDoc dflt) {
102     return properties.getProperty("net.fenyo." + key, dflt);
103   }
104
105   /**
106    * Returns the locale associated with this configuration.
107    * @param none.
108    * @return Locale locale.
109    */

110   public Locale getLocale() {
111     return locale;
112   }
113
114   /**
115    * Returns the i18n resource bundle associated with this configuration.
116    * @param none.
117    * @return ResourceBundle resource bundle.
118    */

119   public ResourceBundle getBundle() {
120     return bundle;
121   }
122
123   /**
124    * Returns an i18n message.
125    * @param key i18n key.
126    * @return String locale dependant message.
127    */

128   public String JavaDoc getString(final String JavaDoc key) {
129     return bundle.getString(key);
130   }
131
132   /**
133    * Returns an i18n message.
134    * @param key i18n key.
135    * @param params array of arguments to scatter in the i18n locale dependant message.
136    * @return String locale dependant message.
137    */

138   public String JavaDoc getPattern(final String JavaDoc key, final Object JavaDoc [] params) {
139     final MessageFormat formatter = new MessageFormat("");
140     formatter.setLocale(locale);
141     formatter.applyPattern(getString(key));
142     return formatter.format(params);
143   }
144
145   /**
146    * Returns an i18n message.
147    * @param key i18n key.
148    * @param arg argument for locale dependant message.
149    * @return String locale dependant message.
150    */

151   public String JavaDoc getPattern(final String JavaDoc key, final Object JavaDoc arg) {
152     Object JavaDoc [] msgArgs = { arg };
153     return getPattern(key, msgArgs);
154   }
155
156   /**
157    * Returns an i18n message.
158    * @param key i18n key.
159    * @param arg1 argument for locale dependant message.
160    * @param arg2 argument for locale dependant message.
161    * @return String locale dependant message.
162    */

163   public String JavaDoc getPattern(final String JavaDoc key, final Object JavaDoc arg1, final Object JavaDoc arg2) {
164     Object JavaDoc [] msgArgs = { arg1, arg2 };
165     return getPattern(key, msgArgs);
166   }
167
168   /**
169    * Returns an i18n message.
170    * @param key i18n key.
171    * @param arg1 argument for locale dependant message.
172    * @param arg2 argument for locale dependant message.
173    * @param arg3 argument for locale dependant message.
174    * @return String locale dependant message.
175    */

176   public String JavaDoc getPattern(final String JavaDoc key, final Object JavaDoc arg1, final Object JavaDoc arg2, final Object JavaDoc arg3) {
177     Object JavaDoc [] msgArgs = { arg1, arg2, arg3 };
178     return getPattern(key, msgArgs);
179   }
180
181   /**
182    * Returns an i18n message.
183    * @param key i18n key.
184    * @param arg1 argument for locale dependant message.
185    * @param arg2 argument for locale dependant message.
186    * @param arg3 argument for locale dependant message.
187    * @param arg4 argument for locale dependant message.
188    * @return String locale dependant message.
189    */

190   public String JavaDoc getPattern(final String JavaDoc key, final Object JavaDoc arg1, final Object JavaDoc arg2, final Object JavaDoc arg3, final Object JavaDoc arg4) {
191     Object JavaDoc [] msgArgs = { arg1, arg2, arg3, arg4 };
192     return getPattern(key, msgArgs);
193   }
194
195   /**
196    * Returns an i18n message.
197    * @param key i18n key.
198    * @param arg1 argument for locale dependant message.
199    * @param arg2 argument for locale dependant message.
200    * @param arg3 argument for locale dependant message.
201    * @param arg4 argument for locale dependant message.
202    * @param arg5 argument for locale dependant message.
203    * @return String locale dependant message.
204    */

205   public String JavaDoc getPattern(final String JavaDoc key, final Object JavaDoc arg1, final Object JavaDoc arg2,
206          final Object JavaDoc arg3, final Object JavaDoc arg4, final Object JavaDoc arg5) {
207     Object JavaDoc [] msgArgs = { arg1, arg2, arg3, arg4, arg5 };
208     return getPattern(key, msgArgs);
209   }
210 }
211
Popular Tags