1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.classfile.visitor;
22
23import proguard.classfile.*;
24import proguard.util.*;
25
26import java.util.List;
27
28/**
29 * This <code>ClassVisitor</code> delegates its visits to another given
30 * <code>ClassVisitor</code>, but only when the visited class has a name that
31 * matches a given regular expression.
32 *
33 * @author Eric Lafortune
34 */
35public class ClassNameFilter implements ClassVisitor
36{
37    private final StringMatcher regularExpressionMatcher;
38    private final ClassVisitor  classVisitor;
39
40
41    /**
42     * Creates a new ClassNameFilter.
43     * @param regularExpression the regular expression against which class names
44     *                          will be matched.
45     * @param classVisitor      the <code>ClassVisitor</code> to which visits
46     *                          will be delegated.
47     */
48    public ClassNameFilter(String       regularExpression,
49                           ClassVisitor classVisitor)
50    {
51        this(new ListParser(new ClassNameParser()).parse(regularExpression),
52             classVisitor);
53    }
54
55
56    /**
57     * Creates a new ClassNameFilter.
58     * @param regularExpression the regular expression against which class names
59     *                          will be matched.
60     * @param classVisitor      the <code>ClassVisitor</code> to which visits
61     *                          will be delegated.
62     */
63    public ClassNameFilter(List         regularExpression,
64                           ClassVisitor classVisitor)
65    {
66        this(new ListParser(new ClassNameParser()).parse(regularExpression),
67             classVisitor);
68    }
69
70
71    /**
72     * Creates a new ClassNameFilter.
73     * @param regularExpressionMatcher the regular expression against which
74     *                                 class names will be matched.
75     * @param classVisitor             the <code>ClassVisitor</code> to which
76     *                                 visits will be delegated.
77     */
78    public ClassNameFilter(StringMatcher regularExpressionMatcher,
79                           ClassVisitor  classVisitor)
80    {
81        this.regularExpressionMatcher = regularExpressionMatcher;
82        this.classVisitor             = classVisitor;
83    }
84
85
86    // Implementations for ClassVisitor.
87
88    public void visitProgramClass(ProgramClass programClass)
89    {
90        if (accepted(programClass.getName()))
91        {
92            classVisitor.visitProgramClass(programClass);
93        }
94    }
95
96
97    public void visitLibraryClass(LibraryClass libraryClass)
98    {
99        if (accepted(libraryClass.getName()))
100        {
101            classVisitor.visitLibraryClass(libraryClass);
102        }
103    }
104
105
106    // Small utility methods.
107
108    private boolean accepted(String name)
109    {
110        return regularExpressionMatcher.matches(name);
111    }
112}
113