Resources: now allow "--config" and external .properties files
[jvcard.git] / src / be / nikiroo / jvcard / resources / Bundles.java
index 76c471f6efddda0ed97a6dddae6e99363a646ca0..945e7f13fec0a73eea71d5080a6f7929bfa5b9c9 100644 (file)
@@ -10,6 +10,8 @@ import java.util.ResourceBundle;
  *
  */
 public class Bundles {
+       static private String confDir = getConfDir();
+
        /**
         * Return the non-localised bundle of the given name.
         * 
@@ -20,7 +22,7 @@ public class Bundles {
         */
        static public ResourceBundle getBundle(String name) {
                return ResourceBundle.getBundle(Bundles.class.getPackage().getName()
-                               + "." + name, new FixedResourceBundleControl());
+                               + "." + name, new FixedResourceBundleControl(confDir));
        }
 
        /**
@@ -34,8 +36,47 @@ public class Bundles {
         * @return the localised bundle
         */
        static public ResourceBundle getBundle(String name, Locale locale) {
-
                return ResourceBundle.getBundle(Bundles.class.getPackage().getName()
-                               + "." + name, locale, new FixedResourceBundleControl());
+                               + "." + name, locale, new FixedResourceBundleControl(confDir));
        }
+
+       /**
+        * Set the primary configuration directory to look for <tt>.properties</tt>
+        * files in.
+        * 
+        * All {@link ResourceBundle}s returned by this class after that point will
+        * respect this new directory.
+        * 
+        * @param confDir
+        *            the new directory
+        */
+       static public void setDirectory(String confDir) {
+               Bundles.confDir = confDir;
+       }
+
+       /**
+        * Return the configuration directory where to try to find the
+        * <tt>.properties</tt> files in priority.
+        * 
+        * @return the configuration directory
+        */
+       static private String getConfDir() {
+               // Do not override user-supplied config directory (see --help)
+               if (Bundles.confDir != null)
+                       return Bundles.confDir;
+
+               try {
+                       ResourceBundle bundle = ResourceBundle.getBundle(Bundles.class
+                                       .getPackage().getName() + "." + "jvcard",
+                                       Locale.getDefault(), new FixedResourceBundleControl(null));
+
+                       String configDir = bundle.getString("CONFIG_DIR");
+                       if (configDir != null && configDir.trim().length() > 0)
+                               return configDir;
+               } catch (Exception e) {
+               }
+
+               return null;
+       }
+
 }