+ public static String substituteHomeDir(String path)
+ {
+ return path.startsWith("~" + File.separator)
+ ? System.getProperty("user.home") + path.substring(1)
+ : path;
+ }
+
+ /*
+ * This method returns the basename of File file
+ */
+ public static String getBasename(File file)
+ {
+ return getBasenameOrExtension(file, false);
+ }
+
+ /*
+ * This method returns the extension of File file.
+ */
+ public static String getExtension(File file)
+ {
+ return getBasenameOrExtension(file, true);
+ }
+
+ public static String getBasenameOrExtension(File file, boolean extension)
+ {
+ if (file == null)
+ return null;
+
+ String value = null;
+ String filename = file.getName();
+ int lastDot = filename.lastIndexOf('.');
+ if (lastDot > 0) // don't truncate if starts with '.'
+ {
+ value = extension ? filename.substring(lastDot + 1)
+ : filename.substring(0, lastDot);
+ }
+ else
+ {
+ value = extension ? "" : filename;
+ }
+ return value;
+ }
+
+ /*
+ * This method returns the dirname of the first --append or --open value.
+ * Used primarily for substitutions in output filenames.
+ */
+ public static String getDirname(File file)
+ {
+ if (file == null)
+ return null;
+
+ String dirname = null;
+ try
+ {
+ File p = file.getParentFile();
+ File d = new File(substituteHomeDir(p.getPath()));
+ dirname = d.getCanonicalPath();
+ } catch (IOException e)
+ {
+ Console.debug(
+ "Exception when getting dirname of '" + file.getPath() + "'",
+ e);
+ dirname = "";
+ }
+ return dirname;
+ }
+}