Resources: now allow "--config" and external .properties files
authorNiki Roo <roo.niki@gmail.com>
Fri, 11 Mar 2016 12:01:18 +0000 (13:01 +0100)
committerNiki Roo <roo.niki@gmail.com>
Fri, 11 Mar 2016 12:01:18 +0000 (13:01 +0100)
src/be/nikiroo/jvcard/i18n/Trans.java
src/be/nikiroo/jvcard/resources/Bundles.java
src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java
src/be/nikiroo/jvcard/resources/jvcard.properties [new file with mode: 0644]
src/be/nikiroo/jvcard/tui/Main.java
src/be/nikiroo/jvcard/tui/panes/FileList.java

index c87a71f8155e06391be6aa22db35698002378c1d..2a4438383378e19a1fa2b8a5daea72842f6ead35 100644 (file)
@@ -22,7 +22,7 @@ import com.googlecode.lanterna.input.KeyStroke;
  * 
  */
 public class Trans {
-       ResourceBundle map;
+       private ResourceBundle map;
 
        /**
         * Create a translation service with the default language.
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;
+       }
+
 }
index e67ace1c485c2d347df069ea6b231cfb5d28cca9..ca3114e4ccf6014ab7a6e7d83e3f304839a8a4f6 100644 (file)
@@ -1,5 +1,7 @@
 package be.nikiroo.jvcard.resources;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -13,16 +15,33 @@ import java.util.ResourceBundle.Control;
 /**
  * Fixed ResourceBundle.Control class. It will use UTF-8 for the files to load.
  * 
+ * Also support an option to first check into the given path before looking into
+ * the resources.
+ * 
  * @author niki
  *
  */
 class FixedResourceBundleControl extends Control {
+       private String outsideWorld = null;
+
+       /**
+        * Create a new {@link FixedResourceBundleControl}.
+        * 
+        * @param outsideWorld
+        *            NULL if you are only interested into the resources, a path to
+        *            first check into it before looking at the actual resources
+        */
+       public FixedResourceBundleControl(String outsideWorld) {
+               this.outsideWorld = outsideWorld;
+       }
+
        public ResourceBundle newBundle(String baseName, Locale locale,
                        String format, ClassLoader loader, boolean reload)
                        throws IllegalAccessException, InstantiationException, IOException {
                // The below is a copy of the default implementation.
                String bundleName = toBundleName(baseName, locale);
                String resourceName = toResourceName(bundleName, "properties");
+
                ResourceBundle bundle = null;
                InputStream stream = null;
                if (reload) {
@@ -35,11 +54,31 @@ class FixedResourceBundleControl extends Control {
                                }
                        }
                } else {
-                       stream = loader.getResourceAsStream(resourceName);
+                       // New code to support outside resources:
+                       if (outsideWorld != null) {
+                               String pkg = this.getClass().getPackage().getName()
+                                               .replaceAll("\\.", File.separator)
+                                               + File.separator;
+
+                               if (resourceName.startsWith(pkg)) {
+                                       try {
+                                               String file = outsideWorld + File.separator
+                                                               + resourceName.substring(pkg.length());
+                                               stream = new FileInputStream(file);
+                                       } catch (Exception e) {
+                                               // file not in priority directory,
+                                               // fallback to default resource
+                                       }
+                               }
+                       }
+
+                       if (stream == null)
+                               stream = loader.getResourceAsStream(resourceName);
+                       //
                }
                if (stream != null) {
                        try {
-                               // Only this line is changed to make it to read properties files
+                               // This line is changed to make it to read properties files
                                // as UTF-8.
                                // How can someone use an archaic encoding such as ISO 8859-1 by
                                // *DEFAULT* is beyond me...
diff --git a/src/be/nikiroo/jvcard/resources/jvcard.properties b/src/be/nikiroo/jvcard/resources/jvcard.properties
new file mode 100644 (file)
index 0000000..30bb786
--- /dev/null
@@ -0,0 +1,7 @@
+# application configuration
+# 
+
+# the directory where to find ".properties" files
+# (this directory will be searched before using the default file)
+# (note that *this* file can be found in the current working directory)
+CONFIG_DIR = 
index 165623454554248377e76027417a60068c79b59e..6f12a2b7dba4c161daa1f6a029ea89c070ee9522 100644 (file)
@@ -9,6 +9,7 @@ import java.util.List;
 
 import be.nikiroo.jvcard.i18n.Trans;
 import be.nikiroo.jvcard.i18n.Trans.StringId;
+import be.nikiroo.jvcard.resources.Bundles;
 import be.nikiroo.jvcard.tui.panes.FileList;
 
 import com.googlecode.lanterna.gui2.Window;
@@ -62,7 +63,8 @@ public class Main {
         * Start the application.
         * 
         * @param args
-        *            the parameters (see --help to know hich are supported)
+        *            the parameters (see <tt>--help</tt> to know which are
+        *            supported)
         */
        public static void main(String[] args) {
                Boolean textMode = null;
@@ -72,9 +74,9 @@ public class Main {
                // get the "system default" language to help translate the --help
                // message if needed
                String language = null;
-               transService = new Trans(null);
+               transService = new Trans(language);
 
-               List<File> files = new LinkedList<File>();
+               List<String> files = new LinkedList<String>();
                for (int index = 0; index < args.length; index++) {
                        String arg = args[index];
                        if (!noMoreParams && arg.equals("--")) {
@@ -89,8 +91,9 @@ public class Main {
                                                                + "\t--tui: force pure text mode even if swing treminal is available\n"
                                                                + "\t--gui: force swing terminal mode\n"
                                                                + "\t--noutf: force non-utf8 mode if you need it\n"
+                                                               + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n"
                                                                + "everyhing else is either a file to open or a directory to open\n"
-                                                               + "(we will only open 1st level files in given directories)");
+                                                               + "(we will only open 1st level files in given directories)\n");
                                return;
                        } else if (!noMoreParams && arg.equals("--tui")) {
                                textMode = true;
@@ -103,6 +106,12 @@ public class Main {
                                if (index < args.length)
                                        language = args[index];
                                transService = new Trans(language);
+                       } else if (!noMoreParams && arg.equals("--config")) {
+                               index++;
+                               if (index < args.length) {
+                                       Bundles.setDirectory(args[index]);
+                                       transService = new Trans(language);
+                               }
                        } else {
                                filesTried = true;
                                files.addAll(open(arg));
@@ -141,18 +150,18 @@ public class Main {
         * 
         * @return the list of opened files
         */
-       static private List<File> open(String path) {
-               List<File> files = new LinkedList<File>();
+       static private List<String> open(String path) {
+               List<String> files = new LinkedList<String>();
 
                File file = new File(path);
                if (file.exists()) {
                        if (file.isDirectory()) {
                                for (File subfile : file.listFiles()) {
                                        if (!subfile.isDirectory())
-                                               files.add(subfile);
+                                               files.add(subfile.getAbsolutePath());
                                }
                        } else {
-                               files.add(file);
+                               files.add(file.getAbsolutePath());
                        }
                } else {
                        System.err.println("File or directory not found: \"" + path + "\"");
index a59311b86819b1a7983eefb25539d037ac12abf3..75f9352cf92f389060276e67c1983b7f811068c3 100644 (file)
@@ -19,10 +19,10 @@ import be.nikiroo.jvcard.tui.UiColors.Element;
 import com.googlecode.lanterna.input.KeyType;
 
 public class FileList extends MainContentList {
-       private List<File> files;
+       private List<String> files;
        private List<Card> cards;
 
-       public FileList(List<File> files) {
+       public FileList(List<String> files) {
                setFiles(files);
        }
 
@@ -32,13 +32,13 @@ public class FileList extends MainContentList {
         * @param files
         *            the new files
         */
-       public void setFiles(List<File> files) {
+       public void setFiles(List<String> files) {
                clearItems();
                this.files = files;
                cards = new ArrayList<Card>();
 
-               for (File file : files) {
-                       addItem(file.getName());
+               for (String file : files) {
+                       addItem(file); // TODO
                        cards.add(null);
                }
 
@@ -67,7 +67,11 @@ public class FileList extends MainContentList {
                if (cards.get(index) != null)
                        count += cards.get(index).size();
 
-               String name = files.get(index).getName();
+               String name = files.get(index).replaceAll("\\\\", "/");
+               int indexSl = name.lastIndexOf('/');
+               if (indexSl >= 0) {
+                       name = name.substring(indexSl + 1);
+               }
 
                name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode());
 
@@ -100,18 +104,10 @@ public class FileList extends MainContentList {
                                if (cards.get(index) != null)
                                        return cards.get(index);
 
-                               File file = files.get(index);
-                               Format format = Format.Abook;
-                               String ext = file.getName();
-                               if (ext.contains(".")) {
-                                       String tab[] = ext.split("\\.");
-                                       if (tab.length > 1
-                                                       && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
-                                               format = Format.VCard21;
-                                       }
-                               }
+                               String file = files.get(index);
+
                                try {
-                                       Card card = new Card(file, format);
+                                       Card card = FileList.getCard(file);
                                        cards.set(index, card);
 
                                        invalidate();
@@ -126,4 +122,25 @@ public class FileList extends MainContentList {
 
                return actions;
        }
+
+       static private Card getCard(String input) throws IOException {
+               Format format = Format.Abook;
+               String ext = input;
+               if (ext.contains(".")) {
+                       String tab[] = ext.split("\\.");
+                       if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
+                               format = Format.VCard21;
+                       }
+               }
+
+               Card card = null;
+               try {
+                       card = new Card(new File(input), format);
+               } catch (IOException ioe) {
+                       ioe.printStackTrace();
+                       throw ioe;
+               }
+
+               return card;
+       }
 }