[seam-commits] Seam SVN: r9104 - in trunk/src/excel/org/jboss/seam/excel: ui and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Sep 22 16:21:44 EDT 2008


Author: danielc.roth
Date: 2008-09-22 16:21:44 -0400 (Mon, 22 Sep 2008)
New Revision: 9104

Modified:
   trunk/src/excel/org/jboss/seam/excel/csv/CsvExcelWorkbook.java
   trunk/src/excel/org/jboss/seam/excel/ui/UIWorkbook.java
Log:
Improved CsvExcelBook and removed some old comments

Modified: trunk/src/excel/org/jboss/seam/excel/csv/CsvExcelWorkbook.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/csv/CsvExcelWorkbook.java	2008-09-22 19:41:51 UTC (rev 9103)
+++ trunk/src/excel/org/jboss/seam/excel/csv/CsvExcelWorkbook.java	2008-09-22 20:21:44 UTC (rev 9104)
@@ -1,9 +1,8 @@
 package org.jboss.seam.excel.csv;
 
-import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.TreeMap;
 
 import org.jboss.seam.document.DocumentData;
 import org.jboss.seam.document.DocumentData.DocumentType;
@@ -12,7 +11,7 @@
 import org.jboss.seam.excel.WorksheetItem;
 import org.jboss.seam.excel.ui.UICell;
 import org.jboss.seam.excel.ui.UIColumn;
-import org.jboss.seam.excel.ui.UIImage;
+import org.jboss.seam.excel.ui.UIHyperlink;
 import org.jboss.seam.excel.ui.UILink;
 import org.jboss.seam.excel.ui.UIWorkbook;
 import org.jboss.seam.excel.ui.UIWorksheet;
@@ -21,33 +20,27 @@
 import org.jboss.seam.log.Logging;
 
 /**
- * 10 minute (quite poor) implementation of csv excel workbook... Perhaps better
- * would be to use some kind of library for this.
- * 
- * Use at own risk.. :)
- * 
  * @author Daniel Roth (danielc.roth at gmail.com)
  */
 public class CsvExcelWorkbook implements ExcelWorkbook
 {
-   private int column = 0;
-   private int row = 0;
-   private int sheet = -1;
-   private int maxrow = 0;
-   private int maxcolumn = 0;
-   private int maxsheet = 0;
-   
+   private int currentColumn = 0;
+   private int currentRow = 0;
+   private int maxRow = 0;
+   private int maxColumn = 0;
+
    private final String COLUMN_DELIMITER = "\"";
    private final String LINEBREAK = "\n";
-   
-   private Map<Integer, Map<Integer, List<String>>> table = null;
-   private List<String> sheets = new ArrayList<String>();
-   
+   private final String COMMA = ",";
+
+   private Map<String, String> table = null;
+   private String sheetName = null;
+
    private Log log = Logging.getLog(getClass());
 
    public void createWorkbook(UIWorkbook uiWorkbook) throws ExcelWorkbookException
    {
-      table = new TreeMap<Integer, Map<Integer, List<String>>>();
+      table = new HashMap<String, String>();
 
    }
 
@@ -59,84 +52,92 @@
 
    private void createOrSelectWorksheet(String worksheetName, Integer startRow, Integer startColumn)
    {
-      column = 0;
-      row = 0;
-      if (sheets.contains(worksheetName))
+      if (sheetName != null && sheetName.equals(worksheetName))
       {
-         sheet = sheets.indexOf(sheets);
-         column = startColumn;
-         row = startRow;
+         throw new RuntimeException("You cannot export multiple sheet workbooks to excel.");
       }
-      else
-      {
-         sheet++;
-         sheets.add(worksheetName);
-      }
 
+      sheetName = worksheetName;
+      currentColumn = (startColumn == null) ? 0 : startColumn;
+      currentRow = (startRow == null) ? 0 : startRow;
    }
 
    public byte[] getBytes()
    {
       StringBuffer buffer = new StringBuffer();
-      for (int i = 0; i <= maxsheet; i++)
+      for (int i = 0; i <= maxRow; i++)
       {
-         Map<Integer, List<String>> sheet = table.get(i);
-         if (sheet != null)
+         for (int j = 0; j <= maxColumn; j++)
          {
-            for (int j = 0; j <= maxrow; j++)
-            {
-               for (List<String> col : sheet.values())
-               {
-                  if (col.get(j) != null)
-                     buffer.append(COLUMN_DELIMITER).append(String.valueOf(col.get(j))).append(COLUMN_DELIMITER).append(",");
-               }
-
-               buffer.append(LINEBREAK);
-            }
-
+            String value = table.get(hash(i, j));
+            value = (value == null) ? "" : value;
+            buffer.append(COLUMN_DELIMITER).append(value).append(COLUMN_DELIMITER).append(COMMA);
          }
+         buffer.deleteCharAt(buffer.length() - 1);
+         buffer.append(LINEBREAK);
       }
+
       return buffer.toString().getBytes();
    }
 
-   private void addCell(int sheet, int column, int row, UICell uiCell) throws ExcelWorkbookException
+   public void nextColumn()
    {
-      if (table.get(sheet) == null)
-         table.put(sheet, new TreeMap<Integer, List<String>>());
+      currentColumn++;
+      currentRow = 0;
+   }
 
-      Map<Integer, List<String>> columns = table.get(sheet);
-      if (columns.get(column) == null)
-         columns.put(column, new ArrayList<String>());
+   public DocumentType getDocumentType()
+   {
+      return new DocumentData.DocumentType("csv", "text/csv");
+   }
 
-      List<String> rows = columns.get(column);
-
-      rows.add(String.valueOf(uiCell.getValue()));
-      maxrow = (row > maxrow) ? row : maxrow;
-      maxcolumn = (column > maxcolumn) ? column : maxcolumn;
-      maxsheet = (sheet > maxsheet) ? sheet : maxsheet;
-
+   public void addItem(WorksheetItem item)
+   {
+      switch (item.getItemType())
+      {
+      case cell:
+         addCell((UICell) item);
+         break;
+      case hyperlink:
+         addHyperLink((UIHyperlink) item);
+         break;
+      case image:
+         log.trace("You cannot export an image to CSV", new Object[0]);
+         break;
+      }
    }
 
-   public void nextColumn()
+   private void addCell(UICell cell)
    {
-      column++;
-      row = 0;
+      int row = (cell.getRow() == null) ? currentRow : cell.getRow();
+      int column = (cell.getColumn() == null) ? currentColumn : cell.getColumn();
+      addCsvCell(column, row, String.valueOf(cell.getValue()));
+      
+      if (cell.getColumn() == null && cell.getRow() == null)
+         currentRow++;
    }
 
-   public DocumentType getDocumentType()
+   private void addHyperLink(UIHyperlink link)
    {
-      return new DocumentData.DocumentType("csv", "text/csv");
+      int row = (link.getStartRow() == null) ? currentRow : link.getStartRow();
+      int column = (link.getStartColumn() == null) ? currentColumn : link.getStartColumn();
+      if (link.getEndColumn() != null || link.getEndRow() != null)
+         log.warn("endColumn/endRow is not supported by csv exporter", new Object[0]);
+      addCsvCell(column, row, String.valueOf(link.getURL()));
    }
 
-   public void addImage(UIImage uiImage)
+   private void addCsvCell(int column, int row, String value) throws ExcelWorkbookException
    {
-      log.warn("addImage() is not supported by CSV exporter", new Object[0]);
+      table.put(hash(row, column), value);
+      maxRow = (row > maxRow) ? row : maxRow;
+      maxColumn = (column > maxColumn) ? column : maxColumn;
    }
 
-   public void addItem(WorksheetItem item)
+   private String hash(int row, int column)
    {
-      UICell cell = (UICell) item;
-      addCell(sheet, column, row++, cell);
+      StringBuffer buffer = new StringBuffer();
+      buffer.append(row).append(COMMA).append(column);
+      return buffer.toString();
    }
 
    public void applyWorksheetSettings(UIWorksheet uiWorksheet)
@@ -156,20 +157,22 @@
 
    public void addWorksheetFooter(WorksheetItem item, int colspan)
    {
-      // TODO Auto-generated method stub
-      
+      if (colspan > 0)
+         log.warn("footer colspan are not supported by CSV exporter", new Object[0]);
+      addItem(item);
    }
 
    public void addWorksheetHeader(WorksheetItem item, int colspan)
    {
-      // TODO Auto-generated method stub
-      
+      if (colspan > 0)
+         log.warn("header colspan are not supported by CSV exporter", new Object[0]);
+      addItem(item);
+
    }
 
    public void setStylesheets(List<UILink> stylesheets)
    {
-      // TODO Auto-generated method stub
-      
+      log.trace("styleSheets are not supported by CSV exporter", new Object[0]);
    }
 
 }

Modified: trunk/src/excel/org/jboss/seam/excel/ui/UIWorkbook.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/ui/UIWorkbook.java	2008-09-22 19:41:51 UTC (rev 9103)
+++ trunk/src/excel/org/jboss/seam/excel/ui/UIWorkbook.java	2008-09-22 20:21:44 UTC (rev 9104)
@@ -313,10 +313,6 @@
 
       DocumentType type = excelWorkbook.getDocumentType();
 
-      /**
-       * Code below is the same as for PDF generation. With a seam core document
-       * store (or equivalent), this might need modifications
-       */
       String viewId = Pages.getViewId(context);
       String baseName = baseNameForViewId(viewId);
 




More information about the seam-commits mailing list