Improved wildcard feature in probe command (#1 thx Kepon).
This commit is contained in:
parent
94916ef5d1
commit
5fd94e6fbd
3 changed files with 57 additions and 15 deletions
|
@ -33,6 +33,7 @@
|
|||
<li>@setop</li>
|
||||
<li>Jérémy Collot</li>
|
||||
<li>Pilou</li>
|
||||
<li>Kepon</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue