From 7f82bf682ab5747b2d4ccdccae9ea3fcdb013cee Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Fri, 11 Mar 2016 13:01:18 +0100 Subject: [PATCH] Resources: now allow "--config" and external .properties files --- src/be/nikiroo/jvcard/i18n/Trans.java | 2 +- src/be/nikiroo/jvcard/resources/Bundles.java | 47 +++++++++++++++-- .../resources/FixedResourceBundleControl.java | 43 +++++++++++++++- .../jvcard/resources/jvcard.properties | 7 +++ src/be/nikiroo/jvcard/tui/Main.java | 25 ++++++--- src/be/nikiroo/jvcard/tui/panes/FileList.java | 51 ++++++++++++------- 6 files changed, 144 insertions(+), 31 deletions(-) create mode 100644 src/be/nikiroo/jvcard/resources/jvcard.properties diff --git a/src/be/nikiroo/jvcard/i18n/Trans.java b/src/be/nikiroo/jvcard/i18n/Trans.java index c87a71f..2a44383 100644 --- a/src/be/nikiroo/jvcard/i18n/Trans.java +++ b/src/be/nikiroo/jvcard/i18n/Trans.java @@ -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. diff --git a/src/be/nikiroo/jvcard/resources/Bundles.java b/src/be/nikiroo/jvcard/resources/Bundles.java index 76c471f..945e7f1 100644 --- a/src/be/nikiroo/jvcard/resources/Bundles.java +++ b/src/be/nikiroo/jvcard/resources/Bundles.java @@ -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 .properties + * 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 + * .properties 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; + } + } diff --git a/src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java b/src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java index e67ace1..ca3114e 100644 --- a/src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java +++ b/src/be/nikiroo/jvcard/resources/FixedResourceBundleControl.java @@ -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 index 0000000..30bb786 --- /dev/null +++ b/src/be/nikiroo/jvcard/resources/jvcard.properties @@ -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 = diff --git a/src/be/nikiroo/jvcard/tui/Main.java b/src/be/nikiroo/jvcard/tui/Main.java index 1656234..6f12a2b 100644 --- a/src/be/nikiroo/jvcard/tui/Main.java +++ b/src/be/nikiroo/jvcard/tui/Main.java @@ -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 --help 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 files = new LinkedList(); + List files = new LinkedList(); 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 open(String path) { - List files = new LinkedList(); + static private List open(String path) { + List files = new LinkedList(); 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 + "\""); diff --git a/src/be/nikiroo/jvcard/tui/panes/FileList.java b/src/be/nikiroo/jvcard/tui/panes/FileList.java index a59311b..75f9352 100644 --- a/src/be/nikiroo/jvcard/tui/panes/FileList.java +++ b/src/be/nikiroo/jvcard/tui/panes/FileList.java @@ -19,10 +19,10 @@ import be.nikiroo.jvcard.tui.UiColors.Element; import com.googlecode.lanterna.input.KeyType; public class FileList extends MainContentList { - private List files; + private List files; private List cards; - public FileList(List files) { + public FileList(List files) { setFiles(files); } @@ -32,13 +32,13 @@ public class FileList extends MainContentList { * @param files * the new files */ - public void setFiles(List files) { + public void setFiles(List files) { clearItems(); this.files = files; cards = new ArrayList(); - 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; + } } -- 2.27.0