From 5fd94e6fbd3f46a9721c672bdabb2154b60c9106 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 20 Dec 2021 01:40:36 +0100 Subject: [PATCH] Improved wildcard feature in probe command (#1 thx Kepon). --- .../devinsy/statoolinfos/htmlize/about.xhtml | 1 + src/fr/devinsy/statoolinfos/util/Files.java | 32 ++++++++++++++- .../devinsy/statoolinfos/util/FilesUtils.java | 39 ++++++++++++------- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/htmlize/about.xhtml b/src/fr/devinsy/statoolinfos/htmlize/about.xhtml index 7d3d735..67bde0f 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/about.xhtml +++ b/src/fr/devinsy/statoolinfos/htmlize/about.xhtml @@ -33,6 +33,7 @@
  • @setop
  • Jérémy Collot
  • Pilou
  • +
  • Kepon
  • diff --git a/src/fr/devinsy/statoolinfos/util/Files.java b/src/fr/devinsy/statoolinfos/util/Files.java index 757b1ac..7f793ec 100644 --- a/src/fr/devinsy/statoolinfos/util/Files.java +++ b/src/fr/devinsy/statoolinfos/util/Files.java @@ -56,7 +56,7 @@ public class Files extends ArrayList * the regex * @return the files */ - public Files keep(final String regex) + public Files keepName(final String regex) { Files result; @@ -79,6 +79,36 @@ public class Files extends ArrayList return result; } + /** + * Keep by path. + * + * @param regex + * the regex + * @return the files + */ + public Files keepPath(final String regex) + { + Files result; + + Pattern pattern = Pattern.compile(regex); + + Iterator iterator = iterator(); + while (iterator.hasNext()) + { + File file = iterator.next(); + + if (!pattern.matcher(file.getAbsolutePath()).matches()) + { + iterator.remove(); + } + } + + result = this; + + // + return result; + } + /** * Keep directories. * diff --git a/src/fr/devinsy/statoolinfos/util/FilesUtils.java b/src/fr/devinsy/statoolinfos/util/FilesUtils.java index 90a5c72..771334c 100644 --- a/src/fr/devinsy/statoolinfos/util/FilesUtils.java +++ b/src/fr/devinsy/statoolinfos/util/FilesUtils.java @@ -21,7 +21,6 @@ package fr.devinsy.statoolinfos.util; import java.io.File; import java.util.regex.Pattern; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; /** @@ -49,7 +48,7 @@ public class FilesUtils Files result; result = new Files(); - if ((source != null) && (source.exists())) + if ((source != null) && (source.exists()) && (source.canRead())) { if (source.isFile()) { @@ -109,7 +108,7 @@ public class FilesUtils * Search by wildcard. * * @param source - * the source + * the source. Example: /var/log/apache2/www.*.org/*access?? * @return the files */ public static Files searchByWildcard(final String source) @@ -120,23 +119,35 @@ public class FilesUtils if (!StringUtils.isBlank(source)) { - if (source.endsWith("*")) + // Get the first parent without wildcard. + File parent = null; + File current = new File(source); + boolean ended = false; + while (!ended) { - String prefix = source.substring(0, source.length() - 1); - String shortPrefix = FilenameUtils.getName(prefix); - for (File file : new File(prefix).getParentFile().listFiles()) + if (current.getParentFile().equals(current)) { - if (file.getName().startsWith(shortPrefix)) + ended = true; + parent = current; + } + else + { + if (StringUtils.containsAny(current.getAbsolutePath(), '?', '*')) { - result.add(file); + current = current.getParentFile(); + } + else + { + ended = true; + parent = current; } } } - else - { - File file = new File(source); - result.add(file); - } + + // + System.out.println("parent=" + parent); + String regex = source.replace(".", "\\.").replace("?", ".").replace("*", ".*"); + result = listRecursively(parent).keepPath(regex); } //