X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Futil%2FFileUtils.java;h=e7d274c117fcb01c12901dedcfc728889c568d5d;hb=3fdc889794e3566af57628f0b6a308eb23886f96;hp=b6f4e907ad8bd7900ef18bbf97da80b1027f6946;hpb=ad47e73ed0aaa1a24a976802084c3912422f2be7;p=jalview.git
diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java
index b6f4e90..e7d274c 100644
--- a/src/jalview/util/FileUtils.java
+++ b/src/jalview/util/FileUtils.java
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.util;
import java.io.File;
@@ -12,6 +32,7 @@ import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Collectors;
@@ -35,6 +56,8 @@ public class FileUtils
boolean allowSingleFilenameThatDoesNotExist)
{
pattern = substituteHomeDir(pattern);
+ String relativePattern = pattern.startsWith(File.separator) ? null
+ : pattern;
List files = new ArrayList<>();
/*
* For efficiency of the Files.walkFileTree(), let's find the longest path that doesn't need globbing.
@@ -59,7 +82,11 @@ public class FileUtils
{
String pS = pattern.substring(0, lastFS + 1);
String rest = pattern.substring(lastFS + 1);
- Path parentDir = Paths.get(pS).toAbsolutePath();
+ if ("".equals(pS))
+ {
+ pS = ".";
+ }
+ Path parentDir = Paths.get(pS);
if (parentDir.toFile().exists())
{
try
@@ -67,10 +94,11 @@ public class FileUtils
String glob = "glob:" + parentDir.toString() + File.separator
+ rest;
PathMatcher pm = FileSystems.getDefault().getPathMatcher(glob);
- int maxDepth = rest.contains("**") ? Integer.MAX_VALUE
+ int maxDepth = rest.contains("**") ? 1028
: (int) (rest.chars()
.filter(ch -> ch == File.separatorChar).count())
+ 1;
+
Files.walkFileTree(parentDir,
EnumSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth,
new SimpleFileVisitor()
@@ -108,6 +136,7 @@ public class FileUtils
files.add(f);
}
}
+ Collections.sort(files);
return files;
}
@@ -127,30 +156,43 @@ public class FileUtils
}
/*
- * This method returns the basename of the first --open or --opennew value.
- * Used primarily for substitutions in output filenames.
+ * 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 basename = null;
+ String value = null;
String filename = file.getName();
int lastDot = filename.lastIndexOf('.');
if (lastDot > 0) // don't truncate if starts with '.'
{
- basename = filename.substring(0, lastDot);
+ value = extension ? filename.substring(lastDot + 1)
+ : filename.substring(0, lastDot);
}
else
{
- basename = filename;
+ value = extension ? "" : filename;
}
- return basename;
+ return value;
}
/*
- * This method returns the dirname of the first --open or --opennew 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)
@@ -159,16 +201,94 @@ public class FileUtils
return null;
String dirname = null;
- try
- {
- dirname = file.getParentFile().getCanonicalPath();
- } catch (IOException e)
+ File p = file.getParentFile();
+ if (p == null)
{
- Console.debug(
- "Exception when getting dirname of '" + file.getPath() + "'",
- e);
- dirname = "";
+ p = new File(".");
}
+ File d = new File(substituteHomeDir(p.getPath()));
+ dirname = d.getPath();
return dirname;
}
+
+ public static String convertWildcardsToPath(String value, String wildcard,
+ String dirname, String basename)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+ StringBuilder path = new StringBuilder();
+ int lastFileSeparatorIndex = value.lastIndexOf(File.separatorChar);
+ int wildcardBeforeIndex = value.indexOf(wildcard);
+ if (lastFileSeparatorIndex > wildcard.length() - 1
+ && wildcardBeforeIndex < lastFileSeparatorIndex)
+ {
+ path.append(value.substring(0, wildcardBeforeIndex));
+ path.append(dirname);
+ path.append(value.substring(wildcardBeforeIndex + wildcard.length(),
+ lastFileSeparatorIndex + 1));
+ }
+ else
+ {
+ path.append(value.substring(0, lastFileSeparatorIndex + 1));
+ }
+ int wildcardAfterIndex = value.indexOf(wildcard,
+ lastFileSeparatorIndex);
+ if (wildcardAfterIndex > lastFileSeparatorIndex)
+ {
+ path.append(value.substring(lastFileSeparatorIndex + 1,
+ wildcardAfterIndex));
+ path.append(basename);
+ path.append(value.substring(wildcardAfterIndex + wildcard.length()));
+ }
+ else
+ {
+ path.append(value.substring(lastFileSeparatorIndex + 1));
+ }
+ return path.toString();
+ }
+
+ public static File getParentDir(File file)
+ {
+ if (file == null)
+ {
+ return null;
+ }
+ File parentDir = file.getAbsoluteFile().getParentFile();
+ return parentDir;
+ }
+
+ public static boolean checkParentDir(File file, boolean mkdirs)
+ {
+ if (file == null)
+ {
+ return false;
+ }
+ File parentDir = getParentDir(file);
+ if (parentDir.exists())
+ {
+ // already exists, nothing to do so nothing to worry about!
+ return true;
+ }
+
+ if (!mkdirs)
+ {
+ return false;
+ }
+
+ Path path = file.toPath();
+ for (int i = 0; i < path.getNameCount(); i++)
+ {
+ Path p = path.getName(i);
+ if ("..".equals(p.toString()))
+ {
+ Console.warn("Cautiously not running mkdirs on " + file.toString()
+ + " because the path to be made contains '..'");
+ return false;
+ }
+ }
+
+ return parentDir.mkdirs();
+ }
}