1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 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 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 )it.next()); 81 } 82 83 tree = new JTree(new DefaultTreeModel(n)); 84 } catch (Throwable 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 root) { 94 File[] files = dir.listFiles(filter); 95 for (int i = 0; i < files.length; i++) { 96 String 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 hc = Class.forName(name); 103 Method hm = hc.getMethod("id", null); 104 list.add((String )hm.invoke(null, null)); 105 } catch (Exception 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 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 name = jarEntry.getName(); 124 if (name.endsWith("Helper.class")) { 125 Class hc = Class.forName(name.substring(0, name.lastIndexOf(".")).replace('/', '.').replace('\\', '.')); 126 Method hm = hc.getMethod("id", null); 127 list.add((String )hm.invoke(null, null)); 128 } 129 } 130 } catch (Exception e) { 131 } 132 } 133 134 138 139 private static class FNFilter implements FilenameFilter { 140 private String ext; 141 142 public FNFilter(String ext) { 143 this.ext = ext; 144 } 145 146 public boolean accept(File dir, String 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 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 id) { 178 int idx = id.indexOf("/"); 179 if (idx >= 0) { 180 String n1 = id.substring(0, idx); 181 String 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 n) { 190 IdNode c = new IdNode(true); 191 c.setUserObject(n); 192 return c; 193 } 194 195 private IdNode getChild(String n) { 196 IdNode c = null; 197 for (int i = 0; i < getChildCount(); i++) { 198 c = (IdNode)getChildAt(i); 199 if (((String )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 |