Coverage Report - net.sourceforge.cobertura.util.RegexUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
RegexUtil
82%
14/17
100%
4/4
3
 
 1  
 /*
 2  
  * Cobertura - http://cobertura.sourceforge.net/
 3  
  *
 4  
  * Copyright (C) 2005 Mark Doliner
 5  
  * Copyright (C) 2006 John Lewis
 6  
  *
 7  
  * Note: This file is dual licensed under the GPL and the Apache
 8  
  * Source License (so that it can be used from both the main
 9  
  * Cobertura classes and the ant tasks).
 10  
  *
 11  
  * Cobertura is free software; you can redistribute it and/or modify
 12  
  * it under the terms of the GNU General Public License as published
 13  
  * by the Free Software Foundation; either version 2 of the License,
 14  
  * or (at your option) any later version.
 15  
  *
 16  
  * Cobertura is distributed in the hope that it will be useful, but
 17  
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 18  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 19  
  * General Public License for more details.
 20  
  *
 21  
  * You should have received a copy of the GNU General Public License
 22  
  * along with Cobertura; if not, write to the Free Software
 23  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 24  
  * USA
 25  
  */
 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  
 /**
 39  
  * Abstract, not to be instantiated utility class for Regex functions.
 40  
  * 
 41  
  * @author John Lewis (logic copied from MethodInstrumenter)
 42  
  */
 43  0
 public abstract class RegexUtil
 44  
 {
 45  
 
 46  4
         private static final Logger logger = Logger.getLogger(RegexUtil.class);
 47  
 
 48  4
         private final static Perl5Matcher pm = new Perl5Matcher();
 49  
 
 50  
         /**
 51  
          * <p>
 52  
          * Check to see if one of the regular expressions in a collection match
 53  
          * an input string.
 54  
          * </p>
 55  
          *
 56  
          * @param regexs The collection of regular expressions.
 57  
          * @param str The string to check for a match.
 58  
          * @return True if a match is found.
 59  
          */
 60  
         public static boolean matches(Collection regexs, String str)
 61  
         {
 62  109
                 Iterator iter = regexs.iterator();
 63  161
                 while (iter.hasNext())
 64  
                 {
 65  77
                         Pattern regex = (Pattern)iter.next();
 66  77
                         if (pm.matches(str, regex))
 67  
                         {
 68  25
                                 return true;
 69  
                         }
 70  52
                 }
 71  
 
 72  84
                 return false;
 73  
         }
 74  
 
 75  
         public static void addRegex(Collection list, String regex)
 76  
         {
 77  
                 try
 78  
                 {
 79  7
                         Perl5Compiler pc = new Perl5Compiler();
 80  7
                         Pattern pattern = pc.compile(regex);
 81  7
                         list.add(pattern);
 82  
                 }
 83  0
                 catch (MalformedPatternException e)
 84  
                 {
 85  0
                         logger.warn("The regular expression " + regex + " is invalid: "
 86  
                                         + e.getLocalizedMessage());
 87  7
                 }
 88  7
         }
 89  
 
 90  
 }