Initial Commit
[packages] / xemacs-packages / jde / java / bsh-commands / bsh / commands / whichClass.bsh
1 import java.util.zip.ZipFile;
2 import java.util.zip.ZipEntry;
3
4 String whichClass(String strClass)
5 {
6         String strBootCP = System.getProperty("sun.boot.class.path");
7         String strCP = System.getProperty("java.class.path");
8         if (strBootCP != null)
9         {
10                 strCP = strCP + File.pathSeparator + strBootCP;
11         }
12         
13         return whichClassWithClassPath(strClass, strCP);
14 }
15
16
17 String whichClassWithClassPath(String strClass, String strCP)
18 {
19         String convertName(String str)
20                 {
21                         int nLength = str.length();
22                         StringBuffer strRetVal = new StringBuffer(str);
23                         for (int nCount = 0; nCount < nLength; nCount++)
24                         {
25                                 if (strRetVal.charAt(nCount) == '.')
26                                 {
27                                         strRetVal.setCharAt(nCount, File.separatorChar);
28                                 }
29                         }
30
31                         strRetVal.append(".class");
32                         return strRetVal.toString();
33                 }
34         
35         String convertToZipName(String str)
36                 {
37                         int nLength = str.length();
38                         StringBuffer strRetVal = new StringBuffer(str);
39                         for (int nCount = 0; nCount < nLength; nCount++)
40                         {
41                                 if (strRetVal.charAt(nCount) == '.')
42                                 {
43                                         strRetVal.setCharAt(nCount, '/');
44                                 }
45                         }
46
47                         strRetVal.append(".class");
48                         return strRetVal.toString();
49                 }
50         boolean processEntry(String strFile, String strClass, String strZipName)
51                 {
52                         File file = new File(strFile);
53                         if (file.exists())
54                                 if (file.isDirectory())
55                                 {
56                                         String strPath = strFile + File.separatorChar + strClass;
57                                         File classFile = new File(strPath);
58                                         return classFile.exists();
59                                 }
60                                 else
61                                 {
62                                         try
63                                         {                               
64                                                 ZipFile zip = new ZipFile(file);
65                                                 ZipEntry found = zip.getEntry(strZipName);
66                                                 return (found != null);
67                                         }
68                                         catch (Exception ex)
69                                         {
70                                                 System.out.println(ex.toString());
71                                         }
72                         
73                                 }
74                         return false;
75                 }
76         
77         String strZipName = convertToZipName(strClass);
78         String strConvertedName = convertName(strClass);
79         String token = null;
80         if (strCP == null)
81         {
82                 return null;
83         }
84         
85         print("Using classpath " + strCP + "\n\n");
86         StringTokenizer tokenizer = new StringTokenizer(strCP, File.pathSeparator);
87         while (tokenizer.hasMoreElements())
88         {
89                 token = tokenizer.nextToken();
90                 if (processEntry(token, strConvertedName, strZipName))
91                 {
92                         print("Class " + strClass + " is loaded from " + token);
93                         return token;
94                 }       
95         }
96         print("No definition found for " + strClass);
97         return null;
98 }
99