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>@setop</li>
<li>Jérémy Collot</li> <li>Jérémy Collot</li>
<li>Pilou</li> <li>Pilou</li>
<li>Kepon</li>
</ul> </ul>
</body> </body>
</html> </html>

View file

@ -56,7 +56,7 @@ public class Files extends ArrayList<File>
* the regex * the regex
* @return the files * @return the files
*/ */
public Files keep(final String regex) public Files keepName(final String regex)
{ {
Files result; Files result;
@ -79,6 +79,36 @@ public class Files extends ArrayList<File>
return result; 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. * Keep directories.
* *

View file

@ -21,7 +21,6 @@ package fr.devinsy.statoolinfos.util;
import java.io.File; import java.io.File;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
/** /**
@ -49,7 +48,7 @@ public class FilesUtils
Files result; Files result;
result = new Files(); result = new Files();
if ((source != null) && (source.exists())) if ((source != null) && (source.exists()) && (source.canRead()))
{ {
if (source.isFile()) if (source.isFile())
{ {
@ -109,7 +108,7 @@ public class FilesUtils
* Search by wildcard. * Search by wildcard.
* *
* @param source * @param source
* the source * the source. Example: /var/log/apache2/www.*.org/*access??
* @return the files * @return the files
*/ */
public static Files searchByWildcard(final String source) public static Files searchByWildcard(final String source)
@ -120,23 +119,35 @@ public class FilesUtils
if (!StringUtils.isBlank(source)) 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); if (current.getParentFile().equals(current))
String shortPrefix = FilenameUtils.getName(prefix);
for (File file : new File(prefix).getParentFile().listFiles())
{ {
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); System.out.println("parent=" + parent);
result.add(file); String regex = source.replace(".", "\\.").replace("?", ".").replace("*", ".*");
} result = listRecursively(parent).keepPath(regex);
} }
// //