Author: nickarls
Date: 2010-07-22 06:29:54 -0400 (Thu, 22 Jul 2010)
New Revision: 13474
Modified:
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/SpreadsheetWriter.java
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/writer/JXLSpreadsheetWriter.java
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Workbook.java
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/JXLSpreadsheetWriterTest.java
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/command/CellMergeCommandTest.java
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/WorkbookTest.java
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/builder/WorkbookBuilderTest.java
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/formatting/SpreadsheetTest.java
Log:
workbook settings
splitting up writer interface into process and write
Modified:
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/SpreadsheetWriter.java
===================================================================
---
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/SpreadsheetWriter.java 2010-07-22
09:24:25 UTC (rev 13473)
+++
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/SpreadsheetWriter.java 2010-07-22
10:29:54 UTC (rev 13474)
@@ -4,7 +4,7 @@
public interface SpreadsheetWriter
{
- public abstract byte[] writeWorkbook(Workbook workbook);
- public abstract byte[] writeWorkbook(Workbook workbook, byte[] template);
+ public abstract SpreadsheetWriter processWorkbook(Workbook workbook);
+ public byte[] writeWorkbook();
public abstract Object getRawWorkbook();
}
Modified:
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/writer/JXLSpreadsheetWriter.java
===================================================================
---
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/writer/JXLSpreadsheetWriter.java 2010-07-22
09:24:25 UTC (rev 13473)
+++
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/jxl/writer/JXLSpreadsheetWriter.java 2010-07-22
10:29:54 UTC (rev 13474)
@@ -29,6 +29,7 @@
private CellFactory cellFactory;
private CellFormatResolver cellFormatResolver;
private WritableWorkbook jxlWorkbook;
+ private ByteArrayOutputStream outStream;
private SettingsHelper settingsHelper = new SettingsHelper();
@@ -36,34 +37,40 @@
private Events events;
@Override
- public byte[] writeWorkbook(Workbook workbook) throws SpreadsheetException
+ public SpreadsheetWriter processWorkbook(Workbook workbook) throws
SpreadsheetException
{
if (workbook.getWorksheets().isEmpty())
{
throw new SpreadsheetException("You must have at least one
worksheet");
}
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
cellFactory = new CellFactory();
cellFormatResolver = new CellFormatResolver(workbook.getCellFormatRules());
try
{
- jxlWorkbook = jxl.Workbook.createWorkbook(outStream);
- processWorkbook(workbook);
- jxlWorkbook.write();
- jxlWorkbook.close();
+ initWorkbook(workbook);
+ _processWorkbook(workbook);
}
catch (IOException e)
{
throw new SpreadsheetException("Could not create workbook", e);
}
- catch (WriteException e)
+ return this;
+ }
+
+ private void initWorkbook(Workbook workbook) throws IOException
+ {
+ outStream = new ByteArrayOutputStream();
+ if (workbook.getSettings() == null)
{
- throw new SpreadsheetException("Could not write workbook", e);
+ jxlWorkbook = jxl.Workbook.createWorkbook(outStream);
}
- return outStream.toByteArray();
+ else
+ {
+ jxlWorkbook = jxl.Workbook.createWorkbook(outStream, new
SettingsHelper().createWorkbookSettings(workbook.getSettings()));
+ }
}
- private void processWorkbook(Workbook workbook)
+ private void _processWorkbook(Workbook workbook)
{
for (Worksheet worksheet : workbook.getWorksheets())
{
@@ -120,15 +127,28 @@
}
@Override
- public byte[] writeWorkbook(Workbook workbook, byte[] template) throws
SpreadsheetException
+ public Object getRawWorkbook()
{
- return null;
+ return jxlWorkbook;
}
@Override
- public Object getRawWorkbook()
+ public byte[] writeWorkbook()
{
- return jxlWorkbook;
+ try
+ {
+ jxlWorkbook.write();
+ jxlWorkbook.close();
+ }
+ catch (WriteException e)
+ {
+ throw new SpreadsheetException(String.format("Could not generate
workbook"), e);
+ }
+ catch (IOException e)
+ {
+ throw new SpreadsheetException(String.format("Could not write
workbook"), e);
+ }
+ return outStream.toByteArray();
}
}
Modified:
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Workbook.java
===================================================================
---
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Workbook.java 2010-07-22
09:24:25 UTC (rev 13473)
+++
sandbox/modules/spreadsheet/src/main/java/org/jboss/seam/spreadsheet/model/Workbook.java 2010-07-22
10:29:54 UTC (rev 13474)
@@ -12,6 +12,8 @@
private List<CellFormatRule> cellFormatRules = new
ArrayList<CellFormatRule>();
private List<Command> commands = new ArrayList<Command>();
+ private WorkbookSettings settings;
+
public List<Worksheet> getWorksheets()
{
return worksheets;
@@ -47,4 +49,14 @@
{
return String.format("Workbook with %d sheets", worksheets.size());
}
+
+ public WorkbookSettings getSettings()
+ {
+ return settings;
+ }
+
+ public void setSettings(WorkbookSettings settings)
+ {
+ this.settings = settings;
+ }
}
Modified:
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/JXLSpreadsheetWriterTest.java
===================================================================
---
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/JXLSpreadsheetWriterTest.java 2010-07-22
09:24:25 UTC (rev 13473)
+++
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/JXLSpreadsheetWriterTest.java 2010-07-22
10:29:54 UTC (rev 13474)
@@ -45,6 +45,7 @@
workbook.getWorksheets().add(worksheet);
worksheet.getCells().add(Cell.of("foo", Coordinate.of(0, 0)));
worksheet.getCommands().add(new ColumnWidthCommand(1, 100));
+ @SuppressWarnings("unused")
jxl.Workbook jxlWorkbook = getWorkbook(workbook);
Assert.assertTrue(eventObserver.isCellAdded());
Assert.assertTrue(eventObserver.isWorksheetCreated());
Modified:
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/command/CellMergeCommandTest.java
===================================================================
---
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/command/CellMergeCommandTest.java 2010-07-22
09:24:25 UTC (rev 13473)
+++
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/jxl/command/CellMergeCommandTest.java 2010-07-22
10:29:54 UTC (rev 13474)
@@ -1,7 +1,5 @@
package org.jboss.seam.spreadsheet.jxl.command;
-import junit.framework.Assert;
-
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.seam.spreadsheet.jxl.writer.JXLSpreadsheetWriter;
Modified:
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/WorkbookTest.java
===================================================================
---
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/WorkbookTest.java 2010-07-22
09:24:25 UTC (rev 13473)
+++
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/WorkbookTest.java 2010-07-22
10:29:54 UTC (rev 13474)
@@ -25,7 +25,7 @@
}
@Test
- public void testFreshWorkbook()
+ public void freshWorkbookStateTest()
{
Workbook workbook = new Workbook();
Assert.assertTrue(workbook.getWorksheets().isEmpty());
@@ -33,9 +33,21 @@
}
@Test(expected = SpreadsheetException.class)
- public void testImplicitWorksheet()
+ public void noWorksheetsFailTest()
{
Workbook workbook = new Workbook();
+ @SuppressWarnings("unused")
jxl.Workbook jxlWorkbook = getWorkbook(workbook);
}
+
+ @Test
+ public void workbookSettingsTest() {
+ Workbook workbook = new Workbook();
+ workbook.getWorksheets().add(Worksheet.named("foo"));
+ WorkbookSettings settings = new WorkbookSettings();
+ settings.setEncoding("UTF-8");
+ workbook.getWorksheets().iterator().next().getCells().add(Cell.of("���",
Coordinate.ORIGO));
+ workbook.setSettings(settings);
+ workbookToFile(workbook, "c:/temp/foo.xls");
+ }
}
Modified:
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/builder/WorkbookBuilderTest.java
===================================================================
---
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/builder/WorkbookBuilderTest.java 2010-07-22
09:24:25 UTC (rev 13473)
+++
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/builder/WorkbookBuilderTest.java 2010-07-22
10:29:54 UTC (rev 13474)
@@ -1,14 +1,10 @@
package org.jboss.seam.spreadsheet.model.builder;
-import java.io.FileOutputStream;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
-import org.jboss.seam.spreadsheet.SpreadsheetWriter;
-import org.jboss.seam.spreadsheet.jxl.writer.JXLSpreadsheetWriter;
import org.jboss.seam.spreadsheet.model.Cell;
import org.jboss.seam.spreadsheet.model.Coordinate;
import org.jboss.seam.spreadsheet.model.Range;
@@ -51,6 +47,7 @@
final Person person = new Person("Nicklas Karlsson", 35);
List<Person> persons = new ArrayList<Person>()
{
+ private static final long serialVersionUID = 1L;
{
add(person);
}
@@ -143,6 +140,7 @@
final Person pete = new Person("Pete Muir", 26);
List<Person> persons = new ArrayList<Person>()
{
+ private static final long serialVersionUID = 1L;
{
add(nik);
add(pete);
Modified:
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/formatting/SpreadsheetTest.java
===================================================================
---
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/formatting/SpreadsheetTest.java 2010-07-22
09:24:25 UTC (rev 13473)
+++
sandbox/modules/spreadsheet/src/test/java/org/jboss/seam/spreadsheet/model/formatting/SpreadsheetTest.java 2010-07-22
10:29:54 UTC (rev 13474)
@@ -1,7 +1,6 @@
package org.jboss.seam.spreadsheet.model.formatting;
import java.io.ByteArrayInputStream;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
@@ -47,7 +46,7 @@
{
try
{
- byte[] data = getSpreadsheetWriter().writeWorkbook(workbook);
+ byte[] data = getSpreadsheetWriter().processWorkbook(workbook).writeWorkbook();
return jxl.Workbook.getWorkbook(new ByteArrayInputStream(data));
}
catch (BiffException e)
@@ -136,7 +135,7 @@
try
{
FileOutputStream out = new FileOutputStream(fileName);
- out.write(sw.writeWorkbook(workbook));
+ out.write(sw.processWorkbook(workbook).writeWorkbook());
out.flush();
out.close();
}