1 11 package org.eclipse.pde.internal.ui.wizards.site; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.PrintWriter ; 15 import java.io.StringWriter ; 16 import java.lang.reflect.InvocationTargetException ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.StructuredSelection; 25 import org.eclipse.pde.internal.core.natures.PDE; 26 import org.eclipse.pde.internal.core.site.WorkspaceSiteModel; 27 import org.eclipse.pde.internal.core.util.CoreUtility; 28 import org.eclipse.pde.internal.ui.IPDEUIConstants; 29 import org.eclipse.pde.internal.ui.PDEPlugin; 30 import org.eclipse.pde.internal.ui.PDEUIMessages; 31 import org.eclipse.swt.widgets.Display; 32 import org.eclipse.ui.IWorkbenchPage; 33 import org.eclipse.ui.IWorkbenchPart; 34 import org.eclipse.ui.IWorkbenchWindow; 35 import org.eclipse.ui.PartInitException; 36 import org.eclipse.ui.actions.WorkspaceModifyOperation; 37 import org.eclipse.ui.ide.IDE; 38 import org.eclipse.ui.part.FileEditorInput; 39 import org.eclipse.ui.part.ISetSelectionTarget; 40 41 42 public class NewSiteProjectCreationOperation extends WorkspaceModifyOperation { 43 private Display fDisplay; 44 private IProject fProject; 45 private IPath fPath; 46 private String fWebLocation; 47 48 public NewSiteProjectCreationOperation(Display display, IProject project, IPath path, String webLocation) { 49 fDisplay = display; 50 fProject = project; 51 fPath = path; 52 fWebLocation = webLocation; 53 } 54 55 58 protected void execute(IProgressMonitor monitor) throws CoreException, 59 InvocationTargetException , InterruptedException { 60 int numUnits = fWebLocation == null ? 3 : 4; 61 62 monitor.beginTask(PDEUIMessages.NewSiteWizard_creatingProject, numUnits); 63 64 CoreUtility.createProject(fProject, fPath, monitor); 65 fProject.open(monitor); 66 CoreUtility.addNatureToProject(fProject, PDE.SITE_NATURE, monitor); 67 monitor.worked(1); 68 69 if (fWebLocation != null){ 70 CoreUtility.createFolder(fProject.getFolder(fWebLocation)); 71 createXSLFile(); 72 createCSSFile(); 73 createHTMLFile(); 74 monitor.worked(1); 75 } 76 77 monitor.subTask(PDEUIMessages.NewSiteWizard_creatingManifest); 78 IFile file = createSiteManifest(); 79 monitor.worked(1); 80 81 openFile(file); 82 monitor.worked(1); 83 84 } 85 86 private IFile createSiteManifest() throws CoreException { 87 IFile file = fProject.getFile("site.xml"); if (file.exists()) return file; 89 90 WorkspaceSiteModel model = new WorkspaceSiteModel(file); 91 model.getSite(); 92 model.save(); 94 model.dispose(); 95 IDE.setDefaultEditor(file, IPDEUIConstants.SITE_EDITOR_ID); 97 return file; 98 } 99 100 private void openFile(final IFile file) { 101 fDisplay.asyncExec(new Runnable () { 102 public void run() { 103 IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow(); 104 if (ww == null) { 105 return; 106 } 107 IWorkbenchPage page = ww.getActivePage(); 108 if (page == null || !file.exists()) 109 return; 110 IWorkbenchPart focusPart = page.getActivePart(); 111 if (focusPart instanceof ISetSelectionTarget) { 112 ISelection selection = new StructuredSelection(file); 113 ((ISetSelectionTarget) focusPart).selectReveal(selection); 114 } 115 try { 116 page.openEditor(new FileEditorInput(file), 117 IPDEUIConstants.SITE_EDITOR_ID); 118 } catch (PartInitException e) { 119 } 120 } 121 }); 122 } 123 124 private void createHTMLFile(){ 125 StringWriter swriter = new StringWriter (); 126 PrintWriter writer = new PrintWriter (swriter); 127 128 writer.println("<html>"); writer.println("<head>"); writer.println("<title>"+fProject.getName()+"</title>"); writer.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"); writer.println("<style>@import url(\""+fWebLocation+"/site.css\");</style>"); writer.println("<script type=\"text/javascript\">"); writer.println(" var returnval = 0;"); writer.println(" var stylesheet, xmlFile, cache, doc;"); writer.println(" function init(){"); writer.println(" // NSCP 7.1+ / Mozilla 1.4.1+ / Safari"); writer.println(" // Use the standard DOM Level 2 technique, if it is supported"); writer.println(" if (document.implementation && document.implementation.createDocument) {"); writer.println(" xmlFile = document.implementation.createDocument(\"\", \"\", null);"); writer.println(" stylesheet = document.implementation.createDocument(\"\", \"\", null);"); writer.println(" if (xmlFile.load){"); writer.println(" xmlFile.load(\"site.xml\");"); writer.println(" stylesheet.load(\""+fWebLocation+"/site.xsl\");"); writer.println(" } else {"); writer.println(" alert(\"" + PDEUIMessages.SiteHTML_loadError + "\");"); writer.println(" }"); writer.println(" xmlFile.addEventListener(\"load\", transform, false);"); writer.println(" stylesheet.addEventListener(\"load\", transform, false);"); writer.println(" }"); writer.println(" //IE 6.0+ solution"); writer.println(" else if (window.ActiveXObject) {"); writer.println(" xmlFile = new ActiveXObject(\"msxml2.DOMDocument.3.0\");"); writer.println(" xmlFile.async = false;"); writer.println(" xmlFile.load(\"site.xml\");"); writer.println(" stylesheet = new ActiveXObject(\"msxml2.FreeThreadedDOMDocument.3.0\");"); writer.println(" stylesheet.async = false;"); writer.println(" stylesheet.load(\""+fWebLocation+"/site.xsl\");"); writer.println(" cache = new ActiveXObject(\"msxml2.XSLTemplate.3.0\");"); writer.println(" cache.stylesheet = stylesheet;"); writer.println(" transformData();"); writer.println(" }"); writer.println(" }"); writer.println(" // separate transformation function for IE 6.0+"); writer.println(" function transformData(){"); writer.println(" var processor = cache.createProcessor();"); writer.println(" processor.input = xmlFile;"); writer.println(" processor.transform();"); writer.println(" data.innerHTML = processor.output;"); writer.println(" }"); writer.println(" // separate transformation function for NSCP 7.1+ and Mozilla 1.4.1+ "); writer.println(" function transform(){"); writer.println(" returnval+=1;"); writer.println(" if (returnval==2){"); writer.println(" var processor = new XSLTProcessor();"); writer.println(" processor.importStylesheet(stylesheet); "); writer.println(" doc = processor.transformToDocument(xmlFile);"); writer.println(" document.getElementById(\"data\").innerHTML = doc.documentElement.innerHTML;"); writer.println(" }"); writer.println(" }"); writer.println("</script>"); writer.println("</head>"); writer.println("<body onload=\"init();\">"); writer.println("<!--[insert static HTML here]-->"); writer.println("<div id=\"data\"><!-- this is where the transformed data goes --></div>"); writer.println("</body>"); writer.println("</html>"); 189 writer.flush(); 190 writeFile(fProject.getFile("index.html"), swriter); } 192 193 private void createCSSFile(){ 194 StringWriter swriter = new StringWriter (); 195 PrintWriter writer = new PrintWriter (swriter); 196 writer.println("<STYLE type=\"text/css\">"); writer.println("td.spacer {padding-bottom: 10px; padding-top: 10px;}"); writer.println(".title { font-family: sans-serif; color: #99AACC;}"); writer.println(".bodyText { font-family: sans-serif; font-size: 9pt; color:#000000; }"); writer.println(".sub-header { font-family: sans-serif; font-style: normal; font-weight: bold; font-size: 9pt; color: white;}"); writer.println(".log-text {font-family: sans-serif; font-style: normal; font-weight: lighter; font-size: 8pt; color:black;}"); writer.println(".big-header { font-family: sans-serif; font-style: normal; font-weight: bold; font-size: 9pt; color: white; border-top:10px solid white;}"); writer.println(".light-row {background:#FFFFFF}"); writer.println(".dark-row {background:#EEEEFF}"); writer.println(".header {background:#99AADD}"); writer.println("#indent {word-wrap : break-word;width :300px;text-indent:10px;}"); writer.println("</STYLE>"); 209 writer.flush(); 210 writeFile(fProject.getFile(fWebLocation + "/site.css"), swriter); } 212 213 private void createXSLFile(){ 214 StringWriter swriter = new StringWriter (); 215 PrintWriter writer = new PrintWriter (swriter); 216 writer.println("<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\">"); writer.println("<xsl:output method=\"html\" encoding=\"UTF-8\"/>"); writer.println("<xsl:key name=\"cat\" match=\"category\" use=\"@name\"/>"); writer.println("<xsl:template match=\"/\">"); writer.println("<xsl:for-each select=\"site\">"); writer.println(" <html>"); writer.println(" <head>"); writer.println(" <title>"+fProject.getName()+"</title>"); writer.println(" <style>@import url(\"" + fWebLocation + "/site.css\");</style>"); writer.println(" </head>"); writer.println(" <body>"); writer.println(" <h1 class=\"title\">" + fProject.getName() +"</h1>"); writer.println(" <p class=\"bodyText\"><xsl:value-of select=\"description\"/></p>"); writer.println(" <table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\">"); writer.println(" <xsl:for-each select=\"category-def\">"); writer.println(" <xsl:sort select=\"@label\" order=\"ascending\" case-order=\"upper-first\"/>"); writer.println(" <xsl:sort select=\"@name\" order=\"ascending\" case-order=\"upper-first\"/>"); writer.println(" <xsl:if test=\"count(key('cat',@name)) != 0\">"); writer.println(" <tr class=\"header\">"); writer.println(" <td class=\"sub-header\" width=\"30%\">"); writer.println(" <xsl:value-of select=\"@name\"/>"); writer.println(" </td>"); writer.println(" <td class=\"sub-header\" width=\"70%\">"); writer.println(" <xsl:value-of select=\"@label\"/>"); writer.println(" </td>"); writer.println(" </tr>"); writer.println(" <xsl:for-each select=\"key('cat',@name)\">"); writer.println(" <xsl:sort select=\"ancestor::feature//@version\" order=\"ascending\"/>"); writer.println(" <xsl:sort select=\"ancestor::feature//@id\" order=\"ascending\" case-order=\"upper-first\"/>"); writer.println(" <tr>"); writer.println(" <xsl:choose>"); writer.println(" <xsl:when test=\"(position() mod 2 = 1)\">"); writer.println(" <xsl:attribute name=\"class\">dark-row</xsl:attribute>"); writer.println(" </xsl:when>"); writer.println(" <xsl:otherwise>"); writer.println(" <xsl:attribute name=\"class\">light-row</xsl:attribute>"); writer.println(" </xsl:otherwise>"); writer.println(" </xsl:choose>"); writer.println(" <td class=\"log-text\" id=\"indent\">"); writer.println(" <xsl:choose>"); writer.println(" <xsl:when test=\"ancestor::feature//@label\">"); writer.println(" <a HREF=\"{ancestor::feature//@url}\"><xsl:value-of select=\"ancestor::feature//@label\"/></a>"); writer.println(" <br/>"); writer.println(" <div id=\"indent\">"); writer.println(" (<xsl:value-of select=\"ancestor::feature//@id\"/> - <xsl:value-of select=\"ancestor::feature//@version\"/>)"); writer.println(" </div>"); writer.println(" </xsl:when>"); writer.println(" <xsl:otherwise>"); writer.println(" <a HREF=\"{ancestor::feature//@url}\"><xsl:value-of select=\"ancestor::feature//@id\"/> - <xsl:value-of select=\"ancestor::feature//@version\"/></a>"); writer.println(" </xsl:otherwise>"); writer.println(" </xsl:choose>"); writer.println(" <br />"); writer.println(" </td>"); writer.println(" <td>"); writer.println(" <table>"); writer.println(" <xsl:if test=\"ancestor::feature//@os\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Operating Systems:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"ancestor::feature//@os\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"ancestor::feature//@ws\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Windows Systems:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"ancestor::feature//@ws\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"ancestor::feature//@nl\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Languages:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"ancestor::feature//@nl\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"ancestor::feature//@arch\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Architecture:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"ancestor::feature//@arch\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" </table>"); writer.println(" </td>"); writer.println(" </tr>"); writer.println(" </xsl:for-each>"); writer.println(" <tr><td class=\"spacer\"><br/></td><td class=\"spacer\"><br/></td></tr>"); writer.println(" </xsl:if>"); writer.println(" </xsl:for-each>"); writer.println(" <xsl:if test=\"count(feature) > count(feature/category)\">"); writer.println(" <tr class=\"header\">"); writer.println(" <td class=\"sub-header\" colspan=\"2\">"); writer.println(" Uncategorized"); writer.println(" </td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:choose>"); writer.println(" <xsl:when test=\"function-available('msxsl:node-set')\">"); writer.println(" <xsl:variable name=\"rtf-nodes\">"); writer.println(" <xsl:for-each select=\"feature[not(category)]\">"); writer.println(" <xsl:sort select=\"@id\" order=\"ascending\" case-order=\"upper-first\"/>"); writer.println(" <xsl:sort select=\"@version\" order=\"ascending\" />"); writer.println(" <xsl:value-of select=\".\"/>"); writer.println(" <xsl:copy-of select=\".\" />"); writer.println(" </xsl:for-each>"); writer.println(" </xsl:variable>"); writer.println(" <xsl:variable name=\"myNodeSet\" select=\"msxsl:node-set($rtf-nodes)/*\"/>"); writer.println(" <xsl:for-each select=\"$myNodeSet\">"); writer.println(" <tr>"); writer.println(" <xsl:choose>"); writer.println(" <xsl:when test=\"position() mod 2 = 1\">"); writer.println(" <xsl:attribute name=\"class\">dark-row</xsl:attribute>"); writer.println(" </xsl:when>"); writer.println(" <xsl:otherwise>"); writer.println(" <xsl:attribute name=\"class\">light-row</xsl:attribute>"); writer.println(" </xsl:otherwise>"); writer.println(" </xsl:choose>"); writer.println(" <td class=\"log-text\" id=\"indent\">"); writer.println(" <xsl:choose>"); writer.println(" <xsl:when test=\"@label\">"); writer.println(" <a HREF=\"{@url}\"><xsl:value-of select=\"@label\"/></a>"); writer.println(" <br />"); writer.println(" <div id=\"indent\">"); writer.println(" (<xsl:value-of select=\"@id\"/> - <xsl:value-of select=\"@version\"/>)"); writer.println(" </div>"); writer.println(" </xsl:when>"); writer.println(" <xsl:otherwise>"); writer.println(" <a HREF=\"{@url}\"><xsl:value-of select=\"@id\"/> - <xsl:value-of select=\"@version\"/></a>"); writer.println(" </xsl:otherwise>"); writer.println(" </xsl:choose>"); writer.println(" <br /><br />"); writer.println(" </td>"); writer.println(" <td>"); writer.println(" <table>"); writer.println(" <xsl:if test=\"@os\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Operating Systems:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"@os\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"@ws\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Windows Systems:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"@ws\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"@nl\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Languages:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"@nl\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"@arch\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Architecture:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"@arch\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" </table>"); writer.println(" </td>"); writer.println(" </tr>"); writer.println(" </xsl:for-each>"); writer.println(" </xsl:when>"); writer.println(" <xsl:otherwise>"); writer.println(" <xsl:for-each select=\"feature[not(category)]\">"); writer.println(" <xsl:sort select=\"@id\" order=\"ascending\" case-order=\"upper-first\"/>"); writer.println(" <xsl:sort select=\"@version\" order=\"ascending\" />"); writer.println(" <tr>"); writer.println(" <xsl:choose>"); writer.println(" <xsl:when test=\"count(preceding-sibling::feature[not(category)]) mod 2 = 1\">"); writer.println(" <xsl:attribute name=\"class\">dark-row</xsl:attribute>"); writer.println(" </xsl:when>"); writer.println(" <xsl:otherwise>"); writer.println(" <xsl:attribute name=\"class\">light-row</xsl:attribute>"); writer.println(" </xsl:otherwise>"); writer.println(" </xsl:choose>"); writer.println(" <td class=\"log-text\" id=\"indent\">"); writer.println(" <xsl:choose>"); writer.println(" <xsl:when test=\"@label\">"); writer.println(" <a HREF=\"{@url}\"><xsl:value-of select=\"@label\"/></a>"); writer.println(" <br />"); writer.println(" <div id=\"indent\">"); writer.println(" (<xsl:value-of select=\"@id\"/> - <xsl:value-of select=\"@version\"/>)"); writer.println(" </div>"); writer.println(" </xsl:when>"); writer.println(" <xsl:otherwise>"); writer.println(" <a HREF=\"{@url}\"><xsl:value-of select=\"@id\"/> - <xsl:value-of select=\"@version\"/></a>"); writer.println(" </xsl:otherwise>"); writer.println(" </xsl:choose>"); writer.println(" <br /><br />"); writer.println(" </td>"); writer.println(" <td>"); writer.println(" <table>"); writer.println(" <xsl:if test=\"@os\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Operating Systems:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"@os\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"@ws\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Windows Systems:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"@ws\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"@nl\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Languages:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"@nl\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" <xsl:if test=\"@arch\">"); writer.println(" <tr><td class=\"log-text\" id=\"indent\">Architecture:</td>"); writer.println(" <td class=\"log-text\" id=\"indent\"><xsl:value-of select=\"@arch\"/></td>"); writer.println(" </tr>"); writer.println(" </xsl:if>"); writer.println(" </table>"); writer.println(" </td>"); writer.println(" </tr>"); writer.println(" </xsl:for-each>"); writer.println(" </xsl:otherwise>"); writer.println(" </xsl:choose>"); writer.println(" </table>"); writer.println(" </body>"); writer.println(" </html>"); writer.println("</xsl:for-each>"); writer.println("</xsl:template>"); writer.println("</xsl:stylesheet>"); 431 writer.flush(); 432 writeFile(fProject.getFile(fWebLocation + "/site.xsl"), swriter); } 434 435 private void writeFile(IFile file, StringWriter swriter) { 436 try { 437 ByteArrayInputStream stream = new ByteArrayInputStream (swriter 438 .toString().getBytes("UTF8")); if (file.exists()) { 440 file.setContents(stream, false, false, null); 441 } else { 442 file.create(stream, false, null); 443 } 444 stream.close(); 445 swriter.close(); 446 } catch (Exception e) { 447 PDEPlugin.logException(e); 448 } 449 } 450 451 } 452 | Popular Tags |