1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Stack ; 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 ; 49 import org.w3c.dom.Element ; 50 import org.w3c.dom.NodeList ; 51 52 55 public class J2SEProjectGenerator { 56 57 static final String MINIMUM_ANT_VERSION = "1.6.5"; 58 59 private J2SEProjectGenerator() {} 60 61 68 public static AntProjectHelper createProject(final File dir, final String name, final String mainClass, final String manifestFile) throws IOException { 69 final FileObject dirFO = FileUtil.createFolder(dir); 70 final AntProjectHelper[] h = new AntProjectHelper[1]; 72 dirFO.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 73 public void run() throws IOException { 74 h[0] = createProject(dirFO, name, "src", "test", mainClass, manifestFile, manifestFile == null); Project p = ProjectManager.getDefault().findProject(dirFO); 76 ProjectManager.getDefault().saveProject(p); 77 FileObject srcFolder = dirFO.createFolder("src"); dirFO.createFolder("test"); if ( mainClass != null ) { 80 createMainClass( mainClass, srcFolder ); 81 } 82 } 83 }); 84 85 return h[0]; 86 } 87 88 public static AntProjectHelper createProject(final File dir, final String name, 89 final File [] sourceFolders, final File [] testFolders, final String manifestFile) throws IOException { 90 assert sourceFolders != null && testFolders != null: "Package roots can't be null"; final FileObject dirFO = FileUtil.createFolder(dir); 92 final AntProjectHelper[] h = new AntProjectHelper[1]; 93 dirFO.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 95 public void run () throws IOException { 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 >() { 101 public Void run() throws Exception { 102 Element data = h[0].getPrimaryConfigurationData(true); 103 Document doc = data.getOwnerDocument(); 104 NodeList nl = data.getElementsByTagNameNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); 105 assert nl.getLength() == 1; 106 Element sourceRoots = (Element ) nl.item(0); 107 nl = data.getElementsByTagNameNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); assert nl.getLength() == 1; 109 Element testRoots = (Element ) nl.item(0); 110 for (int i=0; i<sourceFolders.length; i++) { 111 String propName; 112 if (i == 0) { 113 propName = "src.dir"; } 116 else { 117 String name = sourceFolders[i].getName(); 118 propName = name + ".dir"; } 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"; } 127 String srcReference = refHelper.createForeignFileReference(sourceFolders[i], JavaProjectConstants.SOURCES_TYPE_JAVA); 128 Element root = doc.createElementNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); root.setAttribute ("id",propName); 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); } 135 for (int i = 0; i < testFolders.length; i++) { 136 if (!testFolders[i].exists()) { 137 testFolders[i].mkdirs(); 138 } 139 String propName; 140 if (i == 0) { 141 propName = "test.src.dir"; } 144 else { 145 String name = testFolders[i].getName(); 146 propName = "test." + name + ".dir"; } 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"; } 154 String testReference = refHelper.createForeignFileReference(testFolders[i], JavaProjectConstants.SOURCES_TYPE_JAVA); 155 Element root = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE, "root"); root.setAttribute("id", propName); testRoots.appendChild(root); 158 props = h[0].getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 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 name, 176 String srcRoot, String testRoot, String mainClass, String manifestFile, boolean isLibrary) throws IOException { 177 AntProjectHelper h = ProjectGenerator.createProject(dirFO, J2SEProjectType.TYPE); 178 Element data = h.getPrimaryConfigurationData(true); 179 Document doc = data.getOwnerDocument(); 180 Element nameEl = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); nameEl.appendChild(doc.createTextNode(name)); 182 data.appendChild(nameEl); 183 Element minant = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE, "minimum-ant-version"); minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION)); data.appendChild(minant); 186 EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 187 Element sourceRoots = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); if (srcRoot != null) { 189 Element root = doc.createElementNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); root.setAttribute ("id","src.dir"); sourceRoots.appendChild(root); 192 ep.setProperty("src.dir", srcRoot); } 194 data.appendChild (sourceRoots); 195 Element testRoots = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); if (testRoot != null) { 197 Element root = doc.createElementNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); root.setAttribute ("id","test.src.dir"); testRoots.appendChild (root); 200 ep.setProperty("test.src.dir", testRoot); } 202 data.appendChild (testRoots); 203 h.putPrimaryConfigurationData(data, true); 204 ep.setProperty("dist.dir", "dist"); ep.setComment("dist.dir", new String [] {"# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_dist.dir")}, false); ep.setProperty("dist.jar", "${dist.dir}/" + PropertyUtils.getUsablePropertyName(name) + ".jar"); ep.setProperty("javac.classpath", new String [0]); ep.setProperty("build.sysclasspath", "ignore"); ep.setComment("build.sysclasspath", new String [] {"# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_build.sysclasspath")}, false); ep.setProperty("run.classpath", new String [] { "${javac.classpath}:", "${build.classes.dir}", }); 214 ep.setProperty("debug.classpath", new String [] { "${run.classpath}", }); 217 ep.setProperty("jar.compress", "false"); if (!isLibrary) { 219 ep.setProperty("main.class", mainClass == null ? "" : mainClass); } 221 222 ep.setProperty("javac.compilerargs", ""); ep.setComment("javac.compilerargs", new String [] { 224 "# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_javac.compilerargs"), }, false); 226 SpecificationVersion sourceLevel = getDefaultSourceLevel(); 227 ep.setProperty("javac.source", sourceLevel.toString()); ep.setProperty("javac.target", sourceLevel.toString()); ep.setProperty("javac.deprecation", "false"); ep.setProperty("javac.test.classpath", new String [] { "${javac.classpath}:", "${build.classes.dir}:", "${libs.junit.classpath}", }); 235 ep.setProperty("run.test.classpath", new String [] { "${javac.test.classpath}:", "${build.test.classes.dir}", }); 239 ep.setProperty("debug.test.classpath", new String [] { "${run.test.classpath}", }); 242 243 ep.setProperty("build.generated.dir", "${build.dir}/generated"); ep.setProperty("meta.inf.dir", "${src.dir}/META-INF"); 246 ep.setProperty("build.dir", "build"); ep.setComment("build.dir", new String [] {"# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_build.dir")}, false); ep.setProperty("build.classes.dir", "${build.dir}/classes"); ep.setProperty("build.test.classes.dir", "${build.dir}/test/classes"); ep.setProperty("build.test.results.dir", "${build.dir}/test/results"); ep.setProperty("build.classes.excludes", "**/*.java,**/*.form"); ep.setProperty("dist.javadoc.dir", "${dist.dir}/javadoc"); ep.setProperty("platform.active", "default_platform"); 255 ep.setProperty("run.jvmargs", ""); ep.setComment("run.jvmargs", new String [] { 257 "# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_run.jvmargs"), "# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_run.jvmargs_2"), "# " + NbBundle.getMessage(J2SEProjectGenerator.class, "COMMENT_run.jvmargs_3"), }, false); 261 262 ep.setProperty(J2SEProjectProperties.JAVADOC_PRIVATE, "false"); ep.setProperty(J2SEProjectProperties.JAVADOC_NO_TREE, "false"); ep.setProperty(J2SEProjectProperties.JAVADOC_USE, "true"); ep.setProperty(J2SEProjectProperties.JAVADOC_NO_NAVBAR, "false"); ep.setProperty(J2SEProjectProperties.JAVADOC_NO_INDEX, "false"); ep.setProperty(J2SEProjectProperties.JAVADOC_SPLIT_INDEX, "true"); ep.setProperty(J2SEProjectProperties.JAVADOC_AUTHOR, "false"); ep.setProperty(J2SEProjectProperties.JAVADOC_VERSION, "false"); ep.setProperty(J2SEProjectProperties.JAVADOC_WINDOW_TITLE, ""); ep.setProperty(J2SEProjectProperties.JAVADOC_ENCODING, ""); ep.setProperty(J2SEProjectProperties.JAVADOC_ADDITIONALPARAM, ""); 274 if (manifestFile != null) { 275 ep.setProperty("manifest.file", manifestFile); } 277 h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 278 return h; 279 } 280 281 private static void createMainClass( String mainClassName, FileObject srcFolder ) throws IOException { 282 283 int lastDotIdx = mainClassName.lastIndexOf( '.' ); 284 String 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" ); 300 if ( mainTemplate == null ) { 301 return; } 303 304 DataObject mt = DataObject.find( mainTemplate ); 305 306 FileObject pkgFolder = srcFolder; 307 if ( pName != null ) { 308 String fName = pName.replace( '.', '/' ); pkgFolder = FileUtil.createFolder( srcFolder, fName ); 310 } 311 DataFolder pDf = DataFolder.findFolder( pkgFolder ); 312 mt.createFromTemplate( pDf, mName ); 313 314 } 315 316 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 return new SpecificationVersion("1.5"); 329 } else { 330 return v; 331 } 332 } 333 } 334 335 341 public static void setDefaultSourceLevel (SpecificationVersion version) { 342 defaultSourceLevel = version; 343 } 344 } 345 346 347 | Popular Tags |