001: package com.projity.preference;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.util.Locale;
006: import java.util.Properties;
007: import java.util.StringTokenizer;
008:
009: public class ConfigurationFile {
010:
011: private static final String[] OPENPROJ_CONF_DIRS = { ".openproj",
012: "openproj", "OpenProj" };
013: private static File confFile;
014:
015: public static File getConfDir() {
016: if (confFile == null) {
017: String home = System.getProperty("user.home");
018: if (home != null) {
019: File f;
020: for (int i = 0; i < OPENPROJ_CONF_DIRS.length; i++) {
021: f = new File(home + File.separator
022: + OPENPROJ_CONF_DIRS[i]);
023: if (f.isDirectory()) {
024: System.out.println("Conf file " + f.getPath()
025: + " found");
026: confFile = f;
027: return f;
028: }
029: }
030: }
031: }
032: return confFile;
033: }
034:
035: private static final String OPENPROJ_CONF_FILE = "openproj.conf";
036: private static Properties confProps;
037:
038: public static String getProperty(String key) {
039: if (confProps == null) {
040: File confDir = getConfDir();
041: if (confDir == null)
042: return null;
043: File f = new File(confDir, OPENPROJ_CONF_FILE);
044: if (!f.exists())
045: return null;
046: confProps = new Properties();
047: try {
048: FileInputStream in = new FileInputStream(f);
049: confProps.load(in);
050: in.close();
051: } catch (Exception e) {
052: }
053: }
054: return confProps.getProperty(key);
055: }
056:
057: private static Locale locale = null;
058:
059: public static Locale getLocale() {
060: if (locale == null) {
061: String l = getProperty("locale");
062: if (l == null)
063: locale = Locale.getDefault();
064: else {
065: String language = null;
066: String country = null;
067: String variant = null;
068: StringTokenizer st = new StringTokenizer(l, "_-");
069: if (!st.hasMoreTokens())
070: locale = Locale.getDefault();
071: else {
072: language = st.nextToken();
073: if (!st.hasMoreTokens())
074: locale = new Locale(language);
075: else {
076: country = st.nextToken();
077: if (!st.hasMoreTokens())
078: locale = new Locale(language, country);
079: else {
080: variant = st.nextToken();
081: locale = new Locale(language, country,
082: variant);
083: }
084:
085: }
086:
087: }
088:
089: }
090: }
091: return locale;
092: }
093:
094: private static final String OPENPROJ_RUN_CONF_FILE = "run.conf";
095: private static Properties runProps;
096:
097: public static String getRunProperty(String key) {
098: if (runProps == null) {
099: File confDir = getConfDir();
100: if (confDir == null)
101: return null;
102: File f = new File(confDir, OPENPROJ_RUN_CONF_FILE);
103: if (!f.exists())
104: return null;
105: runProps = new Properties();
106: try {
107: FileInputStream in = new FileInputStream(f);
108: runProps.load(in);
109: in.close();
110: } catch (Exception e) {
111: }
112: }
113: return runProps.getProperty(key);
114: }
115:
116: }
|