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

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