1 24 25 26 package org.aspectj.util; 27 28 import java.util.*; 29 import java.io.*; 30 31 34 public class ConfigFileUtil { 35 36 37 private static final File USERDIR; 38 39 private static final File DOTDIR; 40 41 private static final FileFilter SOURCEFILTER; 42 static { 43 File userDir = null; 44 File dotDir = null; 45 try { 46 dotDir = new File("."); 47 userDir = new File(System.getProperty("user.dir")).getCanonicalFile(); 48 } catch (Throwable t) { 49 if (null == userDir) userDir = dotDir; } finally { 51 DOTDIR = dotDir; 52 USERDIR = userDir; 53 } 54 SOURCEFILTER = new FileFilter() { 55 public boolean accept(File f) { 56 return ((f != null) && 57 (f.getName().endsWith(".java"))); 58 } 59 }; 60 } 61 62 69 public static List getLstFilesInDir(String dirPath) { List configs = new ArrayList(); 71 File f = new File(dirPath); 72 File[] dirContents = f.listFiles(); 73 for (int j = 0; j < dirContents.length; j++) { 74 if (dirContents[j].isDirectory()) { 75 configs.addAll(getLstFilesInDir(dirContents[j].getAbsolutePath())); 76 } else if (dirContents[j].getName().endsWith(".lst")) { 77 configs.add(dirContents[j].getAbsolutePath().replace('\\', '/')); 78 } 79 } 80 return configs; 81 } 82 83 98 public static File qualifiedFile(String name, File currentWorkingDir) 99 throws IOException { 100 name = name.replace('/', File.separatorChar); File file = new File(name); 102 if (!file.isAbsolute()) { 103 if (null == currentWorkingDir) { 104 currentWorkingDir = USERDIR; 105 } 106 file = new File(currentWorkingDir, name).getAbsoluteFile(); 107 } 108 return file.getCanonicalFile(); 109 } 110 111 120 public static List expandLstFile(String filename) throws IOException, LstFileEntryInvalidException { 121 return expandLstFile(filename, null); 122 } 123 124 138 public static List expandLstFile(String filename, File relativeTo) 139 throws IOException, LstFileEntryInvalidException { 140 List fileContents = new ArrayList(); 141 expandAtFile(qualifiedFile(filename, relativeTo), fileContents, null); 142 return fileContents; 143 } 144 145 158 private static void expandAtFile(File file, List args, List atFiles) 159 throws IOException, LstFileEntryInvalidException { 160 BufferedReader in = new BufferedReader(new FileReader(file)); 161 final File parentDir = (null == file? null : file.getParentFile()); 162 String line; 163 while ((line = in.readLine()) != null) { 164 if (line == null || line.length() < 1) continue; 165 line = line.trim(); 166 if (line.startsWith("//")) continue; 167 168 int indexStarJava = line.lastIndexOf("*.java"); 169 int indexStar = line.lastIndexOf("*"); 170 String dirName = null; 171 if (indexStarJava != -1) { 172 dirName = line.substring(0, indexStarJava); 173 } else if (indexStar != -1) { 174 dirName = line.substring(0, indexStar); 175 } 176 if (dirName != null) { 177 File newDirFile = qualifiedFile(dirName, parentDir); 178 File[] javafiles = newDirFile.listFiles(SOURCEFILTER); 179 if (javafiles == null) { 180 cantResolve(newDirFile, "unable to file java files in wildcard directory " 181 + newDirFile.getPath()); 182 } else { 183 for (int i = 0; i < javafiles.length; i++) { 184 if (!maybeAdd(javafiles[i], args)) { 185 cantResolve(javafiles[i], "unable to add wildcard to collection"); 186 } 187 } 188 } 189 } else if (line.startsWith("@")) { 190 File newfile = qualifiedFile(line.substring(1).trim(), parentDir); 191 if (!newfile.exists()) { 192 cantResolve(newfile, "@file does not exist"); 193 } else { String filePath = newfile.getCanonicalPath().intern(); if (null == atFiles) { 196 atFiles = new ArrayList(); 197 atFiles.add(file.getCanonicalPath().intern()); 198 } 199 if (!atFiles.contains(filePath)) { 200 atFiles.add(filePath); 201 expandAtFile(newfile, args, atFiles); 202 } } 204 } else { 205 File newfile = qualifiedFile(line, parentDir); 206 if (newfile.exists()) { 207 if (!maybeAdd(newfile, args)) { 208 cantResolve(newfile, "unable to add to collection"); 209 } 210 } else if (!(newfile.getName().equals("*.java") || 211 newfile.getName().equals("*"))) { 212 cantResolve(newfile, "expecting @{listFile}, *, *.java, or {path}.java"); 213 } else { cantResolve(newfile, "unable to find file " 221 + newfile); 222 } 231 } 232 } 233 in.close(); 234 } 235 236 public static class LstFileEntryInvalidException extends Exception { 237 public final String lstFile; 238 public final int lineNumber; 239 240 public LstFileEntryInvalidException(String lstFile, int lineNumber) { 241 this(lstFile, lineNumber, ""); 242 } 243 public LstFileEntryInvalidException(String lstFile, int lineNumber, String reason) { 244 super("" + lstFile + ":" + lineNumber + ": " + reason 245 + " (invalid entry - expecting *.java or {path}.java or @{listFile})"); 246 this.lstFile = lstFile; 247 this.lineNumber = lineNumber; 248 } 249 } 250 251 254 private static void cantResolve(File file, String reason) throws LstFileEntryInvalidException{ 255 throw new LstFileEntryInvalidException(file.getAbsolutePath(), -1, reason); 256 } 257 private static void cantResolve(File file) throws LstFileEntryInvalidException{ 258 throw new LstFileEntryInvalidException(file.getAbsolutePath(), -1); 259 } 260 261 private static boolean maybeAdd(File file, Collection files) { 262 if (isJavaFile(file)) { 263 files.add(file.getAbsolutePath()); 264 return true; 265 } 266 return false; 267 } 268 269 private static boolean isJavaFile(File file) { 270 return file != null && file.exists() && !file.isDirectory() 271 && file.getName().endsWith(".java"); 272 } 273 } 274 | Popular Tags |