use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.
the class DynamicGenericNodeModel method transferIncomingPorts2Config.
/**
* Transfers the incoming ports into the config, that it can be written out
* into a config file or can be transferred to the command line.
*
* @param inData
* The incoming port objects.
* @throws Exception
*/
private void transferIncomingPorts2Config(PortObject[] inData) throws Exception {
// Transfer settings from the input ports into the configuration object
for (int i = 0; i < inData.length; i++) {
// find the internal port for this PortObject
Port port = m_nodeConfig.getInputPorts().get(i);
IURIPortObject po = (IURIPortObject) inData[i];
String name = port.getName();
// find the associated parameter in the configuration
Parameter<?> p = m_nodeConfig.getParameter(name);
boolean isMultiFile = port.isMultiFile();
boolean isPrefix = port.isPrefix();
// skip optional and unconnected inport ports
if (inData[i] == null) {
((FileParameter) p).setValue(null);
continue;
}
// connected: check contents
List<URIContent> uris = po.getURIContents();
// check validity of subtypes with actual inputs
if (uris.size() > 1 && (!isMultiFile && !isPrefix)) {
throw new Exception("IURIPortObject with multiple URIs supplied at single URI port #" + i);
}
// port
if (!(p instanceof IFileParameter)) {
throw new Exception("Invalid reference from port to non-file parameter. URI port #" + i);
}
if (isPrefix) {
// we pass only the prefix to the tool
IPrefixURIPortObject puri = (IPrefixURIPortObject) inData[i];
((FileParameter) p).setValue(puri.getPrefix());
} else if (isMultiFile) {
// we need to collect all filenames and then set them as a batch
// in the config
List<String> filenames = new ArrayList<String>();
for (URIContent uric : uris) {
URI uri = uric.getURI();
filenames.add(new File(uri).getAbsolutePath());
}
((FileListParameter) p).setValue(filenames);
} else {
// just one filename
URI uri = uris.get(0).getURI();
String filename = new File(uri).getAbsolutePath();
((FileParameter) p).setValue(filename);
}
}
}
use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.
the class GenericKnimeNodeModel method transferIncomingPorts2Config.
/**
* Transfers the incoming ports into the config, that it can be written out
* into a config file or can be transferred to the command line.
*
* @param inData
* The incoming port objects.
* @throws Exception
*/
private void transferIncomingPorts2Config(PortObject[] inData) throws Exception {
// Transfer settings from the input ports into the configuration object
for (int i = 0; i < inData.length; i++) {
// find the internal port for this PortObject
Port port = m_nodeConfig.getInputPorts().get(i);
IURIPortObject po = (IURIPortObject) inData[i];
String name = port.getName();
// find the associated parameter in the configuration
Parameter<?> p = m_nodeConfig.getParameter(name);
boolean isMultiFile = port.isMultiFile();
boolean isPrefix = port.isPrefix();
// skip optional and unconnected inport ports
if (inData[i] == null) {
p.setValue(null);
continue;
}
// connected: check contents
List<URIContent> uris = po.getURIContents();
// check validity of subtypes with actual inputs
if (uris.size() > 1 && (!isMultiFile && !isPrefix)) {
throw new Exception("IURIPortObject with multiple URIs supplied at single URI port #" + i);
}
// port
if (!(p instanceof IFileParameter)) {
throw new Exception("Invalid reference from port to non-file parameter. URI port #" + i);
}
if (isPrefix) {
// we pass only the prefix to the tool
IPrefixURIPortObject puri = (IPrefixURIPortObject) inData[i];
((FileParameter) p).setValue(puri.getPrefix());
} else if (isMultiFile) {
// we need to collect all filenames and then set them as a batch
// in the config
List<String> filenames = new ArrayList<String>();
for (URIContent uric : uris) {
URI uri = uric.getURI();
filenames.add(new File(uri).getAbsolutePath());
}
((FileListParameter) p).setValue(filenames);
} else {
// just one filename
URI uri = uris.get(0).getURI();
String filename = new File(uri).getAbsolutePath();
((FileParameter) p).setValue(filename);
}
}
}
use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.
the class PortToFileStoreNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
IURIPortObject input = (IURIPortObject) inData[0];
DataContainer dc = exec.createDataContainer(createSpec());
/**
* Files that are not yet managed by KNIME (e.g. when they come from an InputFiles node) come as URIPortObject
* and must be copied into a FileStore to be handled properly.
* Other files already have a file store and can be put into a <code>PortObjectHandlerCell</code>.
*/
if (input instanceof AbstractFileStoreURIPortObject) {
PortObjectHandlerCell cell = new PortObjectHandlerCell((AbstractFileStoreURIPortObject) input);
dc.addRowToTable(new DefaultRow(new RowKey("files"), cell));
} else {
FileStore fs = exec.createFileStore("files");
fs.getFile().mkdirs();
for (URIContent uc : input.getURIContents()) {
String filename = Paths.get(uc.getURI()).getFileName().toString();
if (!filename.endsWith(uc.getExtension())) {
filename = filename.concat(".").concat(uc.getExtension());
}
// TODO: Report progress
Files.copy(Paths.get(uc.getURI()), Paths.get(fs.getFile().toURI()).resolve(filename));
AbstractFileStoreURIPortObject portObject = new FileStoreURIPortObject(fs);
portObject.registerFile(filename);
PortObjectHandlerCell cell = new PortObjectHandlerCell(portObject);
dc.addRowToTable(new DefaultRow(new RowKey(filename), cell));
}
}
dc.close();
return new PortObject[] { (BufferedDataTable) dc.getTable() };
}
use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.
the class AbstractFileStoreURIPortObject method load.
/**
* Reconstruct the {@link AbstractFileStoreURIPortObject} from the given
* {@link ModelContentRO}.
*
* @param model
* The {@link ModelContentRO} from where the object should be
* reconstructed.
* @param spec
* The expected {@link PortObjectSpec}.
* @param exec
* The current {@link ExecutionContext}.
* @throws InvalidSettingsException
* Thrown if the content is invalid.
*/
void load(final ModelContentRO model, PortObjectSpec spec, ExecutionMonitor exec) throws InvalidSettingsException {
List<URIContent> list = new ArrayList<URIContent>();
List<String> relPathList = new ArrayList<String>();
for (String key : model.keySet()) {
if (key.startsWith("file-")) {
ModelContentRO child = model.getModelContent(key);
list.add(URIContent.load(child));
relPathList.add(child.getString(SETTINGS_KEY_REL_PATH));
}
}
m_uriContents = list;
m_relPaths = relPathList;
m_uriPortObjectSpec = (URIPortObjectSpec) spec;
}
use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.
the class AbstractFileStoreURIPortObject method registerFile.
/**
* Adds the given file to the {@link FileStoreURIPortObject}.
*
* @param filename
* The relative path that should be stored inside the file-store,
* e.g., outfile.txt or subfolder/outfile.txt.
* @return A {@link File} object pointing to the registered file.
*/
public File registerFile(String filename) {
// register the URIContent
File child = new File(getFileStoreRootDirectory(), filename);
URIContent uric = new URIContent(child.toURI(), MIMETypeHelper.getMIMEtypeExtension(filename));
// update content and spec accordingly
m_uriContents.add(uric);
m_uriPortObjectSpec = URIPortObjectSpec.create(m_uriContents);
m_relPaths.add(filename);
// give the file object to the client so he can work with it
return child;
}
Aggregations