1 19 20 package org.netbeans.modules.ruby.spi.project.support.rake; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.net.URL ; 26 import java.util.Iterator ; 27 import javax.xml.transform.Transformer ; 28 import javax.xml.transform.TransformerException ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.stream.StreamResult ; 31 import javax.xml.transform.stream.StreamSource ; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.modules.ruby.modules.project.rake.RakeBasedProjectFactorySingleton; 35 import org.netbeans.modules.ruby.modules.project.rake.Util; 36 import org.openide.filesystems.FileLock; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.util.Lookup; 40 import org.openide.util.Mutex; 41 import org.openide.util.MutexException; 42 import org.openide.xml.XMLUtil; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 46 50 public class ProjectGenerator { 51 52 private ProjectGenerator() {} 53 54 78 public static RakeProjectHelper createProject(final FileObject directory, final String type) throws IOException , IllegalArgumentException { 79 return createProject0(directory, type, null); 80 } 81 82 private static RakeProjectHelper createProject0(final FileObject directory, final String type, final String name) throws IOException , IllegalArgumentException { 83 try { 84 return ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<RakeProjectHelper>() { 85 public RakeProjectHelper run() throws IOException { 86 if (ProjectManager.getDefault().findProject(directory) != null) { 87 throw new IllegalArgumentException ("Already a project in " + directory); } 89 FileObject projectXml = directory.getFileObject(RakeProjectHelper.PROJECT_XML_PATH); 90 if (projectXml != null) { 91 throw new IllegalArgumentException ("Already a " + projectXml); } 93 projectXml = FileUtil.createData(directory, RakeProjectHelper.PROJECT_XML_PATH); 94 Document doc = XMLUtil.createDocument("project", RakeProjectHelper.PROJECT_NS, null, null); Element el = doc.createElementNS(RakeProjectHelper.PROJECT_NS, "type"); el.appendChild(doc.createTextNode(type)); 97 doc.getDocumentElement().appendChild(el); 98 if (name != null) { 99 el = doc.createElementNS(RakeProjectHelper.PROJECT_NS, "name"); el.appendChild(doc.createTextNode(name)); 101 doc.getDocumentElement().appendChild(el); 102 } 103 el = doc.createElementNS(RakeProjectHelper.PROJECT_NS, "configuration"); doc.getDocumentElement().appendChild(el); 105 FileLock lock = projectXml.lock(); 106 try { 107 OutputStream os = projectXml.getOutputStream(lock); 108 try { 109 XMLUtil.write(doc, os, "UTF-8"); } finally { 111 os.close(); 112 } 113 } finally { 114 lock.releaseLock(); 115 } 116 ProjectManager.getDefault().clearNonProjectCache(); 119 Project p = ProjectManager.getDefault().findProject(directory); 120 if (p == null) { 121 Iterator it = Lookup.getDefault().lookupAll(RakeBasedProjectType.class).iterator(); 123 while (it.hasNext()) { 124 RakeBasedProjectType abpt = (RakeBasedProjectType)it.next(); 125 if (abpt.getType().equals(type)) { 126 throw new IllegalArgumentException ("For some reason the folder " + directory + " with a new project of type " + type + " is still not recognized"); } 129 } 130 throw new IllegalArgumentException ("No Ant-based project factory for type " + type); } 132 RakeProjectHelper helper = RakeBasedProjectFactorySingleton.getHelperFor(p); 133 if (helper == null) { 134 throw new IllegalArgumentException ("Project " + p + " was not recognized as an Ant-based project"); } 136 helper.markModified(); 137 return helper; 138 } 139 }); 140 } catch (MutexException e) { 141 throw (IOException )e.getException(); 142 } 143 } 144 145 } 146 | Popular Tags |