KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > ajde > internal > LstBuildConfigManager


1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3  *
4  * This file is part of the IDE support for the AspectJ(tm)
5  * programming language; see http://aspectj.org
6  *
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.1 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is AspectJ.
18  *
19  * The Initial Developer of the Original Code is Xerox Corporation. Portions
20  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21  * All Rights Reserved.
22  *
23  * Contributor(s):
24  */

25  
26 package org.aspectj.ajde.internal;
27
28 import java.util.*;
29 import java.io.*;
30
31 import org.aspectj.ajde.*;
32 import org.aspectj.ajde.ui.*;
33 import org.aspectj.asm.*;
34 import org.aspectj.util.ConfigParser;
35
36 /**
37  * @author Mik Kersten
38  */

39 public class LstBuildConfigManager implements BuildConfigManager {
40     
41     private List configFiles = new ArrayList();
42     private List listeners = new ArrayList();
43     private LstBuildConfigFileUpdater fileUpdater = new LstBuildConfigFileUpdater();
44     protected String JavaDoc currConfigFilePath = null;
45     
46     private static final FilenameFilter SOURCE_FILE_FILTER = new FilenameFilter() {
47         public boolean accept(File dir, String JavaDoc name) {
48             return name.endsWith(".java")
49                 || name.endsWith(".aj")
50                 || name.endsWith(".lst");
51         }
52     };
53
54     private static final FileFilter DIR_FILTER = new FileFilter() {
55         public boolean accept(File file) {
56             return file.isDirectory();
57         }
58     };
59
60     public BuildConfigModel buildModel(String JavaDoc configFilePath) {
61         File configFile = new File(configFilePath);
62         String JavaDoc rootPath = configFile.getParent();
63         String JavaDoc configFileName = configFile.getName();
64         BuildConfigModel model = new BuildConfigModel(configFilePath);
65         List configFiles = new ArrayList();
66         List importedFiles = new ArrayList();
67         List badEntries = null;
68         try {
69             LstBuildConfigFileParser configParser = new LstBuildConfigFileParser(configFilePath);
70             configParser.parseConfigFile(new File(configFilePath));
71             configFiles = configParser.getFiles();
72             importedFiles = configParser.getImportedFiles();
73             badEntries = configParser.getProblemEntries();
74         } catch (ConfigParser.ParseException pe) {
75             String JavaDoc filePath = "<unknown>";
76             if (pe.getFile() != null) filePath = pe.getFile().getAbsolutePath();
77             Ajde.getDefault().getTaskListManager().addSourcelineTask(
78                 pe.getMessage(),
79                 new SourceLocation(filePath, pe.getLine(), 1),
80                 StructureMessage.Kind.ERROR);
81         }
82         
83         List relativePaths = relativizeFilePaths(configFiles, rootPath);
84         BuildConfigNode root = new BuildConfigNode(configFileName, BuildConfigNode.Kind.FILE_LST, rootPath);
85         buildDirTree(root, rootPath, importedFiles, configFileName);
86         model.setRoot(root);
87         addFilesToDirTree(model, relativePaths, badEntries);
88         
89         pruneEmptyDirs(root);
90         sortModel((BuildConfigNode)model.getRoot(), ALPHABETICAL_COMPARATOR);
91         //addImportedFilesToDirTree(model, importedFiles);
92

93         addProblemEntries(root, badEntries);
94         return model;
95     }
96
97     private void addProblemEntries(BuildConfigNode root, List badEntries) {
98         for (Iterator it = badEntries.iterator(); it.hasNext(); ) {
99             root.addChild(new BuildConfigNode(
100                 it.next().toString(),
101                 BuildConfigNode.Kind.ERROR, null)
102             );
103         }
104     }
105     
106     public void writeModel(BuildConfigModel model) {
107 // final List paths = new ArrayList();
108
// StructureWalker walker = new StructureWalker() {
109
// protected void postProcess(StructureNode node) {
110
// BuildConfigNode configNode = (BuildConfigNode)node;
111
// if (configNode.isActive() && configNode.isValidResource()) {
112
// paths.add(configNode.getResourcePath());
113
// }
114
// }
115
// };
116
// model.getRoot().walk(walker);
117

118         List activeSourceFiles = model.getActiveNodes(BuildConfigNode.Kind.FILE_ASPECTJ);
119         activeSourceFiles.addAll(model.getActiveNodes(BuildConfigNode.Kind.FILE_JAVA));
120         List activeImportedFiles = model.getActiveNodes(BuildConfigNode.Kind.FILE_LST);
121         fileUpdater.writeConfigFile(model.getSourceFile(), activeSourceFiles, activeImportedFiles);
122     }
123     
124     public void writePaths(String JavaDoc configFilePath, List files) {
125         fileUpdater.writeConfigFile(configFilePath, files);
126     }
127     
128     public void addFilesToConfig(String JavaDoc configFilePath, List paths) {
129             
130     }
131     
132     public void removeFilesFromConfig(String JavaDoc configFilePath, List files) {
133         
134     }
135     
136     private List relativizeFilePaths(List configFiles, String JavaDoc rootPath) {
137         List relativePathsList = new ArrayList();
138         for (Iterator it = configFiles.iterator(); it.hasNext(); ) {
139             File file = (File)it.next();
140             relativePathsList.add(fileUpdater.relativizePath(file.getPath(), rootPath));
141         }
142         return relativePathsList;
143     }
144     
145 // private String relativizePath(String path, String rootPath) {
146
// path = path.replace('\\', '/');
147
// rootPath = rootPath.replace('\\', '/');
148
// int pathIndex = path.indexOf(rootPath);
149
// if (pathIndex > -1) {
150
// return path.substring(pathIndex + rootPath.length() + 1);
151
// } else {
152
// return path;
153
// }
154
// }
155

156     private void buildDirTree(BuildConfigNode node, String JavaDoc rootPath, List importedFiles, String JavaDoc configFileName) {
157         File[] dirs = new File(node.getResourcePath()).listFiles(DIR_FILTER);
158         if (dirs == null) return;
159         for (int i = 0; i < dirs.length; i++) {
160             BuildConfigNode dir = new BuildConfigNode(dirs[i].getName(), BuildConfigNode.Kind.DIRECTORY, dirs[i].getPath());
161             File[] files = dirs[i].listFiles(SOURCE_FILE_FILTER);
162             for (int j = 0; j < files.length; j++) {
163                 if (files[j] != null) {// && !files[j].getName().endsWith(".lst")) {
164
String JavaDoc filePath = fileUpdater.relativizePath(files[j].getPath(), rootPath);
165                     BuildConfigNode.Kind kind = BuildConfigNode.Kind.FILE_JAVA;
166                     if (!files[j].getName().endsWith(".lst")) {
167                         //kind = BuildConfigNode.Kind.FILE_LST;
168
BuildConfigNode file = new BuildConfigNode(files[j].getName(), kind, filePath);
169                         file.setActive(false);
170                         dir.addChild(file);
171                     }
172                 }
173             }
174             node.addChild(dir);
175             boolean foundMatch = false;
176             for (Iterator it = importedFiles.iterator(); it.hasNext(); ) {
177                 File importedFile = (File)it.next();
178                 if (importedFile.getParentFile().getAbsolutePath().equals(dirs[i].getAbsolutePath())) {
179                     foundMatch = true;
180                     BuildConfigNode importedFileNode = new BuildConfigNode(
181                         importedFile.getName(),
182                         BuildConfigNode.Kind.FILE_LST,
183                         fileUpdater.relativizePath(importedFile.getPath(), rootPath));
184                     importedFileNode.setActive(true);
185                     //dir.getChildren().clear();
186
boolean found = false;
187                     for (Iterator it2 = dir.getChildren().iterator(); it2.hasNext(); ) {
188                         if (((BuildConfigNode)it2.next()).getName().equals(importedFile.getName())) {
189                             found = true;
190                         }
191                     }
192                     if (!found) dir.addChild(importedFileNode);
193                 }
194                     
195             }
196             //if (!foundMatch)
197
buildDirTree(dir, rootPath, importedFiles, configFileName);
198         }
199         
200         if (node.getName().endsWith(".lst")) {
201             File[] files = new File(rootPath).listFiles(SOURCE_FILE_FILTER);
202             if (files == null) return;
203             for (int i = 0; i < files.length; i++) {
204                 if (files[i] != null && !files[i].getName().equals(configFileName)) {// && !files[i].getName().endsWith(".lst")) {
205
BuildConfigNode.Kind kind = BuildConfigNode.Kind.FILE_JAVA;
206                     if (files[i].getName().endsWith(".lst")) {
207                         kind = BuildConfigNode.Kind.FILE_LST;
208                     }
209                     BuildConfigNode file = new BuildConfigNode(files[i].getName(), kind, files[i].getName());
210                     file.setActive(false);
211                     node.addChild(file);
212                 }
213             }
214         }
215     }
216       
217     private void addFilesToDirTree(BuildConfigModel model, List configFiles, List badEntries) {
218         for (Iterator it = configFiles.iterator(); it.hasNext(); ) {
219             String JavaDoc path = (String JavaDoc)it.next();
220             if (path.startsWith("..")) {
221                 File file = new File(path);
222                 BuildConfigNode node = new BuildConfigNode(file.getName(), BuildConfigNode.Kind.FILE_JAVA, path);
223                 BuildConfigNode upPath = model.getNodeForPath(file.getParentFile().getPath());
224                 if (upPath == model.getRoot()) {
225                     upPath = new BuildConfigNode(file.getParentFile().getPath(), BuildConfigNode.Kind.DIRECTORY, file.getParentFile().getAbsolutePath());
226                     model.getRoot().addChild(upPath);
227                 }
228                 node.setActive(true);
229                 upPath.addChild(node);
230             } else if (!(new File(path).isAbsolute())) {
231                 String JavaDoc name = new File(path).getName();
232                 BuildConfigNode existingNode = model.getNodeForPath(path);
233                 existingNode.setActive(true);
234             } else {
235                 badEntries.add("Use relative paths only, omitting: " + path);
236             }
237         }
238     }
239     
240     private boolean pruneEmptyDirs(BuildConfigNode node) {
241         List nodesToRemove = new ArrayList();
242         for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
243             BuildConfigNode currNode = (BuildConfigNode)it.next();
244             boolean hasValidChildren = pruneEmptyDirs(currNode);
245             if (!currNode.isValidResource() && !hasValidChildren) {
246                 nodesToRemove.add(currNode);
247             }
248         }
249         
250         for (Iterator it = nodesToRemove.iterator(); it.hasNext(); ) {
251             StructureNode currNode = (StructureNode)it.next();
252             node.removeChild(currNode);
253         }
254         return node.getChildren().size() > 0;
255     }
256     
257     public String JavaDoc getActiveConfigFile() {
258         if (currConfigFilePath == null) return null;
259         if (currConfigFilePath.equals(DEFAULT_CONFIG_LABEL)) {
260             return Ajde.getDefault().getProjectProperties().getDefaultBuildConfigFile();// getDefaultConfigFile();
261
} else {
262             return currConfigFilePath;
263         }
264     }
265     
266     public void setActiveConfigFile(String JavaDoc currConfigFilePath) {
267         if (currConfigFilePath.equals(DEFAULT_CONFIG_LABEL)) {
268             this.currConfigFilePath = Ajde.getDefault().getProjectProperties().getDefaultBuildConfigFile();//getDefaultConfigFile();
269
} else {
270             this.currConfigFilePath = currConfigFilePath;
271         }
272         notifyConfigChanged();
273     }
274     
275     public void addListener(BuildConfigListener configurationListener) {
276         listeners.add(configurationListener);
277     }
278
279     public void removeListener(BuildConfigListener configurationListener) {
280         listeners.remove(configurationListener);
281     }
282     
283     private void notifyConfigChanged() {
284         for (Iterator it = listeners.iterator(); it.hasNext(); ) {
285             ((BuildConfigListener)it.next()).currConfigChanged(currConfigFilePath);
286         }
287     }
288
289     private void notifyConfigsListUpdated() {
290         for (Iterator it = listeners.iterator(); it.hasNext(); ) {
291             ((BuildConfigListener)it.next()).configsListUpdated(configFiles);
292         }
293     }
294     
295     private void sortModel(BuildConfigNode node, Comparator comparator) {
296         if (node == null || node.getChildren() == null) return;
297         Collections.sort(node.getChildren(), comparator);
298         for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
299             BuildConfigNode nextNode = (BuildConfigNode)it.next();
300             if (nextNode != null) sortModel(nextNode, comparator);
301         }
302     }
303     
304     private static final Comparator ALPHABETICAL_COMPARATOR = new Comparator() {
305         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
306             BuildConfigNode n1 = (BuildConfigNode)o1;
307             BuildConfigNode n2 = (BuildConfigNode)o2;
308             return n1.getName().compareTo(n2.getName());
309         }
310     };
311 }
312
313
314
Popular Tags