Search in sources :

Example 1 with CsvReader

use of com.csvreader.CsvReader in project gephi by gephi.

the class ImportCSVUIVisualPanel1 method refreshPreviewTable.

public void refreshPreviewTable() {
    if (selectedFile != null && selectedFile.exists()) {
        try {
            CsvReader reader = new CsvReader(new FileInputStream(selectedFile), getSelectedSeparator(), getSelectedCharset());
            reader.setTrimWhitespace(false);
            String[] headers;
            try {
                reader.readHeaders();
                headers = reader.getHeaders();
            } catch (Exception ex) {
                //Some charsets can be problematic with unreal columns lenght. Don't show table when there are problems
                headers = new String[0];
            }
            columnCount = headers.length;
            //Check for repeated column names:
            Set<String> columnNamesSet = new HashSet<>();
            hasColumnNamesRepeated = false;
            hasSourceNodeColumn = false;
            hasTargetNodeColumn = false;
            int sourceColumnIndex = 0, targetColumnIndex = 0, currentColumn = 0;
            for (String header : headers) {
                if (header.equalsIgnoreCase("source")) {
                    hasSourceNodeColumn = true;
                    sourceColumnIndex = currentColumn;
                }
                if (header.equalsIgnoreCase("target")) {
                    hasTargetNodeColumn = true;
                    targetColumnIndex = currentColumn;
                }
                if (columnNamesSet.contains(header)) {
                    hasColumnNamesRepeated = true;
                    break;
                }
                columnNamesSet.add(header);
                currentColumn++;
            }
            ArrayList<String[]> records = new ArrayList<>();
            hasRowsMissingSourcesOrTargets = false;
            ImportCSVUIWizardAction.Mode mode = getMode();
            if (columnCount > 0) {
                String[] currentRecord;
                while (reader.readRecord()) {
                    int recordColumnCount = reader.getColumnCount();
                    currentRecord = new String[recordColumnCount];
                    for (int i = 0; i < currentRecord.length; i++) {
                        currentRecord[i] = reader.get(i);
                    }
                    // Search for missing source or target columns for edges table
                    if (mode == ImportCSVUIWizardAction.Mode.EDGES_TABLE) {
                        if (recordColumnCount < sourceColumnIndex || currentRecord[sourceColumnIndex].trim().isEmpty() || recordColumnCount < targetColumnIndex || currentRecord[targetColumnIndex].trim().isEmpty()) {
                            hasRowsMissingSourcesOrTargets = true;
                        }
                    }
                    if (records.size() < MAX_ROWS_PREVIEW) {
                        records.add(currentRecord);
                    }
                }
            }
            reader.close();
            final String[] columnNames = headers;
            final String[][] values = records.toArray(new String[0][]);
            previewTable.setModel(new TableModel() {

                @Override
                public int getRowCount() {
                    return values.length;
                }

                @Override
                public int getColumnCount() {
                    return columnNames.length;
                }

                @Override
                public String getColumnName(int columnIndex) {
                    return columnNames[columnIndex];
                }

                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return String.class;
                }

                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return false;
                }

                @Override
                public Object getValueAt(int rowIndex, int columnIndex) {
                    if (values[rowIndex].length > columnIndex) {
                        return values[rowIndex][columnIndex];
                    } else {
                        return null;
                    }
                }

                @Override
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                }

                @Override
                public void addTableModelListener(TableModelListener l) {
                }

                @Override
                public void removeTableModelListener(TableModelListener l) {
                }
            });
        } catch (FileNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, getMessage("ImportCSVUIVisualPanel1.validation.error"), getMessage("ImportCSVUIVisualPanel1.validation.file-permissions-error"), JOptionPane.ERROR_MESSAGE);
        }
    }
    wizard1.fireChangeEvent();
    //To fire validation panel messages.
    pathTextField.setText(pathTextField.getText());
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CsvReader(com.csvreader.CsvReader) TableModelListener(javax.swing.event.TableModelListener) TableModel(javax.swing.table.TableModel) HashSet(java.util.HashSet)

Example 2 with CsvReader

use of com.csvreader.CsvReader in project BachelorPraktikum by lucasbuschlinger.

the class CSVImporter method getValidatedCreader.

/*HACK: uses filenameforlogging instead of fileInputStream*/
/**
 * Method used to detect the valid delimiter by trying to validate the header using a given delimiter.
 *
 * @param delimiter   the delimiters to use to read the file
 * @param file        the file to read
 * @param metaEntries placeholder for future extensions
 * @return a CsvReader pointing to the headers, null if the headers could not be validated
 * @throws IOException if file reading goes wrong
 */
private CsvReader getValidatedCreader(final char delimiter, final String file, final List<String[]> metaEntries) throws IOException {
    // open file
    CsvReader creader = new CsvReader(file, delimiter, Charset.forName("UTF-8"));
    do {
        if (!creader.readHeaders()) {
            LOG.log(Level.FINEST, "automatic delimiter detection detected invalid delimiter: " + delimiter);
            metaEntries.clear();
            return null;
        }
        metaEntries.add(creader.getHeaders());
    } while (!validator.validateHeader(creader.getHeaders()));
    // remove valid header
    metaEntries.remove(metaEntries.size() - 1);
    LOG.log(Level.INFO, "automatic delimiter detection detected valid delimiter: " + delimiter);
    return creader;
}
Also used : CsvReader(com.csvreader.CsvReader)

Example 3 with CsvReader

use of com.csvreader.CsvReader in project BachelorPraktikum by lucasbuschlinger.

the class CSVImporter method processImport.

/**
 * {@inheritDoc}
 */
public List<VaultEntry> processImport(final InputStream fileInputStream, final String filenameForLogging) throws Exception {
    List<VaultEntry> importedData = new ArrayList<>();
    final int maxProgress = 100;
    // This list is used as a placeholder for future extensions
    List<String[]> metaEntries = new ArrayList<>();
    this.notifyStatus(0, "Reading Header");
    CsvReader creader = null;
    if (getDelimiter() == AUTO_DELIMITER) {
        // try to detect the delimiter by trial and error
        LOG.log(Level.INFO, "using automatic delimiter detection");
        char[] delimiterList = { ',', ';', '\t' };
        for (char delimiter : delimiterList) {
            creader = getValidatedCreader(delimiter, filenameForLogging, metaEntries);
            if (null != creader) {
                setDelimiter(delimiter);
                break;
            }
        }
    } else {
        // use the delimiter that was set
        creader = getValidatedCreader(getDelimiter(), filenameForLogging, metaEntries);
    }
    if (creader == null) {
        // header could not be validated
        LOG.log(Level.SEVERE, "No valid header found in File: " + filenameForLogging);
        throw new Exception("No valid header found in File: " + filenameForLogging);
    }
    // read entries
    while (creader.readRecord()) {
        /*here the method template is used to process all records */
        List<VaultEntry> entryList = parseEntry(creader);
        if (entryList != null && !entryList.isEmpty()) {
            importedData.addAll(entryList);
        }
    }
    this.notifyStatus(maxProgress, "Done importing all entries");
    return importedData;
}
Also used : CsvReader(com.csvreader.CsvReader) VaultEntry(de.opendiabetes.vault.container.VaultEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 4 with CsvReader

use of com.csvreader.CsvReader in project components by Talend.

the class BulkResultSetTest method testResultSet.

@Test
public void testResultSet() throws IOException {
    final int recordCount = 100;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter csvWriter = new CsvWriter(new BufferedOutputStream(out), ',', Charset.forName("UTF-8"));
    for (int i = 0; i < recordCount; i++) {
        csvWriter.writeRecord(new String[] { "fieldValueA" + i, "fieldValueB" + i, "fieldValueC" + i });
    }
    csvWriter.close();
    CsvReader csvReader = new CsvReader(new BufferedInputStream(new ByteArrayInputStream(out.toByteArray())), ',', Charset.forName("UTF-8"));
    BulkResultSet resultSet = new BulkResultSet(csvReader, Arrays.asList("fieldA", "fieldB", "fieldC"));
    int count = 0;
    BulkResult result;
    while ((result = resultSet.next()) != null) {
        assertEquals("fieldValueA" + count, result.getValue("fieldA"));
        assertEquals("fieldValueB" + count, result.getValue("fieldB"));
        assertEquals("fieldValueC" + count, result.getValue("fieldC"));
        count++;
    }
    assertEquals(recordCount, count);
}
Also used : CsvReader(com.csvreader.CsvReader) BulkResultSet(org.talend.components.salesforce.runtime.BulkResultSet) CsvWriter(com.csvreader.CsvWriter) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) BulkResult(org.talend.components.salesforce.runtime.BulkResult) Test(org.junit.Test)

Example 5 with CsvReader

use of com.csvreader.CsvReader in project components by Talend.

the class BulkResultSetTest method testResultSetIOError.

@Test(expected = IOException.class)
public void testResultSetIOError() throws IOException {
    InputStream in = mock(InputStream.class);
    doThrow(new IOException("I/O ERROR")).when(in).read();
    when(in.read(any(byte[].class))).thenThrow(new IOException("I/O ERROR"));
    CsvReader csvReader = new CsvReader(in, ',', Charset.forName("UTF-8"));
    BulkResultSet resultSet = new BulkResultSet(csvReader, Arrays.asList("fieldA", "fieldB", "fieldC"));
    while (resultSet.next() != null) {
    }
}
Also used : CsvReader(com.csvreader.CsvReader) BulkResultSet(org.talend.components.salesforce.runtime.BulkResultSet) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

CsvReader (com.csvreader.CsvReader)24 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)8 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 HashSet (java.util.HashSet)3 Column (org.gephi.graph.api.Column)3 Metadata (org.hisp.dhis.dxf2.metadata.Metadata)3 CsvWriter (com.csvreader.CsvWriter)2 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Optional (java.util.Optional)2 Supplier (java.util.function.Supplier)2