TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
DirectoryReader.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 System.IO;
20 using TraceLabSDK.Types.Generics.Collections;
21 using TraceLabSDK.Component.Config;
22 
23 namespace TraceLab.Components.Library.Helpers
24 {
25  [Component(Name = "Directory Reader",
26  Description = "It reads files from the directory that match the specified pattern and outputs the files into the list of Strings. It also outputs the count of all the files.",
27  Author = "SAREC",
28  Version = "1.0.0.0",
29  ConfigurationType=typeof(DirectoryReaderConfig))]
30  [IOSpec(IOSpecType.Output, "files", typeof(StringList))]
31  [IOSpec(IOSpecType.Output, "numberOfFiles", typeof(int))]
32  [Tag("Helper components")]
33  public class DirectoryReader : BaseComponent
34  {
35  public DirectoryReader(ComponentLogger log) : base(log)
36  {
37  config = new DirectoryReaderConfig();
38  Configuration = config;
39  }
40 
41  private DirectoryReaderConfig config;
42 
46  public override void Compute()
47  {
48  //validate config
49  if (config.Directory == null)
50  {
51  throw new ComponentException("Directory has not been specified.");
52  }
53  if (Directory.Exists(config.Directory) == false)
54  {
55  throw new ComponentException(String.Format("Directory does not exist '{0}'.", config.Directory.Absolute));
56  }
57 
58  string[] files;
59  if (String.IsNullOrEmpty(config.SearchPattern) == true)
60  {
61  files = Directory.GetFiles(config.Directory, "*", TranslateSearchOption(config.SearchOption));
62  }
63  else
64  {
65  files = Directory.GetFiles(config.Directory, config.SearchPattern, TranslateSearchOption(config.SearchOption));
66  }
67 
68  StringList listOfFiles = new StringList();
69  listOfFiles.AddRange(files);
70 
71  Workspace.Store("files", listOfFiles);
72  Workspace.Store("numberOfFiles", listOfFiles.Count);
73  Logger.Trace(String.Format("Found {0} files in the given directory that match given search pattern.", listOfFiles.Count));
74  }
75 
76  public static System.IO.SearchOption TranslateSearchOption(DirectoryReaderSearchOption value)
77  {
78  switch (value)
79  {
80  case DirectoryReaderSearchOption.TopDirectoryOnly:
81  return SearchOption.TopDirectoryOnly;
82 
83  case DirectoryReaderSearchOption.AllDirectories:
84  return SearchOption.AllDirectories;
85 
86  default: return SearchOption.TopDirectoryOnly;
87  }
88  }
89  }
90 
91  public class DirectoryReaderConfig
92  {
93  public DirectoryPath Directory
94  {
95  get;
96  set;
97  }
98 
99  public string SearchPattern
100  {
101  get;
102  set;
103  }
104 
105  public DirectoryReaderSearchOption SearchOption
106  {
107  get;
108  set;
109  }
110  }
111 
113  {
114  // Summary:
115  // Includes only the current directory in a search.
116  TopDirectoryOnly = 0,
117  //
118  // Summary:
119  // Includes the current directory and all the subdirectories in a search operation.
120  // This option includes reparse points like mounted drives and symbolic links
121  // in the search.
122  AllDirectories = 1,
123  }
124 }