in taverna-spreadsheet-import-activity/src/main/java/org/apache/taverna/activities/spreadsheet/SpreadsheetImportActivity.java [135:232]
public void executeAsynch(final Map<String, T2Reference> data,
final AsynchronousActivityCallback callback) {
callback.requestRun(new Runnable() {
public void run() {
Map<String, T2Reference> outputData = new HashMap<String, T2Reference>();
InvocationContext context = callback.getContext();
ReferenceService referenceService = context.getReferenceService();
try {
T2Reference inputRef = data.get(INPUT_PORT_NAME);
SpreadsheetRowProcessor spreadsheetRowProcessor = null;
Map<String, List<T2Reference>> outputLists = null;
StringWriter output = null;
if (outputFormat.equals(SpreadsheetOutputFormat.PORT_PER_COLUMN)) {
outputLists = new HashMap<String, List<T2Reference>>();
for (Port port : getOutputPorts()) {
outputLists.put(port.getName(), new ArrayList<T2Reference>());
}
spreadsheetRowProcessor = new MultiplePortRowProcessor(referenceService, outputLists, context);
} else {
output = new StringWriter();
char csvDelimiterCharacter = ',';
if (csvDelimiter != null && csvDelimiter.length() > 0) {
csvDelimiterCharacter = csvDelimiter.charAt(0);
}
CsvWriter csvWriter = new CsvWriter(output, csvDelimiterCharacter);
csvWriter.setEscapeMode(CsvWriter.ESCAPE_MODE_DOUBLED);
csvWriter.setTextQualifier('"');
csvWriter.setUseTextQualifier(true);
spreadsheetRowProcessor = new SingleOutputRowProcessor(csvWriter);
}
InputStream inputStream = getInputStream(context, referenceService, inputRef);
if (inputStream == null) {
logger.warn("Input is not a file reference or a file name");
callback.fail("Input is not a file reference or a file name");
return;
}
try {
try {
new ExcelSpreadsheetReader().read(inputStream, new Range(rowRange),
new Range(columnRange), ignoreBlankRows, spreadsheetRowProcessor);
} catch (SpreadsheetReadException e) {
inputStream.close();
inputStream = getInputStream(context, referenceService, inputRef);
try {
new ODFSpreadsheetReader().read(inputStream, new Range(rowRange),
new Range(columnRange), ignoreBlankRows, spreadsheetRowProcessor);
} catch (SpreadsheetReadException e2) {
inputStream.close();
inputStream = getInputStream(context, referenceService, inputRef);
new CSVSpreadsheetReader().read(inputStream, new Range(rowRange),
new Range(columnRange), ignoreBlankRows, spreadsheetRowProcessor);
}
} finally {
inputStream.close();
}
} catch (IOException e1) {
logger.warn("Failed to close spereadsheet stream", e1);
}
// get outputs
if (outputFormat.equals(SpreadsheetOutputFormat.PORT_PER_COLUMN)) {
for (OutputPort outputPort : getOutputPorts()) {
String name = outputPort.getName();
Object value = outputLists.get(name);
T2Reference id = referenceService.register(value, outputPort.getDepth(),
true, context);
outputData.put(name, id);
}
} else {
T2Reference id = referenceService.register(output.toString(), 0, true, context);
outputData.put(OUTPUT_PORT_NAME, id);
}
callback.receiveResult(outputData, new int[0]);
} catch (ReferenceServiceException e) {
logger.warn("Error accessing spreadsheet input/output data", e);
callback.fail("Error accessing spreadsheet input/output data", e);
} catch (SpreadsheetReadException e) {
logger.warn("Spreadsheet input cannot be read", e);
callback.fail("Spreadsheet input cannot be read", e);
} catch (FileNotFoundException e) {
logger.warn("Input spreadsheet file does not exist", e);
callback.fail("Input spreadsheet file does not exist", e);
} catch (IOException e) {
logger.warn("Error reading spreadsheet", e);
callback.fail("Error reading spreadsheet", e);
}
}
});
}