public static File[] glob(String filePattern) throws IOException { Set<File> fileSet = new TreeSet<>(); glob(filePattern, fileSet); return fileSet.toArray(new File[fileSet.size()]); }
public static void glob(String filePattern, Set<File> fileSet) throws IOException { final File patternFile = new File(filePattern); if (patternFile.exists()) { fileSet.add(patternFile.getCanonicalFile()); return; } boolean windowsOs = isWindowsOs(); String[] patternSplit = splitBasePath(filePattern, windowsOs); String basePath = patternSplit[0]; String patternPath = patternSplit[1]; if (patternPath.isEmpty()) { // no pattern given, but no file exist return; } File canonicalBaseFile = new File(basePath).getCanonicalFile(); String newpattern = canonicalBaseFile.getPath() + "/" + patternPath; WildcardMatcher matcher = new WildcardMatcher(newpattern); HashSet<File> visitedDirs = new HashSet<>(); collectFiles(matcher, canonicalBaseFile, fileSet, visitedDirs); }
private static void collectFiles(WildcardMatcher matcher, File dir, Set<File> fileSet, Set<File> visitedDirs) throws IOException { if (visitedDirs.contains(dir.getCanonicalFile())) { return; } else { visitedDirs.add(dir.getCanonicalFile()); } File[] files = dir.listFiles(); if (files == null) { throw new IOException(String.format("Failed to access directory '%s'", dir)); } for (File file : files) { // check for both to catch symlinks as well if (matcher.matches(file.getCanonicalPath()) || matcher.matches(file.getPath())) { fileSet.add(file.getCanonicalFile()); } if (file.isDirectory()) { collectFiles(matcher, file, fileSet, visitedDirs); } } }
@Test public void testMatchesAntExamplePattern3() throws Exception { WildcardMatcher m = new WildcardMatcher("org/apache/**/CVS/*"); assertEquals(m.isWindowsFs() ? "org/apache((/.*/)?|/)cvs/[^/:]*" : "org/apache((/.*/)?|/)CVS/[^/:]*", m.getRegex()); assertTrue(m.matches("org/apache/CVS/Entries")); assertTrue(m.matches("org/apache/jakarta/tools/ant/CVS/Entries")); assertFalse(m.matches("org/apache/CVS/foo/bar/Entries")); }
@Test public void testMatchesStarAtEnd() throws Exception { WildcardMatcher m = new WildcardMatcher("foo/*"); assertEquals("foo/[^/:]*", m.getRegex()); assertTrue(m.matches("foo/test.txt")); assertTrue(m.matches("foo/bar")); assertFalse(m.matches("foo")); assertFalse(m.matches("foo/bar/test.txt")); assertFalse(m.matches("/foo/")); assertFalse(m.matches("foo/bar/")); assertFalse(m.matches("foo/bar/doz/gna/test.txt")); }
private boolean nameMatchesGroupPath(String name, String[] groupPath) { for (String group : groupPath) { if (wildcardMap.containsKey(group)) { return wildcardMap.get(group).matches(name); } if (!name.contains(group)) { return false; } } return true; }
private AutoGroupingImpl(String[][] paths) { this.paths = paths.clone(); this.indexes = new Index[paths.length]; this.wildcardMap = new HashMap<>(); for (int i = 0; i < paths.length; i++) { String[] path = paths[i]; String entry = path.length > 0 ? path[0] : ""; indexes[i] = new Index(entry, i); for (String pathEntry : path) { if (pathEntry.contains("*") || pathEntry.contains("?")) { wildcardMap.put(pathEntry, new WildcardMatcher(pathEntry)); } } } Arrays.sort(indexes, new Comparator<Index>() { @Override public int compare(Index o1, Index o2) { if (o1.entry.length() != o2.entry.length()) { return o2.entry.length() - o1.entry.length(); } return o2.entry.compareTo(o1.entry); } }); }
@Test public void testSplitBasePath() throws Exception { assertArrayEquals(new String[]{"test.N1", ""}, WildcardMatcher.splitBasePath("test.N1", false)); assertArrayEquals(new String[]{"", "te?t.N1"}, WildcardMatcher.splitBasePath("te?t.N1", false)); assertArrayEquals(new String[]{"/home/norman/meris/data.nc", ""}, WildcardMatcher.splitBasePath("/home/norman/meris/data.nc", false)); assertArrayEquals(new String[]{"C:/Users/Norman/MERIS/data.nc", ""}, WildcardMatcher.splitBasePath("C:\\Users\\Norman\\MERIS\\data.nc", true)); assertArrayEquals(new String[]{"foo/", "*"}, WildcardMatcher.splitBasePath("foo/*", false)); assertArrayEquals(new String[]{"foo/", "*/test.txt"}, WildcardMatcher.splitBasePath("foo/*/test.txt", false)); assertArrayEquals(new String[]{"/foo/", "*/test.txt"}, WildcardMatcher.splitBasePath("/foo/*/test.txt", false)); assertArrayEquals(new String[]{"", "**/CVS/*"}, WildcardMatcher.splitBasePath("**/CVS/*", false)); } }
static String[] splitBasePath(String filePattern, boolean iswindows) { if (iswindows) { filePattern = filePattern.replace("\\", "/"); } String basePath = filePattern.startsWith("/") ? "/" : ""; String[] parts = filePattern.split("/"); int firstPatternIndex = 0; for (int i = 0; i < parts.length && !containsWildcardChar(parts[i]); i++) { if (!parts[i].isEmpty()) { basePath += parts[i]; if (i < parts.length - 1) { basePath += "/"; } firstPatternIndex = i + 1; } } String patterPath = ""; for (int i = firstPatternIndex; i < parts.length ; i++) { patterPath += parts[i]; if (i < parts.length - 1) { patterPath += "/"; } } return new String[] {basePath, patterPath}; }
@Test public void testMatchesSingleFileNoWildcardUsed() throws Exception { WildcardMatcher m = new WildcardMatcher("test.N1"); assertEquals(m.isWindowsFs() ? "test\\.n1" : "test\\.N1", m.getRegex()); assertTrue(m.matches("test.N1")); assertFalse(m.matches("test.jpg")); assertFalse(m.matches("rest.N1")); assertFalse(m.matches("x/test.N1")); }
@Test public void testMatchesDoubleStarInBetween() throws Exception { WildcardMatcher m = new WildcardMatcher("foo/**/test.txt"); assertEquals("foo((/.*/)?|/)test\\.txt", m.getRegex()); assertTrue(m.matches("foo/test.txt")); assertTrue(m.matches("foo/bar/test.txt")); assertTrue(m.matches("foo/bar/doz/test.txt")); assertTrue(m.matches("foo/bar/doz/gna/test.txt")); assertFalse(m.matches("/foo/test.txt")); assertFalse(m.matches("foo/bar/doz/gna/test.zip")); }
public static Set<File> getSourceProductFileSet(String[] sourceProductPaths, Logger logger) { Set<File> sourceProductFileSet = new TreeSet<>(); String[] paths = trimSourceProductPaths(sourceProductPaths); if (paths != null && paths.length != 0) { for (String path : paths) { try { WildcardMatcher.glob(path, sourceProductFileSet); } catch (IOException e) { logger.severe("I/O problem occurred while scanning source product files: " + e.getMessage()); } } if (sourceProductFileSet.isEmpty()) { logger.log(Level.WARNING, "No valid source product path found."); } } return sourceProductFileSet; }
@Test public void testMatchesQuoteInFilename() throws Exception { WildcardMatcher m = new WildcardMatcher("te?t.N1"); assertEquals(m.isWindowsFs() ? "te.t\\.n1" : "te.t\\.N1", m.getRegex()); assertTrue(m.matches("test.N1")); assertTrue(m.matches("te?t.N1")); assertFalse(m.matches("tet.N1")); }
@Test public void testMatchesAntExamplePattern4() throws Exception { WildcardMatcher m = new WildcardMatcher("**/test/**"); assertEquals("(.*/)?test(/.*)?", m.getRegex()); assertTrue(m.matches("test")); assertTrue(m.matches("test/java")); assertTrue(m.matches("src/test/java")); assertTrue(m.matches("src/test")); assertFalse(m.matches("src/main/java")); }
private File[] getProductsToLoad() { SortedSet<File> fileSet = new TreeSet<File>(); if (sourceProductPaths != null) { for (String filePattern : sourceProductPaths) { if (filePattern == null) { continue; } try { WildcardMatcher.glob(filePattern, fileSet); } catch (IOException e) { logReadProductError(filePattern); } } } return fileSet.toArray(new File[fileSet.size()]); }
@Test public void testMatchesStarInFilename() throws Exception { WildcardMatcher m = new WildcardMatcher("*.N1"); assertEquals(m.isWindowsFs() ? "[^/:]*\\.n1" : "[^/:]*\\.N1", m.getRegex()); assertTrue(m.matches("test.N1")); assertTrue(m.matches("MER_RR.N1")); assertFalse(m.matches("MER_RR")); assertFalse(m.matches("MER_RR.txt")); assertFalse(m.matches("data/MER_RR.N1")); }
@Test public void testMatchesMultiDirectoryNoWildcardUsed_Unix() throws Exception { WildcardMatcher m = new WildcardMatcher("/home/norman/meris/data.nc", false); assertEquals("/home/norman/meris/data\\.nc", m.getRegex()); assertTrue(m.matches("/home/norman/meris/data.nc")); assertFalse(m.matches("home/norman/meris/data.nc")); assertFalse(m.matches("/home/norman/MERIS/data.nc")); assertFalse(m.matches("/home/norman/meris/data.NC")); }
@Override protected Product doInBackground() throws Exception { for (String inputPath : inputPaths) { if (inputPath == null || inputPath.trim().length() == 0) { continue; } try { final TreeSet<File> fileSet = new TreeSet<>(); WildcardMatcher.glob(inputPath, fileSet); for (File file : fileSet) { final Product product = ProductIO.readProduct(file); if (product != null) { return product; } } } catch (IOException e) { Logger logger = BeamLogManager.getSystemLogger(); logger.severe("I/O problem occurred while scanning source product files: " + e.getMessage()); } } return null; }
@Test public void testMatchesAntExamplePattern1() throws Exception { WildcardMatcher m = new WildcardMatcher("**/CVS/*"); assertEquals(m.isWindowsFs() ? "(.*/)?cvs/[^/:]*" : "(.*/)?CVS/[^/:]*", m.getRegex()); assertTrue(m.matches("CVS/Repository")); assertTrue(m.matches("org/apache/CVS/Entries")); assertTrue(m.matches("org/apache/jakarta/tools/ant/CVS/Entries")); assertFalse(m.matches("org/apache/CVS/foo/bar/Entries")); }
@Test public void testMatchesStarInBetween() throws Exception { WildcardMatcher m = new WildcardMatcher("foo/*/test.txt"); assertEquals("foo/[^/:]*/test\\.txt", m.getRegex()); assertTrue(m.matches("foo//test.txt")); assertTrue(m.matches("foo/bar/test.txt")); assertFalse(m.matches("/foo/test.txt")); assertFalse(m.matches("foo/bar/doz/gna/test.txt")); }