public PatternFileRef(File file) { File dir = file.getParentFile(); File[] files = dir.listFiles(); if (files != null) { FileMatcher fm = FileMatcher.getMatcher(file); for (File f : files) { if (fm.match(f.getName())) { key = fm.getKey(); value = fm.getValue(); this.file = f; break; } } } }
public static Match<File> find(File filePattern) { File dir = filePattern.getParentFile(); File[] files = dir.listFiles(); if (files != null) { FileMatcher fm = FileMatcher.getMatcher(filePattern.getName().concat("-{v:r?[0-9]+.*}\\.jar")); String studioSnapshotName = filePattern.getName().concat(".jar"); for (File f : files) { if (fm.match(f.getName())) { Match<File> result = new Match<File>(); result.version = fm.getValue(); result.object = f; return result; } if (studioSnapshotName.equals(f.getName())) { Match<File> result = new Match<File>(); result.version = UpdateManager.STUDIO_SNAPSHOT_VERSION; result.object = f; return result; } } } return null; }
public static FileMatcher getMatcher(File file) { return getMatcher(file.getName()); }
public static FileMatcher getMatcher(String path) { Matcher m = VAR.matcher(path); if (m.matches()) { String prefix = m.group(1); String key = m.group(2).trim(); String value = m.group(3).trim(); String suffix = m.group(4); Pattern pattern = null; if (prefix.length() == 0) { if (suffix.length() == 0) { pattern = Pattern.compile("(" + value + ")"); } else { pattern = Pattern.compile("(" + value + ")" + suffix); } } else if (suffix.length() == 0) { pattern = Pattern.compile(prefix + "(" + value + ")"); } else { pattern = Pattern.compile(prefix + "(" + value + ")" + suffix); } return new FileMatcher(pattern, key); } return new FileMatcher(null, path); }
FileMatcher filenameMatcher = FileMatcher.getMatcher("{n:.*-}[0-9]+.*\\.jar"); boolean isVersionnedJarFile = filenameMatcher.match(fileToCopy.getName()); if (isVersionnedJarFile) { log.warn(String.format( String filenameWithoutVersion = filenameMatcher.getValue(); FileMatcher versionMatcher = FileMatcher.getMatcher(filenameWithoutVersion + "{v:[0-9]+.*}\\.jar"); if (versionMatcher.match(fileToCopy.getName())) { fileToCopyVersion = new FileVersion(versionMatcher.getValue()); if (list != null) { for (File f : list) { if (versionMatcher.match(f.getName())) { dst = f; dstVersion = new FileVersion(versionMatcher.getValue()); break;
/** * Look for a matching file for the given path. * * @param path Searched file path, optionally including the pattern. * @param map The pattern variable will be put in the given map if any. Since 5.5, map can be null. * @return File found. Null if none. */ public static File getMatchingFile(String path, Map<String, Object> map) { File file = new File(path); FileMatcher matcher = getMatcher(path); if (matcher.getPattern() == null) { return file; } // a pattern -> find the first file that match that pattern File dir = file.getParentFile(); File[] list = dir.listFiles(); if (list != null) { for (File f : list) { if (matcher.match(f.getName())) { if (map != null) { map.put(matcher.getKey(), matcher.getValue()); } return f; } } } return null; }