Search in sources :

Example 1 with RemoteFile

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) };
}
Also used : URIPortObject(org.knime.core.data.uri.URIPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) MutableInteger(org.knime.core.util.MutableInteger) ConnectionMonitor(org.knime.base.filehandling.remote.files.ConnectionMonitor) ArrayList(java.util.ArrayList) URI(java.net.URI) RemoteFile(org.knime.base.filehandling.remote.files.RemoteFile) File(java.io.File) URIPortObject(org.knime.core.data.uri.URIPortObject) ConnectionInformationPortObject(org.knime.base.filehandling.remote.connectioninformation.port.ConnectionInformationPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) PortObject(org.knime.core.node.port.PortObject) URIContent(org.knime.core.data.uri.URIContent)

Example 2 with RemoteFile

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());
            }
        }
    }
}
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 3 with RemoteFile

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()]);
}
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

RemoteFile (org.knime.base.filehandling.remote.files.RemoteFile)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Connection (org.knime.base.filehandling.remote.files.Connection)2 URIContent (org.knime.core.data.uri.URIContent)2 File (java.io.File)1 IOException (java.io.IOException)1 ConnectionInformationPortObject (org.knime.base.filehandling.remote.connectioninformation.port.ConnectionInformationPortObject)1 ConnectionMonitor (org.knime.base.filehandling.remote.files.ConnectionMonitor)1 Filter (org.knime.base.node.io.listfiles.ListFiles.Filter)1 IURIPortObject (org.knime.core.data.uri.IURIPortObject)1 URIPortObject (org.knime.core.data.uri.URIPortObject)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 PortObject (org.knime.core.node.port.PortObject)1 MutableInteger (org.knime.core.util.MutableInteger)1