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

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