001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package util;
028:
029: import java.util.Vector;
030:
031: /*
032: * A ClassnameFilterList stores a list of ClassnameFilter objects.
033: * It is used for mapping a classname into group, such as "JNI"
034: */
035:
036: public class ClassnameFilterList {
037:
038: public ClassnameFilterList() {
039: groups = new Vector();
040: filters = new Vector();
041: }
042:
043: public void addTypePatterns(String group, String patterns) {
044: boolean conditional;
045: // If a pattern starts with '-', it matches only if there
046: // were no earlier matches
047: if (patterns.charAt(0) == '-') {
048: conditional = true;
049: patterns = patterns.substring(1);
050: } else {
051: conditional = false;
052: }
053: ClassnameFilter f = new ClassnameFilter(conditional);
054: parseClassList(patterns, f);
055: groups.addElement(group.intern());
056: filters.addElement(f);
057: }
058:
059: public String[] getTypes(String classname) {
060: Vector types = new Vector(1);
061: int l = groups.size();
062: for (int i = 0; i < l; ++i) {
063: String name = (String) groups.elementAt(i);
064: ClassnameFilter f = (ClassnameFilter) filters.elementAt(i);
065: // If a pattern is conditional, it matches only if there
066: // were no earlier matches
067: if (types.size() == 0 || !f.conditional) {
068: if (f.accept(null, classname)) {
069: types.addElement(name);
070: }
071: }
072: }
073: String[] strings = new String[types.size()];
074: types.copyInto(strings);
075: return strings;
076: }
077:
078: public boolean isType(String classname, String type) {
079: String[] types = getTypes(classname);
080: for (int i = 0; i < types.length; ++i) {
081: if (types[i] == type.intern()) {
082: return true;
083: }
084: }
085: return false;
086: }
087:
088: // Parse the rest of the string as a list of classes.
089: private void parseClassList(String val, ClassnameFilter filter) {
090: java.util.StringTokenizer tkn = new java.util.StringTokenizer(
091: val, " ,", false);
092: while (tkn.hasMoreTokens()) {
093: String classname = util.LinkerUtil.sanitizeClassname(tkn
094: .nextToken());
095: filter.includeName(classname);
096: }
097: }
098:
099: private Vector groups;
100: private Vector filters;
101:
102: }
|