001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch;
024:
025: import java.io.*;
026: import java.util.*;
027: import java.text.*;
028: import org.hibernate.cfg.*;
029: import org.apache.commons.logging.*;
030:
031: /**
032: * Instances of this class maintain general parameters like configuration properties.
033: * @author Alexandre Fenyo
034: * @version $Id: Config.java,v 1.7 2007/03/03 00:38:19 fenyo Exp $
035: */
036:
037: public class Config {
038: private static Log log = LogFactory.getLog(Config.class);
039:
040: // Configuration properties.
041: private final Properties properties;
042:
043: // Localization state variables.
044: private Locale locale = null;
045: private ResourceBundle bundle = null;
046:
047: private boolean needEnd = false;
048:
049: /**
050: * Constructor.
051: * Reads the configuration properties from the initialization file.
052: * main thread
053: * @param none.
054: * @throws IOException file not found.
055: */
056: public Config() throws IOException {
057: properties = new Properties();
058: properties.loadFromXML(new FileInputStream("config.xml"));
059:
060: locale = new Locale(getProperty("language"),
061: getProperty("country"));
062: bundle = ResourceBundle.getBundle("i18n", locale);
063: }
064:
065: /**
066: * Declare that the application will exit soon.
067: * main thread
068: * @param none.
069: * @return void.
070: */
071: // main thread
072: public void setEnd() {
073: needEnd = true;
074: }
075:
076: /**
077: * Checks the application state.
078: * @param none.
079: * @return boolean application state.
080: */
081: // Background, Capture, Queue, AwtGUI & main threads
082: public boolean isEnd() {
083: return needEnd;
084: }
085:
086: /**
087: * Gets a property value.
088: * @param key.
089: * @return String property value.
090: */
091: public String getProperty(final String key) {
092: return properties.getProperty("net.fenyo." + key);
093: }
094:
095: /**
096: * Gets a property value.
097: * @param key key.
098: * @param dflt default value.
099: * @return String property value.
100: */
101: public String getProperty(final String key, final String 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 getString(final String 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 getPattern(final String key, final Object[] 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 getPattern(final String key, final Object arg) {
152: Object[] 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 getPattern(final String key, final Object arg1,
164: final Object arg2) {
165: Object[] msgArgs = { arg1, arg2 };
166: return getPattern(key, msgArgs);
167: }
168:
169: /**
170: * Returns an i18n message.
171: * @param key i18n key.
172: * @param arg1 argument for locale dependant message.
173: * @param arg2 argument for locale dependant message.
174: * @param arg3 argument for locale dependant message.
175: * @return String locale dependant message.
176: */
177: public String getPattern(final String key, final Object arg1,
178: final Object arg2, final Object arg3) {
179: Object[] msgArgs = { arg1, arg2, arg3 };
180: return getPattern(key, msgArgs);
181: }
182:
183: /**
184: * Returns an i18n message.
185: * @param key i18n key.
186: * @param arg1 argument for locale dependant message.
187: * @param arg2 argument for locale dependant message.
188: * @param arg3 argument for locale dependant message.
189: * @param arg4 argument for locale dependant message.
190: * @return String locale dependant message.
191: */
192: public String getPattern(final String key, final Object arg1,
193: final Object arg2, final Object arg3, final Object arg4) {
194: Object[] msgArgs = { arg1, arg2, arg3, arg4 };
195: return getPattern(key, msgArgs);
196: }
197:
198: /**
199: * Returns an i18n message.
200: * @param key i18n key.
201: * @param arg1 argument for locale dependant message.
202: * @param arg2 argument for locale dependant message.
203: * @param arg3 argument for locale dependant message.
204: * @param arg4 argument for locale dependant message.
205: * @param arg5 argument for locale dependant message.
206: * @return String locale dependant message.
207: */
208: public String getPattern(final String key, final Object arg1,
209: final Object arg2, final Object arg3, final Object arg4,
210: final Object arg5) {
211: Object[] msgArgs = { arg1, arg2, arg3, arg4, arg5 };
212: return getPattern(key, msgArgs);
213: }
214: }
|