Search in sources :

Example 1 with Connection

use of org.knime.base.filehandling.remote.files.Connection 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());
            }
        }
    }
}
Also used : Connection(org.knime.base.filehandling.remote.files.Connection) URI(java.net.URI) URIContent(org.knime.core.data.uri.URIContent) RemoteFile(org.knime.base.filehandling.remote.files.RemoteFile)

Example 2 with Connection

use of org.knime.base.filehandling.remote.files.Connection 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()]);
}
Also used : Filter(org.knime.base.node.io.listfiles.ListFiles.Filter) Connection(org.knime.base.filehandling.remote.files.Connection) ArrayList(java.util.ArrayList) RemoteFile(org.knime.base.filehandling.remote.files.RemoteFile) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Aggregations

Connection (org.knime.base.filehandling.remote.files.Connection)2 RemoteFile (org.knime.base.filehandling.remote.files.RemoteFile)2 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Filter (org.knime.base.node.io.listfiles.ListFiles.Filter)1 URIContent (org.knime.core.data.uri.URIContent)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1