Search in sources :

Example 6 with BufferedFileReader

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();
    }
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) BufferedFileReader(org.knime.base.node.util.BufferedFileReader) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) HashSet(java.util.HashSet)

Example 7 with BufferedFileReader

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.");
    }
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) BufferedFileReader(org.knime.base.node.util.BufferedFileReader) IOException(java.io.IOException)

Example 8 with BufferedFileReader

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() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) BufferedFileReader(org.knime.base.node.util.BufferedFileReader) IOException(java.io.IOException) URL(java.net.URL) StringCell(org.knime.core.data.def.StringCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

BufferedFileReader (org.knime.base.node.util.BufferedFileReader)8 Tokenizer (org.knime.core.util.tokenizer.Tokenizer)5 IOException (java.io.IOException)3 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)2 URL (java.net.URL)1 HashSet (java.util.HashSet)1 NoSuchElementException (java.util.NoSuchElementException)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 DataType (org.knime.core.data.DataType)1 RowKey (org.knime.core.data.RowKey)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 StringCell (org.knime.core.data.def.StringCell)1 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1