• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 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  */
21 package proguard.util;
22 
23 /**
24  * This StringParser can create StringMatcher instances for regular expressions
25  * matching names. The regular expressions are interpreted as comma-separated
26  * lists of names, optionally prefixed with '!' negators.
27  * If a name with a negator matches, a negative match is returned, without
28  * considering any subsequent entries in the list.
29  * Names can contain the following wildcards:
30  * '?'  for a single character, and
31  * '*'  for any number of characters.
32  *
33  * @author Eric Lafortune
34  */
35 public class NameParser implements StringParser
36 {
37     // Implementations for StringParser.
38 
parse(String regularExpression)39     public StringMatcher parse(String regularExpression)
40     {
41         int           index;
42         StringMatcher nextMatcher = new EmptyStringMatcher();
43 
44         // Look for wildcards.
45         for (index = 0; index < regularExpression.length(); index++)
46         {
47             // Is there a '*' wildcard?
48             if (regularExpression.charAt(index) == '*')
49             {
50                 // Create a matcher for the wildcard and, recursively, for the
51                 // remainder of the string.
52                 nextMatcher =
53                     new VariableStringMatcher(null,
54                                               null,
55                                               0,
56                                               Integer.MAX_VALUE,
57                                               parse(regularExpression.substring(index + 1)));
58                 break;
59             }
60 
61             // Is there a '?' wildcard?
62             else if (regularExpression.charAt(index) == '?')
63             {
64                 // Create a matcher for the wildcard and, recursively, for the
65                 // remainder of the string.
66                 nextMatcher =
67                     new VariableStringMatcher(null,
68                                               null,
69                                               1,
70                                               1,
71                                               parse(regularExpression.substring(index + 1)));
72                 break;
73             }
74         }
75 
76         // Return a matcher for the fixed first part of the regular expression,
77         // if any, and the remainder.
78         return index != 0 ?
79             (StringMatcher)new FixedStringMatcher(regularExpression.substring(0, index), nextMatcher) :
80             (StringMatcher)nextMatcher;
81     }
82 
83 
84     /**
85      * A main method for testing name matching.
86      */
main(String[] args)87     public static void main(String[] args)
88     {
89         try
90         {
91             System.out.println("Regular expression ["+args[0]+"]");
92             NameParser parser  = new NameParser();
93             StringMatcher  matcher = parser.parse(args[0]);
94             for (int index = 1; index < args.length; index++)
95             {
96                 String string = args[index];
97                 System.out.print("String             ["+string+"]");
98                 System.out.println(" -> match = "+matcher.matches(args[index]));
99             }
100         }
101         catch (Exception ex)
102         {
103             ex.printStackTrace();
104         }
105     }
106 }
107