Parse inputs, add tests
diff --git a/taverna-scufl2-cwl/src/main/java/org/apache/taverna/scufl2/cwl/Parser.java b/taverna-scufl2-cwl/src/main/java/org/apache/taverna/scufl2/cwl/Parser.java
index 8cc6655..65844b9 100644
--- a/taverna-scufl2-cwl/src/main/java/org/apache/taverna/scufl2/cwl/Parser.java
+++ b/taverna-scufl2-cwl/src/main/java/org/apache/taverna/scufl2/cwl/Parser.java
@@ -77,8 +77,100 @@
}
}
+ public ArrayList<InputField> parseInputs() {
+ int startIndex = 0;
+ int endIndex = -1;
+ int depth = -1;
+ /**
+ * Search for start and end of inputs section
+ */
+ for(Map.Entry<Integer, String> entry: yamlFile.entrySet()) {
+ int index = entry.getKey();
+ String line = entry.getValue();
+ String key = getKeyFromLine(line);
+ if(key.equals("inputs")) {
+ startIndex = index;
+ endIndex = index;
+ depth = getDepth(line);
+ } else if(!line.equals("") && getDepth(line) <= depth) {
+ break;
+ } else {
+ endIndex++;
+ }
+ }
+ /**
+ * Parse each input
+ */
+ ArrayList<InputField> result = new ArrayList<>();
+ for(int i = startIndex+1; i <= endIndex; i++) {
+ int curDepth = getDepth(yamlFile.get(i));
+ // If current element is a child of inputs key
+ if(curDepth == depth + 1) {
+ result.add(parseInputField(i));
+ }
+ }
+ return result;
+ }
+
+ public InputField parseInputField(int startIndex) {
+ String line = yamlFile.get(startIndex);
+ int depth = getDepth(line);
+ String id = getKeyFromLine(line);
+ String value = getValueFromLine(line);
+
+ if(!value.equals("")) {
+ return new InputField(id, value);
+ }
+
+ InputField field = new InputField(id);
+ for(int i = startIndex+1; i < length; i++) {
+ String curLine = yamlFile.get(i);
+ if(curLine.equals("")) {
+ // Ignore empty lines
+ continue;
+ }
+ if(getDepth(curLine) <= depth) {
+ // Out of input section
+ break;
+ }
+ String key = getKeyFromLine(curLine);
+ value = getValueFromLine(curLine);
+
+ if(key.trim().equals("type")) {
+ field.type = value;
+ } else if(key.trim().equals("inputBinding")) {
+
+ int curDepth = getDepth(curLine);
+ int nextIndex = getNextLineIndex(i);
+ String nextLine = yamlFile.get(nextIndex);
+ String nextKey = getKeyFromLine(nextLine);
+ String nextValue = getValueFromLine(nextLine);
+
+ if(nextKey.equals("position")){
+ field.position = Integer.parseInt(nextValue);
+ } else if(nextKey.equals("prefix")){
+ field.prefix = nextValue;
+ }
+
+ // Check if we have another inputBinding property
+ nextIndex = getNextLineIndex(nextIndex);
+ nextLine = yamlFile.get(nextIndex);
+ if(getDepth(nextLine) == curDepth + 1) {
+ nextKey = getKeyFromLine(nextLine);
+ nextValue = getValueFromLine(nextLine);
+ if(nextKey.equals("position")){
+ field.position = Integer.parseInt(nextValue);
+ } else if(nextKey.equals("prefix")){
+ field.prefix = nextValue.trim();
+ }
+ }
+ }
+ }
+
+ return field;
+ }
private int getNextLineIndex(int index) {
index++;
diff --git a/taverna-scufl2-cwl/src/test/java/org/apache/taverna/scufl2/cwl/TestParser.java b/taverna-scufl2-cwl/src/test/java/org/apache/taverna/scufl2/cwl/TestParser.java
new file mode 100644
index 0000000..01b5bc2
--- /dev/null
+++ b/taverna-scufl2-cwl/src/test/java/org/apache/taverna/scufl2/cwl/TestParser.java
@@ -0,0 +1,84 @@
+package org.apache.taverna.scufl2.cwl;
+
+import java.io.*;
+import java.util.ArrayList;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import org.apache.commons.io.FileUtils;
+
+import org.apache.taverna.scufl2.cwl.Parser;
+import org.apache.taverna.scufl2.cwl.InputField;
+
+public class TestParser {
+
+ private static final String SIMPLE_STRING_INPUT = "/simple_string_input.cwl";
+ private static final String INT_INPUT = "/int_input.cwl";
+
+ @Test
+ public void testGetDepth() throws Exception {
+
+ assert Parser.getDepth(" test") == 1;
+ assert Parser.getDepth("test") == 0;
+ assert Parser.getDepth(" test") == 2;
+ }
+
+ @Test
+ public void testGetKey() throws Exception {
+
+ assert Parser.getKeyFromLine(" test: test_value").equals("test");
+ assert Parser.getKeyFromLine("test: 1 ").equals("test");
+ assert Parser.getKeyFromLine(" test:").equals("test");
+ }
+
+ @Test
+ public void testGetValue() throws Exception {
+
+ assert Parser.getValueFromLine(" test: test_value").equals("test_value");
+ assert Parser.getValueFromLine("test: 1 ").equals("1");
+ assert Parser.getValueFromLine(" test:").equals("");
+ }
+
+ @Test
+ public void testSimpleInput() throws Exception {
+ File yaml = FileUtils.getFile("src", "test", "resources", SIMPLE_STRING_INPUT);
+
+ Parser parser = new Parser(yaml);
+
+ ArrayList<InputField> inputs = parser.parseInputs();
+
+ assertEquals(1, inputs.size());
+ assertEquals("example_string", inputs.get(0).key);
+ }
+
+ @Test
+ public void testIntInput() throws Exception {
+ File yaml = FileUtils.getFile("src", "test", "resources", INT_INPUT);
+
+ Parser parser = new Parser(yaml);
+
+ ArrayList<InputField> inputs = parser.parseInputs();
+
+ assertEquals(1, inputs.size());
+ assertEquals("example_int", inputs.get(0).key);
+ assertEquals("int", inputs.get(0).type);
+ assertEquals(2, inputs.get(0).position);
+ assertEquals("-i", inputs.get(0).prefix);
+ }
+
+ void printFile(File yaml) throws Exception {
+ /**
+ * Print file
+ */
+ FileReader fdesc = new FileReader(yaml);
+ BufferedReader bufferedReader = new BufferedReader(fdesc);
+ String yamlLine;
+ while((yamlLine = bufferedReader.readLine()) != null) {
+ System.out.println(yamlLine);
+ }
+
+ System.out.println("*************************");
+ bufferedReader.close();
+ /*****/
+ }
+}
diff --git a/taverna-scufl2-cwl/src/test/resources/1st-tool.cwl b/taverna-scufl2-cwl/src/test/resources/1st-tool.cwl
new file mode 100644
index 0000000..e292134
--- /dev/null
+++ b/taverna-scufl2-cwl/src/test/resources/1st-tool.cwl
@@ -0,0 +1,10 @@
+cwlVersion: v1.0
+class: CommandLineTool
+baseCommand: echo
+inputs:
+ message:
+ type: string
+
+ inputBinding:
+ position: 1
+outputs: []
\ No newline at end of file
diff --git a/taverna-scufl2-cwl/src/test/resources/int_input.cwl b/taverna-scufl2-cwl/src/test/resources/int_input.cwl
new file mode 100644
index 0000000..ffb79e6
--- /dev/null
+++ b/taverna-scufl2-cwl/src/test/resources/int_input.cwl
@@ -0,0 +1,6 @@
+inputs:
+ example_int:
+ type: int
+ inputBinding:
+ position: 2
+ prefix: -i
diff --git a/taverna-scufl2-cwl/src/test/resources/simple_string_input.cwl b/taverna-scufl2-cwl/src/test/resources/simple_string_input.cwl
new file mode 100644
index 0000000..c075b22
--- /dev/null
+++ b/taverna-scufl2-cwl/src/test/resources/simple_string_input.cwl
@@ -0,0 +1,2 @@
+inputs:
+ example_string: string
\ No newline at end of file
diff --git a/taverna-scufl2-cwl/src/test/resources/worklflow2.cwl b/taverna-scufl2-cwl/src/test/resources/worklflow2.cwl
new file mode 100644
index 0000000..43f164c
--- /dev/null
+++ b/taverna-scufl2-cwl/src/test/resources/worklflow2.cwl
@@ -0,0 +1,27 @@
+#!/usr/bin/env cwl-runner
+cwlVersion: v1.0
+class: Workflow
+
+inputs:
+ message: string
+
+outputs:
+ download:
+ type: File
+ outputSource: "#step1/curl"
+
+steps:
+ step1:
+ run:
+ class: CommandLineTool
+ baseCommand: echo
+ inputs:
+ text:
+ type: string
+ inputBinding:
+ position: 1
+ outputs: []
+ in:
+ text: message
+
+ out: [curl]