Skip to content

Commit 7a3cb74

Browse files
authored
Merge pull request #262 from kneubert/feature/DirectoryLoader-with-relative-path
Feature/directory loader with relative path
2 parents f3f3bee + 059347b commit 7a3cb74

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import org.knime.core.node.port.PortObject;
4242
import org.knime.core.node.port.PortObjectSpec;
4343
import org.knime.core.node.port.PortType;
44+
import org.knime.core.util.FileUtil;
4445

45-
import com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject;
4646
import com.genericworkflownodes.knime.base.data.port.PrefixURIPortObject;
4747

4848
/**
@@ -155,47 +155,59 @@ protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec)
155155
throws Exception {
156156

157157
String directoryname = m_directory_name.getStringValue();
158-
List<URIContent> uris = new ArrayList<URIContent>();
159-
File in = new File(convertToURL(directoryname).toURI());
160-
161-
if (!in.canRead()) {
158+
URL url_directory = convertToURL(directoryname);
159+
File directory = FileUtil.getFileFromURL(url_directory);
160+
String prefix = directory.getAbsolutePath();
161+
List<File> files_list_ext = listf(directory);
162+
163+
if (!directory.canRead()) {
162164
throw new Exception("Cannot read from input file: "
163-
+ in.getAbsolutePath());
165+
+ directory.getAbsolutePath());
164166
}
165167

166-
String prefix = directoryname;
167-
if (directoryname.contains(".")) {
168-
prefix = prefix.split("\\.",2)[0];
169-
}
170-
171-
List<File> files_list_ext = listf(directoryname);
172-
168+
List<URIContent> uris = new ArrayList<URIContent>();
173169
if (files_list_ext.isEmpty()) {
174170
throw new Exception("Could not find any files in the selected directory");
175171
}
176172
for (File file : files_list_ext) {
177173
uris.add(new URIContent(file.toURI(), FilenameUtils.getExtension(file.toString())));
178174
}
179175

180-
IPrefixURIPortObject uri_prefix_object = null;
176+
PrefixURIPortObject uri_prefix_object = null;
181177
uri_prefix_object = new PrefixURIPortObject(uris, prefix);
182178

183179
return new PortObject[] { (URIPortObject) uri_prefix_object };
184180
}
185181

186-
public static List<File> listf(String directoryName) {
187-
File directory = new File(directoryName);
182+
183+
/**
184+
* List files in a given directory
185+
*
186+
* @param dirctory
187+
* Directory file handle.
188+
* @return list
189+
* List of Files.
190+
* @throws Exception
191+
* If there are no files found in the given directory.
192+
*/
193+
public static List<File> listf(File directory)
194+
throws Exception {
188195

189196
List<File> resultList = new ArrayList<File>();
190197

191198
// get all the files from a directory
192-
File[] fList = directory.listFiles();
199+
File[] fList;
200+
if (directory.list() == null) {
201+
throw new Exception("Could not find any files in the selected directory ".concat(directory.getAbsolutePath()));
202+
}
203+
else {
204+
fList = directory.listFiles();
205+
}
206+
193207
resultList.addAll(Arrays.asList(fList));
194208
for (File file : fList) {
195-
if (file.isFile()) {
196-
System.out.println(file.getAbsolutePath());
197-
} else if (file.isDirectory()) {
198-
resultList.addAll(listf(file.getAbsolutePath()));
209+
if (file.isDirectory()) {
210+
resultList.addAll(listf(file));
199211
}
200212
}
201213

0 commit comments

Comments
 (0)