1 19 20 package org.netbeans.modules.web.examples; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.Stack ; 28 import java.util.zip.ZipEntry ; 29 import java.util.zip.ZipInputStream ; 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 ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 import org.w3c.dom.NodeList ; 40 import org.w3c.dom.Text ; 41 import org.xml.sax.InputSource ; 42 43 48 public class WebSampleProjectGenerator { 49 50 private WebSampleProjectGenerator() {} 51 52 public static final String PROJECT_CONFIGURATION_NAMESPACE = "http://www.netbeans.org/ns/web-project/3"; public static final String JSPC_CLASSPATH = "jspc.classpath"; 54 55 public static FileObject createProjectFromTemplate(final FileObject template, File projectLocation, final String name) throws IOException { 56 assert template != null && projectLocation != null && name != null; 57 FileObject prjLoc = createProjectFolder(projectLocation); 58 if (template.getExt().endsWith("zip")) { unzip(template.getInputStream(), prjLoc); 60 try { 62 File projXml = FileUtil.toFile(prjLoc.getFileObject(AntProjectHelper.PROJECT_XML_PATH)); 63 Document doc = XMLUtil.parse(new InputSource (projXml.toURI().toString()), false, true, null, null); 64 NodeList nlist = doc.getElementsByTagNameNS(PROJECT_CONFIGURATION_NAMESPACE, "name"); if (nlist != null) { 66 for (int i=0; i < nlist.getLength(); i++) { 67 Node n = nlist.item(i); 68 if (n.getNodeType() != Node.ELEMENT_NODE) { 69 continue; 70 } 71 Element e = (Element )n; 72 73 replaceText(e, name); 74 } 75 saveXml(doc, prjLoc, AntProjectHelper.PROJECT_XML_PATH); 76 } 77 } catch (Exception e) { 78 throw new IOException (e.toString()); 79 } 80 81 prjLoc.refresh(false); 82 } 83 return prjLoc; 84 } 85 86 private static FileObject createProjectFolder(File projectFolder) throws IOException { 87 FileObject projLoc; 88 Stack nameStack = new Stack (); 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 )nameStack.pop()); 95 assert projLoc != null; 96 } 97 return projLoc; 98 } 99 100 private static void unzip(InputStream source, FileObject targetFolder) throws IOException { 101 ZipInputStream zip=new ZipInputStream (source); 103 try { 104 ZipEntry 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 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 134 private static void replaceText(Element parent, String name) { 135 NodeList l = parent.getChildNodes(); 136 for (int i = 0; i < l.getLength(); i++) { 137 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 138 Text text = (Text )l.item(i); 139 text.setNodeValue(name); 140 return; 141 } 142 } 143 } 144 145 149 private static void saveXml(Document doc, FileObject dir, String path) throws IOException { 150 FileObject xml = FileUtil.createData(dir, path); 151 FileLock lock = xml.lock(); 152 try { 153 OutputStream os = xml.getOutputStream(lock); 154 try { 155 XMLUtil.write(doc, os, "UTF-8"); } finally { 157 os.close(); 158 } 159 } finally { 160 lock.releaseLock(); 161 } 162 } 163 164 } 165 | Popular Tags |