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.

LstBuildConfigManager.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Helen Hawkins Converted to new interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.ajde.internal;
  15. import java.io.File;
  16. import java.io.FileFilter;
  17. import java.io.FilenameFilter;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.Comparator;
  21. import java.util.List;
  22. import org.aspectj.ajde.Ajde;
  23. import org.aspectj.ajde.ui.BuildConfigModel;
  24. import org.aspectj.ajde.ui.BuildConfigNode;
  25. import org.aspectj.ajdt.ajc.ConfigParser;
  26. import org.aspectj.bridge.IMessage;
  27. import org.aspectj.bridge.Message;
  28. import org.aspectj.bridge.SourceLocation;
  29. import org.aspectj.util.FileUtil;
  30. /**
  31. * @author Mik Kersten
  32. */
  33. public class LstBuildConfigManager implements BuildConfigManager {
  34. private List<String> allBuildConfigFiles;
  35. private List<BuildConfigListener> listeners = new ArrayList<>();
  36. private LstBuildConfigFileUpdater fileUpdater = new LstBuildConfigFileUpdater();
  37. protected String currConfigFilePath = null;
  38. private static final FilenameFilter SOURCE_FILE_FILTER = new FilenameFilter() {
  39. @Override
  40. public boolean accept(File dir, String name) {
  41. return FileUtil.hasSourceSuffix(name) || name.endsWith(".lst");
  42. }
  43. };
  44. private static final FileFilter DIR_FILTER = new FileFilter() {
  45. @Override
  46. public boolean accept(File file) {
  47. return file.isDirectory();
  48. }
  49. };
  50. @Override
  51. public BuildConfigModel buildModel(String configFilePath) {
  52. File configFile = new File(configFilePath);
  53. String rootPath = configFile.getParent();
  54. String configFileName = configFile.getName();
  55. BuildConfigModel model = new BuildConfigModel(configFilePath);
  56. List<File> configFiles = new ArrayList<>();
  57. List<File> importedFiles = new ArrayList<>();
  58. List<String> badEntries = null;
  59. try {
  60. LstBuildConfigFileParser configParser = new LstBuildConfigFileParser(configFilePath);
  61. configParser.parseConfigFile(new File(configFilePath));
  62. configFiles = configParser.getFiles();
  63. importedFiles = configParser.getImportedFiles();
  64. badEntries = configParser.getProblemEntries();
  65. } catch (ConfigParser.ParseException pe) {
  66. // String filePath = "<unknown>";
  67. // if (pe.getFile() != null) filePath = pe.getFile().getAbsolutePath();
  68. IMessage message = new Message(pe.getMessage(), IMessage.ERROR, pe, new SourceLocation(pe.getFile(), pe.getLine(), 1));
  69. Ajde.getDefault().getMessageHandler().handleMessage(message);
  70. }
  71. List<String> relativePaths = relativizeFilePaths(configFiles, rootPath);
  72. BuildConfigNode root = new BuildConfigNode(configFileName, BuildConfigNode.Kind.FILE_LST, rootPath);
  73. buildDirTree(root, rootPath, importedFiles, configFileName);
  74. model.setRoot(root);
  75. addFilesToDirTree(model, relativePaths, badEntries);
  76. pruneEmptyDirs(root);
  77. sortModel(model.getRoot(), ALPHABETICAL_COMPARATOR);
  78. // addImportedFilesToDirTree(model, importedFiles);
  79. addProblemEntries(root, badEntries);
  80. return model;
  81. }
  82. private void addProblemEntries(BuildConfigNode root, List<String> badEntries) {
  83. for (String string : badEntries) {
  84. root.addChild(new BuildConfigNode(string.toString(), BuildConfigNode.Kind.ERROR, null));
  85. }
  86. }
  87. @Override
  88. public void writeModel(BuildConfigModel model) {
  89. // final List paths = new ArrayList();
  90. // StructureWalker walker = new StructureWalker() {
  91. // protected void postProcess(StructureNode node) {
  92. // BuildConfigNode configNode = (BuildConfigNode)node;
  93. // if (configNode.isActive() && configNode.isValidResource()) {
  94. // paths.add(configNode.getResourcePath());
  95. // }
  96. // }
  97. // };
  98. // model.getRoot().walk(walker);
  99. List<BuildConfigNode> activeSourceFiles = model.getActiveNodes(BuildConfigNode.Kind.FILE_ASPECTJ);
  100. activeSourceFiles.addAll(model.getActiveNodes(BuildConfigNode.Kind.FILE_JAVA));
  101. List<BuildConfigNode> activeImportedFiles = model.getActiveNodes(BuildConfigNode.Kind.FILE_LST);
  102. fileUpdater.writeConfigFile(model.getSourceFile(), activeSourceFiles, activeImportedFiles);
  103. }
  104. @Override
  105. public void writePaths(String configFilePath, List<String> files) {
  106. fileUpdater.writeConfigFile(configFilePath, files);
  107. }
  108. @Override
  109. public void addFilesToConfig(String configFilePath, List paths) {
  110. }
  111. @Override
  112. public void removeFilesFromConfig(String configFilePath, List files) {
  113. }
  114. private List<String> relativizeFilePaths(List<File> configFiles, String rootPath) {
  115. List<String> relativePathsList = new ArrayList<>();
  116. for (File file : configFiles) {
  117. relativePathsList.add(fileUpdater.relativizePath(file.getPath(), rootPath));
  118. }
  119. return relativePathsList;
  120. }
  121. // private String relativizePath(String path, String rootPath) {
  122. // path = path.replace('\\', '/');
  123. // rootPath = rootPath.replace('\\', '/');
  124. // int pathIndex = path.indexOf(rootPath);
  125. // if (pathIndex > -1) {
  126. // return path.substring(pathIndex + rootPath.length() + 1);
  127. // } else {
  128. // return path;
  129. // }
  130. // }
  131. private void buildDirTree(BuildConfigNode node, String rootPath, List importedFiles, String configFileName) {
  132. File[] dirs = new File(node.getResourcePath()).listFiles(DIR_FILTER);
  133. if (dirs == null)
  134. return;
  135. for (File dir2 : dirs) {
  136. BuildConfigNode dir = new BuildConfigNode(dir2.getName(), BuildConfigNode.Kind.DIRECTORY, dir2.getPath());
  137. File[] files = dir2.listFiles(SOURCE_FILE_FILTER);
  138. for (File file2 : files) {
  139. if (file2 != null) {// && !files[j].getName().endsWith(".lst")) {
  140. String filePath = fileUpdater.relativizePath(file2.getPath(), rootPath);
  141. BuildConfigNode.Kind kind = BuildConfigNode.Kind.FILE_JAVA;
  142. if (!file2.getName().endsWith(".lst")) {
  143. // kind = BuildConfigNode.Kind.FILE_LST;
  144. BuildConfigNode file = new BuildConfigNode(file2.getName(), kind, filePath);
  145. file.setActive(false);
  146. dir.addChild(file);
  147. }
  148. }
  149. }
  150. node.addChild(dir);
  151. // boolean foundMatch = false;
  152. for (Object file : importedFiles) {
  153. File importedFile = (File) file;
  154. if (importedFile.getParentFile().getAbsolutePath().equals(dir2.getAbsolutePath())) {
  155. // foundMatch = true;
  156. BuildConfigNode importedFileNode = new BuildConfigNode(importedFile.getName(), BuildConfigNode.Kind.FILE_LST,
  157. fileUpdater.relativizePath(importedFile.getPath(), rootPath));
  158. importedFileNode.setActive(true);
  159. // dir.getChildren().clear();
  160. boolean found = false;
  161. for (BuildConfigNode buildConfigNode : dir.getChildren()) {
  162. if (buildConfigNode.getName().equals(importedFile.getName())) {
  163. found = true;
  164. }
  165. }
  166. if (!found)
  167. dir.addChild(importedFileNode);
  168. }
  169. }
  170. // if (!foundMatch)
  171. buildDirTree(dir, rootPath, importedFiles, configFileName);
  172. }
  173. if (node.getName().endsWith(".lst")) {
  174. File[] files = new File(rootPath).listFiles(SOURCE_FILE_FILTER);
  175. if (files == null)
  176. return;
  177. for (File file2 : files) {
  178. if (file2 != null && !file2.getName().equals(configFileName)) {// && !files[i].getName().endsWith(".lst")) {
  179. BuildConfigNode.Kind kind = BuildConfigNode.Kind.FILE_JAVA;
  180. if (file2.getName().endsWith(".lst")) {
  181. kind = BuildConfigNode.Kind.FILE_LST;
  182. }
  183. BuildConfigNode file = new BuildConfigNode(file2.getName(), kind, file2.getName());
  184. file.setActive(false);
  185. node.addChild(file);
  186. }
  187. }
  188. }
  189. }
  190. private void addFilesToDirTree(BuildConfigModel model, List configFiles, List badEntries) {
  191. for (Object configFile : configFiles) {
  192. String path = (String) configFile;
  193. if (path.startsWith("..")) {
  194. File file = new File(path);
  195. BuildConfigNode node = new BuildConfigNode(file.getName(), BuildConfigNode.Kind.FILE_JAVA, path);
  196. BuildConfigNode upPath = model.getNodeForPath(file.getParentFile().getPath());
  197. if (upPath == model.getRoot()) {
  198. upPath = new BuildConfigNode(file.getParentFile().getPath(), BuildConfigNode.Kind.DIRECTORY, file
  199. .getParentFile().getAbsolutePath());
  200. model.getRoot().addChild(upPath);
  201. }
  202. node.setActive(true);
  203. upPath.addChild(node);
  204. } else if (!(new File(path).isAbsolute())) {
  205. // String name = new File(path).getName();
  206. BuildConfigNode existingNode = model.getNodeForPath(path);
  207. existingNode.setActive(true);
  208. } else {
  209. badEntries.add("Use relative paths only, omitting: " + path);
  210. }
  211. }
  212. }
  213. private boolean pruneEmptyDirs(BuildConfigNode node) {
  214. List<BuildConfigNode> nodesToRemove = new ArrayList<>();
  215. for (BuildConfigNode currNode : node.getChildren()) {
  216. boolean hasValidChildren = pruneEmptyDirs(currNode);
  217. if (!currNode.isValidResource() && !hasValidChildren) {
  218. nodesToRemove.add(currNode);
  219. }
  220. }
  221. for (BuildConfigNode currNode : nodesToRemove) {
  222. node.removeChild(currNode);
  223. }
  224. return node.getChildren().size() > 0;
  225. }
  226. @Override
  227. public String getActiveConfigFile() {
  228. return currConfigFilePath;
  229. }
  230. @Override
  231. public void setActiveConfigFile(String currConfigFilePath) {
  232. if (currConfigFilePath == null)
  233. return;
  234. this.currConfigFilePath = currConfigFilePath;
  235. notifyConfigChanged();
  236. }
  237. @Override
  238. public void addListener(BuildConfigListener configurationListener) {
  239. listeners.add(configurationListener);
  240. }
  241. @Override
  242. public void removeListener(BuildConfigListener configurationListener) {
  243. listeners.remove(configurationListener);
  244. }
  245. private void notifyConfigChanged() {
  246. for (BuildConfigListener element : listeners) {
  247. element.currConfigChanged(currConfigFilePath);
  248. }
  249. }
  250. // private void notifyConfigsListUpdated() {
  251. // for (Iterator it = listeners.iterator(); it.hasNext(); ) {
  252. // ((BuildConfigListener)it.next()).configsListUpdated(configFiles);
  253. // }
  254. // }
  255. //
  256. private void sortModel(BuildConfigNode node, Comparator<BuildConfigNode> comparator) {
  257. if (node == null || node.getChildren() == null)
  258. return;
  259. node.getChildren().sort(comparator);
  260. for (BuildConfigNode nextNode : node.getChildren()) {
  261. if (nextNode != null)
  262. sortModel(nextNode, comparator);
  263. }
  264. }
  265. private static final Comparator<BuildConfigNode> ALPHABETICAL_COMPARATOR = new Comparator<BuildConfigNode>() {
  266. @Override
  267. public int compare(BuildConfigNode n1, BuildConfigNode n2) {
  268. return n1.getName().compareTo(n2.getName());
  269. }
  270. };
  271. @Override
  272. public List<String> getAllBuildConfigFiles() {
  273. if (allBuildConfigFiles == null) {
  274. allBuildConfigFiles = new ArrayList<>();
  275. if (getActiveConfigFile() != null) {
  276. allBuildConfigFiles.add(getActiveConfigFile());
  277. }
  278. }
  279. return allBuildConfigFiles;
  280. }
  281. }