Improved wildcard feature in probe command (#1 thx Kepon).

This commit is contained in:
Christian P. MOMON 2021-12-20 01:40:36 +01:00
parent 94916ef5d1
commit 5fd94e6fbd
3 changed files with 57 additions and 15 deletions

View file

@ -33,6 +33,7 @@
<li>@setop</li>
<li>Jérémy Collot</li>
<li>Pilou</li>
<li>Kepon</li>
</ul>
</body>
</html>

View file

@ -56,7 +56,7 @@ public class Files extends ArrayList<File>
* 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<File>
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<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (!pattern.matcher(file.getAbsolutePath()).matches())
{
iterator.remove();
}
}
result = this;
//
return result;
}
/**
* Keep directories.
*

View file

@ -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))
{
result.add(file);
}
}
ended = true;
parent = current;
}
else
{
File file = new File(source);
result.add(file);
if (StringUtils.containsAny(current.getAbsolutePath(), '?', '*'))
{
current = current.getParentFile();
}
else
{
ended = true;
parent = current;
}
}
}
//
System.out.println("parent=" + parent);
String regex = source.replace(".", "\\.").replace("?", ".").replace("*", ".*");
result = listRecursively(parent).keepPath(regex);
}
//