Author: tmortagne
Date: 2007-12-19 15:20:34 +0100 (Wed, 19 Dec 2007)
New Revision: 6428

Modified:
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/export/html/HtmlPackager.java
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/ExportAction.java
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/ExportURLFactory.java
Log:
XWIKI-564: Export pages in HTML, in a zip file
* Checkstyle/javadoc corrections.
* Fix URLFactory.createURL* that infinitely call itself some times in place of 
super.createURL*

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/export/html/HtmlPackager.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/export/html/HtmlPackager.java
      2007-12-19 13:58:48 UTC (rev 6427)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/export/html/HtmlPackager.java
      2007-12-19 14:20:34 UTC (rev 6428)
@@ -21,13 +21,44 @@
 import com.xpn.xwiki.web.ExportURLFactory;
 
 /**
- * Create a zip package containing a range of HTML pages with skin and 
attachment dependencies.
+ * Create a ZIP package containing a range of HTML pages with skin and 
attachment dependencies.
  * 
  * @version $Id: $
+ * @since XWiki Platform 1.3M1
  */
 public class HtmlPackager
 {
     /**
+     * A point.
+     */
+    private static final String POINT = ".";
+
+    /**
+     * Name of the context property containing the document.
+     */
+    private static final String CONTEXT_TDOC = "tdoc";
+
+    /**
+     * Name of the Velocity context property containing the document.
+     */
+    private static final String VCONTEXT_DOC = "doc";
+
+    /**
+     * Name of the Velocity context property containing the document.
+     */
+    private static final String VCONTEXT_CDOC = "cdoc";
+
+    /**
+     * Name of the Velocity context property containing the document.
+     */
+    private static final String VCONTEXT_TDOC = CONTEXT_TDOC;
+
+    /**
+     * The separator in an internal zip path.
+     */
+    private static final String ZIPPATH_SEPARATOR = "/";
+
+    /**
      * The name of the package for which packager append ".zip".
      */
     private String name = "html.export";
@@ -101,112 +132,133 @@
     }
 
     /**
-     * Apply export and create the ZIP package.
+     * Add rendered document to ZIP stream.
      * 
-     * @param context the XWiki context used to render pages.
-     * @throws IOException error when creating the package.
-     * @throws XWikiException error when render the pages.
+     * @param pageName the name (used with
+     *            [EMAIL PROTECTED] 
com.xpn.xwiki.XWiki.XWiki#getDocument(String, XWikiContext)}) of the page
+     *            to render.
+     * @param zos the ZIP output stream.
+     * @param context the XWiki context.
+     * @param vcontext the Velocity context.
+     * @throws XWikiException error when rendering document.
+     * @throws IOException error when rendering document.
      */
-    public void export(XWikiContext context) throws IOException, XWikiException
+    private void renderDocument(String pageName, ZipOutputStream zos, 
XWikiContext context,
+        VelocityContext vcontext) throws XWikiException, IOException
     {
-        // ////////////////////////////////////////////
-        // Create custom URL factory
-        // ////////////////////////////////////////////
+        XWikiDocument doc = context.getWiki().getDocument(pageName, context);
 
-        ExportURLFactory urlf = new ExportURLFactory();
-        File dir =
-            (File) 
context.getEngineContext().getAttribute("javax.servlet.context.tempdir");
-        File tempdir = new File(dir, RandomStringUtils.randomAlphanumeric(8));
-        tempdir.mkdirs();
-        File attachmentDir = new File(tempdir, "attachment");
-        attachmentDir.mkdirs();
+        String zipname = doc.getDatabase() + POINT + doc.getSpace() + POINT + 
doc.getName();
+        String language = doc.getLanguage();
+        if (language != null && language.length() != 0) {
+            zipname += POINT + language;
+        }
 
-        // ////////////////////////////////////////////
-        // Configure response
-        // ////////////////////////////////////////////
+        zipname += ".html";
 
-        context.getResponse().setContentType("application/zip");
-        context.getResponse().addHeader("Content-disposition",
-            "attachment; filename=" + context.getWiki().getURLEncoded(name) + 
".zip");
-        context.setFinished(true);
+        ZipEntry zipentry = new ZipEntry(zipname);
+        zos.putNextEntry(zipentry);
 
-        // ////////////////////////////////////////////
-        // Render pages to export
-        // ////////////////////////////////////////////
+        context.setDatabase(doc.getDatabase());
+        context.setDoc(doc);
+        vcontext.put(VCONTEXT_DOC, doc.newDocument(context));
+        vcontext.put(VCONTEXT_CDOC, vcontext.get(VCONTEXT_DOC));
 
-        ZipOutputStream zos = new 
ZipOutputStream(context.getResponse().getOutputStream());
+        XWikiDocument tdoc = doc.getTranslatedDocument(context);
+        context.put(CONTEXT_TDOC, tdoc);
+        vcontext.put(VCONTEXT_TDOC, tdoc.newDocument(context));
 
-        VelocityContext vcontext = (VelocityContext)context.get("vcontext");
-        
-        Document currentDocument = (Document)vcontext.get("cdoc");
-        Document currentCDocument = (Document)vcontext.get("cdoc");
-        Document currentTDocument = (Document)vcontext.get("tdoc");
-        
+        String content = context.getWiki().parseTemplate("view.vm", context);
+
+        zos.write(content.getBytes(context.getWiki().getEncoding()));
+        zos.closeEntry();
+    }
+
+    /**
+     * Init provided [EMAIL PROTECTED] ExportURLFactory} and add rendered 
documents to ZIP stream.
+     * 
+     * @param zos the ZIP output stream.
+     * @param tempdir the directory where to copy attached files.
+     * @param urlf the [EMAIL PROTECTED] 
com.xpn.xwiki.web.XWikiURLFactory.XWikiURLFactory} used to render the
+     *            documents.
+     * @param context the XWiki context.
+     * @throws XWikiException error when render documents.
+     * @throws IOException error when render documents.
+     */
+    private void renderDocuments(ZipOutputStream zos, File tempdir, 
ExportURLFactory urlf,
+        XWikiContext context) throws XWikiException, IOException
+    {
+        VelocityContext vcontext = (VelocityContext) context.get("vcontext");
+
+        Document currentDocument = (Document) vcontext.get(VCONTEXT_DOC);
+        Document currentCDocument = (Document) vcontext.get(VCONTEXT_CDOC);
+        Document currentTDocument = (Document) vcontext.get(VCONTEXT_TDOC);
+
         try {
             XWikiContext renderContext = (XWikiContext) context.clone();
+            renderContext.put("action", "view");
 
             vcontext = XWikiVelocityRenderer.prepareContext(renderContext);
 
             urlf.init(this.pages, tempdir, renderContext);
             renderContext.setURLFactory(urlf);
 
-            renderContext.put("action", "view");
-
             for (Iterator it = this.pages.iterator(); it.hasNext();) {
                 String pageName = (String) it.next();
 
-                XWikiDocument doc = 
renderContext.getWiki().getDocument(pageName, renderContext);
+                renderDocument(pageName, zos, renderContext, vcontext);
+            }
+        } finally {
+            // Clean velocity context
+            vcontext = XWikiVelocityRenderer.prepareContext(context);
 
-                String zipname = doc.getDatabase() + "." + doc.getSpace() + 
"." + doc.getName();
-                String language = doc.getLanguage();
-                if ((language != null) && (!language.equals(""))) {
-                    zipname += "." + language;
-                }
+            vcontext.put(VCONTEXT_DOC, currentDocument);
+            vcontext.put(VCONTEXT_CDOC, currentCDocument);
+            vcontext.put(VCONTEXT_TDOC, currentTDocument);
+        }
+    }
 
-                zipname += ".html";
+    /**
+     * Apply export and create the ZIP package.
+     * 
+     * @param context the XWiki context used to render pages.
+     * @throws IOException error when creating the package.
+     * @throws XWikiException error when render the pages.
+     */
+    public void export(XWikiContext context) throws IOException, XWikiException
+    {
+        context.getResponse().setContentType("application/zip");
+        context.getResponse().addHeader("Content-disposition",
+            "attachment; filename=" + context.getWiki().getURLEncoded(name) + 
".zip");
+        context.setFinished(true);
 
-                ZipEntry zipentry = new ZipEntry(zipname);
-                zos.putNextEntry(zipentry);
+        ZipOutputStream zos = new 
ZipOutputStream(context.getResponse().getOutputStream());
 
-                renderContext.setDatabase(doc.getDatabase());
-                renderContext.setDoc(doc);
-                vcontext.put("doc", doc.newDocument(renderContext));
-                vcontext.put("cdoc", vcontext.get("doc"));
+        File dir =
+            (File) 
context.getEngineContext().getAttribute("javax.servlet.context.tempdir");
+        File tempdir = new File(dir, RandomStringUtils.randomAlphanumeric(8));
+        tempdir.mkdirs();
+        File attachmentDir = new File(tempdir, "attachment");
+        attachmentDir.mkdirs();
 
-                XWikiDocument tdoc = doc.getTranslatedDocument(renderContext);
-                renderContext.put("tdoc", tdoc);
-                vcontext.put("tdoc", tdoc.newDocument(renderContext));
+        // Create custom URL factory
+        ExportURLFactory urlf = new ExportURLFactory();
 
-                String content = 
renderContext.getWiki().parseTemplate("view.vm", renderContext);
+        // Render pages to export
+        renderDocuments(zos, tempdir, urlf, context);
 
-                
zos.write(content.getBytes(renderContext.getWiki().getEncoding()));
-                zos.closeEntry();
-            }
-        } finally {
-            // Clean velocity context
-            vcontext = XWikiVelocityRenderer.prepareContext(context);
-            
-            vcontext.put("doc", currentDocument);
-            vcontext.put("cdoc", currentCDocument);
-            vcontext.put("tdoc", currentTDocument);
-        }
-
-        // ////////////////////////////////////////////
-        // Add required skins to zip file
-        // ////////////////////////////////////////////
+        // Add required skins to ZIP file
         for (Iterator it = urlf.getNeededSkins().iterator(); it.hasNext();) {
             String skinName = (String) it.next();
             addSkinToZip(skinName, zos, context);
         }
 
-        // ////////////////////////////////////////////
-        // Add resources files to zip file
-        // ////////////////////////////////////////////
+        // Add resources files to ZIP file
         addDirToZip(tempdir, zos, "");
 
         zos.setComment(description);
 
-        // Finish zip file
+        // Finish ZIP file
         zos.finish();
         zos.flush();
 
@@ -258,7 +310,7 @@
     {
         File file =
             new 
File(context.getWiki().getEngineContext().getRealPath("/skins/" + skinName));
-        addDirToZip(file, out, "skins/" + skinName + "/");
+        addDirToZip(file, out, "skins" + ZIPPATH_SEPARATOR + skinName + 
ZIPPATH_SEPARATOR);
     }
 
     /**
@@ -287,7 +339,7 @@
         for (int i = 0; i < files.length; ++i) {
             File file = files[i];
             if (file.isDirectory()) {
-                addDirToZip(file, out, basePath + file.getName() + "/");
+                addDirToZip(file, out, basePath + file.getName() + 
ZIPPATH_SEPARATOR);
                 continue;
             }
 

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/ExportAction.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/ExportAction.java
      2007-12-19 13:58:48 UTC (rev 6427)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/ExportAction.java
      2007-12-19 14:20:34 UTC (rev 6428)
@@ -77,6 +77,7 @@
      * @return always return null.
      * @throws XWikiException error when exporting HTML ZIP package.
      * @throws IOException error when exporting HTML ZIP package.
+     * @since XWiki Platform 1.3M1
      */
     private String exportHTML(XWikiContext context) throws XWikiException, 
IOException
     {
@@ -86,7 +87,7 @@
         String name = request.get("name");
         String[] pages = request.getParameterValues("pages");
 
-        List pageList = new ArrayList();;
+        List pageList = new ArrayList();
         if (pages == null || pages.length == 0) {
             pageList.add(context.getDoc().getFullName());
 

Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/ExportURLFactory.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/ExportURLFactory.java
  2007-12-19 13:58:48 UTC (rev 6427)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/ExportURLFactory.java
  2007-12-19 14:20:34 UTC (rev 6428)
@@ -137,7 +137,7 @@
         XWikiContext context)
     {
         if (!"skins".equals(web)) {
-            return createSkinURL(filename, web, name, xwikidb, context);
+            return super.createSkinURL(filename, web, name, xwikidb, context);
         }
 
         try {
@@ -200,7 +200,7 @@
             e.printStackTrace();
         }
 
-        return super.createExternalURL(web, name, action, querystring, anchor, 
xwikidb, context);
+        return super.createURL(web, name, action, querystring, anchor, 
xwikidb, context);
     }
 
     /**

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to