You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigParser.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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.ArrayList;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.StringTokenizer;
  23. public class ConfigParser {
  24. Location location;
  25. protected File relativeDirectory = null;
  26. protected List<File> files = new LinkedList<>();
  27. protected List<File> xmlfiles = new ArrayList<>();
  28. private boolean fileParsed = false;
  29. protected static String CONFIG_MSG = "build config error: ";
  30. public List<File> getFiles() {
  31. return files;
  32. }
  33. public List<File> getXmlFiles() {
  34. return xmlfiles;
  35. }
  36. public void parseCommandLine(String[] argsArray) throws ParseException {
  37. location = new CommandLineLocation();
  38. LinkedList<Arg> args = new LinkedList<>();
  39. for (String s : argsArray) {
  40. args.add(new Arg(s, location));
  41. }
  42. String aspectjOptions = null;
  43. try {
  44. aspectjOptions = System.getenv("ASPECTJ_OPTS");
  45. if (aspectjOptions == null) {
  46. aspectjOptions = System.getProperty("ASPECTJ_OPTS");
  47. }
  48. } catch (Throwable t) {
  49. aspectjOptions = null;
  50. }
  51. if (aspectjOptions != null) {
  52. StringTokenizer st = new StringTokenizer(aspectjOptions);
  53. while (st.hasMoreElements()) {
  54. args.add(new Arg(st.nextToken(),location));
  55. }
  56. }
  57. parseArgs(args);
  58. }
  59. public void parseConfigFile(File configFile) throws ParseException {
  60. if (fileParsed == true) {
  61. throw new ParseException(CONFIG_MSG + "The file has already been parsed.", null);
  62. } else {
  63. parseConfigFileHelper(configFile);
  64. }
  65. }
  66. /**
  67. * @throws ParseException if the config file has already been prased.
  68. */
  69. private void parseConfigFileHelper(File configFile) {
  70. if (!configFile.exists()) {
  71. showError("file does not exist: " + configFile.getPath());
  72. return;
  73. }
  74. LinkedList<Arg> args = new LinkedList<>();
  75. int lineNum = 0;
  76. try {
  77. BufferedReader stream = new BufferedReader(new FileReader(configFile));
  78. String line = null;
  79. while ((line = stream.readLine()) != null) {
  80. lineNum += 1;
  81. line = stripWhitespaceAndComments(line);
  82. if (line.length() == 0) {
  83. continue;
  84. }
  85. args.add(new Arg(line, new CPSourceLocation(configFile, lineNum)));
  86. }
  87. stream.close();
  88. } catch (IOException e) {
  89. location = new CPSourceLocation(configFile, lineNum);
  90. showError("error reading config file: " + e.toString());
  91. }
  92. File oldRelativeDirectory = relativeDirectory; // for nested arg files;
  93. relativeDirectory = configFile.getParentFile();
  94. parseArgs(args);
  95. relativeDirectory = oldRelativeDirectory;
  96. fileParsed = true;
  97. }
  98. File getCurrentDir() {
  99. return location.getDirectory();
  100. }
  101. String stripSingleLineComment(String s, String commentString) {
  102. int commentStart = s.indexOf(commentString);
  103. if (commentStart == -1) {
  104. return s;
  105. } else {
  106. return s.substring(0, commentStart);
  107. }
  108. }
  109. String stripWhitespaceAndComments(String s) {
  110. s = stripSingleLineComment(s, "//");
  111. s = stripSingleLineComment(s, "#");
  112. s = s.trim();
  113. if (s.startsWith("\"") && s.endsWith("\"")) {
  114. if (s.length() == 1) {
  115. return "";
  116. } else {
  117. s = s.substring(1, s.length() - 1);
  118. }
  119. }
  120. return s;
  121. }
  122. /**
  123. * ??? We would like to call a showNonFatalError method here to show all errors in config files before aborting the compilation
  124. */
  125. protected void addFile(File sourceFile) {
  126. if (!sourceFile.isFile()) {
  127. showError("source file does not exist: " + sourceFile.getPath());
  128. }
  129. files.add(sourceFile);
  130. }
  131. protected void addXmlFile(File xmlFile) {
  132. if (!xmlFile.isFile()) {
  133. showError("XML file does not exist: " + xmlFile.getPath());
  134. }
  135. xmlfiles.add(xmlFile);
  136. }
  137. void addFileOrPattern(File sourceFile) {
  138. if (sourceFile.getName().charAt(0) == '*') {
  139. if (sourceFile.getName().equals("*.java")) {
  140. addFiles(sourceFile.getParentFile(), new FileFilter() {
  141. @Override
  142. public boolean accept(File f) {
  143. return f != null && f.getName().endsWith(".java");
  144. }
  145. });
  146. } else if (sourceFile.getName().equals("*.aj")) {
  147. addFiles(sourceFile.getParentFile(), new FileFilter() {
  148. @Override
  149. public boolean accept(File f) {
  150. return f != null && f.getName().endsWith(".aj");
  151. }
  152. });
  153. } else {
  154. addFile(sourceFile);
  155. }
  156. } else {
  157. addFile(sourceFile);
  158. }
  159. }
  160. void addFiles(File dir, FileFilter filter) {
  161. if (dir == null) {
  162. dir = new File(System.getProperty("user.dir"));
  163. }
  164. if (!dir.isDirectory()) {
  165. showError("can't find " + dir.getPath());
  166. } else {
  167. File[] files = dir.listFiles(filter);
  168. if (files.length == 0) {
  169. showWarning("no matching files found in: " + dir);
  170. }
  171. for (File file : files) {
  172. addFile(file);
  173. }
  174. }
  175. }
  176. protected void parseOption(String arg, LinkedList<Arg> args) {
  177. showWarning("unrecognized option: " + arg);
  178. }
  179. protected void showWarning(String message) {
  180. if (location != null) {
  181. message += " at " + location.toString();
  182. }
  183. System.err.println(CONFIG_MSG + message);
  184. }
  185. protected void showError(String message) {
  186. throw new ParseException(CONFIG_MSG + message, location);
  187. }
  188. void parseArgs(LinkedList<Arg> args) {
  189. while (args.size() > 0) {
  190. parseOneArg(args);
  191. }
  192. }
  193. protected Arg removeArg(LinkedList<Arg> args) {
  194. if (args.size() == 0) {
  195. showError("value missing");
  196. return null;
  197. } else {
  198. return args.removeFirst();
  199. }
  200. }
  201. protected String removeStringArg(LinkedList<Arg> args) {
  202. Arg arg = removeArg(args);
  203. if (arg == null) {
  204. return null;
  205. }
  206. return arg.getValue();
  207. }
  208. /**
  209. * aop.xml configuration files can be passed on the command line.
  210. */
  211. boolean isXml(String s) {
  212. return s.endsWith(".xml");
  213. }
  214. boolean isSourceFileName(String s) {
  215. if (s.endsWith(".java")) {
  216. return true;
  217. }
  218. if (s.endsWith(".aj")) {
  219. return true;
  220. }
  221. // if (s.endsWith(".ajava")) {
  222. // showWarning(".ajava is deprecated, replace with .aj or .java: " + s);
  223. // return true;
  224. // }
  225. return false;
  226. }
  227. void parseOneArg(LinkedList<Arg> args) {
  228. Arg arg = removeArg(args);
  229. String v = arg.getValue();
  230. location = arg.getLocation();
  231. if (v.startsWith("@")) {
  232. parseImportedConfigFile(v.substring(1));
  233. } else if (v.equals("-argfile")) {
  234. parseConfigFileHelper(makeFile(removeArg(args).getValue()));
  235. } else if (isSourceFileName(v)) {
  236. addFileOrPattern(makeFile(v));
  237. if (v.endsWith("module-info.java")) {
  238. parseOption(arg.getValue(), args);
  239. }
  240. } else if (isXml(v)) {
  241. addXmlFile(makeFile(v));
  242. } else {
  243. parseOption(arg.getValue(), args);
  244. }
  245. }
  246. protected void parseImportedConfigFile(String relativeFilePath) {
  247. parseConfigFileHelper(makeFile(relativeFilePath));
  248. }
  249. public File makeFile(String name) {
  250. if (relativeDirectory != null) {
  251. return makeFile(relativeDirectory, name);
  252. } else {
  253. return makeFile(getCurrentDir(), name);
  254. }
  255. }
  256. private File makeFile(File dir, String name) {
  257. name = name.replace('/', File.separatorChar);
  258. File ret = new File(name);
  259. boolean isAbsolute = ret.isAbsolute() || (ret.exists() && ret.getPath().startsWith(File.separator));
  260. if (!isAbsolute && (dir != null)) {
  261. ret = new File(dir, name);
  262. }
  263. try {
  264. ret = ret.getCanonicalFile();
  265. } catch (IOException ioEx) {
  266. // proceed without canonicalization
  267. // so nothing to do here
  268. }
  269. return ret;
  270. }
  271. protected static class Arg {
  272. private Location location;
  273. private String value;
  274. @Override
  275. public String toString() {
  276. return "Arg[location="+location+" value="+value+"]";
  277. }
  278. public Arg(String value, Location location) {
  279. this.value = value;
  280. this.location = location;
  281. }
  282. public void setValue(String value) {
  283. this.value = value;
  284. }
  285. public void setLocation(Location location) {
  286. this.location = location;
  287. }
  288. public String getValue() {
  289. return value;
  290. }
  291. public Location getLocation() {
  292. return location;
  293. }
  294. }
  295. static abstract class Location {
  296. public abstract File getFile();
  297. public abstract File getDirectory();
  298. public abstract int getLine();
  299. @Override
  300. public abstract String toString();
  301. }
  302. static class CPSourceLocation extends Location {
  303. private int line;
  304. private File file;
  305. public CPSourceLocation(File file, int line) {
  306. this.line = line;
  307. this.file = file;
  308. }
  309. @Override
  310. public File getFile() {
  311. return file;
  312. }
  313. @Override
  314. public File getDirectory() {
  315. return file.getParentFile();
  316. }
  317. @Override
  318. public int getLine() {
  319. return line;
  320. }
  321. @Override
  322. public String toString() {
  323. return file.getPath() + ":" + line;
  324. }
  325. }
  326. static class CommandLineLocation extends Location {
  327. @Override
  328. public File getFile() {
  329. return new File(System.getProperty("user.dir"));
  330. }
  331. @Override
  332. public File getDirectory() {
  333. return new File(System.getProperty("user.dir"));
  334. }
  335. @Override
  336. public int getLine() {
  337. return -1;
  338. }
  339. @Override
  340. public String toString() {
  341. return "command-line";
  342. }
  343. }
  344. public static class ParseException extends RuntimeException {
  345. private Location location;
  346. public ParseException(String message, Location location) {
  347. super(message);
  348. this.location = location;
  349. }
  350. public int getLine() {
  351. if (location == null) {
  352. return -1;
  353. }
  354. return location.getLine();
  355. }
  356. public File getFile() {
  357. if (location == null) {
  358. return null;
  359. }
  360. return location.getFile();
  361. }
  362. }
  363. }