KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > 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.spi.project.support.ant;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.Iterator JavaDoc;
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 JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * Utilities to create new Ant-based projects on disk.
40  * @author Jesse Glick
41  */

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

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