KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > obfuscator > modules > WildCard


1 /* WildCard Copyright (C) 1999-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; see the file COPYING. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: WildCard.java.in,v 1.1.2.1 2002/05/28 17:34:17 hoenicke Exp $
18  */

19
20 package jode.obfuscator.modules;
21 import jode.obfuscator.*;
22 import java.util.Collection JavaDoc;
23
24 public class WildCard implements IdentifierMatcher, OptionHandler {
25
26     String JavaDoc wildcard;
27     int firstStar;
28
29     public WildCard() {
30     }
31
32     public WildCard(String JavaDoc wild) {
33     wildcard = wild;
34     firstStar = wildcard.indexOf('*');
35     }
36
37     public void setOption(String JavaDoc option, Collection JavaDoc values) {
38     if (option.equals("value")) {
39         if (values.size() != 1)
40         throw new IllegalArgumentException JavaDoc
41             ("Wildcard supports only one value.");
42         wildcard = (String JavaDoc) values.iterator().next();
43         firstStar = wildcard.indexOf('*');
44     } else
45         throw new IllegalArgumentException JavaDoc("Invalid option `"+option+"'.");
46     }
47
48     public String JavaDoc getNextComponent(Identifier ident) {
49     String JavaDoc prefix = ident.getFullName();
50     if (prefix.length() > 0)
51         prefix += ".";
52
53     int lastDot = prefix.length();
54     if (!wildcard.startsWith(prefix))
55         return null;
56
57     int nextDot = wildcard.indexOf('.', lastDot);
58     if (nextDot > 0
59         && (nextDot <= firstStar || firstStar == -1))
60         return wildcard.substring(lastDot, nextDot);
61     else if (firstStar == -1)
62         return wildcard.substring(lastDot);
63     else
64         return null;
65     }
66
67     public boolean matchesSub(Identifier ident, String JavaDoc subident) {
68     String JavaDoc prefix = ident.getFullName();
69     if (prefix.length() > 0)
70         prefix += ".";
71     if (subident != null)
72         prefix += subident;
73     if (firstStar == -1 || firstStar >= prefix.length())
74         return wildcard.startsWith(prefix);
75     return prefix.startsWith(wildcard.substring(0, firstStar));
76     }
77
78     public boolean matches(Identifier ident) {
79     String JavaDoc test = ident.getFullName();
80     if (firstStar == -1) {
81         if (wildcard.equals(test)) {
82         return true;
83         }
84         return false;
85     }
86     if (!test.startsWith(wildcard.substring(0, firstStar)))
87         return false;
88
89     test = test.substring(firstStar);
90     int lastWild = firstStar;
91     int nextWild;
92     while ((nextWild = wildcard.indexOf('*', lastWild + 1)) != -1) {
93         String JavaDoc pattern = wildcard.substring(lastWild+1, nextWild);
94         while (!test.startsWith(pattern)) {
95         if (test.length() == 0)
96             return false;
97         test = test.substring(1);
98         }
99         test = test.substring(nextWild - lastWild - 1);
100         lastWild = nextWild;
101     }
102
103     return test.endsWith(wildcard.substring(lastWild+1));
104     }
105
106     public String JavaDoc toString() {
107     return "Wildcard "+wildcard;
108     }
109 }
110
Popular Tags