kitaev@213: /* kitaev@213: * Copyright (c) 2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.internal; kitaev@213: kitaev@213: import java.util.regex.Pattern; kitaev@213: import java.util.regex.PatternSyntaxException; kitaev@213: kitaev@213: import org.tmatesoft.hg.util.Path; kitaev@213: import org.tmatesoft.hg.util.Path.Matcher; kitaev@213: kitaev@213: /** kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class PathRegexpMatcher implements Matcher { kitaev@213: private Pattern[] patterns; kitaev@213: kitaev@213: // disjunction, matches if any pattern found kitaev@213: // uses pattern.find(), not pattern.matches() kitaev@213: public PathRegexpMatcher(Pattern... p) { kitaev@213: if (p == null) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: patterns = p; kitaev@213: } kitaev@213: kitaev@213: public PathRegexpMatcher(String... p) throws PatternSyntaxException { kitaev@213: this(compile(p)); kitaev@213: } kitaev@213: kitaev@213: private static Pattern[] compile(String[] p) throws PatternSyntaxException { kitaev@213: // deliberately do no check for null, let it fail kitaev@213: Pattern[] rv = new Pattern[p.length]; kitaev@213: int i = 0; kitaev@213: for (String s : p) { kitaev@213: rv[i++] = Pattern.compile(s); kitaev@213: } kitaev@213: return rv; kitaev@213: } kitaev@213: kitaev@213: public boolean accept(Path path) { kitaev@213: for (Pattern p : patterns) { kitaev@213: if (p.matcher(path).find()) { kitaev@213: return true; kitaev@213: } kitaev@213: } kitaev@213: return false; kitaev@213: } kitaev@213: }