1 19 20 package org.netbeans.modules.j2ee.blueprints.ui.projects; 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.HashMap ; 28 import java.util.List ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipInputStream ; 31 import org.netbeans.modules.j2ee.blueprints.catalog.SolutionsCatalog; 32 import org.netbeans.spi.project.support.ant.AntProjectHelper; 33 import org.netbeans.spi.project.support.ant.EditableProperties; 34 import org.openide.filesystems.FileLock; 35 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.xml.XMLUtil; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.NodeList ; 43 import org.w3c.dom.Text ; 44 import org.xml.sax.*; 45 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbcatalog; 46 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbcategory; 47 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbexample; 48 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbsolution; 49 import org.netbeans.modules.j2ee.blueprints.catalog.bpcatalogxmlparser.Nbwriteup; 50 import org.netbeans.modules.j2ee.blueprints.ui.BpcatalogLocalizedResource; 51 import org.netbeans.modules.j2ee.blueprints.ui.overview.OverviewPageTopComponent; 52 import org.openide.modules.InstalledFileLocator; 53 import org.openide.windows.TopComponent; 54 import org.openide.windows.WindowManager; 55 56 61 public class J2eeSampleProjectGenerator { 62 63 private J2eeSampleProjectGenerator() {} 64 65 public static final String PROJECT_CONFIGURATION_NAMESPACE = "http://www.netbeans.org/ns/web-project/3"; public static final String PROJECT_CONFIGURATION_NS_FREE = "http://www.netbeans.org/ns/freeform-project/1"; 67 public static final String EJBJAR_NAMESPACE = "http://www.netbeans.org/ns/j2ee-ejbjarproject/3"; 68 public static final String PROJECT_CONFIGURATION_J2SE = "http://www.netbeans.org/ns/j2se-project/2"; 69 public static final String SUNWEBDD_XMLLOC = "web/WEB-INF/sun-web.xml"; 70 71 public static FileObject createProjectFromTemplate(final FileObject template, File projectLocation, final String name) throws IOException { 72 FileObject prjLoc = null; 73 String slashname = "/"+name; 74 if (template.getExt().endsWith("zip")) { unzip(template.getInputStream(), projectLocation); 76 String extra = (String )template.getAttribute("extrazip"); 77 if (extra!=null){ 78 FileObject p = template.getParent().getFileObject(extra+".zip"); 79 80 if (p!=null){ 81 unzip(p.getInputStream(), new File ( projectLocation.getParentFile(),extra)); 82 } 83 } 84 85 try { 86 prjLoc = FileUtil.toFileObject(projectLocation); 87 88 NodeList nlist = null; 89 File projXml = FileUtil.toFile(prjLoc.getFileObject(AntProjectHelper.PROJECT_XML_PATH)); 91 Document doc = XMLUtil.parse(new InputSource(projXml.toURI().toString()), false, true, null, null); 92 nlist = doc.getElementsByTagNameNS(PROJECT_CONFIGURATION_NAMESPACE, "name"); if (nlist==null || nlist.getLength()==0){ 97 nlist = doc.getElementsByTagNameNS(EJBJAR_NAMESPACE, "name"); 99 } 100 if (nlist == null || nlist.getLength() == 0) { 101 nlist = doc.getElementsByTagNameNS(PROJECT_CONFIGURATION_J2SE, "name"); 102 } 103 if (nlist != null) { 104 for (int i=0; i < nlist.getLength(); i++) { 105 Node n = nlist.item(i); 106 if (n.getNodeType() != Node.ELEMENT_NODE) { 107 continue; 108 } 109 Element e = (Element )n; 110 111 replaceText(e, name); 112 saveXml(doc, prjLoc, AntProjectHelper.PROJECT_XML_PATH); 113 } 114 } 115 116 118 FileObject sunwebfile = prjLoc.getFileObject(SUNWEBDD_XMLLOC); 119 if (sunwebfile!=null) { 122 File sunwebXml = FileUtil.toFile(sunwebfile); 123 Document swdoc = XMLUtil.parse(new InputSource(sunwebXml.toURI().toString()), false, true, null, SunWebDDResolver.getInstance()); 126 NodeList swnlist = swdoc.getElementsByTagName("context-root"); if (swnlist != null) { 129 for (int i=0; i < swnlist.getLength(); i++) { 130 Node n = swnlist.item(i); 131 if (n.getNodeType() != Node.ELEMENT_NODE) { 132 continue; 133 } 134 Element e = (Element )n; 135 replaceText(e, slashname); 136 saveXml(swdoc, prjLoc, SUNWEBDD_XMLLOC); 137 } 138 } 139 } 140 141 replaceProperties(prjLoc, name); 143 144 } catch (Exception e) { 145 throw new IOException (e.toString()); 146 } 147 148 prjLoc.refresh(false); 149 } 150 return prjLoc; 151 } 152 153 private static void replaceProperties(FileObject dir, String prjName) throws IOException { 154 155 final String DIST_JAR = "dist.jar"; final String DIST_DIR = "${dist.dir}/"; final String DIST_EAR_JAR = "dist.ear.jar"; final String WAR_NAME = "war.name"; final String WAR_EAR_NAME = "war.ear.name"; 163 String BP_LIB_DIR = "bp-lib.dir"; String BP_UI5_JAR = "file.reference.bp-ui-5.jar"; 166 String currentProp; 167 String suffix; 168 169 EditableProperties props = new EditableProperties(true); 170 FileObject prjProp = FileUtil.createData(dir, AntProjectHelper.PROJECT_PROPERTIES_PATH); 171 172 try { 173 InputStream in = prjProp.getInputStream(); 174 props.load(in); 175 in.close(); 176 } catch (Exception e) { 177 throw new IOException (e.toString()); 178 } 179 180 String distJar = props.getProperty(DIST_JAR); 181 if (distJar != null) { 183 props.setProperty(DIST_JAR, DIST_DIR + prjName + 184 distJar.substring(distJar.lastIndexOf('.'))); currentProp = props.getProperty(DIST_EAR_JAR); 187 if (currentProp != null) { 188 props.setProperty(DIST_EAR_JAR, 189 DIST_DIR + prjName + currentProp.substring(currentProp.indexOf("."))); } 191 } else if (props.getProperty(WAR_NAME) != null) { 193 props.setProperty(WAR_NAME, prjName + ".war"); currentProp = props.getProperty(WAR_EAR_NAME); 197 if (currentProp != null) { 198 props.setProperty(WAR_EAR_NAME, 199 prjName + currentProp.substring(currentProp.indexOf("."))); } 201 } 202 203 String bpLibsLocation = getBlueprintsLibsLocation(); 205 props.setProperty(BP_LIB_DIR, bpLibsLocation); 206 if(props.getProperty(BP_UI5_JAR) != null){ 207 props.setProperty(BP_UI5_JAR, bpLibsLocation + File.separator + "bp-ui-5.jar"); } 209 210 FileLock lock = prjProp.lock(); 212 try { 213 OutputStream os = prjProp.getOutputStream(lock); 214 try { 215 props.store(os); 216 } finally { 217 os.close(); 218 } 219 } finally { 220 lock.releaseLock(); 221 } 222 } 223 224 private static void unzip(InputStream source, File targetFolder) throws IOException { 225 ZipInputStream zip=new ZipInputStream (source); 227 try { 228 ZipEntry ent; 229 while ((ent = zip.getNextEntry()) != null) { 230 File f = new File (targetFolder, ent.getName()); 231 if (ent.isDirectory()) { 232 f.mkdirs(); 233 } else { 234 f.getParentFile().mkdirs(); 235 FileOutputStream out = new FileOutputStream (f); 236 try { 237 FileUtil.copy(zip, out); 238 } finally { 239 out.close(); 240 } 241 } 242 } 243 } finally { 244 zip.close(); 245 } 246 } 247 248 254 private static void replaceText(Element parent, String name) { 255 NodeList l = parent.getChildNodes(); 256 for (int i = 0; i < l.getLength(); i++) { 257 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 258 Text text = (Text )l.item(i); 259 text.setNodeValue(name); 260 return; 261 } 262 } 263 } 264 265 269 private static void saveXml(Document doc, FileObject dir, String path) throws IOException { 270 FileObject xml = FileUtil.createData(dir, path); 271 FileLock lock = xml.lock(); 272 try { 273 OutputStream os = xml.getOutputStream(lock); 274 try { 275 XMLUtil.write(doc, os, "UTF-8"); } finally { 277 os.close(); 278 } 279 } finally { 280 lock.releaseLock(); 281 } 282 } 283 284 private static class SunWebDDResolver implements EntityResolver { 285 static SunWebDDResolver resolver; 286 static synchronized SunWebDDResolver getInstance() { 287 if (resolver==null) { 288 resolver=new SunWebDDResolver(); 289 } 290 return resolver; 291 } 292 public InputSource resolveEntity (String publicId, String systemId) { 293 String resource=null; 294 resource = "/org/netbeans/modules/j2ee/blueprints/ui/resources/sun-web-app_2_4-1.dtd"; 306 java.net.URL url = this.getClass().getResource(resource); 307 return new InputSource(url.toString()); 308 } 309 } 310 311 private static String getBlueprintsLibsLocation(){ 312 String location = ""; 313 File f = InstalledFileLocator.getDefault().locate("modules/ext/blueprints/bp-ui-14.jar", null, true); 314 if(f != null) 315 location = f.getParentFile().getAbsolutePath(); 316 return location; 317 } 318 319 public static void getOverviewPage(org.openide.loaders.TemplateWizard templateWizard){ 320 TopComponent tc = WindowManager.getDefault().findTopComponent("BluePrints"); 321 if(tc.isOpened()){ 322 return; 323 } 324 325 FileObject template = templateWizard.getTemplate().getPrimaryFile(); 326 String currentModName = template.getName(); 327 SolutionsCatalog solutionsCatalog = SolutionsCatalog.getInstance(); 328 329 Nbcatalog nbcatalog = solutionsCatalog.getCatalogXml(); 330 HashMap overviewFiles = populateEntries(nbcatalog); 331 String pagePath = (String )overviewFiles.get(currentModName); 332 String overviewPage = getLocalizedPath(pagePath); 333 334 TopComponent win = OverviewPageTopComponent.findInstance(); 335 OverviewPageTopComponent comp = (OverviewPageTopComponent)win; 336 if(comp.isOpened()) 337 comp.close(); 338 comp.setOverviewFile(overviewPage); 339 comp.open(); 340 comp.requestActive(); 341 } 342 343 private static HashMap populateEntries(Nbcatalog nbcatalog){ 344 HashMap overviewFiles = new HashMap (); 345 List cats = nbcatalog.fetchNbcategoryList(); 346 for(int catNum=0; catNum<cats.size(); catNum++){ 347 Nbcategory category = (Nbcategory)cats.get(catNum); 348 List sols = category.fetchNbsolutionList(); 349 for(int solNum=0; solNum<sols.size(); solNum++){ 350 Nbsolution sol = (Nbsolution)sols.get(solNum); 351 Nbwriteup write = sol.getNbwriteup(); 352 String categoryID = category.getId(); 353 String articlePath = write.getArticlePath(); 354 if(categoryID.equals("Ajax")) { articlePath = "docs/ajax/overview-Ajax.html"; 356 }else if(categoryID.equals("JavaPersistence")) { articlePath = "docs/persistence/overview-JavaPersistence.html"; 358 } 359 overviewFiles.put(sol.getExampleId(), articlePath); 360 } 361 } 362 return overviewFiles; 363 } 364 365 private static String getLocalizedPath(String articlePath){ 366 String CATALOG_RESOURCES_URL = "/org/netbeans/modules/j2ee/blueprints/catalog/resources"; String path = CATALOG_RESOURCES_URL + "/" + articlePath; BpcatalogLocalizedResource htmlrsc = 369 new BpcatalogLocalizedResource(path, "html"); String localizedPath = htmlrsc.getResourcePath(); 371 return localizedPath; 372 } 373 } 374 | Popular Tags |