/**-IWorksheetReader - an interface that defines the methods for reading a worksheet document
*@author: czetsuya
*@since: Oct 9, 2009
**/
package org.irri.cril.jicis.worksheet;
import java.io.IOException;
/**
* This class is the root of the Worksheet type class.
* It should be extended if another worksheet class is to be supported.
* Currently supported worksheets:
* Microsoft Office's Excel and
* Open Office's Calc
* @author czetsuya
*/
public abstract class AbstractWorksheet {
/**
* Initialize the AbstractWorksheet reader.
*/
public abstract void activateReader();
/**
* Initialize the AbstractWorksheet writer.
*/
public abstract void activateWriter();
/**
* Sets the active worksheet index.
* @param x worksheet index
*/
public abstract void setSheet(int x);
/**
* Returns the worksheet instance.
* @return Object
*/
public abstract Object getWorksheet();
/**
* Saves the modified worksheet.
* @throws IOException
*/
public abstract void save() throws IOException;
}
/**-IWorksheetWriter - an interface that defines the methods for writing a worksheet document
*@author: czetsuya
*@since: Oct 9, 2009
**/
package org.irri.cril.jicis.worksheet;
/**
* A class should implement this interface if it will read a worksheet document.
* @author czetsuya
*/
public interface IWorksheetReader {
/**
* Returns the value of the cell in row col.
* @param row
* @param col
* @return
*/
Object getCellValueAt(int row, int col);
/**
* Returns the value of the cell specified.
* @param cell
* @return
*/
Object getCellValueAt(String cell);
/**
* Returns the cell of the current active worksheet.
* @param row
* @param col
* @return
*/
Object getCellAt(int row, int col);
/**
* Read the cells foreground fill color.
* @param row
* @param col
* @return true if the cell foreground fill color is not white.
*/
boolean isFilledBackgroundColor(int row, int col);
}
/**
*@author: czetsuya
*@since: Oct 9, 2009
**/
package org.irri.cril.jicis.worksheet;
/**
* Worksheet writer classes should implement this interface.
* @author czetsuya
*/
public interface IWorksheetWriter {
/**
* Sets the value of a cell.
* @param val value
* @param row
* @param col
*/
void setValueAt(Object val, int row, int col);
/**
* Sets the value of a cell.
* @param val
* @param cellReference Worksheet row column: A1, A2, etc.
*/
void setValueAt(Object val, String cellReference);
}
/**Note: Both the reader and writer class are not initialize by default.
*@author: czetsuya
*@since: Oct 9, 2009
**/
package org.irri.cril.jicis.worksheet.jopendocument;
import java.io.File;
import java.io.IOException;
import org.irri.cril.jicis.worksheet.AbstractWorksheet;
import org.irri.cril.jicis.worksheet.IWorksheetReader;
import org.irri.cril.jicis.worksheet.IWorksheetWriter;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;
/**
* Handles an Open Office's Calc Worksheet Document.
* @author czetsuya
*/
public class JOpenDocument extends AbstractWorksheet implements IWorksheetReader, IWorksheetWriter {
/**
* Open Office worksheet reader.
*/
private IWorksheetReader reader;
/**
* Open Office worksheet writer.
*/
private IWorksheetWriter writer;
/**
* Open Office spreadsheet. Array of Sheet objects.
*/
private SpreadSheet spreadSheet;
/**
* Open Office sheet.
*/
private Sheet sheet;
/**
* Filename of worksheet.
*/
private String workSheetName;
/**
* Creates an instance of the JOpenDocument.
* @param worksheet
* @throws IOException
*/
public JOpenDocument(String worksheet) throws IOException {
workSheetName = worksheet;
spreadSheet = SpreadSheet.createFromFile(new File(worksheet));
sheet = spreadSheet.getSheet(0);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.AbstractWorksheet#activateReader()
*/
@Override
public final void activateReader() {
reader = new JOpenDocumentReader(sheet);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.AbstractWorksheet#activateWriter()
*/
@Override
public final void activateWriter() {
writer = new JOpenDocumentWriter(sheet);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.AbstractWorksheet#setSheet(int)
*/
@Override
public final void setSheet(int x) {
sheet = spreadSheet.getSheet(x - 1);
((JOpenDocumentReader)reader).setSheet(sheet);
((JOpenDocumentWriter)writer).setSheet(sheet);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.AbstractWorksheet#save()
*/
@Override
public final void save() throws IOException {
spreadSheet.saveAs(new File(workSheetName));
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(int, int)
*/
@Override
public final Object getCellValueAt(int row, int col) {
return reader.getCellValueAt(row, col);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(java.lang.String)
*/
@Override
public final Object getCellValueAt(String cell) {
return reader.getCellValueAt(cell);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.AbstractWorksheet#getWorksheet()
*/
@Override
public final Object getWorksheet() {
return sheet;
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetWriter#setValueAt(java.lang.Object, int, int)
*/
@Override
public final void setValueAt(Object val, int row, int col) {
writer.setValueAt(val, row, col);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetWriter#setValueAt(java.lang.Object, java.lang.String)
*/
@Override
public final void setValueAt(Object val, String cellReference) {
writer.setValueAt(val, cellReference);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getFillBackgroundColor(int, int)
*/
@Override
public final boolean isFilledBackgroundColor(int row, int col) {
return ((IWorksheetReader)reader).isFilledBackgroundColor(row, col);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(int, int)
*/
@Override
public final Object getCellAt(int row, int col) {
return ((IWorksheetReader)reader).getCellAt(row, col);
}
}
package org.irri.cril.jicis.worksheet.jopendocument;3.) Writer
/**
*@author: czetsuya
*@since: Oct 9, 2009
**/
import org.irri.cril.jicis.worksheet.IWorksheetReader;
import org.jopendocument.dom.spreadsheet.Sheet;
/**
* Worksheet reader implementation for Open Office's Calc.
* @author czetsuya
*/
public class JOpenDocumentReader implements IWorksheetReader {
/**
* Open Office worksheet.
*/
private Sheet sheet;
/**
* Creates an instance of JOpenDocumentReader.
* @param sheet
*/
public JOpenDocumentReader(Sheet sheet) {
this.sheet = sheet;
}
/**
* Sets the sheet for reading.
* @param sheet
*/
public final void setSheet(Sheet sheet) {
this.sheet = sheet;
}
/*
* (non-Javadoc)
*
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(int, int)
*/
@Override
public final Object getCellValueAt(int row, int col) {
return sheet.getCellAt(col - 1, row - 1).getValue();
}
/*
* (non-Javadoc)
*
* @see
* org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(java.lang.String
* )
*/
@Override
public final Object getCellValueAt(String cell) {
return sheet.getCellAt(cell).getValue();
}
/*
* (non-Javadoc)
*
* @see
* org.irri.cril.jicis.worksheet.IWorksheetReader#getFillBackgroundColor
* (int, int)
*/
@Override
public final boolean isFilledBackgroundColor(int row, int col) {
throw new UnsupportedOperationException();
}
/*
* (non-Javadoc)
*
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(int, int)
*/
@Override
public final Object getCellAt(int row, int col) {
return sheet.getCellAt(col, row);
}
}
/**
*@author: czetsuya
*@since: Oct 9, 2009
**/
package org.irri.cril.jicis.worksheet.jopendocument;
import org.irri.cril.jicis.worksheet.IWorksheetWriter;
import org.jopendocument.dom.spreadsheet.Sheet;
/**
* Worksheet writer implementation for Open Office's Calc.
* @author czetsuya
*/
public class JOpenDocumentWriter implements IWorksheetWriter {
/**
* Open office worksheet.
*/
private Sheet sheet;
/**
* Creates an instance of JOpenDocumentWriter.
* @param sheet
*/
public JOpenDocumentWriter(Sheet sheet) {
this.sheet = sheet;
}
/**
* Sets the sheet for writing.
* @param sheet
*/
public final void setSheet(Sheet sheet) {
this.sheet = sheet;
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetWriter#setValueAt(java.lang.Object, int, int)
*/
@Override
public final void setValueAt(Object val, int row, int col) {
sheet.ensureRowCount(row);
sheet.ensureColumnCount(col);
sheet.setValueAt(val, col - 1, row - 1);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetWriter#setValueAt(java.lang.Object, java.lang.String)
*/
@Override
public final void setValueAt(Object val, String cellReference) {
sheet.getCellAt(cellReference).setValue(val);
}
}
/**
*@author: czetsuya
*@since: Oct 9, 2009
**/
package org.irri.cril.jicis.worksheet;
import java.io.IOException;
import org.irri.cril.jicis.worksheet.jopendocument.JOpenDocument;
import org.irri.cril.jicis.worksheet.msexcel.ExcelDocument;
/**
* This class serves as the controller which instantiate the appropriate class
* for the specified file. It instantiate an AbstractWorksheet class base on the file extension.
* ods - @see org.irri.cril.jicis.worksheet.jopendocument
* xls/xlsx - org.irri.cril.jicis.worksheet.msexcel
* @author czetsuya
*/
public class WorksheetClient implements IWorksheetReader, IWorksheetWriter {
/**
* AbstractWorksheet.
*/
private AbstractWorksheet workSheet;
/**
* Extension length. .xls, .ods
*/
private static final int EXT_LENGTH = 3;
/**
* Parse a string, determine its type and creates an abstract worksheet.
* @param worksheet
*/
public final void openWorksheet(String worksheet) throws IOException {
String type = worksheet.substring(worksheet.length() - EXT_LENGTH);
if(type.equals("ods")) { //open office calc
workSheet = new JOpenDocument(worksheet);
} else if(type.equals("xls") || type.equals("xlsx")) { //excel
workSheet = new ExcelDocument(worksheet);
}
workSheet.activateReader();
workSheet.activateWriter();
}
/**
* Sets the active sheet number.
* @param x - worksheet number
*/
public final void setSheet(int x) {
workSheet.setSheet(x);
}
/**
* Saves the active worksheet.
* @throws IOException
*/
public final void save() throws IOException {
workSheet.save();
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(int, int)
*/
@Override
public final Object getCellValueAt(int row, int col) {
return ((IWorksheetReader)workSheet).getCellValueAt(row, col);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(java.lang.String)
*/
@Override
public final Object getCellValueAt(String cell) {
return ((IWorksheetReader)workSheet).getCellValueAt(cell);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetWriter#setValueAt(java.lang.Object, int, int)
*/
@Override
public final void setValueAt(Object val, int row, int col) {
((IWorksheetWriter)workSheet).setValueAt(val, row, col);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetWriter#setValueAt(java.lang.Object, java.lang.String)
*/
@Override
public final void setValueAt(Object val, String cellReference) {
((IWorksheetWriter)workSheet).setValueAt(val, cellReference);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getFillBackgroundColor(int, int)
*/
@Override
public final boolean isFilledBackgroundColor(int row, int col) {
return ((IWorksheetReader)workSheet).isFilledBackgroundColor(row, col);
}
/* (non-Javadoc)
* @see org.irri.cril.jicis.worksheet.IWorksheetReader#getCellAt(int, int)
*/
@Override
public final Object getCellAt(int row, int col) {
return ((IWorksheetReader)workSheet).getCellAt(row, col);
}
}