From ca7c00b4491fff9bc6a27f1ff8aa5ff9dcddff58 Mon Sep 17 00:00:00 2001 From: Kerstin Neubert Date: Tue, 22 Oct 2019 13:17:03 +0200 Subject: [PATCH 1/2] allow workflow relative path and variable setting --- .../dirloader/DirectoryLoaderNodeModel.java | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java b/com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java index 8f90e610..49a16495 100644 --- a/com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java +++ b/com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java @@ -41,8 +41,9 @@ import org.knime.core.node.port.PortObject; import org.knime.core.node.port.PortObjectSpec; import org.knime.core.node.port.PortType; +import org.knime.core.util.FileUtil; -import com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject; +//import com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject; import com.genericworkflownodes.knime.base.data.port.PrefixURIPortObject; /** @@ -155,21 +156,17 @@ protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) throws Exception { String directoryname = m_directory_name.getStringValue(); - List uris = new ArrayList(); - File in = new File(convertToURL(directoryname).toURI()); - - if (!in.canRead()) { + URL url_directory = convertToURL(directoryname); + File directory = FileUtil.getFileFromURL(url_directory); + String prefix = directory.getAbsolutePath(); + List files_list_ext = listf(directory); + + if (!directory.canRead()) { throw new Exception("Cannot read from input file: " - + in.getAbsolutePath()); + + directory.getAbsolutePath()); } - String prefix = directoryname; - if (directoryname.contains(".")) { - prefix = prefix.split("\\.",2)[0]; - } - - List files_list_ext = listf(directoryname); - + List uris = new ArrayList(); if (files_list_ext.isEmpty()) { throw new Exception("Could not find any files in the selected directory"); } @@ -177,25 +174,41 @@ protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) uris.add(new URIContent(file.toURI(), FilenameUtils.getExtension(file.toString()))); } - IPrefixURIPortObject uri_prefix_object = null; + PrefixURIPortObject uri_prefix_object = null; uri_prefix_object = new PrefixURIPortObject(uris, prefix); return new PortObject[] { (URIPortObject) uri_prefix_object }; } - public static List listf(String directoryName) { - File directory = new File(directoryName); + + /** + * List files in a given directory + * + * @param dirctory + * Directory file handle. + * @return list + * List of Files. + * @throws Exception + * If there are no files found in the given directory. + */ + public static List listf(File directory) + throws Exception { List resultList = new ArrayList(); // get all the files from a directory - File[] fList = directory.listFiles(); + File[] fList; + if (directory.list() == null) { + throw new Exception("Could not find any files in the selected directory ".concat(directory.getAbsolutePath())); + } + else { + fList = directory.listFiles(); + } + resultList.addAll(Arrays.asList(fList)); for (File file : fList) { - if (file.isFile()) { - System.out.println(file.getAbsolutePath()); - } else if (file.isDirectory()) { - resultList.addAll(listf(file.getAbsolutePath())); + if (file.isDirectory()) { + resultList.addAll(listf(file)); } } From 059347b8b121ea9f16eed975e6b1aea758821db2 Mon Sep 17 00:00:00 2001 From: Kerstin Neubert Date: Tue, 22 Oct 2019 13:20:26 +0200 Subject: [PATCH 2/2] use PrefixURIPortObject --- .../knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java | 1 - 1 file changed, 1 deletion(-) diff --git a/com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java b/com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java index 49a16495..943e549a 100644 --- a/com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java +++ b/com.genericworkflownodes.knime/src/com/genericworkflownodes/knime/nodes/io/dirloader/DirectoryLoaderNodeModel.java @@ -43,7 +43,6 @@ import org.knime.core.node.port.PortType; import org.knime.core.util.FileUtil; -//import com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject; import com.genericworkflownodes.knime.base.data.port.PrefixURIPortObject; /**