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.Hashtable;
030:
031: /*
032: * A ClassnameFilter is a pretty simple instance of a FilenameFilter.
033: * It is used for processing command-line options which involve
034: * lists of sets of classes. It understands a single trailing * in
035: * a name to mean all the classes in a package.
036: */
037:
038: public class ClassnameFilter implements java.io.FilenameFilter {
039:
040: boolean includeAll = false;
041: boolean conditional;
042:
043: Hashtable includedClass = new Hashtable(); // string class names
044: Hashtable includedPackage = new Hashtable(); // string package names
045:
046: public ClassnameFilter(boolean conditional) {
047: this .conditional = conditional;
048: }
049:
050: public ClassnameFilter() {
051: this (false);
052: }
053:
054: public void includeClass(String className) {
055: includedClass.put(className, className);
056: }
057:
058: public void includePackage(String packageName) {
059: if (packageName == null || (packageName.length() == 0))
060: includeAll = true;
061: else
062: includedPackage.put(packageName, packageName);
063: }
064:
065: /*
066: * To include a class or package named by the given string.
067: * If it ends in *, we treat it as a package name.
068: * otherwise, it is a class.
069: */
070: public void includeName(String classname) {
071: if (classname.charAt(classname.length() - 1) == '*') {
072: // this is a package name, not a class name
073: // strip off trailing junk and put it on the included package
074: // list.
075: int pkgLength = classname.lastIndexOf('/');
076: if (pkgLength < 0)
077: includePackage(null);
078: else
079: includePackage(classname.substring(0, pkgLength));
080: } else {
081: includeClass(classname);
082: }
083: }
084:
085: public boolean accept(java.io.File dir, String className) {
086: if (includedClass.get(className) != null) {
087: return true;
088: } else if (includeAll) {
089: return true;
090: } else {
091: String pkgName = className;
092: int pkgLength = className.lastIndexOf('/');
093: // look at immediate package, and superpackage, and ...
094: while (pkgLength > 0) {
095: pkgName = pkgName.substring(0, pkgLength);
096: if (includedPackage.get(pkgName) != null) {
097: return true;
098: }
099: pkgLength = pkgName.lastIndexOf('/');
100: }
101: }
102: return false;
103: }
104: }
|