KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > idltree > IdList


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.idltree;
26
27 import java.io.*;
28 import java.util.*;
29 import java.util.jar.*;
30 import javax.swing.*;
31 import javax.swing.tree.*;
32 import java.lang.reflect.*;
33
34 public class IdList {
35     private static FNFilter filter;
36     private static DirFilter dirFilter;
37     private static JTree tree;
38     
39     private IdList() {
40     }
41         
42     static {
43         try {
44             List list = new LinkedList();
45             try {
46                 filter = new FNFilter("Helper.class");
47                 dirFilter = new DirFilter();
48                 String JavaDoc path[] = IdlNode.splitName(System.getProperty("java.class.path"), File.pathSeparator);
49                 for (int i = 0; i < path.length; i++) {
50                     if(path[i].endsWith(".jar")) {
51                         addIds(list, path[i]);
52                     } else {
53                         addIds(list, new File(path[i]), path[i]);
54                     }
55                 }
56             } catch (Throwable JavaDoc t) {
57             }
58             IdNode n = new IdNode();
59             n.add("octet");
60             n.add("short");
61             n.add("ushort");
62             n.add("long");
63             n.add("ulong");
64             n.add("longlong");
65             n.add("ulonglong");
66             n.add("float");
67             n.add("double");
68             n.add("longdouble");
69             n.add("fixed");
70             n.add("char");
71             n.add("wchar");
72             n.add("string");
73             n.add("wstring");
74             n.add("fixed");
75             n.add("any");
76     
77             Collections.sort(list);
78             Iterator it = list.iterator();
79             while(it.hasNext()) {
80                 n.add((String JavaDoc)it.next());
81             }
82             
83             tree = new JTree(new DefaultTreeModel(n));
84         } catch (Throwable JavaDoc tt) {
85             tt.printStackTrace();
86         }
87     }
88         
89     public static JTree getTree() {
90         return tree;
91     }
92
93     private static void addIds(List list, File dir, String JavaDoc root) {
94         File[] files = dir.listFiles(filter);
95         for (int i = 0; i < files.length; i++) {
96             String JavaDoc name = files[i].getPath();
97             name = name.substring(root.length() + 1, name.lastIndexOf("."));
98             name = name.replace('/', '.');
99             name = name.replace('\\', '.');
100             if (!name.endsWith(".Helper")) {
101                 try {
102                     Class JavaDoc hc = Class.forName(name);
103                     Method hm = hc.getMethod("id", null);
104                     list.add((String JavaDoc)hm.invoke(null, null));
105                 } catch (Exception JavaDoc e) {
106                     System.err.println("Can't create class for: " + name);
107                 }
108             }
109         }
110
111         File[] dirs = dir.listFiles(dirFilter);
112         for (int i = 0; i < dirs.length; i++) {
113             addIds(list, dirs[i], root);
114         }
115     }
116
117     private static void addIds(List list, String JavaDoc jarName) {
118         File jarFile = new File(jarName);
119         JarEntry jarEntry = null;
120         try {
121             JarInputStream input = new JarInputStream(new BufferedInputStream(new FileInputStream(jarFile)));
122             while ((jarEntry = input.getNextJarEntry()) != null) {
123                 String JavaDoc name = jarEntry.getName();
124                 if (name.endsWith("Helper.class")) {
125                     Class JavaDoc hc = Class.forName(name.substring(0, name.lastIndexOf(".")).replace('/', '.').replace('\\', '.'));
126                     Method hm = hc.getMethod("id", null);
127                     list.add((String JavaDoc)hm.invoke(null, null));
128                 }
129             }
130         } catch (Exception JavaDoc e) {
131         }
132     }
133             
134 /*
135     public String[] getList() {
136     }
137 */

138
139     private static class FNFilter implements FilenameFilter {
140         private String JavaDoc ext;
141
142         public FNFilter(String JavaDoc ext) {
143             this.ext = ext;
144         }
145     
146         public boolean accept(File dir, String JavaDoc name) {
147             File file = new File(dir, name);
148             if (file.isFile() && name.endsWith(ext)) {
149                 return true;
150             }
151             return false;
152         }
153     }
154
155     private static class DirFilter implements FilenameFilter {
156         public boolean accept(File dir, String JavaDoc name) {
157             File file = new File(dir, name);
158             if (file.isDirectory()) {
159                 return true;
160             }
161             return false;
162         }
163     }
164     
165     private static class IdNode extends DefaultMutableTreeNode {
166         private boolean isLeaf;
167         
168         public IdNode() {
169             this(false);
170         }
171
172         public IdNode(boolean leaf) {
173             isLeaf = leaf;
174             setUserObject("");
175         }
176     
177         public void add(String JavaDoc id) {
178             int idx = id.indexOf("/");
179             if (idx >= 0) {
180                 String JavaDoc n1 = id.substring(0, idx);
181                 String JavaDoc n2 = id.substring(idx + 1);
182                 IdNode c = getChild(n1);
183                 c.add(n2);
184             } else {
185                 add(getLeaf(id));
186             }
187         }
188
189         private IdNode getLeaf(String JavaDoc n) {
190             IdNode c = new IdNode(true);
191             c.setUserObject(n);
192             return c;
193         }
194             
195         private IdNode getChild(String JavaDoc n) {
196             IdNode c = null;
197             for (int i = 0; i < getChildCount(); i++) {
198                 c = (IdNode)getChildAt(i);
199                 if (((String JavaDoc)c.getUserObject()).equals(n)) {
200                     return c;
201                 }
202             }
203             c = new IdNode();
204             c.setUserObject(n);
205             add(c);
206             return c;
207         }
208     }
209 }
Popular Tags