01: /* ===========================================================================
02: * $RCSfile: PatternList.java,v $
03: * ===========================================================================
04: *
05: * RetroGuard -- an obfuscation package for Java classfiles.
06: *
07: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
08: *
09: * This program can be redistributed and/or modified under the terms of the
10: * Version 2 of the GNU General Public License as published by the Free
11: * Software Foundation.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: */
19:
20: package COM.rl.obf;
21:
22: import java.util.*;
23:
24: /**
25: * Class used for limited pattern matching with '**' (matches across package
26: * boundaries) and '*' (does not match across package boundaries) wildcards.
27: *
28: * @author Mark Welsh
29: */
30: public class PatternList {
31: // Constants -------------------------------------------------------------
32:
33: // Fields ----------------------------------------------------------------
34: private Vector subs;
35: private int sc = -1;
36:
37: // Class Methods ---------------------------------------------------------
38: public static PatternList create(String pattern) throws Exception {
39: return new PatternList(pattern);
40: }
41:
42: // Instance Methods ------------------------------------------------------
43: // Ctor.
44: private PatternList(String pattern) throws Exception {
45: subs = new Vector();
46: int scFirst = pattern.indexOf("**");
47: int scLast = pattern.lastIndexOf("**");
48: int pos = -1;
49: while (pos < pattern.length()) {
50: int oldpos = pos;
51: pos = pattern.indexOf(ClassTree.PACKAGE_LEVEL, oldpos + 1);
52: if (pos == -1)
53: pos = pattern.length();
54: if (scFirst >= 0 && oldpos + 1 <= scFirst
55: && scFirst + 2 <= pos) {
56: sc = length();
57: pos = pattern.indexOf(ClassTree.PACKAGE_LEVEL,
58: scLast + 2);
59: if (pos == -1)
60: pos = pattern.length();
61: }
62: subs.addElement(pattern.substring(oldpos + 1, pos));
63: }
64: }
65:
66: /** Number of segments in the list. */
67: public int length() {
68: return subs.size();
69: }
70:
71: /** Does a '**' wildcard segment exist? */
72: public boolean scExists() {
73: return sc >= 0;
74: }
75:
76: /** Index of the '**' wildcard segment. */
77: public int scIndex() {
78: return sc;
79: }
80:
81: /** Return the i'th segment. */
82: public String getSub(int i) {
83: return (String) subs.elementAt(i);
84: }
85:
86: /** Return the i'th through j'th segments, joined by package separators. */
87: public String getSub(int i, int j) {
88: if (i < 0 || i > j || j >= length())
89: throw new IllegalArgumentException();
90: StringBuffer sb = new StringBuffer();
91: for (int k = i; k <= j; k++) {
92: sb.append(getSub(k));
93: if (k < j)
94: sb.append(ClassTree.PACKAGE_LEVEL);
95: }
96: return sb.toString();
97: }
98: }
|