Search in sources :

Example 16 with URIContent

use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.

the class ListMimeFileImporterNodeModel method execute.

@Override
protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) throws Exception {
    String[] filenames = m_filenames.getStringArrayValue();
    List<URIContent> uris = new ArrayList<URIContent>();
    for (String filename : filenames) {
        File in = new File(convertToURL(filename).toURI());
        if (!in.canRead()) {
            throw new Exception("Cannot read from input file: " + in.getAbsolutePath());
        }
        uris.add(new URIContent(in.toURI(), (m_file_extension.isActive() ? m_file_extension.getStringValue() : MIMETypeHelper.getMIMEtypeExtension(filename))));
    }
    return new PortObject[] { new URIPortObject(uris) };
}
Also used : IURIPortObject(org.knime.core.data.uri.IURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) ArrayList(java.util.ArrayList) SettingsModelOptionalString(org.knime.core.node.defaultnodesettings.SettingsModelOptionalString) File(java.io.File) IURIPortObject(org.knime.core.data.uri.IURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) PortObject(org.knime.core.node.port.PortObject) URIContent(org.knime.core.data.uri.URIContent) MalformedURLException(java.net.MalformedURLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 17 with URIContent

use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.

the class ManglerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    // translate portobject to table
    BufferedDataTable table = (BufferedDataTable) inData[0];
    // create a file where we can write to
    FileStoreURIPortObject fsupo;
    fsupo = new FileStoreURIPortObject(exec.createFileStore("ManglerNodeModel"));
    // File file = fileStash.getFile(demangler.getMIMEType(), "mime");
    File file = fsupo.registerFile("mangled_file." + demangler.getMIMEType());
    // translate the filename to a URIContent
    URIContent outputURI = new URIContent(file.toURI(), demangler.getMIMEType());
    // write file
    demangler.mangle(table, outputURI.getURI());
    // create list
    List<URIContent> uriList = new ArrayList<URIContent>();
    uriList.add(outputURI);
    return new FileStoreURIPortObject[] { fsupo };
}
Also used : FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) ArrayList(java.util.ArrayList) File(java.io.File) URIContent(org.knime.core.data.uri.URIContent)

Example 18 with URIContent

use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.

the class OutputFileNodeModel method execute.

@Override
protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) throws Exception {
    IURIPortObject obj = (IURIPortObject) inObjects[0];
    List<URIContent> uris = obj.getURIContents();
    if (uris.size() == 0) {
        throw new Exception("There were no URIs in the supplied IURIPortObject at port 0");
    }
    String filename = m_filename.getStringValue();
    File in = new File(uris.get(0).getURI());
    File out = new File(filename);
    FileUtils.copyFile(in, out);
    data = Helper.readFileSummary(in, 50);
    return new PortObject[] {};
}
Also used : IURIPortObject(org.knime.core.data.uri.IURIPortObject) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) ZipFile(java.util.zip.ZipFile) File(java.io.File) IURIPortObject(org.knime.core.data.uri.IURIPortObject) PortObject(org.knime.core.node.port.PortObject) URIContent(org.knime.core.data.uri.URIContent) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 19 with URIContent

use of org.knime.core.data.uri.URIContent 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 20 with URIContent

use of org.knime.core.data.uri.URIContent 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)

Aggregations

URIContent (org.knime.core.data.uri.URIContent)21 File (java.io.File)13 ArrayList (java.util.ArrayList)13 IURIPortObject (org.knime.core.data.uri.IURIPortObject)13 IOException (java.io.IOException)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)10 URIPortObject (org.knime.core.data.uri.URIPortObject)6 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)6 PortObject (org.knime.core.node.port.PortObject)6 URI (java.net.URI)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 FileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)3 IPrefixURIPortObject (com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject)2 PortObjectHandlerCell (com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell)2 NoBinaryAvailableException (com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException)2 UnknownCommandGeneratorException (com.genericworkflownodes.knime.execution.UnknownCommandGeneratorException)2 UnknownToolExecutorException (com.genericworkflownodes.knime.execution.UnknownToolExecutorException)2 FileParameter (com.genericworkflownodes.knime.parameter.FileParameter)2 IFileParameter (com.genericworkflownodes.knime.parameter.IFileParameter)2 InvalidParameterValueException (com.genericworkflownodes.knime.parameter.InvalidParameterValueException)2