Created
March 19, 2018 10:00
-
-
Save kalgon/c4b6f9261f357342c42280769a09c913 to your computer and use it in GitHub Desktop.
Example of globs in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Glob { | |
private static final Pattern PATTERN = Pattern.compile("\\.|\\?|\\*+"); | |
public static Pattern toPattern(String glob, int flags) { | |
return Pattern.compile(toRegex(glob), flags); | |
} | |
public static Pattern toPattern(String glob) { | |
return toPattern(glob, 0); | |
} | |
public static String toRegex(String glob) { | |
StringBuffer buffer = new StringBuffer("^"); | |
Matcher matcher = PATTERN.matcher(glob.trim()); | |
while (matcher.find()) { | |
matcher.appendReplacement(buffer, replace(matcher.group())); | |
} | |
return matcher.appendTail(buffer).append("$").toString(); | |
} | |
private static String replace(String replaced) { | |
switch (replaced) { | |
case ".": | |
return "\\\\."; | |
case "?": | |
return "[^/]"; | |
case "*": | |
return "[^/]*"; | |
default: | |
return ".*"; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.UncheckedIOException; | |
import java.util.function.Consumer; | |
public interface IOConsumer<T> { | |
void accept(T target) throws IOException; | |
default Consumer<T> asConsumer() { | |
return target -> { | |
try { | |
accept(target); | |
} catch (IOException ioException) { | |
throw new UncheckedIOException(ioException); | |
} | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.FileVisitResult; | |
import java.nio.file.FileVisitor; | |
import java.nio.file.Path; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import static java.nio.file.FileVisitResult.CONTINUE; | |
import static java.nio.file.FileVisitResult.SKIP_SUBTREE; | |
public class MatchingFileVisitor implements FileVisitor<Path> { | |
private final Pattern pattern; | |
private final IOConsumer<? super Path> consumer; | |
public MatchingFileVisitor(Pattern pattern, IOConsumer<? super Path> consumer) { | |
this.pattern = pattern; | |
this.consumer = consumer; | |
} | |
@Override | |
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attributes) { | |
Matcher matcher = pattern.matcher(dir.toUri().getPath()); | |
return !matcher.matches() && matcher.hitEnd() ? CONTINUE : SKIP_SUBTREE; | |
} | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { | |
if (pattern.matcher(file.toUri().getPath()).matches()) { | |
consumer.accept(file); | |
} | |
return CONTINUE; | |
} | |
@Override | |
public FileVisitResult visitFileFailed(Path path, IOException exception) { | |
return CONTINUE; | |
} | |
@Override | |
public FileVisitResult postVisitDirectory(Path dir, IOException exception) { | |
return CONTINUE; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.FileSystems; | |
import java.nio.file.FileVisitor; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.regex.Pattern; | |
public class Test { | |
public static void main(String... args) { | |
Pattern pattern = Glob.toPattern("/D:/tmp/**/*.TXT"); | |
FileVisitor<Path> visitor = new MatchingFileVisitor(pattern, path -> System.out.println(path.toUri())); | |
IOConsumer<Path> ioConsumer = path -> Files.walkFileTree(path, visitor); | |
FileSystems.getDefault().getRootDirectories().forEach(ioConsumer.asConsumer()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment