KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEProjectGenerator


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.modules.java.j2seproject;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Stack JavaDoc;
25 import org.netbeans.api.java.platform.JavaPlatform;
26 import org.netbeans.api.java.platform.JavaPlatformManager;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.api.java.project.JavaProjectConstants;
30 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
31 import org.netbeans.spi.project.support.ant.AntProjectHelper;
32 import org.netbeans.spi.project.support.ant.EditableProperties;
33 import org.netbeans.spi.project.support.ant.ProjectGenerator;
34 import org.netbeans.spi.project.support.ant.PropertyUtils;
35 import org.netbeans.spi.project.support.ant.ReferenceHelper;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileSystem;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.filesystems.Repository;
40 import org.openide.filesystems.FileStateInvalidException;
41 import org.openide.loaders.DataFolder;
42 import org.openide.loaders.DataObject;
43 import org.openide.modules.SpecificationVersion;
44 import org.openide.util.Mutex;
45 import org.openide.util.MutexException;
46 import org.openide.ErrorManager;
47 import org.openide.util.NbBundle;
48 import org.w3c.dom.Document JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50 import org.w3c.dom.NodeList JavaDoc;
51
52 /**
53  * Creates a J2SEProject from scratch according to some initial configuration.
54  */

55 public class J2SEProjectGenerator {
56     
57     static final String JavaDoc MINIMUM_ANT_VERSION = "1.6.5";
58     
59     private J2SEProjectGenerator() {}
60     
61     /**
62      * Create a new empty J2SE project.
63      * @param dir the top-level directory (need not yet exist but if it does it must be empty)
64      * @param name the name for the project
65      * @return the helper object permitting it to be further customized
66      * @throws IOException in case something went wrong
67      */

68     public static AntProjectHelper createProject(final File JavaDoc dir, final String JavaDoc name, final String JavaDoc mainClass, final String JavaDoc manifestFile) throws IOException JavaDoc {
69         final FileObject dirFO = FileUtil.createFolder(dir);
70         // if manifestFile is null => it's TYPE_LIB
71
final AntProjectHelper[] h = new AntProjectHelper[1];
72         dirFO.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
73             public void run() throws IOException JavaDoc {
74                 h[0] = createProject(dirFO, name, "src", "test", mainClass, manifestFile, manifestFile == null); //NOI18N
75
Project p = ProjectManager.getDefault().findProject(dirFO);
76                 ProjectManager.getDefault().saveProject(p);
77                 FileObject srcFolder = dirFO.createFolder("src"); // NOI18N
78
dirFO.createFolder("test"); // NOI18N
79
if ( mainClass != null ) {
80                     createMainClass( mainClass, srcFolder );
81                 }
82             }
83         });
84         
85         return h[0];
86     }
87
88     public static AntProjectHelper createProject(final File JavaDoc dir, final String JavaDoc name,
89                                                   final File JavaDoc[] sourceFolders, final File JavaDoc[] testFolders, final String JavaDoc manifestFile) throws IOException JavaDoc {
90         assert sourceFolders != null && testFolders != null: "Package roots can't be null"; //NOI18N
91
final FileObject dirFO = FileUtil.createFolder(dir);
92         final AntProjectHelper[] h = new AntProjectHelper[1];
93         // this constructor creates only java application type
94
dirFO.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
95             public void run () throws IOException JavaDoc {
96                 h[0] = createProject(dirFO, name, null, null, null, manifestFile, false);
97                 final J2SEProject p = (J2SEProject) ProjectManager.getDefault().findProject(dirFO);
98                 final ReferenceHelper refHelper = p.getReferenceHelper();
99                 try {
100                     ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
101                     public Void JavaDoc run() throws Exception JavaDoc {
102                         Element JavaDoc data = h[0].getPrimaryConfigurationData(true);
103                         Document JavaDoc doc = data.getOwnerDocument();
104                         NodeList JavaDoc nl = data.getElementsByTagNameNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
105                         assert nl.getLength() == 1;
106                         Element JavaDoc sourceRoots = (Element JavaDoc) nl.item(0);
107                         nl = data.getElementsByTagNameNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
108
assert nl.getLength() == 1;
109                         Element JavaDoc testRoots = (Element JavaDoc) nl.item(0);
110                         for (int i=0; i<sourceFolders.length; i++) {
111                             String JavaDoc propName;
112                             if (i == 0) {
113                                 //Name the first src root src.dir to be compatible with NB 4.0
114
propName = "src.dir"; //NOI18N
115
}
116                             else {
117                                 String JavaDoc name = sourceFolders[i].getName();
118                                 propName = name + ".dir"; //NOI18N
119
}
120
121                             int rootIndex = 1;
122                             EditableProperties props = h[0].getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
123                             while (props.containsKey(propName)) {
124                                 rootIndex++;
125                                 propName = name + rootIndex + ".dir"; //NOI18N
126
}
127                             String JavaDoc srcReference = refHelper.createForeignFileReference(sourceFolders[i], JavaProjectConstants.SOURCES_TYPE_JAVA);
128                             Element JavaDoc root = doc.createElementNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
129
root.setAttribute ("id",propName); //NOI18N
130
sourceRoots.appendChild(root);
131                             props = h[0].getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
132                             props.put(propName,srcReference);
133                             h[0].putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); // #47609
134
}
135                         for (int i = 0; i < testFolders.length; i++) {
136                             if (!testFolders[i].exists()) {
137                                 testFolders[i].mkdirs();
138                             }
139                             String JavaDoc propName;
140                             if (i == 0) {
141                                 //Name the first test root test.src.dir to be compatible with NB 4.0
142
propName = "test.src.dir"; //NOI18N
143
}
144                             else {
145                                 String JavaDoc name = testFolders[i].getName();
146                                 propName = "test." + name + ".dir"; // NOI18N
147
}
148                             int rootIndex = 1;
149                             EditableProperties props = h[0].getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
150                             while (props.containsKey(propName)) {
151                                 rootIndex++;
152                                 propName = "test." + name + rootIndex + ".dir"; // NOI18N
153
}
154                             String JavaDoc testReference = refHelper.createForeignFileReference(testFolders[i], JavaProjectConstants.SOURCES_TYPE_JAVA);
155                             Element JavaDoc root = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE, "root"); // NOI18N
156
root.setAttribute("id", propName); // NOI18N
157
testRoots.appendChild(root);
158                             props = h[0].getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); // #47609
159
props.put(propName, testReference);
160                             h[0].putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
161                         }
162                         h[0].putPrimaryConfigurationData(data,true);
163                         ProjectManager.getDefault().saveProject (p);
164                         return null;
165                     }
166                 });
167                 } catch (MutexException me ) {
168                     ErrorManager.getDefault().notify (me);
169                 }
170             }
171         });
172         return h[0];
173     }
174
175     private static AntProjectHelper createProject(FileObject dirFO, String JavaDoc name,
176                                                   String JavaDoc srcRoot, String JavaDoc testRoot, String JavaDoc mainClass, String JavaDoc manifestFile, boolean isLibrary) throws IOException JavaDoc {
177         AntProjectHelper h = ProjectGenerator.createProject(dirFO, J2SEProjectType.TYPE);
178         Element JavaDoc data = h.getPrimaryConfigurationData(true);
179         Document JavaDoc doc = data.getOwnerDocument();
180         Element JavaDoc nameEl = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); // NOI18N
181
nameEl.appendChild(doc.createTextNode(name));
182         data.appendChild(nameEl);
183         Element JavaDoc minant = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE, "minimum-ant-version"); // NOI18N
184
minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION)); // NOI18N
185
data.appendChild(minant);
186         EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
187         Element JavaDoc sourceRoots = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); //NOI18N
188
if (srcRoot != null) {
189             Element JavaDoc root = doc.createElementNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
190
root.setAttribute ("id","src.dir"); //NOI18N
191
sourceRoots.appendChild(root);
192             ep.setProperty("src.dir", srcRoot); // NOI18N
193
}
194         data.appendChild (sourceRoots);
195         Element JavaDoc testRoots = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
196
if (testRoot != null) {
197             Element JavaDoc root = doc.createElementNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
198
root.setAttribute ("id","test.src.dir"); //NOI18N
199
testRoots.appendChild (root);
200             ep.setProperty("test.src.dir", testRoot); // NOI18N
201
}
202         data.appendChild (testRoots);
203         h.putPrimaryConfigurationData(data, true);
204         ep.setProperty("dist.dir", "dist"); // NOI18N
205
ep.setComment("dist.dir", new String JavaDoc[] {"# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_dist.dir")}, false); // NOI18N
206
ep.setProperty("dist.jar", "${dist.dir}/" + PropertyUtils.getUsablePropertyName(name) + ".jar"); // NOI18N
207
ep.setProperty("javac.classpath", new String JavaDoc[0]); // NOI18N
208
ep.setProperty("build.sysclasspath", "ignore"); // NOI18N
209
ep.setComment("build.sysclasspath", new String JavaDoc[] {"# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_build.sysclasspath")}, false); // NOI18N
210
ep.setProperty("run.classpath", new String JavaDoc[] { // NOI18N
211
"${javac.classpath}:", // NOI18N
212
"${build.classes.dir}", // NOI18N
213
});
214         ep.setProperty("debug.classpath", new String JavaDoc[] { // NOI18N
215
"${run.classpath}", // NOI18N
216
});
217         ep.setProperty("jar.compress", "false"); // NOI18N
218
if (!isLibrary) {
219             ep.setProperty("main.class", mainClass == null ? "" : mainClass); // NOI18N
220
}
221         
222         ep.setProperty("javac.compilerargs", ""); // NOI18N
223
ep.setComment("javac.compilerargs", new String JavaDoc[] {
224             "# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_javac.compilerargs"), // NOI18N
225
}, false);
226         SpecificationVersion sourceLevel = getDefaultSourceLevel();
227         ep.setProperty("javac.source", sourceLevel.toString()); // NOI18N
228
ep.setProperty("javac.target", sourceLevel.toString()); // NOI18N
229
ep.setProperty("javac.deprecation", "false"); // NOI18N
230
ep.setProperty("javac.test.classpath", new String JavaDoc[] { // NOI18N
231
"${javac.classpath}:", // NOI18N
232
"${build.classes.dir}:", // NOI18N
233
"${libs.junit.classpath}", // NOI18N
234
});
235         ep.setProperty("run.test.classpath", new String JavaDoc[] { // NOI18N
236
"${javac.test.classpath}:", // NOI18N
237
"${build.test.classes.dir}", // NOI18N
238
});
239         ep.setProperty("debug.test.classpath", new String JavaDoc[] { // NOI18N
240
"${run.test.classpath}", // NOI18N
241
});
242
243         ep.setProperty("build.generated.dir", "${build.dir}/generated"); // NOI18N
244
ep.setProperty("meta.inf.dir", "${src.dir}/META-INF"); // NOI18N
245

246         ep.setProperty("build.dir", "build"); // NOI18N
247
ep.setComment("build.dir", new String JavaDoc[] {"# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_build.dir")}, false); // NOI18N
248
ep.setProperty("build.classes.dir", "${build.dir}/classes"); // NOI18N
249
ep.setProperty("build.test.classes.dir", "${build.dir}/test/classes"); // NOI18N
250
ep.setProperty("build.test.results.dir", "${build.dir}/test/results"); // NOI18N
251
ep.setProperty("build.classes.excludes", "**/*.java,**/*.form"); // NOI18N
252
ep.setProperty("dist.javadoc.dir", "${dist.dir}/javadoc"); // NOI18N
253
ep.setProperty("platform.active", "default_platform"); // NOI18N
254

255         ep.setProperty("run.jvmargs", ""); // NOI18N
256
ep.setComment("run.jvmargs", new String JavaDoc[] {
257             "# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_run.jvmargs"), // NOI18N
258
"# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_run.jvmargs_2"), // NOI18N
259
"# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_run.jvmargs_3"), // NOI18N
260
}, false);
261
262         ep.setProperty(J2SEProjectProperties.JAVADOC_PRIVATE, "false"); // NOI18N
263
ep.setProperty(J2SEProjectProperties.JAVADOC_NO_TREE, "false"); // NOI18N
264
ep.setProperty(J2SEProjectProperties.JAVADOC_USE, "true"); // NOI18N
265
ep.setProperty(J2SEProjectProperties.JAVADOC_NO_NAVBAR, "false"); // NOI18N
266
ep.setProperty(J2SEProjectProperties.JAVADOC_NO_INDEX, "false"); // NOI18N
267
ep.setProperty(J2SEProjectProperties.JAVADOC_SPLIT_INDEX, "true"); // NOI18N
268
ep.setProperty(J2SEProjectProperties.JAVADOC_AUTHOR, "false"); // NOI18N
269
ep.setProperty(J2SEProjectProperties.JAVADOC_VERSION, "false"); // NOI18N
270
ep.setProperty(J2SEProjectProperties.JAVADOC_WINDOW_TITLE, ""); // NOI18N
271
ep.setProperty(J2SEProjectProperties.JAVADOC_ENCODING, ""); // NOI18N
272
ep.setProperty(J2SEProjectProperties.JAVADOC_ADDITIONALPARAM, ""); // NOI18N
273

274         if (manifestFile != null) {
275             ep.setProperty("manifest.file", manifestFile); // NOI18N
276
}
277         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
278         return h;
279     }
280     
281     private static void createMainClass( String JavaDoc mainClassName, FileObject srcFolder ) throws IOException JavaDoc {
282         
283         int lastDotIdx = mainClassName.lastIndexOf( '.' );
284         String JavaDoc mName, pName;
285         if ( lastDotIdx == -1 ) {
286             mName = mainClassName.trim();
287             pName = null;
288         }
289         else {
290             mName = mainClassName.substring( lastDotIdx + 1 ).trim();
291             pName = mainClassName.substring( 0, lastDotIdx ).trim();
292         }
293         
294         if ( mName.length() == 0 ) {
295             return;
296         }
297         
298         FileObject mainTemplate = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Classes/Main.java" ); // NOI18N
299

300         if ( mainTemplate == null ) {
301             return; // Don't know the template
302
}
303                 
304         DataObject mt = DataObject.find( mainTemplate );
305         
306         FileObject pkgFolder = srcFolder;
307         if ( pName != null ) {
308             String JavaDoc fName = pName.replace( '.', '/' ); // NOI18N
309
pkgFolder = FileUtil.createFolder( srcFolder, fName );
310         }
311         DataFolder pDf = DataFolder.findFolder( pkgFolder );
312         mt.createFromTemplate( pDf, mName );
313         
314     }
315     
316     //------------ Used by unit tests -------------------
317
private static SpecificationVersion defaultSourceLevel;
318     
319     private static SpecificationVersion getDefaultSourceLevel () {
320         if (defaultSourceLevel != null) {
321             return defaultSourceLevel;
322         }
323         else {
324             JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform();
325             SpecificationVersion v = defaultPlatform.getSpecification().getVersion();
326             if (v.equals(new SpecificationVersion("1.6")) || v.equals(new SpecificationVersion("1.7"))) {
327                 // #89131: these levels are not actually distinct from 1.5.
328
return new SpecificationVersion("1.5");
329             } else {
330                 return v;
331             }
332         }
333     }
334     
335     /**
336      * Unit test only method. Sets the default source level for tests
337      * where the default platform is not available.
338      * @param version the default source level set to project when it is created
339      *
340      */

341     public static void setDefaultSourceLevel (SpecificationVersion version) {
342         defaultSourceLevel = version;
343     }
344 }
345
346
347
Popular Tags