KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > examples > WebSampleProjectGenerator


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.web.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.Stack JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipInputStream JavaDoc;
30 import org.netbeans.spi.project.support.ant.AntProjectHelper;
31 import org.openide.filesystems.FileLock;
32
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.xml.XMLUtil;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40 import org.w3c.dom.Text JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42
43 /**
44  * Create a sample web project by unzipping a template into some directory
45  *
46  * @author Martin Grebac
47  */

48 public class WebSampleProjectGenerator {
49     
50     private WebSampleProjectGenerator() {}
51
52     public static final String JavaDoc PROJECT_CONFIGURATION_NAMESPACE = "http://www.netbeans.org/ns/web-project/3"; //NOI18N
53
public static final String JavaDoc JSPC_CLASSPATH = "jspc.classpath";
54
55     public static FileObject createProjectFromTemplate(final FileObject template, File JavaDoc projectLocation, final String JavaDoc name) throws IOException JavaDoc {
56         assert template != null && projectLocation != null && name != null;
57         FileObject prjLoc = createProjectFolder(projectLocation);
58         if (template.getExt().endsWith("zip")) { //NOI18N
59
unzip(template.getInputStream(), prjLoc);
60             // update project.xml
61
try {
62                 File JavaDoc projXml = FileUtil.toFile(prjLoc.getFileObject(AntProjectHelper.PROJECT_XML_PATH));
63                 Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(projXml.toURI().toString()), false, true, null, null);
64                 NodeList JavaDoc nlist = doc.getElementsByTagNameNS(PROJECT_CONFIGURATION_NAMESPACE, "name"); //NOI18N
65
if (nlist != null) {
66                     for (int i=0; i < nlist.getLength(); i++) {
67                         Node JavaDoc n = nlist.item(i);
68                         if (n.getNodeType() != Node.ELEMENT_NODE) {
69                             continue;
70                         }
71                         Element JavaDoc e = (Element JavaDoc)n;
72                         
73                         replaceText(e, name);
74                     }
75                     saveXml(doc, prjLoc, AntProjectHelper.PROJECT_XML_PATH);
76                 }
77             } catch (Exception JavaDoc e) {
78                 throw new IOException JavaDoc(e.toString());
79             }
80                    
81             prjLoc.refresh(false);
82         }
83         return prjLoc;
84     }
85     
86     private static FileObject createProjectFolder(File JavaDoc projectFolder) throws IOException JavaDoc {
87         FileObject projLoc;
88         Stack JavaDoc nameStack = new Stack JavaDoc();
89         while ((projLoc = FileUtil.toFileObject(projectFolder)) == null) {
90             nameStack.push(projectFolder.getName());
91             projectFolder = projectFolder.getParentFile();
92         }
93         while (!nameStack.empty()) {
94             projLoc = projLoc.createFolder((String JavaDoc)nameStack.pop());
95             assert projLoc != null;
96         }
97         return projLoc;
98     }
99
100     private static void unzip(InputStream JavaDoc source, FileObject targetFolder) throws IOException JavaDoc {
101         //installation
102
ZipInputStream JavaDoc zip=new ZipInputStream JavaDoc(source);
103         try {
104             ZipEntry JavaDoc ent;
105             while ((ent = zip.getNextEntry()) != null) {
106                 if (ent.isDirectory()) {
107                     FileUtil.createFolder(targetFolder, ent.getName());
108                 } else {
109                     FileObject destFile = FileUtil.createData(targetFolder,ent.getName());
110                     FileLock lock = destFile.lock();
111                     try {
112                         OutputStream JavaDoc out = destFile.getOutputStream(lock);
113                         try {
114                             FileUtil.copy(zip, out);
115                         } finally {
116                             out.close();
117                         }
118                     } finally {
119                         lock.releaseLock();
120                     }
121                 }
122             }
123         } finally {
124             zip.close();
125         }
126     }
127
128     /**
129      * Extract nested text from an element.
130      * Currently does not handle coalescing text nodes, CDATA sections, etc.
131      * @param parent a parent element
132      * @return the nested text, or null if none was found
133      */

134     private static void replaceText(Element JavaDoc parent, String JavaDoc name) {
135         NodeList JavaDoc l = parent.getChildNodes();
136         for (int i = 0; i < l.getLength(); i++) {
137             if (l.item(i).getNodeType() == Node.TEXT_NODE) {
138                 Text JavaDoc text = (Text JavaDoc)l.item(i);
139                 text.setNodeValue(name);
140                 return;
141             }
142         }
143     }
144     
145     /**
146      * Save an XML config file to a named path.
147      * If the file does not yet exist, it is created.
148      */

149     private static void saveXml(Document JavaDoc doc, FileObject dir, String JavaDoc path) throws IOException JavaDoc {
150         FileObject xml = FileUtil.createData(dir, path);
151         FileLock lock = xml.lock();
152         try {
153             OutputStream JavaDoc os = xml.getOutputStream(lock);
154             try {
155                 XMLUtil.write(doc, os, "UTF-8"); // NOI18N
156
} finally {
157                 os.close();
158             }
159         } finally {
160             lock.releaseLock();
161         }
162     }
163     
164 }
165
Popular Tags