1 26 27 package net.sourceforge.cobertura.util; 28 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import org.apache.log4j.Logger; 33 import org.apache.oro.text.regex.MalformedPatternException; 34 import org.apache.oro.text.regex.Pattern; 35 import org.apache.oro.text.regex.Perl5Compiler; 36 import org.apache.oro.text.regex.Perl5Matcher; 37 38 43 public abstract class RegexUtil 44 { 45 46 private static final Logger logger = Logger.getLogger(RegexUtil.class); 47 48 private final static Perl5Matcher pm = new Perl5Matcher(); 49 50 60 public static boolean matches(Collection regexs, String str) 61 { 62 Iterator iter = regexs.iterator(); 63 while (iter.hasNext()) 64 { 65 Pattern regex = (Pattern)iter.next(); 66 if (pm.matches(str, regex)) 67 { 68 return true; 69 } 70 } 71 72 return false; 73 } 74 75 public static void addRegex(Collection list, String regex) 76 { 77 try 78 { 79 Perl5Compiler pc = new Perl5Compiler(); 80 Pattern pattern = pc.compile(regex); 81 list.add(pattern); 82 } 83 catch (MalformedPatternException e) 84 { 85 logger.warn("The regular expression " + regex + " is invalid: " 86 + e.getLocalizedMessage()); 87 } 88 } 89 90 } 91 | Popular Tags |