|
41 | 41 | import org.knime.core.node.port.PortObject; |
42 | 42 | import org.knime.core.node.port.PortObjectSpec; |
43 | 43 | import org.knime.core.node.port.PortType; |
| 44 | +import org.knime.core.util.FileUtil; |
44 | 45 |
|
45 | | -import com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject; |
46 | 46 | import com.genericworkflownodes.knime.base.data.port.PrefixURIPortObject; |
47 | 47 |
|
48 | 48 | /** |
@@ -155,47 +155,59 @@ protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) |
155 | 155 | throws Exception { |
156 | 156 |
|
157 | 157 | 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()) { |
162 | 164 | throw new Exception("Cannot read from input file: " |
163 | | - + in.getAbsolutePath()); |
| 165 | + + directory.getAbsolutePath()); |
164 | 166 | } |
165 | 167 |
|
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>(); |
173 | 169 | if (files_list_ext.isEmpty()) { |
174 | 170 | throw new Exception("Could not find any files in the selected directory"); |
175 | 171 | } |
176 | 172 | for (File file : files_list_ext) { |
177 | 173 | uris.add(new URIContent(file.toURI(), FilenameUtils.getExtension(file.toString()))); |
178 | 174 | } |
179 | 175 |
|
180 | | - IPrefixURIPortObject uri_prefix_object = null; |
| 176 | + PrefixURIPortObject uri_prefix_object = null; |
181 | 177 | uri_prefix_object = new PrefixURIPortObject(uris, prefix); |
182 | 178 |
|
183 | 179 | return new PortObject[] { (URIPortObject) uri_prefix_object }; |
184 | 180 | } |
185 | 181 |
|
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 { |
188 | 195 |
|
189 | 196 | List<File> resultList = new ArrayList<File>(); |
190 | 197 |
|
191 | 198 | // 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 | + |
193 | 207 | resultList.addAll(Arrays.asList(fList)); |
194 | 208 | 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)); |
199 | 211 | } |
200 | 212 | } |
201 | 213 |
|
|
0 commit comments