KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > RegexUtil


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 JavaDoc;
30 import java.util.Iterator JavaDoc;
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 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     /**
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 JavaDoc regexs, String JavaDoc str)
61     {
62         Iterator JavaDoc 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 JavaDoc list, String JavaDoc 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