KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* MultiIdentifierMatcher 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: MultiIdentifierMatcher.java.in,v 1.1.2.2 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 MultiIdentifierMatcher implements IdentifierMatcher, OptionHandler {
25     /**
26      * Useful constant for giving to the constructor.
27      */

28     public static boolean OR = true;
29     /**
30      * Useful constant for giving to the constructor.
31      */

32     public static boolean AND = false;
33
34     IdentifierMatcher[] matchers;
35     boolean isOr;
36
37     /**
38      * Create an empty MultiIdentifierMatcher.
39      */

40     public MultiIdentifierMatcher() {
41     this.matchers = new IdentifierMatcher[0];
42     }
43
44     /**
45      * Create an IdentifierMatcher out of other matchers.
46      * @param isOr if true, match should return the logical (shortcut)
47      * or of the underlying matchers, if false it returns the logical and.
48      * @param matchers the underlying matchers
49      */

50     public MultiIdentifierMatcher(boolean isOr,
51                   IdentifierMatcher[] matchers) {
52     this.isOr = isOr;
53     this.matchers = matchers;
54     }
55
56     public void setOption(String JavaDoc option, Collection JavaDoc values) {
57     if (option.equals("or")) {
58         isOr = true;
59         matchers = (IdentifierMatcher[])
60         values.toArray(new IdentifierMatcher[values.size()]);
61     } else if (option.equals("and")) {
62         isOr = false;
63         matchers = (IdentifierMatcher[])
64         values.toArray(new IdentifierMatcher[values.size()]);
65     } else
66         throw new IllegalArgumentException JavaDoc("Invalid option `"+option+"'.");
67     }
68     
69
70     public boolean matches(Identifier ident) {
71     for (int i=0; i< matchers.length; i++) {
72         if (matchers[i].matches(ident) == isOr)
73         return isOr;
74     }
75     return !isOr;
76     }
77
78     public boolean matchesSub(Identifier ident, String JavaDoc name) {
79     for (int i=0; i< matchers.length; i++) {
80         if (matchers[i].matchesSub(ident, name) == isOr)
81         return isOr;
82     }
83     return !isOr;
84     }
85
86     public String JavaDoc getNextComponent(Identifier ident) {
87     if (isOr == AND) {
88         for (int i=0; i< matchers.length; i++) {
89         String JavaDoc next = matchers[i].getNextComponent(ident);
90         if (next != null && matchesSub(ident, next))
91             return next;
92         }
93         return null;
94     }
95     // OR case
96
String JavaDoc next = null;
97     for (int i = 0; i < matchers.length; i++) {
98         if (!matchesSub(ident, null))
99         continue;
100         if (next != null
101         && !matchers[i].getNextComponent(ident).equals(next))
102         return null;
103         next = matchers[i].getNextComponent(ident);
104         if (next == null)
105         return null;
106     }
107     return next;
108     }
109 }
110
111
Popular Tags