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: /*
028: * Collect classpath components, usually from the command line.
029: * Use this list to search for class files.
030: * In addition to the classic, Unix-style directory names,
031: * we also accept zip files.
032: */
033:
034: package util;
035:
036: import java.util.Vector;
037: import java.io.File;
038: import java.io.FileInputStream;
039: import java.io.BufferedInputStream;
040: import java.io.InputStream;
041: import java.io.IOException;
042: import java.util.zip.*;
043:
044: /*
045: * This auxiliary class represents a component of the search
046: * path, which can be either a directory, a zip file, or
047: * neither of the above (in which case it is ignored).
048: */
049: class searchPathComponent {
050: public String name;
051: public boolean isDirectory;
052: public boolean isZip;
053: public ZipFile zipfile; // only if isZip
054:
055: public searchPathComponent next; // chain.
056:
057: searchPathComponent(String name) {
058: this .name = name;
059: File this File = new File(name);
060: if (this File.exists()) {
061: if (this File.isDirectory()) {
062: this .isDirectory = true;
063: } else {
064: // try to open it as a zip file.
065: // if the open succeeds, it is a zip file.
066: // else we are in error.
067: try {
068: this .zipfile = new ZipFile(this File);
069: this .isZip = true;
070: } catch (java.io.IOException e) {
071: return;
072: }
073: }
074: }
075: //
076: // else it is none of the above.
077: // this is an error.
078: // we do not prevent putting erroneous components
079: // on the path.
080: //
081: }
082:
083: InputStream find(String name) {
084: try {
085: if (isDirectory) {
086: File this File = new File(this .name, name);
087: if (this File.exists() && this File.canRead()) {
088: return new BufferedInputStream(new FileInputStream(
089: this File));
090: }
091: } else if (isZip) {
092: ZipEntry ze = this .zipfile.getEntry(name);
093: if (ze != null)
094: return new BufferedInputStream(this .zipfile
095: .getInputStream(ze));
096: }
097: } catch (IOException e) {
098: return null;
099: }
100: return null;
101: }
102:
103: public String toString() {
104: return Localizer.getString(
105: isDirectory ? "classfilefinder.directory"
106: : isZip ? "classfilefinder.zipfile"
107: : "classfilefinder.path_noop",
108: this .name);
109: }
110:
111: }
112:
113: public class ClassFileFinder {
114:
115: private searchPathComponent searchPath;
116: private searchPathComponent searchPathEnd;
117:
118: public boolean verbose = false;
119:
120: /*
121: * take a single place to look.
122: * add it to the searchPath.
123: */
124: private void addSearchPathEntry(String pathComponent) {
125: searchPathComponent t = new searchPathComponent(pathComponent);
126: if (searchPathEnd == null) {
127: searchPath = searchPathEnd = t;
128: } else {
129: searchPathEnd.next = t;
130: searchPathEnd = t;
131: }
132: }
133:
134: /*
135: * take a colon-separated list of places to look.
136: * parse them out to an array of strings, which gets
137: * added to any searchPath we already have.
138: */
139: public void addToSearchPath(String pathString) {
140: int curbegin = 0;
141: int pl = pathString.length();
142: char sepChar = File.pathSeparatorChar;
143: int colon;
144: while ((colon = pathString.indexOf(sepChar, curbegin)) != -1) {
145: addSearchPathEntry(pathString.substring(curbegin, colon));
146: curbegin = colon + 1;
147: }
148: if (curbegin < pl) {
149: addSearchPathEntry(pathString.substring(curbegin, pl));
150: }
151: }
152:
153: public InputStream findClassFile(String cname) {
154: cname = cname + ".class";
155: for (searchPathComponent spc = searchPath; spc != null; spc = spc.next) {
156: InputStream s = spc.find(cname);
157: if (s != null) {
158: if (verbose) {
159: System.out.print(Localizer.getString(
160: "classfilefinder.foundin", cname, spc));
161: }
162: return s;
163: }
164: if (verbose) {
165: System.out.print(Localizer.getString(
166: "classfilefinder.notfoundin", cname, spc));
167: }
168: }
169: return null;
170: }
171: }
|