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

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