TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
GetStringFromStringList.cs
Go to the documentation of this file.
1 // TraceLab - Software Traceability Instrument to Facilitate and Empower Traceability Research
2 // Copyright © 2012-2013 CoEST - National Science Foundation MRI-R2 Grant # CNS: 0959924
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using System;
18 using TraceLabSDK;
19 using TraceLabSDK.Types.Generics.Collections;
20 
21 namespace TraceLab.Components.Library.Helpers
22 {
23  [Component(Name = "String Getter from String list",
24  Description = "This simple helper component allows getting string from the specified collection of string at the specified index, and outputting it to Workspace.",
25  Author = "SAREC",
26  Version = "1.0.0.0")]
27  [IOSpec(IOSpecType.Input, "listOfStrings", typeof(StringList))]
28  [IOSpec(IOSpecType.Input, "index", typeof(int))]
29  [IOSpec(IOSpecType.Output, "selectedString", typeof(System.String))]
30  [Tag("Helper components")]
31  public class GetStringFromStringList : BaseComponent
32  {
33  public GetStringFromStringList(ComponentLogger log) : base(log) { }
34 
35  public override void Compute()
36  {
37  StringList listOfStrings = (StringList)Workspace.Load("listOfStrings");
38  if (listOfStrings == null)
39  {
40  throw new ComponentException("Received null listOfStrings");
41  }
42  if (listOfStrings.Count == 0)
43  {
44  throw new ComponentException("Received empty listOfStrings");
45  }
46 
47  int index = (int)Workspace.Load("index");
48 
49  if (index < 0)
50  {
51  throw new ComponentException("Received negative index");
52  }
53  if (index > listOfStrings.Count)
54  {
55  throw new ComponentException("Received index greater than amount of elements in the listOfStrings");
56  }
57 
58  string outputString = listOfStrings[index];
59 
60  Workspace.Store("selectedString", outputString);
61  }
62  }
63 }