001/*
002 * Cobertura - http://cobertura.sourceforge.net/
003 *
004 * Copyright (C) 2006 John Lewis
005 *
006 * Note: This file is dual licensed under the GPL and the Apache
007 * Source License (so that it can be used from both the main
008 * Cobertura classes and the ant tasks).
009 *
010 * Cobertura is free software; you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License as published
012 * by the Free Software Foundation; either version 2 of the License,
013 * or (at your option) any later version.
014 *
015 * Cobertura is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 * General Public License for more details.
019 *
020 * You should have received a copy of the GNU General Public License
021 * along with Cobertura; if not, write to the Free Software
022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023 * USA
024 */
025
026package net.sourceforge.cobertura.instrument;
027
028import java.util.Collection;
029import java.util.HashSet;
030
031import net.sourceforge.cobertura.util.RegexUtil;
032
033/**
034 * This class represents a collection of regular expressions that will be used to see
035 * if a classname matches them.
036 * 
037 * Regular expressions are specified by calling add methods.  If no add methods are
038 * called, this class will match any classname.
039 * 
040 * @author John Lewis
041 *
042 */
043public class ClassPattern
044{
045
046        private Collection includeClassesRegexes = new HashSet();
047
048        private Collection excludeClassesRegexes = new HashSet();
049
050        private static final String WEBINF_CLASSES = "WEB-INF/classes/";
051
052        /**
053         * Returns true if any regular expressions have been specified by calling the
054         * add methods.  If none are specified, this class matches anything.
055         * 
056         * @return true if any regular expressions have been specified
057         */
058        boolean isSpecified()
059        {
060                return includeClassesRegexes.size() > 0;
061        }
062
063        /**
064         * Check to see if a class matches this ClassPattern
065         * 
066         * If a pattern has not been specified, this matches anything.
067         * 
068         * This method also looks for "WEB-INF/classes" at the beginning of the
069         * classname.  It is removed before checking for a match.
070         * 
071         * @param filename Either a full classname or a full class filename
072         * @return true if the classname matches this ClassPattern or if this ClassPattern
073         * has not been specified.
074         */
075        boolean matches(String filename)
076        {
077                boolean matches = true;
078
079                if (isSpecified())
080                {
081                        matches = false;
082                        // Remove .class extension if it exists
083                        if (filename.endsWith(".class"))
084                        {
085                                filename = filename.substring(0, filename.length() - 6);
086                        }
087                        filename = filename.replace('\\', '/');
088
089                        filename = removeAnyWebInfClassesString(filename);
090
091                        filename = filename.replace('/', '.');
092                        if (RegexUtil.matches(includeClassesRegexes, filename))
093                        {
094                                matches = true;
095                        }
096                        if (matches && RegexUtil.matches(excludeClassesRegexes, filename))
097                        {
098                                matches = false;
099                        }
100                }
101                return matches;
102        }
103
104        private String removeAnyWebInfClassesString(String 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 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 regex)
129        {
130                RegexUtil.addRegex(excludeClassesRegexes, regex);
131        }
132
133}