KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > util > SymbolMap


1 package alt.jiapi.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FilenameFilter JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7
8 import alt.jiapi.Instrumentor;
9 import alt.jiapi.instrumentor.Strategy;
10
11 /**
12  * SymbolMap maps String names into String values.
13  */

14 public class SymbolMap {
15     private HashMap JavaDoc map;
16
17     /**
18      * used in development phase only. To be removed
19      */

20     public static void main(String JavaDoc [] args) {
21         SymbolMap sm = new SymbolMap("alt.jiapi.instrumentor");
22         System.out.println(sm);
23     }
24
25
26     public SymbolMap() {
27         map = new HashMap JavaDoc();
28     }
29
30     /**
31      * Creates new SymbolMap from given package. Classpath is searched,
32      * and if a given package could be resolved from some point in
33      * classpath, all the classes in that package are added to this map.
34      * package name is stripped from class name and this will be the key
35      * when using getSymbol. For example,
36      * class alt.jiapi.instrumentor.SplitInstrumentor will be stored
37      * with key SplitInstrumentor and value alt.jiapi.instrumentor.SplitInstrumentor.
38      */

39     public SymbolMap(String JavaDoc pkg) {
40         this();
41
42         addPackage(pkg);
43     }
44
45     public void addPackage(String JavaDoc pkg) {
46         String JavaDoc classPath = System.getProperty("java.class.path");
47         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(classPath,File.pathSeparator);
48
49         while(st.hasMoreTokens()) {
50             String JavaDoc pathItem = st.nextToken();
51             resolveClassPathItem(pathItem, pkg);
52         }
53     }
54
55     public void addSymbol(String JavaDoc name, String JavaDoc value) {
56         if(value == null) {
57             throw new NullPointerException JavaDoc("value is null");
58         }
59
60         map.put(name, value);
61     }
62
63     public String JavaDoc getSymbol(String JavaDoc name) {
64         return (String JavaDoc)map.get(name);
65     }
66
67
68     private void resolveClassPathItem(String JavaDoc item, String JavaDoc pkg) {
69         File JavaDoc f = new File JavaDoc(item);
70         if (!f.exists()) {
71             return;
72         }
73
74         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pkg, ".");
75         if (f.isDirectory()) {
76             String JavaDoc dir = item;
77             while(st.hasMoreTokens()) {
78                 String JavaDoc s = st.nextToken();
79                 dir += File.separator + s;
80                 f = new File JavaDoc(dir);
81                 if (!f.exists()) {
82                     return;
83                 }
84             }
85
86             if (!f.isDirectory()) {
87                 return;
88             }
89
90             String JavaDoc [] fileNames = f.list(new Filter JavaDoc(".class"));
91             Class JavaDoc instrumentorClass = Instrumentor.class;
92             Class JavaDoc strategyClass = Strategy.class;
93
94             for(int i = 0; i < fileNames.length; i++) {
95                 String JavaDoc fileName = fileNames[i].substring(0, fileNames[i].lastIndexOf('.'));
96
97                 Class JavaDoc c = null;
98                 try {
99                     c = Class.forName(pkg + "." + fileName);
100                 }
101                 catch (Exception JavaDoc e) {
102                     e.printStackTrace();
103                     continue;
104                 }
105
106                 if (instrumentorClass.isAssignableFrom(c)) {
107                     map.put(fileName, pkg + "." + fileName);
108                 }
109                 if (strategyClass.isAssignableFrom(c)) {
110                     map.put(fileName, pkg + "." + fileName);
111                 }
112             }
113         }
114     }
115
116     private class Filter implements FilenameFilter JavaDoc {
117         private String JavaDoc suffix;
118         public Filter(String JavaDoc suffix) {
119             this.suffix = suffix;
120         }
121
122         public boolean accept(File JavaDoc dir, String JavaDoc name) {
123             if (name.endsWith(suffix)) {
124                 return true;
125             }
126
127             return false;
128         }
129     }
130
131
132     public String JavaDoc toString() {
133         return map.toString();
134     }
135 }
136
137
138
Popular Tags