*
*/
public class Trans {
- ResourceBundle map;
+ private ResourceBundle map;
/**
* Create a translation service with the default language.
*
*/
public class Bundles {
+ static private String confDir = getConfDir();
+
/**
* Return the non-localised bundle of the given name.
*
*/
static public ResourceBundle getBundle(String name) {
return ResourceBundle.getBundle(Bundles.class.getPackage().getName()
- + "." + name, new FixedResourceBundleControl());
+ + "." + name, new FixedResourceBundleControl(confDir));
}
/**
* @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;
+ }
+
}
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;
/**
* 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) {
}
}
} 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...
--- /dev/null
+# 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 =
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;
* 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;
// 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("--")) {
+ "\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;
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));
*
* @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 + "\"");
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);
}
* @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);
}
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());
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();
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;
+ }
}