01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * 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: // Implementations for StringParser.
37:
38: public StringMatcher parse(String regularExpression) {
39: int index;
40: StringMatcher nextMatcher = new EmptyStringMatcher();
41:
42: // Look for wildcards.
43: for (index = 0; index < regularExpression.length(); index++) {
44: // Is there a '*' wildcard?
45: if (regularExpression.charAt(index) == '*') {
46: // Create a matcher for the wildcard and, recursively, for the
47: // remainder of the string.
48: nextMatcher = new VariableStringMatcher(null, null, 0,
49: Integer.MAX_VALUE, parse(regularExpression
50: .substring(index + 1)));
51: break;
52: }
53:
54: // Is there a '?' wildcard?
55: else if (regularExpression.charAt(index) == '?') {
56: // Create a matcher for the wildcard and, recursively, for the
57: // remainder of the string.
58: nextMatcher = new VariableStringMatcher(null, null, 1,
59: 1,
60: parse(regularExpression.substring(index + 1)));
61: break;
62: }
63: }
64:
65: // Return a matcher for the fixed first part of the regular expression,
66: // if any, and the remainder.
67: return index != 0 ? (StringMatcher) new FixedStringMatcher(
68: regularExpression.substring(0, index), nextMatcher)
69: : (StringMatcher) nextMatcher;
70: }
71:
72: /**
73: * A main method for testing name matching.
74: */
75: public static void main(String[] args) {
76: try {
77: System.out.println("Regular expression [" + args[0] + "]");
78: NameParser parser = new NameParser();
79: StringMatcher matcher = parser.parse(args[0]);
80: for (int index = 1; index < args.length; index++) {
81: String string = args[index];
82: System.out.print("String [" + string + "]");
83: System.out.println(" -> match = "
84: + matcher.matches(args[index]));
85: }
86: } catch (Exception ex) {
87: ex.printStackTrace();
88: }
89: }
90: }
|