KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > project > ProjectBuilder


1 package org.antlr.works.project;
2
3 import org.antlr.xjlib.appkit.utils.XJDialogProgress;
4 import org.antlr.xjlib.appkit.utils.XJDialogProgressDelegate;
5 import org.antlr.xjlib.foundation.XJUtils;
6 import org.antlr.works.components.project.CContainerProject;
7 import org.antlr.works.engine.EngineRuntime;
8 import org.antlr.works.utils.StreamWatcherDelegate;
9
10 import javax.swing.*;
11 import java.io.File JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 /*
18
19 [The "BSD licence"]
20 Copyright (c) 2005-2006 Jean Bovet
21 All rights reserved.
22
23 Redistribution and use in source and binary forms, with or without
24 modification, are permitted provided that the following conditions
25 are met:
26
27 1. Redistributions of source code must retain the above copyright
28 notice, this list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright
30 notice, this list of conditions and the following disclaimer in the
31 documentation and/or other materials provided with the distribution.
32 3. The name of the author may not be used to endorse or promote products
33 derived from this software without specific prior written permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
36 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
38 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
39 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46 */

47
48 public class ProjectBuilder implements StreamWatcherDelegate, XJDialogProgressDelegate {
49
50     protected CContainerProject project;
51     protected XJDialogProgress progress;
52
53     protected boolean cancel;
54     protected int buildingProgress;
55
56     protected ProjectFileItem fileToBuild;
57     protected ThreadExecution currentThread;
58
59     public ProjectBuilder(CContainerProject project) {
60         this.project = project;
61         this.progress = new XJDialogProgress(project.getXJFrame(), true);
62         this.progress.setDelegate(this);
63     }
64
65     public List JavaDoc<ProjectBuildList.BuildFile> getListOfDirtyBuildFiles(String JavaDoc type) {
66         return project.getBuildList().getDirtyBuildFilesOfType(type);
67     }
68
69     public List JavaDoc<ProjectBuildList.BuildFile> buildListOfBuildFilesOfType(List JavaDoc<String JavaDoc> filesOnDisk, String JavaDoc fileType) {
70         ProjectBuildList buildList = project.getBuildList();
71
72         // Update the build list with list of files on the disk
73

74         File JavaDoc[] files = new File JavaDoc(project.getSourcePath()).listFiles();
75         for (int i = 0; i < files.length; i++) {
76             File JavaDoc file = files[i];
77             String JavaDoc filePath = file.getAbsolutePath();
78             if(!ProjectFileItem.getFileType(filePath).equals(fileType))
79                 continue;
80
81             if(buildList.isFileExisting(filePath, fileType))
82                 buildList.handleExternalModification(filePath, fileType);
83             else
84                 buildList.addFile(filePath, fileType);
85         }
86
87         // Remove all non-existent file on disk that are still in the the build list.
88

89         for (Iterator JavaDoc<ProjectBuildList.BuildFile> iterator = buildList.getBuildFilesOfType(fileType).iterator(); iterator.hasNext();)
90         {
91             ProjectBuildList.BuildFile buildFile = iterator.next();
92             if(!new File JavaDoc(buildFile.getFilePath()).exists()) {
93                 // The file doesn't exist anymore. Remove it from the build list.
94
buildList.removeFile(buildFile.getFilePath(), fileType);
95             }
96         }
97
98         return getListOfDirtyBuildFiles(fileType);
99     }
100
101     public List JavaDoc<ProjectBuildList.BuildFile> buildListOfGrammarBuildFiles() {
102         List JavaDoc<String JavaDoc> filesOnDisk = new ArrayList JavaDoc<String JavaDoc>();
103         for (Iterator JavaDoc iterator = project.getFileEditorItems().iterator(); iterator.hasNext();) {
104             ProjectFileItem item = (ProjectFileItem) iterator.next();
105             filesOnDisk.add(item.getFilePath());
106         }
107         return buildListOfBuildFilesOfType(filesOnDisk, ProjectFileItem.FILE_TYPE_GRAMMAR);
108     }
109
110     public List JavaDoc<ProjectBuildList.BuildFile> buildListOfJavaBuildFiles() {
111         List JavaDoc<String JavaDoc> filesOnDisk = new ArrayList JavaDoc<String JavaDoc>();
112         File JavaDoc[] files = new File JavaDoc(project.getSourcePath()).listFiles();
113         for (int i = 0; i < files.length; i++) {
114             File JavaDoc file = files[i];
115             filesOnDisk.add(file.getAbsolutePath());
116         }
117         return buildListOfBuildFilesOfType(filesOnDisk, ProjectFileItem.FILE_TYPE_JAVA);
118     }
119
120     public boolean generateGrammarBuildFiles(List JavaDoc<ProjectBuildList.BuildFile> buildFiles) {
121         for (Iterator JavaDoc<ProjectBuildList.BuildFile> iterator = buildFiles.iterator(); iterator.hasNext() && !cancel;) {
122             ProjectBuildList.BuildFile buildFile = iterator.next();
123
124             String JavaDoc file = buildFile.getFilePath();
125             String JavaDoc libPath = buildFile.getFileFolder();
126             String JavaDoc outputPath = buildFile.getFileFolder();
127
128             setProgressStepInfo("Generating \""+ XJUtils.getLastPathComponent(file)+"\"...");
129
130             String JavaDoc error = EngineRuntime.runANTLR(null, file, libPath, outputPath, this);
131             if(error != null) {
132                 project.buildReportError(error);
133                 return false;
134             } else {
135                 buildFile.setDirty(false);
136                 project.changeDone();
137             }
138         }
139         return true;
140     }
141
142     public boolean compileFile(String JavaDoc file) {
143         String JavaDoc outputPath = project.getSourcePath();
144         String JavaDoc error = EngineRuntime.compileFiles(null, new String JavaDoc[] { file }, outputPath, this);
145         if(error != null) {
146             project.buildReportError(error);
147             return false;
148         } else {
149             return true;
150         }
151     }
152
153     public boolean compileJavaBuildFiles(List JavaDoc<ProjectBuildList.BuildFile> buildFiles) {
154         for (Iterator JavaDoc<ProjectBuildList.BuildFile> iterator = buildFiles.iterator(); iterator.hasNext() && !cancel;) {
155             ProjectBuildList.BuildFile buildFile = iterator.next();
156             String JavaDoc file = buildFile.getFilePath();
157             setProgressStepInfo("Compiling \""+ XJUtils.getLastPathComponent(file)+"\"...");
158             if(!compileFile(file))
159                 return false;
160             else {
161                 buildFile.setDirty(false);
162                 project.changeDone();
163             }
164         }
165         return true;
166     }
167
168     public void setProgressStepInfo(String JavaDoc info) {
169         progress.setInfo(info);
170         progress.setProgress(++buildingProgress);
171     }
172
173     public boolean performBuild() {
174         List JavaDoc<ProjectBuildList.BuildFile> grammars = buildListOfGrammarBuildFiles();
175         List JavaDoc<ProjectBuildList.BuildFile> javas = buildListOfJavaBuildFiles();
176
177         int total = grammars.size()+javas.size();
178         if(total == 0)
179             return true;
180
181         progress.setIndeterminate(false);
182         progress.setProgress(0);
183         progress.setProgressMax(total);
184
185         if(generateGrammarBuildFiles(grammars) && !cancel) {
186             if(grammars.size() > 0) {
187                 // Rebuild the list of Java files because ANTLR may have
188
// generated some ;-)
189

190                 javas = buildListOfJavaBuildFiles();
191                 total = grammars.size()+javas.size();
192                 progress.setProgressMax(total);
193             }
194             if(compileJavaBuildFiles(javas))
195                 return true;
196         }
197         return false;
198     }
199
200     public void performBuildFile() {
201         String JavaDoc type = fileToBuild.getFileType();
202         List JavaDoc<ProjectBuildList.BuildFile> files;
203
204         if(type.equals(ProjectFileItem.FILE_TYPE_GRAMMAR))
205             files = buildListOfGrammarBuildFiles();
206         else if(type.equals(ProjectFileItem.FILE_TYPE_JAVA))
207             files = buildListOfJavaBuildFiles();
208         else
209             return;
210
211         for (Iterator JavaDoc<ProjectBuildList.BuildFile> iterator = files.iterator(); iterator.hasNext();) {
212             ProjectBuildList.BuildFile buildFile = iterator.next();
213             if(buildFile.getFilePath().equals(fileToBuild.getFilePath())) {
214                 List JavaDoc<ProjectBuildList.BuildFile> f = Collections.singletonList(buildFile);
215                 if(type.equals(ProjectFileItem.FILE_TYPE_GRAMMAR))
216                     generateGrammarBuildFiles(f);
217                 else if(type.equals(ProjectFileItem.FILE_TYPE_JAVA))
218                     compileJavaBuildFiles(f);
219             }
220         }
221     }
222
223     public void prepare() {
224         cancel = false;
225         buildingProgress = 0;
226         currentThread = null;
227     }
228
229     public void buildFile(ProjectFileItem fileItem) {
230         fileToBuild = fileItem;
231
232         progress.setCancellable(true);
233         progress.setTitle("Build");
234         progress.setInfo("Building...");
235         progress.setIndeterminate(true);
236
237         prepare();
238
239         currentThread = new ThreadExecution(new Runnable JavaDoc() {
240             public void run() {
241                 performBuildFile();
242                 progress.close();
243             }
244         });
245
246         progress.runModal();
247     }
248
249     public void buildAll() {
250         progress.setCancellable(true);
251         progress.setTitle("Build");
252         progress.setInfo("Preparing...");
253         progress.setIndeterminate(true);
254
255         prepare();
256
257         currentThread = new ThreadExecution(new Runnable JavaDoc() {
258             public void run() {
259                 performBuild();
260                 progress.close();
261             }
262         });
263
264         progress.runModal();
265     }
266
267     public void performRun() {
268         String JavaDoc error = EngineRuntime.runJava(null, project.getSourcePath(), project.getRunParameters(), ProjectBuilder.this);
269         if(error != null) {
270             project.buildReportError(error);
271         }
272     }
273
274     public void run() {
275         progress.setCancellable(true);
276         progress.setTitle("Run");
277         progress.setInfo("Preparing...");
278         progress.setIndeterminate(true);
279
280         prepare();
281
282         currentThread = new ThreadExecution(new Runnable JavaDoc() {
283             public void run() {
284                 if(performBuild() && !cancel) {
285                     progress.setInfo("Running...");
286                     progress.setIndeterminate(true);
287                     performRun();
288                 }
289                 progress.close();
290             }
291         });
292
293         progress.runModal();
294     }
295
296     /** This method cleans the project directory by removing the following files:
297      * - *.class
298      */

299
300     public void clean() {
301         File JavaDoc[] files = new File JavaDoc(project.getSourcePath()).listFiles();
302         for (int i = 0; i < files.length; i++) {
303             File JavaDoc file = files[i];
304             String JavaDoc filePath = file.getAbsolutePath();
305             if(filePath.endsWith(".class")) {
306                 file.delete();
307             }
308         }
309
310         // Mark all files as dirty
311
project.getBuildList().setAllFilesToDirty(true);
312
313         // Mark the project as dirty
314
project.changeDone();
315     }
316
317     public void dialogDidCancel() {
318         if(cancel) {
319             // The process may be blocked. Try to kill it.
320
Process JavaDoc p = EngineRuntime.getProcess(currentThread.t);
321             if(p != null)
322                 p.destroy();
323         }
324         cancel = true;
325     }
326
327     public void streamWatcherDidStarted() {
328
329     }
330
331     public void streamWatcherDidReceiveString(String JavaDoc string) {
332         project.printToConsole(string);
333     }
334
335     public void streamWatcherException(Exception JavaDoc e) {
336         project.printToConsole(e);
337     }
338
339     protected class ThreadExecution {
340
341         protected Runnable JavaDoc r;
342         protected Thread JavaDoc t;
343
344         public ThreadExecution(Runnable JavaDoc r) {
345             this.r = r;
346             launch();
347         }
348
349         public void launch() {
350             SwingUtilities.invokeLater(new Runnable JavaDoc() {
351                 public void run() {
352                     t = new Thread JavaDoc(r);
353                     t.start();
354                 }
355             });
356         }
357     }
358 }
359
Popular Tags