KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > instrument > ClassPattern


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2006 John Lewis
5  *
6  * Note: This file is dual licensed under the GPL and the Apache
7  * Source License (so that it can be used from both the main
8  * Cobertura classes and the ant tasks).
9  *
10  * Cobertura is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; either version 2 of the License,
13  * or (at your option) any later version.
14  *
15  * Cobertura is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Cobertura; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  */

25
26 package net.sourceforge.cobertura.instrument;
27
28 import java.util.Collection JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 import net.sourceforge.cobertura.util.RegexUtil;
32
33 /**
34  * This class represents a collection of regular expressions that will be used to see
35  * if a classname matches them.
36  *
37  * Regular expressions are specified by calling add methods. If no add methods are
38  * called, this class will match any classname.
39  *
40  * @author John Lewis
41  *
42  */

43 public class ClassPattern
44 {
45
46     private Collection JavaDoc includeClassesRegexes = new HashSet JavaDoc();
47
48     private Collection JavaDoc excludeClassesRegexes = new HashSet JavaDoc();
49
50     private static final String JavaDoc WEBINF_CLASSES = "WEB-INF/classes/";
51
52     /**
53      * Returns true if any regular expressions have been specified by calling the
54      * add methods. If none are specified, this class matches anything.
55      *
56      * @return true if any regular expressions have been specified
57      */

58     boolean isSpecified()
59     {
60         return includeClassesRegexes.size() > 0;
61     }
62
63     /**
64      * Check to see if a class matches this ClassPattern
65      *
66      * If a pattern has not been specified, this matches anything.
67      *
68      * This method also looks for "WEB-INF/classes" at the beginning of the
69      * classname. It is removed before checking for a match.
70      *
71      * @param filename Either a full classname or a full class filename
72      * @return true if the classname matches this ClassPattern or if this ClassPattern
73      * has not been specified.
74      */

75     boolean matches(String JavaDoc filename)
76     {
77         boolean matches = true;
78
79         if (isSpecified())
80         {
81             matches = false;
82             // Remove .class extension if it exists
83
if (filename.endsWith(".class"))
84             {
85                 filename = filename.substring(0, filename.length() - 6);
86             }
87             filename = filename.replace('\\', '/');
88
89             filename = removeAnyWebInfClassesString(filename);
90
91             filename = filename.replace('/', '.');
92             if (RegexUtil.matches(includeClassesRegexes, filename))
93             {
94                 matches = true;
95             }
96             if (matches && RegexUtil.matches(excludeClassesRegexes, filename))
97             {
98                 matches = false;
99             }
100         }
101         return matches;
102     }
103
104     private String JavaDoc removeAnyWebInfClassesString(String JavaDoc filename)
105     {
106         if (filename.startsWith(WEBINF_CLASSES))
107         {
108             filename = filename.substring(WEBINF_CLASSES.length());
109         }
110         return filename;
111     }
112
113     /**
114      * Add a regex to the list of class regexes to include.
115      *
116      * @param regex A regular expression to add.
117      */

118     void addIncludeClassesRegex(String JavaDoc regex)
119     {
120         RegexUtil.addRegex(includeClassesRegexes, regex);
121     }
122
123     /**
124      * Add a regex to the list of class regexes to exclude.
125      *
126      * @param regex
127      */

128     void addExcludeClassesRegex(String JavaDoc regex)
129     {
130         RegexUtil.addRegex(excludeClassesRegexes, regex);
131     }
132
133 }
134
Popular Tags