KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > yworks > yguard > obf > TreeItem


1 /* ===========================================================================
2  * $RCSfile: TreeItem.java,v $
3  * ===========================================================================
4  *
5  * RetroGuard -- an obfuscation package for Java classfiles.
6  *
7  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  * The author may be contacted at markw@retrologic.com
24  *
25  *
26  * $Date: 2005/05/19 09:55:11 $
27  * $Revision: 1.3 $
28  */

29 package com.yworks.yguard.obf;
30
31 import java.io.*;
32 import java.lang.reflect.*;
33 import java.util.*;
34 import com.yworks.yguard.obf.classfile.*;
35
36 /**
37  * Item that forms a tree structure and can represent a package level, a class,
38  * or a method or field.
39  *
40  * @author Mark Welsh
41  */

42 public class TreeItem
43 {
44     // Constants -------------------------------------------------------------
45

46
47     // Fields ----------------------------------------------------------------
48
protected boolean isSynthetic; // Is a method or field Synthetic?
49
protected int access; // Access level (interpret using java.lang.reflect.Modifier)
50
protected ClassTree classTree = null; // Our owner
51
protected TreeItem parent = null; // Our immediate parent
52
protected String JavaDoc sep = ClassFile.SEP_REGULAR; // Separator preceeding this level's name
53
private String JavaDoc inName = null; // Original name of this item
54
private String JavaDoc outName = null; // Output name of this item
55
private boolean isFixed = false; // Has the name been fixed in some way?
56
private boolean isFromScript = false; // Is this script constrained?
57
private boolean isFromScriptMap = false; // Is this script_map constrained?
58

59
60     // Class Methods ---------------------------------------------------------
61

62     /** Do a non-package-recursive wildcard String match. */
63     public static boolean isNRMatch(String JavaDoc pattern, String JavaDoc string) {
64         Enumeration enum1, enum2;
65         try {
66             for (enum1 = ClassTree.getNameEnum(pattern),
67                      enum2 = ClassTree.getNameEnum(string);
68                  enum1.hasMoreElements() && enum2.hasMoreElements(); ) {
69                 Cons nameSegment1 = (Cons)enum1.nextElement();
70                 char tag1 = ((Character JavaDoc)nameSegment1.car).charValue();
71                 String JavaDoc name1 = (String JavaDoc)nameSegment1.cdr;
72                 Cons nameSegment2 = (Cons)enum2.nextElement();
73                 char tag2 = ((Character JavaDoc)nameSegment2.car).charValue();
74                 String JavaDoc name2 = (String JavaDoc)nameSegment2.cdr;
75                 if (tag1 != tag2 || !isMatch(name1, name2)) {
76                     return false;
77                 }
78             }
79             if (enum1.hasMoreElements() || enum2.hasMoreElements()) {
80                 return false;
81             }
82         } catch (Exception JavaDoc e) {
83             return false;
84         }
85         return true;
86     }
87
88     /** Do a wildcard String match. */
89     public static boolean isMatch(String JavaDoc pattern, String JavaDoc string) {
90         // Sanity check
91
if (pattern == null || string == null) {
92             return false;
93         }
94
95         //System.out.println("1) Pattern: " + pattern + " String: " + string);
96

97         // Not really a wildcard, then check for exact match
98
if (pattern.indexOf('*') == -1) {
99             return pattern.equals(string);
100         }
101
102         // Check for match of head
103
int pos = -1;
104         if (pattern.charAt(0) != '*') {
105             pos = pattern.indexOf('*');
106             String JavaDoc head = pattern.substring(0, pos);
107             if (string.length() < head.length()) {
108                 return false;
109             }
110             if (!string.substring(0, head.length()).equals(head)) {
111                 return false;
112             } else {
113                 pattern = pattern.substring(pos);
114                 string = string.substring(pos);
115             }
116         }
117
118         //System.out.println("2) Pattern: " + pattern + " String: " + string);
119

120         // Check for match of tail
121
if (pattern.charAt(pattern.length() - 1) != '*') {
122             pos = pattern.lastIndexOf('*');
123             String JavaDoc tail = pattern.substring(pos + 1);
124             if (string.length() < tail.length()) {
125                 return false;
126             }
127             if (!string.substring(string.length() - tail.length()).equals(tail)) {
128                 return false;
129             } else {
130                 pattern = pattern.substring(0, pos + 1);
131                 string = string.substring(0, string.length() - tail.length());
132             }
133         }
134
135         //System.out.println("3) Pattern: " + pattern + " String: " + string);
136

137         // Split the pattern at the wildcard positions
138
Vector section = new Vector();
139         pos = pattern.indexOf('*');
140         int rpos = -1;
141         while ((rpos = pattern.indexOf('*', pos+1)) != -1) {
142             if (rpos != pos + 1) {
143                 section.addElement(pattern.substring(pos + 1, rpos));
144             }
145             pos = rpos;
146         }
147         // Check each section for a non-overlapping match in the string
148
for (Enumeration enumeration = section.elements(); enumeration.hasMoreElements(); ) {
149             String JavaDoc chunk = (String JavaDoc)enumeration.nextElement();
150             //System.out.println("Section: " + chunk + " String: " + string);
151
pos = string.indexOf(chunk);
152             if (pos == -1) {
153                 return false;
154             }
155             string = string.substring(pos + chunk.length());
156         }
157         return true;
158     }
159
160
161     // Instance Methods ------------------------------------------------------
162
/** Ctor. */
163     public TreeItem(TreeItem parent, String JavaDoc name)
164     {
165         this.parent = parent;
166         this.inName = name;
167         if (parent != null)
168         {
169             classTree = parent.classTree;
170         }
171     }
172
173     /** Return the modifiers. */
174     public int getModifiers() {return access;}
175
176     /** Return the original name of the entry. */
177     public String JavaDoc getInName() {return inName;}
178
179     /** Set the output name of the entry. */
180     public void setOutName(String JavaDoc outName)
181     {
182         // DEBUG
183
//if (isFixed)
184
//{
185
// System.out.println("BIG TROUBLE: " + inName + " -> " + this.outName + " -> " + outName);
186
//}
187
this.outName = outName;
188         isFixed = true;
189     }
190
191     /** Return the output name of the entry, obfuscated or original. */
192     public String JavaDoc getOutName() {return outName != null ? outName : inName;}
193
194     /** Return the obfuscated name of the entry. */
195     public String JavaDoc getObfName() {return outName;}
196
197     /** Signal that this constraint came from a user script line. */
198     public void setFromScript() {isFromScript = true;}
199
200     /** Signal that this constraint came from a map script line. */
201     public void setFromScriptMap() {isFromScriptMap = true;}
202
203     /** Has the entry been fixed already? */
204     public boolean isFixed() {return isFixed;}
205
206     /** Is this constrained by a user script line? */
207     public boolean isFromScript() {return isFromScript;}
208
209     /** Is this constrained by a map script line? */
210     public boolean isFromScriptMap() {return isFromScriptMap;}
211
212     /** Is a method or field Synthetic? */
213     public boolean isSynthetic() {return isSynthetic;}
214
215     /** Set the parent in the tree -- used when stitching in a Cl to replace a PlaceholderCl. */
216     public void setParent(TreeItem parent) {this.parent = parent;}
217
218     /** Get the parent in the tree. */
219     public TreeItem getParent() {return parent;}
220
221     /** Construct and return the full original name of the entry. */
222     public String JavaDoc getFullInName()
223     {
224         if (parent == null)
225         {
226             return "";
227         }
228         else if (parent.parent == null)
229         {
230             return getInName();
231         }
232         else
233         {
234             return parent.getFullInName() + sep + getInName();
235         }
236     }
237
238     /** Construct and return the full obfuscated name of the entry. */
239     public String JavaDoc getFullOutName()
240     {
241         if (parent == null)
242         {
243             return "";
244         }
245         else if (parent.parent == null)
246         {
247             return getOutName();
248         }
249         else
250         {
251             return parent.getFullOutName() + sep + getOutName();
252         }
253     }
254 }
255
Popular Tags