use of org.knime.base.node.util.BufferedFileReader in project knime-core by knime.
the class FixedWidthFRNodeDialog method colHeaderSettingsChanged.
/**
* save new column header checkbox settings into nodeSettings object and update dialog.
*/
protected void colHeaderSettingsChanged() {
if (!m_loadSettings) {
boolean selected = m_hasColHeaders.isSelected();
if (selected && m_nodeSettings.getFileLocation() != null) {
// set with column names used - for faster uniquification
Set<String> colNames = new HashSet<String>();
try (BufferedFileReader fr = m_nodeSettings.createNewInputReader()) {
FixedWidthTokenizer tokenizer = new FixedWidthTokenizer(fr, m_nodeSettings);
if (tokenizer.nextToken() != null) {
if (!m_nodeSettings.getHasRowHeader()) {
tokenizer.pushBack();
}
} else {
throw new NoSuchElementException("The row iterator proceeded beyond the last line of '" + m_nodeSettings.getFileLocation().toString() + "'. File is probably empty.");
}
DataColumnSpecCreator c;
String name = null;
for (int i = 0; i < m_nodeSettings.getNumberOfIncludedColumns() - 1; i++) {
String prefix = tokenizer.nextToken();
name = prefix;
int count = 2;
while (colNames.contains(name)) {
name = prefix + "(" + count++ + ")";
}
colNames.add(name);
if (!tokenizer.getReachedEndOfLine() && name != null) {
int includedIdx = m_nodeSettings.getColIdxIncluded(i);
c = new DataColumnSpecCreator(name, m_nodeSettings.getColPropertyAt(includedIdx).getColSpec().getType());
m_nodeSettings.getColPropertyAt(includedIdx).setColSpec(c.createSpec());
}
}
m_nodeSettings.setHasColHeader(selected);
} catch (IOException e) {
setErrorLabelText("Could not create new tokenizer to read column headers.", e.getMessage());
}
} else {
m_nodeSettings.setHasColHeader(selected);
}
updateColPropTable();
updateEnables();
updatePreview();
}
}
use of org.knime.base.node.util.BufferedFileReader in project knime-core by knime.
the class FixedWidthFRSettings method checkSettings.
/**
* checks settings.
*
* @throws InvalidSettingsException exception thrown if settings are invalid
*/
public void checkSettings() throws InvalidSettingsException {
CheckUtils.checkSetting(m_fileLocation != null, "No location set");
if (m_fileLocation != null) {
try {
BufferedFileReader f = createNewInputReader();
f.close();
} catch (IOException ioe) {
throw new InvalidSettingsException("Can't read from file '" + m_fileLocation + "'.");
} catch (NullPointerException npe) {
LOGGER.error("Filelocation is null.", npe);
}
}
if (getNumberOfIncludedColumns() > getNumberOfColumns()) {
throw new InvalidSettingsException("Looks like an internal error. More columns included than actual columns available.");
}
if (getNumberOfColumns() < 1) {
throw new InvalidSettingsException("No columns configured.");
}
}
use of org.knime.base.node.util.BufferedFileReader in project knime-core by knime.
the class LineReaderNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
DataTableSpec spec = createOutputSpec();
URL url = m_config.getURL();
BufferedDataContainer container = exec.createDataContainer(spec);
BufferedFileReader fileReader = BufferedFileReader.createNewReader(url);
long fileSize = fileReader.getFileSize();
int currentRow = 0;
final int limitRows = m_config.getLimitRowCount();
final boolean isSkipEmpty = m_config.isSkipEmptyLines();
final boolean matchRegex = !"".equals(m_config.getRegex());
String line;
String rowPrefix = m_config.getRowPrefix();
try {
while ((line = fileReader.readLine()) != null) {
String progMessage = "Reading row " + (currentRow + 1);
if (fileSize > 0) {
long numberOfBytesRead = fileReader.getNumberOfBytesRead();
double prog = (double) numberOfBytesRead / fileSize;
exec.setProgress(prog, progMessage);
} else {
exec.setMessage(progMessage);
}
exec.checkCanceled();
if (isSkipEmpty && line.trim().length() == 0) {
// do not increment currentRow
continue;
}
if (matchRegex && !line.matches(m_config.getRegex())) {
// do not increment currentRow
continue;
}
if (limitRows > 0 && currentRow >= limitRows) {
setWarningMessage("Read only " + limitRows + " row(s) due to user settings.");
break;
}
RowKey key = new RowKey(rowPrefix + (currentRow++));
DefaultRow row = new DefaultRow(key, new StringCell(line));
container.addRowToTable(row);
}
} finally {
container.close();
try {
fileReader.close();
} catch (IOException ioe2) {
// ignore
}
}
return new BufferedDataTable[] { container.getTable() };
}
Aggregations