KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > MakeLayer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.*;
26
27 import org.apache.tools.ant.FileScanner;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.taskdefs.MatchingTask;
30 import org.apache.tools.ant.taskdefs.UpToDate;
31
32 /** Create a fragment of a module's XML layer.
33  *
34  * @author Michal Zlamal
35  */

36 public class MakeLayer extends MatchingTask {
37
38     private File JavaDoc dest = null;
39     private File JavaDoc topdir = null;
40     private boolean absolutePath = false;
41
42     /** Target file containing list of all classes. */
43     public void setDestfile(File JavaDoc f) {
44         dest = f;
45     }
46
47     /** Set the top directory.
48      * There should be subdirectories under this matching pacgages.
49      */

50     public void setTopdir (File JavaDoc t) {
51         topdir = t;
52     }
53     
54     /** Set whether there is absolute path in top dir or not
55      * default value is false
56      */

57     public void setAbsolutePath( boolean absolutePath ) {
58         this.absolutePath = absolutePath;
59     }
60
61     
62     public void execute() throws BuildException {
63         if (topdir == null) {
64             throw new BuildException("You must set at topdir attribute", getLocation());
65         }
66         if (dest == null) {
67             throw new BuildException("You must specify output file", getLocation());
68         }
69         UpToDate upToDate = (UpToDate) this.getProject().createTask( "uptodate" ); //NOI18N
70
fileset.setDir( topdir );
71         upToDate.addSrcfiles( fileset );
72         upToDate.setTargetFile( dest );
73         upToDate.setProperty(dest.getAbsolutePath() + ".property"); //NOI18N
74
upToDate.execute();
75         if (this.getProject().getProperty(dest.getAbsolutePath() + ".property") != null) //NOI18N
76
return;
77         
78         int lengthAdjust = (absolutePath) ? 0 : 1;
79         FileWriter JavaDoc layerFile;
80         try {
81             layerFile = new FileWriter JavaDoc(dest);
82         }
83         catch (IOException JavaDoc e) {
84             throw new BuildException(e, getLocation());
85         }
86         
87         FileScanner scanner = getDirectoryScanner (topdir);
88         for (String JavaDoc file : scanner.getIncludedFiles()) {
89             File JavaDoc aFileName = new File JavaDoc(topdir, file);
90             try {
91                 layerFile.write(("<file name=\""+aFileName.getName()+"\"\n").replace(File.separatorChar,'/')); //NOI18N
92
layerFile.write((" url=\""+aFileName.getAbsolutePath().substring(topdir.getAbsolutePath().length()+lengthAdjust)+"\"/>\n").replace(File.separatorChar,'/')); //NOI18N
93
}
94             catch(IOException JavaDoc ex) {
95                 throw new BuildException("I/O error while writing layer file "+dest.getAbsolutePath(), ex, getLocation());
96             }
97         }
98         
99         try {
100             layerFile.close();
101         }
102         catch (IOException JavaDoc e) {
103             throw new BuildException("I/O error when trying to close layer file "+dest.getAbsolutePath(), e, getLocation());
104         }
105     }
106 }
107
108
109
110
Popular Tags