1 16 package com.buchuki.ensmer.object; 17 18 import java.io.*; 19 import java.util.*; 20 21 33 public class PackagePaths { 34 35 42 public PackagePaths(File root, final String suffix) { 43 this.suffix = suffix; 44 this.root = root; 45 acceptor = 46 new FilenameFilter() { 47 public boolean accept(File dir, String name) { 48 File testfile = new File(dir, name); 49 return testfile.isDirectory() || name.endsWith(suffix); 50 } 51 }; 52 } 53 54 62 public List<String > getFileList() { 63 List<String > retval = new ArrayList<String >(); 65 recurseFileList(retval, root); 66 return retval; 67 } 68 69 75 public List<String > getClassnameList() { 76 List<String > retval = new ArrayList<String >(); 77 List<String > files = getFileList(); 78 for (String name : files) { 79 String temp = name.replace(File.separatorChar, '.'); 80 retval.add(temp.substring(0, temp.length() - suffix.length())); 81 } 82 return retval; 83 } 84 85 92 public List<Class <? extends Backend>> getValidClassList() { 93 return getValidClassList(new Object ().getClass()); 94 } 95 96 106 @SuppressWarnings ( 107 {"unchecked"}) 108 public List<Class <? extends Backend>> getValidClassList(Class <?> parent) { 109 List<Class <? extends Backend>> retval = 110 new ArrayList<Class <? extends Backend>>(); 111 List<String > classes = getClassnameList(); 112 for (String name : classes) { 113 try { 114 Class <? extends Backend> clazz = 115 (Class <? extends Backend>) Class.forName(name); 116 if (parent.isAssignableFrom(clazz)) { 117 retval.add(clazz); 118 } 119 } catch (Exception e) { 120 } 121 } 122 return retval; 123 } 124 125 134 public Map<Class <? extends Backend>, File> getValidClassFileMap(Class parent) { 135 List<Class <? extends Backend>> classes = getValidClassList(parent); 136 List<String > filenames = getFileList(); 137 Map<Class <? extends Backend>, File> retval = 138 new HashMap<Class <? extends Backend>, File>(); 139 for (Class <? extends Backend> item : classes) { 140 String filename = item.getName().replace( 141 '.', File.separatorChar) + suffix; 142 if (filenames.contains(filename)) { 143 File file = new File(root, filename); 144 if (file.exists()) { 145 retval.put(item, file); 146 } 147 } 148 } 149 return retval; 150 } 151 152 159 private void recurseFileList(List<String > list, File directory) { 160 File[] dirContents = directory.listFiles(acceptor); 161 for (File file : dirContents) { 162 if (file.isDirectory()) { 163 recurseFileList(list, file); 164 } 165 else { 166 list.add(file.getAbsolutePath().substring(root.getAbsolutePath().length() + 1)); 167 } 168 } 169 } 170 171 175 private File root; 176 177 181 private FilenameFilter acceptor; 182 183 186 private String suffix; 187 188 } 189 190 | Popular Tags |