Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ConfigParser.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.ajdt.ajc;
  14. import java.io.BufferedReader;
  15. import java.io.File;
  16. import java.io.FileFilter;
  17. import java.io.FileReader;
  18. import java.io.IOException;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. public class ConfigParser {
  22. Location location;
  23. protected File relativeDirectory = null;
  24. protected List files = new LinkedList();
  25. private boolean fileParsed = false;
  26. protected static String CONFIG_MSG = "build config error: ";
  27. public List getFiles() { return files; }
  28. public void parseCommandLine(String[] argsArray) throws ParseException {
  29. location = new CommandLineLocation();
  30. LinkedList args = new LinkedList();
  31. for (int i = 0; i < argsArray.length; i++) {
  32. args.add(new Arg(argsArray[i], location));
  33. }
  34. parseArgs(args);
  35. }
  36. public void parseConfigFile(File configFile) throws ParseException {
  37. if (fileParsed == true) {
  38. throw new ParseException(CONFIG_MSG + "The file has already been parsed.", null);
  39. } else {
  40. parseConfigFileHelper(configFile);
  41. }
  42. }
  43. /**
  44. * @throws ParseException if the config file has already been prased.
  45. */
  46. private void parseConfigFileHelper(File configFile) {
  47. if (!configFile.exists()) {
  48. showError("file does not exist: " + configFile.getPath());
  49. return;
  50. }
  51. LinkedList args = new LinkedList();
  52. int lineNum = 0;
  53. try {
  54. BufferedReader stream =
  55. new BufferedReader(new FileReader(configFile));
  56. String line = null;
  57. while ( (line = stream.readLine()) != null) {
  58. lineNum += 1;
  59. line = stripWhitespaceAndComments(line);
  60. if (line.length() == 0) continue;
  61. args.add(new Arg(line, new CPSourceLocation(configFile, lineNum)));
  62. }
  63. stream.close();
  64. } catch (IOException e) {
  65. location = new CPSourceLocation(configFile, lineNum);
  66. showError("error reading config file: " + e.toString());
  67. }
  68. File oldRelativeDirectory = relativeDirectory; // for nested arg files;
  69. relativeDirectory = configFile.getParentFile();
  70. parseArgs(args);
  71. relativeDirectory = oldRelativeDirectory;
  72. fileParsed = true;
  73. }
  74. File getCurrentDir() {
  75. return location.getDirectory();
  76. }
  77. String stripSingleLineComment(String s, String commentString) {
  78. int commentStart = s.indexOf(commentString);
  79. if (commentStart == -1) return s;
  80. else return s.substring(0, commentStart);
  81. }
  82. String stripWhitespaceAndComments(String s) {
  83. s = stripSingleLineComment(s, "//");
  84. s = stripSingleLineComment(s, "#");
  85. s = s.trim();
  86. if (s.startsWith("\"") && s.endsWith("\"")) {
  87. s = s.substring(1, s.length()-1);
  88. }
  89. return s;
  90. }
  91. /** ??? We would like to call a showNonFatalError method here
  92. * to show all errors in config files before aborting the compilation
  93. */
  94. protected void addFile(File sourceFile) {
  95. if (!sourceFile.isFile()) {
  96. showError("source file does not exist: " + sourceFile.getPath());
  97. }
  98. files.add(sourceFile);
  99. }
  100. void addFileOrPattern(File sourceFile) {
  101. if (sourceFile.getName().equals("*.java")) {
  102. addFiles(sourceFile.getParentFile(), new FileFilter() {
  103. public boolean accept(File f) {
  104. return f != null && f.getName().endsWith(".java");
  105. }});
  106. } else if (sourceFile.getName().equals("*.aj")) {
  107. addFiles(sourceFile.getParentFile(), new FileFilter() {
  108. public boolean accept(File f) {
  109. return f != null && f.getName().endsWith(".aj");
  110. }});
  111. } else {
  112. addFile(sourceFile);
  113. }
  114. }
  115. void addFiles(File dir, FileFilter filter) {
  116. if (dir == null) dir = new File(System.getProperty("user.dir"));
  117. if (!dir.isDirectory()) {
  118. showError("can't find " + dir.getPath());
  119. } else {
  120. File[] files = dir.listFiles(filter);
  121. if (files.length == 0) {
  122. showWarning("no matching files found in: " + dir);
  123. }
  124. for (int i = 0; i < files.length; i++) {
  125. addFile(files[i]);
  126. }
  127. }
  128. }
  129. protected void parseOption(String arg, LinkedList args) {
  130. showWarning("unrecognized option: " + arg);
  131. }
  132. protected void showWarning(String message) {
  133. if (location != null) {
  134. message += " at " + location.toString();
  135. }
  136. System.err.println(CONFIG_MSG + message);
  137. }
  138. protected void showError(String message) {
  139. throw new ParseException(CONFIG_MSG + message, location);
  140. }
  141. void parseArgs(LinkedList args) {
  142. while (args.size() > 0) parseOneArg(args);
  143. }
  144. protected Arg removeArg(LinkedList args) {
  145. if (args.size() == 0) {
  146. showError("value missing");
  147. return null;
  148. } else {
  149. return (Arg)args.removeFirst();
  150. }
  151. }
  152. protected String removeStringArg(LinkedList args) {
  153. Arg arg = removeArg(args);
  154. if (arg == null) return null;
  155. return arg.getValue();
  156. }
  157. boolean isSourceFileName(String s) {
  158. if (s.endsWith(".java")) return true;
  159. if (s.endsWith(".aj")) return true;
  160. if (s.endsWith(".ajava")) {
  161. showWarning(".ajava is deprecated, replace with .aj or .java: " + s);
  162. return true;
  163. }
  164. return false;
  165. }
  166. void parseOneArg(LinkedList args) {
  167. Arg arg = removeArg(args);
  168. String v = arg.getValue();
  169. location = arg.getLocation();
  170. if (v.startsWith("@")) {
  171. parseImportedConfigFile(v.substring(1));
  172. } else if (v.equals("-argfile")) {
  173. parseConfigFileHelper(makeFile(removeArg(args).getValue()));
  174. } else if (isSourceFileName(v)) {
  175. addFileOrPattern(makeFile(v));
  176. } else {
  177. parseOption(arg.getValue(), args);
  178. }
  179. }
  180. protected void parseImportedConfigFile(String relativeFilePath) {
  181. parseConfigFileHelper(makeFile(relativeFilePath));
  182. }
  183. public File makeFile(String name) {
  184. if (relativeDirectory != null) {
  185. return makeFile(relativeDirectory,name);
  186. } else {
  187. return makeFile(getCurrentDir(), name);
  188. }
  189. }
  190. private File makeFile(File dir, String name) {
  191. name = name.replace('/', File.separatorChar);
  192. File ret = new File(name);
  193. boolean isAbsolute = ret.isAbsolute()
  194. || (ret.exists() && ret.getPath().startsWith(File.separator));
  195. if (!isAbsolute && (dir != null)) {
  196. ret = new File(dir, name);
  197. }
  198. try {
  199. ret = ret.getCanonicalFile();
  200. } catch (IOException ioEx) {
  201. // proceed without canonicalization
  202. // so nothing to do here
  203. }
  204. return ret;
  205. }
  206. protected static class Arg {
  207. private Location location;
  208. private String value;
  209. public Arg(String value, Location location) {
  210. this.value = value;
  211. this.location = location;
  212. }
  213. public void setValue(String value) {
  214. this.value = value;
  215. }
  216. public void setLocation(Location location) {
  217. this.location = location;
  218. }
  219. public String getValue() { return value; }
  220. public Location getLocation() { return location; }
  221. }
  222. static abstract class Location {
  223. public abstract File getFile();
  224. public abstract File getDirectory();
  225. public abstract int getLine();
  226. public abstract String toString();
  227. }
  228. static class CPSourceLocation extends Location {
  229. private int line;
  230. private File file;
  231. public CPSourceLocation(File file, int line) {
  232. this.line = line;
  233. this.file = file;
  234. }
  235. public File getFile() { return file; }
  236. public File getDirectory() { return file.getParentFile(); }
  237. public int getLine() { return line; }
  238. public String toString() {
  239. return file.getPath()+":"+line;
  240. }
  241. }
  242. static class CommandLineLocation extends Location {
  243. public File getFile() {
  244. return new File(System.getProperty("user.dir"));
  245. }
  246. public File getDirectory() {
  247. return new File(System.getProperty("user.dir"));
  248. }
  249. public int getLine() { return -1; }
  250. public String toString() {
  251. return "command-line";
  252. }
  253. }
  254. public static class ParseException extends RuntimeException {
  255. private Location location;
  256. public ParseException(String message, Location location) {
  257. super(message);
  258. this.location = location;
  259. }
  260. public int getLine() {
  261. if (location == null) return -1;
  262. return location.getLine();
  263. }
  264. public File getFile() {
  265. if (location == null) return null;
  266. return location.getFile();
  267. }
  268. }
  269. }