1 19 20 package org.netbeans.modules.java.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.Properties ; 28 import java.util.Stack ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipInputStream ; 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 ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.w3c.dom.Text ; 45 import org.xml.sax.InputSource ; 46 47 52 public class J2SESampleProjectGenerator { 53 54 private static final String PROJECT_CONFIGURATION_NAMESPACE = "http://www.netbeans.org/ns/j2se-project/3"; 56 private J2SESampleProjectGenerator() {} 57 58 59 public static FileObject createProjectFromTemplate(final FileObject template, File projectLocation, final String name) throws IOException { 60 assert template != null && projectLocation != null && name != null; 61 FileObject prjLoc = createProjectFolder (projectLocation); 62 if (template.getExt().endsWith("zip")) { unzip(template.getInputStream(), prjLoc); 64 try { 65 File projXml = FileUtil.toFile(prjLoc.getFileObject(AntProjectHelper.PROJECT_XML_PATH)); 67 Document doc = XMLUtil.parse(new InputSource (projXml.toURI().toString()), false, true, null, null); 68 NodeList nlist = doc.getElementsByTagNameNS(PROJECT_CONFIGURATION_NAMESPACE, "name"); if (nlist != null) { 70 for (int i=0; i < nlist.getLength(); i++) { 71 Node n = nlist.item(i); 72 if (n.getNodeType() != Node.ELEMENT_NODE) { 73 continue; 74 } 75 Element e = (Element )n; 76 77 replaceText(e, name); 78 } 79 saveXml(doc, prjLoc, AntProjectHelper.PROJECT_XML_PATH); 80 File privateProperties = createPrivateProperties (prjLoc); 82 Properties p = new Properties (); 84 p.put ("javadoc.preview","true"); FileOutputStream out = new FileOutputStream (privateProperties); 86 try { 87 p.store(out,null); 88 } finally { 89 out.close (); 90 } 91 } 92 93 } catch (Exception e) { 94 throw new IOException (e.toString()); 95 } 96 prjLoc.refresh(false); 97 } 98 return prjLoc; 99 } 100 101 private static FileObject createProjectFolder (File projectFolder) throws IOException { 102 FileObject projLoc; 103 Stack nameStack = new Stack (); 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 )nameStack.pop()); 110 assert projLoc != null; 111 } 112 return projLoc; 113 } 114 115 private static void unzip(InputStream source, FileObject targetFolder) throws IOException { 116 ZipInputStream zip=new ZipInputStream (source); 118 try { 119 ZipEntry 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 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 createPrivateProperties (FileObject fo) throws IOException { 144 String [] 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 161 private static void replaceText(Element parent, String name) { 162 NodeList l = parent.getChildNodes(); 163 for (int i = 0; i < l.getLength(); i++) { 164 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 165 Text text = (Text )l.item(i); 166 text.setNodeValue(name); 167 return; 168 } 169 } 170 } 171 172 176 private static void saveXml(Document doc, FileObject dir, String path) throws IOException { 177 FileObject xml = FileUtil.createData(dir, path); 178 FileLock lock = xml.lock(); 179 try { 180 OutputStream os = xml.getOutputStream(lock); 181 try { 182 XMLUtil.write(doc, os, "UTF-8"); } finally { 184 os.close(); 185 } 186 } finally { 187 lock.releaseLock(); 188 } 189 } 190 191 } 192 | Popular Tags |