1 19 20 package org.netbeans.spi.project.support.ant; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.Iterator ; 25 import org.netbeans.api.project.Project; 26 import org.netbeans.api.project.ProjectManager; 27 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton; 28 import org.openide.filesystems.FileLock; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.util.Lookup; 32 import org.openide.util.Mutex; 33 import org.openide.util.MutexException; 34 import org.openide.xml.XMLUtil; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 38 42 public class ProjectGenerator { 43 44 private ProjectGenerator() {} 45 46 70 public static AntProjectHelper createProject(final FileObject directory, final String type) throws IOException , IllegalArgumentException { 71 return createProject0(directory, type, null); 72 } 73 74 private static AntProjectHelper createProject0(final FileObject directory, final String type, final String name) throws IOException , IllegalArgumentException { 75 try { 76 return ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<AntProjectHelper>() { 77 public AntProjectHelper run() throws IOException { 78 if (ProjectManager.getDefault().findProject(directory) != null) { 79 throw new IllegalArgumentException ("Already a project in " + directory); } 81 FileObject projectXml = directory.getFileObject(AntProjectHelper.PROJECT_XML_PATH); 82 if (projectXml != null) { 83 throw new IllegalArgumentException ("Already a " + projectXml); } 85 projectXml = FileUtil.createData(directory, AntProjectHelper.PROJECT_XML_PATH); 86 Document doc = XMLUtil.createDocument("project", AntProjectHelper.PROJECT_NS, null, null); Element el = doc.createElementNS(AntProjectHelper.PROJECT_NS, "type"); el.appendChild(doc.createTextNode(type)); 89 doc.getDocumentElement().appendChild(el); 90 if (name != null) { 91 el = doc.createElementNS(AntProjectHelper.PROJECT_NS, "name"); el.appendChild(doc.createTextNode(name)); 93 doc.getDocumentElement().appendChild(el); 94 } 95 el = doc.createElementNS(AntProjectHelper.PROJECT_NS, "configuration"); doc.getDocumentElement().appendChild(el); 97 FileLock lock = projectXml.lock(); 98 try { 99 OutputStream os = projectXml.getOutputStream(lock); 100 try { 101 XMLUtil.write(doc, os, "UTF-8"); } finally { 103 os.close(); 104 } 105 } finally { 106 lock.releaseLock(); 107 } 108 ProjectManager.getDefault().clearNonProjectCache(); 111 Project p = ProjectManager.getDefault().findProject(directory); 112 if (p == null) { 113 for (AntBasedProjectType abpt : Lookup.getDefault().lookupAll(AntBasedProjectType.class)) { 115 if (abpt.getType().equals(type)) { 116 throw new IllegalArgumentException ("For some reason the folder " + directory + " with a new project of type " + type + " is still not recognized"); } 119 } 120 throw new IllegalArgumentException ("No Ant-based project factory for type " + type); } 122 AntProjectHelper helper = AntBasedProjectFactorySingleton.getHelperFor(p); 123 if (helper == null) { 124 throw new IllegalArgumentException ("Project " + p + " was not recognized as an Ant-based project"); } 126 helper.markModified(); 127 return helper; 128 } 129 }); 130 } catch (MutexException e) { 131 throw (IOException )e.getException(); 132 } 133 } 134 135 } 136 | Popular Tags |