KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > examples > J2SESampleProjectGenerator


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.java.examples;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Stack JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.util.zip.ZipInputStream JavaDoc;
31 import org.netbeans.spi.project.support.ant.AntProjectHelper;
32
33 import org.openide.modules.InstalledFileLocator;
34 import org.netbeans.spi.project.support.ant.EditableProperties;
35 import org.openide.filesystems.FileLock;
36
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.xml.XMLUtil;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44 import org.w3c.dom.Text JavaDoc;
45 import org.xml.sax.InputSource JavaDoc;
46
47 /**
48  * Create a sample web project by unzipping a template into some directory
49  *
50  * @author Martin Grebac, Tomas Zezula
51  */

52 public class J2SESampleProjectGenerator {
53
54     private static final String JavaDoc PROJECT_CONFIGURATION_NAMESPACE = "http://www.netbeans.org/ns/j2se-project/3"; //NOI18N
55

56     private J2SESampleProjectGenerator() {}
57
58
59     public static FileObject createProjectFromTemplate(final FileObject template, File JavaDoc projectLocation, final String JavaDoc name) throws IOException JavaDoc {
60         assert template != null && projectLocation != null && name != null;
61         FileObject prjLoc = createProjectFolder (projectLocation);
62         if (template.getExt().endsWith("zip")) { //NOI18N
63
unzip(template.getInputStream(), prjLoc);
64             try {
65                 // update project.xml
66
File JavaDoc projXml = FileUtil.toFile(prjLoc.getFileObject(AntProjectHelper.PROJECT_XML_PATH));
67                 Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(projXml.toURI().toString()), false, true, null, null);
68                 NodeList JavaDoc nlist = doc.getElementsByTagNameNS(PROJECT_CONFIGURATION_NAMESPACE, "name"); //NOI18N
69
if (nlist != null) {
70                     for (int i=0; i < nlist.getLength(); i++) {
71                         Node JavaDoc n = nlist.item(i);
72                         if (n.getNodeType() != Node.ELEMENT_NODE) {
73                             continue;
74                         }
75                         Element JavaDoc e = (Element JavaDoc)n;
76                         
77                         replaceText(e, name);
78                     }
79                     saveXml(doc, prjLoc, AntProjectHelper.PROJECT_XML_PATH);
80                     //update private properties
81
File JavaDoc privateProperties = createPrivateProperties (prjLoc);
82                     //No need to load the properties the file is empty
83
Properties JavaDoc p = new Properties JavaDoc ();
84                     p.put ("javadoc.preview","true"); //NOI18N
85
FileOutputStream JavaDoc out = new FileOutputStream JavaDoc (privateProperties);
86                     try {
87                         p.store(out,null);
88                     } finally {
89                         out.close ();
90                     }
91                 }
92                 
93             } catch (Exception JavaDoc e) {
94                 throw new IOException JavaDoc(e.toString());
95             }
96             prjLoc.refresh(false);
97         }
98         return prjLoc;
99     }
100     
101     private static FileObject createProjectFolder (File JavaDoc projectFolder) throws IOException JavaDoc {
102         FileObject projLoc;
103         Stack JavaDoc nameStack = new Stack JavaDoc ();
104         while ((projLoc = FileUtil.toFileObject(projectFolder)) == null) {
105             nameStack.push(projectFolder.getName());
106             projectFolder = projectFolder.getParentFile();
107         }
108         while (!nameStack.empty()) {
109             projLoc = projLoc.createFolder ((String JavaDoc)nameStack.pop());
110             assert projLoc != null;
111         }
112         return projLoc;
113     }
114     
115     private static void unzip(InputStream JavaDoc source, FileObject targetFolder) throws IOException JavaDoc {
116         //installation
117
ZipInputStream JavaDoc zip=new ZipInputStream JavaDoc(source);
118         try {
119             ZipEntry JavaDoc ent;
120             while ((ent = zip.getNextEntry()) != null) {
121                 if (ent.isDirectory()) {
122                     FileUtil.createFolder(targetFolder, ent.getName());
123                 } else {
124                     FileObject destFile = FileUtil.createData(targetFolder,ent.getName());
125                     FileLock lock = destFile.lock();
126                     try {
127                         OutputStream JavaDoc out = destFile.getOutputStream(lock);
128                         try {
129                             FileUtil.copy(zip, out);
130                         } finally {
131                             out.close();
132                         }
133                     } finally {
134                         lock.releaseLock();
135                     }
136                 }
137             }
138         } finally {
139             zip.close();
140         }
141     }
142     
143     private static File JavaDoc createPrivateProperties (FileObject fo) throws IOException JavaDoc {
144         String JavaDoc[] nameElements = AntProjectHelper.PRIVATE_PROPERTIES_PATH.split("/");
145         for (int i=0; i<nameElements.length-1; i++) {
146             FileObject tmp = fo.getFileObject (nameElements[i]);
147             if (tmp == null) {
148                 tmp = fo.createFolder(nameElements[i]);
149             }
150             fo = tmp;
151         }
152         fo = fo.createData(nameElements[nameElements.length-1]);
153         return FileUtil.toFile(fo);
154     }
155
156     /**
157      * Extract nested text from an element.
158      * Currently does not handle coalescing text nodes, CDATA sections, etc.
159      * @param parent a parent element
160      */

161     private static void replaceText(Element JavaDoc parent, String JavaDoc name) {
162         NodeList JavaDoc l = parent.getChildNodes();
163         for (int i = 0; i < l.getLength(); i++) {
164             if (l.item(i).getNodeType() == Node.TEXT_NODE) {
165                 Text JavaDoc text = (Text JavaDoc)l.item(i);
166                 text.setNodeValue(name);
167                 return;
168             }
169         }
170     }
171     
172     /**
173      * Save an XML config file to a named path.
174      * If the file does not yet exist, it is created.
175      */

176     private static void saveXml(Document JavaDoc doc, FileObject dir, String JavaDoc path) throws IOException JavaDoc {
177         FileObject xml = FileUtil.createData(dir, path);
178         FileLock lock = xml.lock();
179         try {
180             OutputStream JavaDoc os = xml.getOutputStream(lock);
181             try {
182                 XMLUtil.write(doc, os, "UTF-8"); // NOI18N
183
} finally {
184                 os.close();
185             }
186         } finally {
187             lock.releaseLock();
188         }
189     }
190     
191 }
192
Popular Tags