http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/37d2833c/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CWLUtil.java ---------------------------------------------------------------------- diff --git a/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CWLUtil.java b/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CWLUtil.java index 722bbf2..86379eb 100644 --- a/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CWLUtil.java +++ b/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CWLUtil.java @@ -58,13 +58,14 @@ public class CWLUtil { public JsonNode getNameSpace() { return nameSpace; } -/** - * This method is processing the CWL NameSpace for later use - * such as figuring out the Format of a input or output - */ + + /** + * This method is processing the CWL NameSpace for later use such as + * figuring out the Format of a input or output + */ public void processNameSpace() { - if (cwlFile.has("$namespaces")) { + if (cwlFile != null && cwlFile.has("$namespaces")) { nameSpace = cwlFile.path("$namespaces"); } @@ -85,11 +86,16 @@ public class CWLUtil { public Map<String, PortDetail> processOutputDetails() { return processdetails(cwlFile.get(OUTPUTS)); } -/** - * This method will go through CWL tool input or out puts and figure outs their IDs and the respective depths - * @param inputs This is JsonNode object which contains the Inputs or outputs of the respective CWL tool - * @return This the respective, ID and the depth of the input or output - */ + + /** + * This method will go through CWL tool input or out puts and figure outs + * their IDs and the respective depths + * + * @param inputs + * This is JsonNode object which contains the Inputs or outputs + * of the respective CWL tool + * @return This the respective, ID and the depth of the input or output + */ public Map<String, Integer> process(JsonNode inputs) { Map<String, Integer> result = new HashMap<>(); @@ -99,7 +105,7 @@ public class CWLUtil { while (iterator.hasNext()) { JsonNode input = iterator.next(); - String currentInputId = input.get(ID).asText(); + String currentInputId = input.get(ID).asText(); JsonNode typeConfigurations; try { @@ -130,29 +136,31 @@ public class CWLUtil { } } else if (inputs.getClass() == ObjectNode.class) { - for (JsonNode parameter :inputs) { + for (JsonNode parameter : inputs) { if (parameter.asText().startsWith("$")) System.out.println("Exception"); } - - - + } return result; } -/** - * This method is used for extracting details of the CWL tool inputs or outputs. - * ex:Lable, Format, Description - * @param inputs This is JsonNode object which contains the Inputs or outputs of the respective CWL tool - * @return - */ + + /** + * This method is used for extracting details of the CWL tool inputs or + * outputs. ex:Lable, Format, Description + * + * @param inputs + * This is JsonNode object which contains the Inputs or outputs + * of the respective CWL tool + * @return + */ private Map<String, PortDetail> processdetails(JsonNode inputs) { Map<String, PortDetail> result = new HashMap<>(); if (inputs.getClass() == ArrayNode.class) { - for (JsonNode input :inputs) { + for (JsonNode input : inputs) { PortDetail detail = new PortDetail(); String currentInputId = input.get(ID).asText(); @@ -165,18 +173,22 @@ public class CWLUtil { } } else if (inputs.getClass() == ObjectNode.class) { - for (JsonNode parameter :inputs) { + for (JsonNode parameter : inputs) { if (parameter.asText().startsWith("$")) System.out.println("Exception"); } } return result; } -/** - * This method is used for extracting the Label of a CWL input or Output - * @param input Single CWL input or output as a JsonNode - * @param detail respective PortDetail Object to hold the extracted Label - */ + + /** + * This method is used for extracting the Label of a CWL input or Output + * + * @param input + * Single CWL input or output as a JsonNode + * @param detail + * respective PortDetail Object to hold the extracted Label + */ public void extractLabel(JsonNode input, PortDetail detail) { if (input != null) if (input.has(LABEL)) { @@ -185,11 +197,14 @@ public class CWLUtil { detail.setLabel(null); } } -/** - * - * @param input Single CWL input or output as a JsonNode - * @param detail respective PortDetail Object to hold the extracted Label - */ + + /** + * + * @param input + * Single CWL input or output as a JsonNode + * @param detail + * respective PortDetail Object to hold the extracted Label + */ public void extractDescription(JsonNode input, PortDetail detail) { if (input != null) if (input.has(DESCRIPTION)) { @@ -198,12 +213,16 @@ public class CWLUtil { detail.setDescription(null); } } -/** - * This method is used for extracting the Formats of a CWL input or Output - * Single argument(Input or Output) can have multiple Formats. - * @param input Single CWL input or output as a JsonNode - * @param detail respective PortDetail Object to hold the extracted Label - */ + + /** + * This method is used for extracting the Formats of a CWL input or Output + * Single argument(Input or Output) can have multiple Formats. + * + * @param input + * Single CWL input or output as a JsonNode + * @param detail + * respective PortDetail Object to hold the extracted Label + */ public void extractFormat(JsonNode input, PortDetail detail) { if (input != null) if (input.has(FORMAT)) { @@ -224,13 +243,17 @@ public class CWLUtil { } } -/** - * Re Format the CWL format using the NameSpace in CWL Tool if possible otherwise it doesn't change the current - * nameSpace => edam:http://edam.org - * format : edam :1245 => http://edamontology.org/1245 - * @param formatInfoString Single Format - * @param detail respective PortDetail Object to hold the extracted Label - */ + + /** + * Re Format the CWL format using the NameSpace in CWL Tool if possible + * otherwise it doesn't change the current nameSpace => edam:http://edam.org + * format : edam :1245 => http://edamontology.org/1245 + * + * @param formatInfoString + * Single Format + * @param detail + * respective PortDetail Object to hold the extracted Label + */ public void figureOutFormats(String formatInfoString, PortDetail detail) { if (formatInfoString.startsWith("$")) { @@ -239,28 +262,32 @@ public class CWLUtil { String format[] = formatInfoString.split(":"); String namespaceKey = format[0]; String urlAppednd = format[1]; - - if (nameSpace.has(namespaceKey)) - detail.addFormat(nameSpace.get(namespaceKey).asText() + urlAppednd); - else - // can't figure out the format - detail.addFormat(formatInfoString); - + + if (nameSpace.has(namespaceKey)) + detail.addFormat(nameSpace.get(namespaceKey).asText() + urlAppednd); + else + // can't figure out the format + detail.addFormat(formatInfoString); + } else { // can't figure out the format detail.addFormat(formatInfoString); } } -/** - * This method is used to check whether the input/output is valid CWL TYPE when the type is represented as type: ["null","int"] - * @param typeConfigurations Type of the CWl input or output - * @return - */ + + /** + * This method is used to check whether the input/output is valid CWL TYPE + * when the type is represented as type: ["null","int"] + * + * @param typeConfigurations + * Type of the CWl input or output + * @return + */ public boolean isValidDataType(JsonNode typeConfigurations) { - for (JsonNode type : typeConfigurations) { - if (!( type.asText().equals(FLOAT) || type.asText().equals(NULL) || type.asText().equals(BOOLEAN) - || type.asText().equals(INT) || type.asText().equals(STRING) || type.asText().equals(DOUBLE) - || type.asText().equals(FILE))) + for (JsonNode type : typeConfigurations) { + if (!(type.asText().equals(FLOAT) || type.asText().equals(NULL) || type.asText().equals(BOOLEAN) + || type.asText().equals(INT) || type.asText().equals(STRING) || type.asText().equals(DOUBLE) + || type.asText().equals(FILE))) return false; } return true;
http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/37d2833c/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CwlContextualUtil.java ---------------------------------------------------------------------- diff --git a/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CwlContextualUtil.java b/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CwlContextualUtil.java new file mode 100644 index 0000000..2318669 --- /dev/null +++ b/taverna-cwl-utilities/src/main/java/org/apache/taverna/cwl/utilities/CwlContextualUtil.java @@ -0,0 +1,127 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *******************************************************************************/ +package org.apache.taverna.cwl.utilities; + +import java.util.ArrayList; +import java.util.Map; + +import com.fasterxml.jackson.databind.JsonNode; + +public class CwlContextualUtil extends CWLUtil { + private static final int MAX_LINE_LENG = 80; + private static final String LINE_BREAK = "\n"; + private static final String SPACE = " "; + + public CwlContextualUtil(JsonNode cwlFile) { + super(cwlFile); + } + + /** + * This method creates HTML representation of the String paragraph + * + * @param summary + * @param paragraph + * @return + */ + public String paragraphToHtml(String summary, String paragraph) { + + summary += "<tr><td colspan='2' align='left'>"; + + for (String line : paragraph.split("[\n|\r]")) + summary += "<p>" + line + "</p>"; + + summary += "</td></tr>"; + + return summary; + } + + /** + * This method creates the HTML tags and details of each input/output for + * service detail panel + * + * @param summary + * current String summary + * @param id + * input/output Id + * @param detail + * PortDetail object of the input/output + * @param depth + * depth of the input/output + * @return + */ + public String extractSummary(String summary, String id, PortDetail detail, int depth) { + summary += "<tr align='left'><td> ID: " + id + " </td><td>Depth: " + depth + "</td></tr>"; + if (detail.getLabel() != null) { + summary += "<tr><td align ='left' colspan ='2'>Label: " + detail.getLabel() + "</td></tr>"; + } + if (detail.getDescription() != null) { + + summary = paragraphToHtml(summary, detail.getDescription()); + + } + if (detail.getFormat() != null) { + summary += "<tr><td align ='left' colspan ='2'>Format: "; + ArrayList<String> formats = detail.getFormat(); + + int Size = formats.size(); + + if (Size == 1) { + summary += formats.get(0); + } else { + for (int i = 0; i < (Size - 1); i++) { + summary += formats.get(i) + ", "; + } + summary += formats.get(Size - 1); + } + summary += "</td></tr>"; + } + summary += "<tr></tr>"; + return summary; + } + + + + public String setUpInputDetails(String summary) { + + Map<String, PortDetail> inputs = processInputDetails(); + Map<String, Integer> inputDepths = processInputDepths(); + return extracSummaries(summary, inputs, inputDepths); + } + + public String setUpOutputDetails(String summary) { + Map<String, PortDetail> outPuts = processOutputDetails(); + Map<String, Integer> outputDepths = processOutputDepths(); + return extracSummaries(summary, outPuts, outputDepths); + } +/** + * + * @param summary current String + * @param parameters respective PortDetail Object which hold the label, description + * @param parameterDepths Map containing parameter Id and the corresponding depth + * @return + */ + public String extracSummaries(String summary, Map<String, PortDetail> parameters, + Map<String, Integer> parameterDepths) { + if ((parameters != null && !parameters.isEmpty()) && (parameterDepths != null && !parameterDepths.isEmpty())) + for (String id : parameters.keySet()) { + PortDetail detail = parameters.get(id); + if (parameterDepths.containsKey(id)) + summary = extractSummary(summary, id, detail, parameterDepths.get(id)); + } + return summary; + } +} http://git-wip-us.apache.org/repos/asf/incubator-taverna-common-activities/blob/37d2833c/taverna-cwl-utilities/src/test/java/org/apache/taverna/cwl/utilities/CwlContextualUtilTest.java ---------------------------------------------------------------------- diff --git a/taverna-cwl-utilities/src/test/java/org/apache/taverna/cwl/utilities/CwlContextualUtilTest.java b/taverna-cwl-utilities/src/test/java/org/apache/taverna/cwl/utilities/CwlContextualUtilTest.java new file mode 100644 index 0000000..0ea1aa5 --- /dev/null +++ b/taverna-cwl-utilities/src/test/java/org/apache/taverna/cwl/utilities/CwlContextualUtilTest.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *******************************************************************************/ +package org.apache.taverna.cwl.utilities; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class CwlContextualUtilTest { + JsonNode cwlFile; + CwlContextualUtil cwlContextualUtil; + JsonNode input; + + @Before + public void setUp() throws Exception { + Yaml reader = new Yaml(); + ObjectMapper mapper = new ObjectMapper(); + cwlFile = mapper.valueToTree(reader.load(getClass().getResourceAsStream("/customtool1.cwl"))); + cwlContextualUtil = new CwlContextualUtil(cwlFile); + } + + @Test + public void paragraphToHtmlTest() { + String summary = ""; + String para = "This is for just testing purposese******************* ****************************\n" + + "********************************** *5**************************** ******** ******\n"; + String expected = "<tr><td colspan='2' align='left'><p>This is for just testing purposese******************* ****************************</p><p>********************************** *5**************************** ******** ******</p></td></tr>"; + assertEquals(expected, cwlContextualUtil.paragraphToHtml(summary, para)); + } + + @Test + public void extractSummaryTest() { + String summary = ""; + String id = "parameter_1"; + int depth = 0; + PortDetail detail = new PortDetail(); + detail.setLabel("Test Label"); + detail.setDescription("Test Description"); + String expected ="<tr align='left'><td> ID: parameter_1 </td><td>Depth: 0</td></tr><tr><td align ='left' colspan ='2'>Label: Test Label</td></tr><tr><td colspan='2' align='left'><p>Test Description</p></td></tr><tr></tr>"; + assertEquals(expected, cwlContextualUtil.extractSummary(summary, id, detail, depth)); + } + +}
