Commit 5f0e3f0d authored by Amandine  TOURNAY's avatar Amandine TOURNAY
Browse files

Initial commit

parents
.idea/
.settings/
build/
target/
*.iml
.classpath
.project
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.bioimageanalysis.icy</groupId>
<artifactId>icy-workbooks</artifactId>
<version>3.4.9</version>
<packaging>jar</packaging>
<name>Workbooks</name>
<description>
Toolbox to create and manipulate result tables with plug-ins and protocols, and export as text file or to your favorite spreadsheet software
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<outputJar>${project.build.outputDirectory}/../plugin</outputJar>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<outputDirectory>${outputJar}</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>plugins.adufour.workbooks.Workbooks</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.bioimageanalysis.icy</groupId>
<artifactId>icy-kernel</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.bioimageanalysis.icy</groupId>
<artifactId>icy-blocks</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.github.yannrichet</groupId>
<artifactId>JMathPlot</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>icy</id>
<url>https://icy-nexus.pasteur.fr/repository/Icy/</url>
</repository>
</repositories>
<distributionManagement>
<snapshotRepository>
<id>icy-dev</id>
<name>icy-dev</name>
<url>https://icy-nexus-dev.pasteur.cloud/repository/icy-core/</url>
</snapshotRepository>
<repository>
<id>icy-prod</id>
<name>icy-prod</name>
<url>https://icy-nexus.pasteur.fr/repository/icy-core/</url>
</repository>
</distributionManagement>
</project>
\ No newline at end of file
package plugins.adufour.blocks.tools.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import icy.plugin.abstract_.Plugin;
import plugins.adufour.blocks.tools.io.WorkbookToFile.MergePolicy;
import plugins.adufour.blocks.util.VarList;
import plugins.adufour.vars.lang.VarFile;
import plugins.adufour.vars.lang.VarWorkbook;
import plugins.adufour.workbooks.Workbooks;
/**
* IO block that reads a workbook file from disk using the POI library
*
* @author Alexandre Dufour
*/
public class FileToWorkbook extends Plugin implements IOBlock
{
VarFile inputFile = new VarFile("input file", null);
VarWorkbook workbook = new VarWorkbook("workbook", (Workbook) null);
@Override
public void run()
{
workbook.setValue(readWorkbook(inputFile.getValue(true)));
}
@Override
public void declareInput(VarList inputMap)
{
inputMap.add("input file", inputFile);
}
@Override
public void declareOutput(VarList outputMap)
{
outputMap.add("workbook", workbook);
}
/**
* Reads the specified file into a workbook. This method will read Excel-compatible (.xls)
* files, but will also read tab-delimited text files. <br/>
* Note: tab-delimited text files may contain multiple sheets, if specified as in the following
* example:<br/>
* <code>== sheet 1 ==<br/>
* item1 (tab) item2<br/>
* == sheet 2 ==<br/>
* item3 (tab) item4</code><br/>
* (the same convention is followed by
* {@link WorkbookToFile#saveAsText(Workbook, String, MergePolicy)}
*
* @param file
* @return
*/
public static Workbook readWorkbook(File file)
{
if (!file.exists() || file.isDirectory()) throw new IllegalArgumentException("Cannot read a workbook from " + file.getPath());
try
{
FileInputStream fis = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(fis);
fis.close();
return workbook;
}
catch (Exception e)
{
// try text
Workbook wb = Workbooks.createEmptyWorkbook();
Sheet sheet = null;
try
{
BufferedReader reader = new BufferedReader(new FileReader(file));
int rowID = 0;
boolean newLine = false;
while (reader.ready())
{
String line = reader.readLine();
if (line.isEmpty())
{
// remember there is a new line, deal with it later
newLine = true;
continue;
}
// 1) fetch (or create) the sheet
// special case: sheet name saved using FileToWorkbook
if (line.startsWith("== "))
{
// don't add the new line if we create a new sheet
newLine = false;
String sheetName = line.substring(3, line.indexOf(" =="));
sheet = wb.createSheet(sheetName);
// reset the row
rowID = 0;
// nothing else interesting in this line...
continue;
}
// otherwise, create a (default) empty sheet
if (sheet == null)
{
sheet = wb.createSheet();
// reset the row
rowID = 0;
}
// 2) add the new line (if any)
if (newLine) sheet.createRow(rowID++);
// 3) Create a new row for the current input
Row row = sheet.createRow(rowID++);
// skip empty lines
if (line.isEmpty()) continue;
int colID = 0;
String[] words = line.split("\t");
for (String word : words)
{
if (!word.isEmpty())
{
Cell cell = row.createCell(colID);
try
{
cell.setCellValue(Double.parseDouble(word));
}
catch (NumberFormatException formatError)
{
cell.setCellValue(word);
}
}
colID++;
}
}
reader.close();
return wb;
}
catch (Exception e2)
{
throw new RuntimeException(e2);
}
}
}
}
package plugins.adufour.blocks.tools.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import icy.file.FileUtil;
import icy.plugin.abstract_.Plugin;
import icy.system.IcyHandledException;
import plugins.adufour.blocks.util.VarList;
import plugins.adufour.vars.lang.VarEnum;
import plugins.adufour.vars.lang.VarFile;
import plugins.adufour.vars.lang.VarWorkbook;
import plugins.adufour.workbooks.Workbooks;
/**
* IO block that writes a workbook to a file using the POI library
*
* @author Alexandre Dufour
*/
public class WorkbookToFile extends Plugin implements IOBlock
{
public enum WorkbookFormat
{
/** A format compatible with most spreadsheet software (~ XLS) */
Spreadsheet,
/** A tab-delimited text format */
Text
}
public enum MergePolicy
{
/** File is overwritten entirely */
Overwrite,
/** New sheets are merged into existing ones if they have the same name */
Merge_sheets,
/**
* New sheets are merged into existing ones if they have the same name, but the first
* (header) row is not copied
*/
Merge_sheets___excluding_first_row;
public String toString()
{
return super.toString().replace("__", ",").replace("_", " ");
}
}
VarWorkbook workbook = new VarWorkbook("workbook", (Workbook) null);
VarFile file = new VarFile("output file", null);
VarEnum<WorkbookFormat> format = new VarEnum<WorkbookFormat>("file format", WorkbookFormat.Spreadsheet);
VarEnum<MergePolicy> append = new VarEnum<MergePolicy>("If file exists", MergePolicy.Overwrite);
@Override
public void run()
{
switch (format.getValue())
{
case Spreadsheet:
saveAsSpreadSheet(workbook.getValue(true), file.getValue(true).getAbsolutePath(), append.getValue());
break;
case Text:
saveAsText(workbook.getValue(true), file.getValue(true).getAbsolutePath(), append.getValue());
break;
}
if (append.getValue() != MergePolicy.Overwrite)
workbook.valueChanged(workbook, null, workbook.getValue());
}
@Override
public void declareInput(VarList inputMap)
{
inputMap.add("workbook", workbook);
inputMap.add("file format", format);
inputMap.add("output file", file);
inputMap.add("If file exists", append);
}
@Override
public void declareOutput(VarList outputMap)
{
}
/**
* Saves the specified workbook to a spreadsheet file. By default, the ".xls" extension will be
* added to the file name if it is not present
*
* @param workbook
* @param fileName
* @deprecated Use {@link #saveAsSpreadSheet(Workbook, String, MergePolicy)} instead.
*/
@Deprecated
public static void saveAsSpreadSheet(Workbook workbook, String fileName)
{
saveAsSpreadSheet(workbook, fileName, MergePolicy.Overwrite);
}
/**
* Saves the specified workbook to a spreadsheet file. By default, the ".xls" extension will be
* added to the file name if it is not present
*
* @param workbook
* @param fileName
*/
public static void saveAsSpreadSheet(Workbook workbook, String fileName, MergePolicy mergePolicy)
{
File file = new File(fileName);
if (file.isDirectory())
throw new IcyHandledException("Cannot save workbook: the specified file is a folder");
// Add a default extension (if necessary)
String extension = Workbooks.getFormat(workbook).getExtension();
if (!file.exists() && !fileName.toLowerCase().endsWith(extension))
file = new File(FileUtil.setExtension(file.getPath(), extension));
// should we merge?
if (file.exists() && mergePolicy != MergePolicy.Overwrite)
{
try
{
Workbook wb = FileToWorkbook.readWorkbook(file);
mergeWorkbooks(workbook, wb, mergePolicy);
workbook = wb;
}
catch (IllegalArgumentException e)
{
String message = "Unable to merge (target file has not the same format)";
throw new IcyHandledException(message);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
try
{
File tmp = File.createTempFile("icy_tmp_workbook", extension);
FileOutputStream stream = new FileOutputStream(tmp);
workbook.write(stream);
stream.close();
FileUtil.rename(tmp, file, true);
}
catch (FileNotFoundException e)
{
String error = "Cannot create file " + fileName + "\n";
error += "Reason: " + e.getMessage() + "\n\n";
error += "Please ensure that:\n";
error += " - You have permission to write to this folder\n";
error += " - The file name does not contains any special characters";
throw new IcyHandledException(error);
}
catch (IOException e)
{
String error = "Cannot write file " + fileName + "\n";
error += "Reason: " + e.getMessage() + "\n\n";
error += "Please ensure that the file is not opened with another application.";
throw new IcyHandledException(error);
}
}
/**
* Saves the specified workbook to a spreadsheet file. By default, the ".txt" extension will be
* added to the file name if it is not present
*
* @param workbook
* @param fileName
* @deprecated Use {@link #saveAsText(Workbook, String, MergePolicy)} instead.
*/
@Deprecated
public static void saveAsText(Workbook workbook, String fileName)
{
saveAsText(workbook, fileName, MergePolicy.Overwrite);
}
/**
* Saves the specified workbook to a spreadsheet file. By default, the ".txt" extension will be
* added to the file name if it is not present. If the workbook contains multiple sheets, they
* will be stored in the text file as in the following example:<br/>
* <code>== sheet 1 ==<br/>
* item1 (tab) item2<br/>
* <br/>
* == sheet 2 ==<br/>
* item3 (tab) item4</code><br/>
*
* @param workbook
* the workbook to save
* @param fileName
* the name of the file to save to
* @param mergePolicy
* indicates if and how the file should be appended
*/
public static void saveAsText(Workbook workbook, String fileName, MergePolicy mergePolicy)
{
File file = new File(fileName);
if (file.isDirectory())
throw new IcyHandledException("Cannot save workbook: the specified file is a folder");
// Add a default extension (if necessary)
if (!file.exists() && !fileName.toLowerCase().endsWith(".txt"))
file = new File(fileName + ".txt");
// should we merge?
if (file.exists() && mergePolicy != MergePolicy.Overwrite)
try
{
Workbook wb = FileToWorkbook.readWorkbook(file);
mergeWorkbooks(workbook, wb, mergePolicy);
workbook = wb;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
try
{
FileWriter fw = new FileWriter(file);
int n = workbook.getNumberOfSheets();
for (int i = 0; i < n; i++)
{
Sheet sheet = workbook.getSheetAt(i);
fw.write("== " + sheet.getSheetName() + " ==\n");
for (int r = sheet.getFirstRowNum(); r <= sheet.getLastRowNum(); r++)
{
Row row = sheet.getRow(r);
if (row != null)
{
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++)
{
Cell cell = row.getCell(c, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
if (cell != null)
fw.write(cell.toString());
fw.write("\t");
}
}
fw.write("\n");
}
fw.write("\n");
}
fw.flush();
fw.close();
}
catch (FileNotFoundException e)
{
String error = "Cannot create file " + fileName + "\n";
error += "Reason: " + e.getMessage() + "\n\n";
error += "Please ensure that:\n";
error += " - You have permission to write to this folder\n";
error += " - The file name does not contains any special characters";
throw new IcyHandledException(error);
}
catch (IOException e)
{
String error = "Cannot write file " + fileName + "\n";
error += "Reason: " + e.getMessage() + "\n\n";
error += "Please ensure that the file is not opened with another application.";
throw new IcyHandledException(error);
}
}
/**
* @param source
* @param target
* @param policy
* @return
*/
private static void mergeWorkbooks(Workbook source, Workbook target, MergePolicy policy)
{
if (policy == MergePolicy.Overwrite)
return;
// case Add_new_sheets:
// for (int i = 0; i < source.getNumberOfSheets(); i++)
// {
// Sheet sourceSheet = source.getSheetAt(i);
// // make sure the name is unique
// String sheetName = sourceSheet.getSheetName();
// // if not, add underscores
// while (target.getSheet(sheetName) != null)
// sheetName = "_" + sheetName;
//
// // create the new sheet
// Sheet targetSheet = target.createSheet(sheetName);
// // copy everything manually (sigh)
// for (int r = 0; r <= sourceSheet.getLastRowNum(); r++)
// {
// Row sourceRow = sourceSheet.getRow(r);
// if (sourceRow == null) continue;
// Row targetRow = targetSheet.createRow(r);
// for (Cell sourceCell : sourceRow)
// {
// // Copy the cell
// Cell targetCell = targetRow.createCell(sourceCell.getColumnIndex());
// CellStyle style = target.createCellStyle();
// style.cloneStyleFrom(sourceCell.getCellStyle());
// targetCell.setCellStyle(style);
// targetCell.setCellType(sourceCell.getCellType());
// switch (sourceCell.getCellType())
// {
// case Cell.CELL_TYPE_BOOLEAN:
// targetCell.setCellValue(sourceCell.getBooleanCellValue());
// break;
// case Cell.CELL_TYPE_FORMULA:
// targetCell.setCellFormula(sourceCell.getCellFormula());