KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > spi > project > support > rake > ProjectGenerator


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.ruby.spi.project.support.rake;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45
46 /**
47  * Utilities to create new Ant-based projects on disk.
48  * @author Jesse Glick
49  */

50 public class ProjectGenerator {
51     
52     private ProjectGenerator() {}
53     
54     /**
55      * Create a new Ant-based project on disk.
56      * It will initially be only minimally configured - just a skeleton <code>project.xml</code>.
57      * It will be marked as modified.
58      * <p>In order to fill in various details of it, call normal methods on the returned
59      * helper object, then save the project when you are done.
60      * (You can use {@link ProjectManager} to find the project object to be saved.)
61      * <p>No <code>build-impl.xml</code> will be created immediately; once you save the project
62      * changes, it will be created. If you wish to create a top-level <code>build.xml</code>
63      * use {@link GeneratedFilesHelper#generateBuildScriptFromStylesheet} after
64      * (or while) saving the project.
65      * <p>Acquires write access. But you are advised to acquire a write lock for
66      * the entire operation of creating, configuring, and saving the new project,
67      * and creating its initial <code>build.xml</code>.
68      * @param directory the main project directory to create it in
69      * (see {@link RakeProjectHelper#getProjectDirectory})
70      * @param type a unique project type identifier (see {@link RakeBasedProjectType#getType})
71      * @return an associated helper object
72      * @throws IOException if there is a problem physically creating the project
73      * @throws IllegalArgumentException if the project type does not match a registered
74      * Ant-based project type factory or if the directory
75      * is already recognized as some kind of project or if the
76      * new project on disk is recognized by some other factory
77      */

78     public static RakeProjectHelper createProject(final FileObject directory, final String JavaDoc type) throws IOException JavaDoc, IllegalArgumentException JavaDoc {
79         return createProject0(directory, type, null);
80     }
81     
82     private static RakeProjectHelper createProject0(final FileObject directory, final String JavaDoc type, final String JavaDoc name) throws IOException JavaDoc, IllegalArgumentException JavaDoc {
83         try {
84             return ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<RakeProjectHelper>() {
85                 public RakeProjectHelper run() throws IOException JavaDoc {
86                     if (ProjectManager.getDefault().findProject(directory) != null) {
87                         throw new IllegalArgumentException JavaDoc("Already a project in " + directory); // NOI18N
88
}
89                     FileObject projectXml = directory.getFileObject(RakeProjectHelper.PROJECT_XML_PATH);
90                     if (projectXml != null) {
91                         throw new IllegalArgumentException JavaDoc("Already a " + projectXml); // NOI18N
92
}
93                     projectXml = FileUtil.createData(directory, RakeProjectHelper.PROJECT_XML_PATH);
94                     Document JavaDoc doc = XMLUtil.createDocument("project", RakeProjectHelper.PROJECT_NS, null, null); // NOI18N
95
Element JavaDoc el = doc.createElementNS(RakeProjectHelper.PROJECT_NS, "type"); // NOI18N
96
el.appendChild(doc.createTextNode(type));
97                     doc.getDocumentElement().appendChild(el);
98                     if (name != null) {
99                         el = doc.createElementNS(RakeProjectHelper.PROJECT_NS, "name"); // NOI18N
100
el.appendChild(doc.createTextNode(name));
101                         doc.getDocumentElement().appendChild(el);
102                     }
103                     el = doc.createElementNS(RakeProjectHelper.PROJECT_NS, "configuration"); // NOI18N
104
doc.getDocumentElement().appendChild(el);
105                     FileLock lock = projectXml.lock();
106                     try {
107                         OutputStream JavaDoc os = projectXml.getOutputStream(lock);
108                         try {
109                             XMLUtil.write(doc, os, "UTF-8"); // NOI18N
110
} finally {
111                             os.close();
112                         }
113                     } finally {
114                         lock.releaseLock();
115                     }
116                     // OK, disk file project.xml has been created.
117
// Load the project into memory and mark it as modified.
118
ProjectManager.getDefault().clearNonProjectCache();
119                     Project p = ProjectManager.getDefault().findProject(directory);
120                     if (p == null) {
121                         // Something is wrong, it is not being recognized.
122
Iterator JavaDoc it = Lookup.getDefault().lookupAll(RakeBasedProjectType.class).iterator();
123                         while (it.hasNext()) {
124                             RakeBasedProjectType abpt = (RakeBasedProjectType)it.next();
125                             if (abpt.getType().equals(type)) {
126                                 // Well, the factory was there.
127
throw new IllegalArgumentException JavaDoc("For some reason the folder " + directory + " with a new project of type " + type + " is still not recognized"); // NOI18N
128
}
129                         }
130                         throw new IllegalArgumentException JavaDoc("No Ant-based project factory for type " + type); // NOI18N
131
}
132                     RakeProjectHelper helper = RakeBasedProjectFactorySingleton.getHelperFor(p);
133                     if (helper == null) {
134                         throw new IllegalArgumentException JavaDoc("Project " + p + " was not recognized as an Ant-based project"); // NOI18N
135
}
136                     helper.markModified();
137                     return helper;
138                 }
139             });
140         } catch (MutexException e) {
141             throw (IOException JavaDoc)e.getException();
142         }
143     }
144
145 }
146
Popular Tags