use of org.knime.base.filehandling.remote.files.RemoteFile in project GenericKnimeNodes by genericworkflownodes.
the class MimeDirectoryImporterNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
// Create connection monitor
final ConnectionMonitor<? extends Connection> monitor = new ConnectionMonitor<>();
// Create output URI container
final List<URIContent> uris = new ArrayList<URIContent>();
try {
URI directoryUri;
if (m_connectionInformation != null) {
exec.setProgress("Connecting to " + m_connectionInformation.toURI());
// Generate URI to the directory
directoryUri = new URI(m_connectionInformation.toURI().toString() + NodeUtils.encodePath(m_configuration.getDirectory()));
} else {
// Create local URI
directoryUri = new File(m_configuration.getDirectory()).toURI();
}
// Create remote file for directory selection
final RemoteFile<? extends Connection> file = RemoteFileFactory.createRemoteFile(directoryUri, m_connectionInformation, monitor);
// List the selected directory
exec.setProgress("Retrieving list of files");
listDirectory(file, uris, true, exec, new MutableInteger(0), new MutableInteger(0));
} finally {
// Close connections
monitor.closeAll();
}
return new PortObject[] { new URIPortObject(uris) };
}
use of org.knime.base.filehandling.remote.files.RemoteFile in project GenericKnimeNodes by genericworkflownodes.
the class MimeDirectoryImporterNodeModel method listDirectory.
/**
* List a directory.
*
* Writes the location of all files in a directory into the container. Files
* will be listed recursively if the option is selected.
*
* @param file The file or directory to be listed
* @param uris List of URIContents to write the reference of the files into
* @param root If this directory is the root directory
* @param exec Execution context to check if the execution has been canceled
* @throws Exception If remote file operation did not succeed
*/
private void listDirectory(final RemoteFile<? extends Connection> file, final List<URIContent> uris, final boolean root, final ExecutionContext exec, final MutableInteger processedEntries, final MutableInteger maxEntries) throws Exception {
// Check if the user canceled
exec.checkCanceled();
if (!root) {
final URI fileUri = file.getURI();
// URI to the file
final String extension = FilenameUtils.getExtension(fileUri.getPath());
final URIContent content = new URIContent(fileUri, extension);
// Add file information to the output
if (!file.isDirectory()) {
uris.add(content);
}
}
// If the source is a directory list inner files
if (file.isDirectory()) {
if (root || m_configuration.getRecursive()) {
final RemoteFile<? extends Connection>[] files = file.listFiles();
Arrays.sort(files);
final RemoteFile<? extends Connection>[] filteredFiles = filterFiles(files);
maxEntries.setValue(maxEntries.intValue() + filteredFiles.length);
exec.setMessage("Scanning " + file.getFullName());
for (final RemoteFile<? extends Connection> file2 : filteredFiles) {
listDirectory(file2, uris, false, exec, processedEntries, maxEntries);
processedEntries.inc();
exec.setProgress(processedEntries.intValue() / maxEntries.doubleValue());
}
}
}
}
use of org.knime.base.filehandling.remote.files.RemoteFile in project GenericKnimeNodes by genericworkflownodes.
the class MimeDirectoryImporterNodeModel method filterFiles.
/**
* @param files
* @return
*/
private RemoteFile<? extends Connection>[] filterFiles(final RemoteFile<? extends Connection>[] files) {
String extString = m_configuration.getExtensionsString();
String expString = m_configuration.getExpressionsString();
Filter filter = m_configuration.getFilter();
m_extension = extString;
switch(filter) {
case None:
break;
case RegExp:
// no break;
case Wildcards:
String patternS;
if (filter.equals(Filter.Wildcards)) {
patternS = WildcardMatcher.wildcardToRegex(expString);
} else {
patternS = expString;
}
if (m_configuration.isCaseSensitive()) {
m_regExpPattern = Pattern.compile(patternS);
} else {
m_regExpPattern = Pattern.compile(patternS, Pattern.CASE_INSENSITIVE);
}
break;
default:
throw new IllegalStateException("Unknown filter: " + filter);
}
m_analyzedFiles = 0;
m_currentRowID = 0;
List<RemoteFile<? extends Connection>> filteredFiles = new ArrayList<RemoteFile<? extends Connection>>();
for (RemoteFile<?> f : files) {
try {
if (f.isDirectory() || satisfiesFilter(f.getName())) {
filteredFiles.add(f);
}
} catch (Exception e) {
// catch or throw?
}
}
return filteredFiles.toArray(new RemoteFile[filteredFiles.size()]);
}
Aggregations