1 24 25 26 package org.aspectj.util; 27 28 import java.util.*; 29 import java.io.*; 30 31 public class ConfigParser { 32 Location location; 33 protected List files = new LinkedList(); 34 private boolean fileParsed = false; 35 36 public List getFiles() { return files; } 37 38 public void parseCommandLine(String [] argsArray) throws ParseException { 39 location = new CommandLineLocation(); 40 LinkedList args = new LinkedList(); 41 for (int i = 0; i < argsArray.length; i++) { 42 args.add(new Arg(argsArray[i], location)); 43 } 44 parseArgs(args); 45 } 46 47 public void parseConfigFile(File configFile) throws ParseException { 48 if (fileParsed == true) { 49 throw new ParseException("The file has already been parsed.", null); 50 } else { 51 parseConfigFileHelper(configFile); 52 } 53 } 54 55 58 private void parseConfigFileHelper(File configFile) { 59 if (!configFile.exists()) { 60 showError("file does not exist: " + configFile.getPath()); 61 return; 62 } 63 64 LinkedList args = new LinkedList(); 65 int lineNum = 0; 66 67 try { 68 BufferedReader stream = 69 new BufferedReader(new FileReader(configFile)); 70 String line = null; 71 while ( (line = stream.readLine()) != null) { 72 lineNum += 1; 73 line = stripWhitespaceAndComments(line); 74 if (line.length() == 0) continue; 75 args.add(new Arg(line, new SourceLocation(configFile, lineNum))); 76 } 77 } catch (IOException e) { 78 location = new SourceLocation(configFile, lineNum); 79 showError("error reading config file: " + e.toString()); 80 } 81 parseArgs(args); 82 fileParsed = true; 83 } 84 85 File getCurrentDir() { 86 return location.getDirectory(); 87 } 88 89 String stripSingleLineComment(String s, String commentString) { 90 int commentStart = s.indexOf(commentString); 91 if (commentStart == -1) return s; 92 else return s.substring(0, commentStart); 93 } 94 95 String stripWhitespaceAndComments(String s) { 96 s = stripSingleLineComment(s, "//"); 97 s = stripSingleLineComment(s, "#"); 98 s = s.trim(); 99 if (s.startsWith("\"") && s.endsWith("\"")) { 100 s = s.substring(1, s.length()-1); 101 } 102 return s; 103 } 104 105 106 109 protected void addFile(File sourceFile) { 110 if (!sourceFile.isFile()) { 111 showError("source file does not exist: " + sourceFile.getPath()); 112 } 113 114 files.add(sourceFile); 115 } 116 117 void addFileOrPattern(File sourceFile) { 118 if (sourceFile.getName().equals("*.java")) { 119 addFiles(sourceFile.getParentFile(), new FileFilter() { 120 public boolean accept(File f) { 121 return f != null && f.getName().endsWith(".java"); 122 }}); 123 } else if (sourceFile.getName().equals("*.aj")) { 124 addFiles(sourceFile.getParentFile(), new FileFilter() { 125 public boolean accept(File f) { 126 return f != null && f.getName().endsWith(".aj"); 127 }}); 128 } else { 129 addFile(sourceFile); 130 } 131 } 132 133 void addFiles(File dir, FileFilter filter) { 134 if (dir == null) dir = new File("."); 135 136 if (!dir.isDirectory()) { 137 showError("can't find " + dir.getPath()); 138 } 139 140 File[] files = dir.listFiles(filter); 141 if (files.length == 0) { 142 showWarning("no matching files found in: " + dir); 143 } 144 145 for (int i = 0; i < files.length; i++) { 146 addFile(files[i]); 147 } 148 } 149 150 protected void parseOption(String arg, LinkedList args) { 151 showWarning("unrecognized option: " + arg); 152 } 153 154 protected void showWarning(String message) { 155 if (location != null) { 156 message += " at " + location.toString(); 157 } 158 System.err.println(message); 159 } 160 161 protected void showError(String message) { 162 throw new ParseException(message, location); 163 } 164 165 void parseArgs(LinkedList args) { 166 while (args.size() > 0) parseOneArg(args); 167 } 168 169 protected Arg removeArg(LinkedList args) { 170 if (args.size() == 0) { 171 showError("value missing"); 172 return null; 173 } else { 174 return (Arg)args.removeFirst(); 175 } 176 } 177 178 protected String removeStringArg(LinkedList args) { 179 Arg arg = removeArg(args); 180 if (arg == null) return null; 181 return arg.getValue(); 182 } 183 184 boolean isSourceFileName(String s) { 185 if (s.endsWith(".java")) return true; 186 if (s.endsWith(".aj")) return true; 187 if (s.endsWith(".ajava")) { 188 showWarning(".ajava is deprecated, replace with .aj or .java: " + s); 189 return true; 190 } 191 return false; 192 } 193 194 void parseOneArg(LinkedList args) { 195 Arg arg = removeArg(args); 196 String v = arg.getValue(); 197 location = arg.getLocation(); 198 if (v.startsWith("@")) { 199 parseImportedConfigFile(v.substring(1)); 200 } else if (v.equals("-argfile")) { 201 parseConfigFileHelper(makeFile(removeArg(args).getValue())); 202 } else if (isSourceFileName(v)) { 203 addFileOrPattern(makeFile(v)); 204 } else { 205 parseOption(arg.getValue(), args); 206 } 207 } 208 209 protected void parseImportedConfigFile(String relativeFilePath) { 210 parseConfigFileHelper(makeFile(relativeFilePath)); 211 } 212 213 public File makeFile(String name) { 214 return makeFile(getCurrentDir(), name); 215 } 216 217 File makeFile(File dir, String name) { 218 name = name.replace('/', File.separatorChar); 219 File ret = new File(name); 220 if (dir == null || ret.isAbsolute()) return ret; 221 return new File(dir, name); 222 } 223 224 225 protected static class Arg { 226 private Location location; 227 private String value; 228 public Arg(String value, Location location) { 229 this.value = value; 230 this.location = location; 231 } 232 233 public String getValue() { return value; } 234 public Location getLocation() { return location; } 235 } 236 237 static abstract class Location { 238 public abstract File getFile(); 239 public abstract File getDirectory(); 240 public abstract int getLine(); 241 public abstract String toString(); 242 } 243 244 static class SourceLocation extends Location { 245 private int line; 246 private File file; 247 public SourceLocation(File file, int line) { 248 this.line = line; 249 this.file = file; 250 } 251 252 public File getFile() { return file; } 253 public File getDirectory() { return file.getParentFile(); } 254 public int getLine() { return line; } 255 256 public String toString() { 257 return file.getPath()+":"+line; 258 } 259 } 260 261 static class CommandLineLocation extends Location { 262 public File getFile() { 263 return new File("."); 264 } 265 266 public File getDirectory() { 267 return new File("."); 268 } 269 public int getLine() { return -1; } 270 public String toString() { 271 return "command-line"; 272 } 273 } 274 275 public static class ParseException extends RuntimeException { 276 private Location location; 277 278 public ParseException(String message, Location location) { 279 super(message); 280 this.location = location; 281 } 282 283 public int getLine() { 284 if (location == null) return -1; 285 return location.getLine(); 286 } 287 public File getFile() { 288 if (location == null) return null; 289 return location.getFile(); 290 } 291 } 292 } 293 | Popular Tags |