Search in sources :

Example 1 with CharsetToolkit

use of org.gephi.utils.CharsetToolkit in project gephi by gephi.

the class SpreadsheetUtils method configureCSVParser.

public static CSVParser configureCSVParser(File file, Character fieldSeparator, Charset charset, boolean withFirstRecordAsHeader) throws IOException {
    if (fieldSeparator == null) {
        fieldSeparator = ',';
    }
    CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(fieldSeparator).withEscape('\\').withIgnoreEmptyLines(true).withNullString("").withIgnoreSurroundingSpaces(true).withTrim(true);
    if (withFirstRecordAsHeader) {
        csvFormat = csvFormat.withFirstRecordAsHeader().withAllowMissingColumnNames(false).withIgnoreHeaderCase(false);
    } else {
        csvFormat = csvFormat.withHeader((String[]) null).withSkipHeaderRecord(false);
    }
    boolean hasBOM = false;
    try (FileInputStream is = new FileInputStream(file)) {
        CharsetToolkit charsetToolkit = new CharsetToolkit(is);
        hasBOM = charsetToolkit.hasUTF8Bom() || charsetToolkit.hasUTF16BEBom() || charsetToolkit.hasUTF16LEBom();
    } catch (IOException e) {
    // NOOP
    }
    FileInputStream fileInputStream = new FileInputStream(file);
    InputStreamReader is = new InputStreamReader(fileInputStream, charset);
    if (hasBOM) {
        try {
            is.read();
        } catch (IOException e) {
        // should never happen, as a file with no content
        // but with a BOM has at least one char
        }
    }
    return new CSVParser(is, csvFormat);
}
Also used : InputStreamReader(java.io.InputStreamReader) CharsetToolkit(org.gephi.utils.CharsetToolkit) CSVParser(org.apache.commons.csv.CSVParser) CSVFormat(org.apache.commons.csv.CSVFormat) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 2 with CharsetToolkit

use of org.gephi.utils.CharsetToolkit in project gephi by gephi.

the class ImportUtils method getTextReader.

/**
 * Returns a <code>LineNumberReader</code> for <code>inputStream</code>. The
 * stream must be a character stream. The charset is detected automatically.
 *
 * @param stream the stream that is to be read
 * @return a reader for the character stream
 * @throws IOException if the stream can't be read
 */
public static LineNumberReader getTextReader(InputStream stream) throws IOException {
    LineNumberReader reader;
    CharsetToolkit charsetToolkit = new CharsetToolkit(stream);
    reader = (LineNumberReader) charsetToolkit.getReader();
    return reader;
}
Also used : CharsetToolkit(org.gephi.utils.CharsetToolkit) LineNumberReader(java.io.LineNumberReader)

Example 3 with CharsetToolkit

use of org.gephi.utils.CharsetToolkit in project gephi by gephi.

the class ImporterSpreadsheetCSV method autoDetectCharset.

private void autoDetectCharset() {
    // Try to auto-detect the charset:
    try {
        FileInputStream is = new FileInputStream(file);
        CharsetToolkit charsetToolkit = new CharsetToolkit(is);
        charsetToolkit.setDefaultCharset(Charset.forName("UTF-8"));
        charset = charsetToolkit.getCharset();
    } catch (Exception ex) {
    }
}
Also used : CharsetToolkit(org.gephi.utils.CharsetToolkit) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 4 with CharsetToolkit

use of org.gephi.utils.CharsetToolkit in project gephi by gephi.

the class DesktopImportControllerUI method importFiles.

private void importFiles(final Reader[] readers, final FileImporter[] importers, FileObject[] fileObjects) {
    try {
        File[] files = new File[readers.length];
        Map<ImporterUI, List<FileImporter>> importerUIs = new HashMap<>();
        for (int i = 0; i < importers.length; i++) {
            FileImporter importer = importers[i];
            ImporterUI ui = controller.getUI(importer);
            if (ui != null) {
                List<FileImporter> l = importerUIs.get(ui);
                if (l == null) {
                    l = new ArrayList<>();
                    importerUIs.put(ui, l);
                }
                l.add(importer);
            }
            if (importer instanceof FileImporter.FileAware) {
                try (Reader reader = readers[i]) {
                    File file = null;
                    if (fileObjects != null) {
                        file = FileUtil.toFile(fileObjects[i]);
                    }
                    if (file == null) {
                        // There is no source file but the importer needs it, create temporary copy:
                        String fileName = "tmp_file" + 1;
                        String charset = "UTF-8";
                        if (fileObjects != null && fileObjects[i] != null) {
                            // Netbeans FileUtil.toFile bug returning null?? Try to recover:
                            fileName = fileObjects[i].getNameExt();
                            CharsetToolkit charsetToolkit = new CharsetToolkit(fileObjects[i].getInputStream());
                            charset = charsetToolkit.getCharset().name();
                        }
                        file = TempDirUtils.createTempDir().createFile(fileName);
                        try (FileOutputStream fos = new FileOutputStream(file)) {
                            FileUtil.copy(new ReaderInputStream(reader, charset), fos);
                        }
                    }
                    files[i] = file;
                    ((FileImporter.FileAware) importer).setFile(file);
                }
            }
        }
        for (Map.Entry<ImporterUI, List<FileImporter>> entry : importerUIs.entrySet()) {
            ImporterUI ui = entry.getKey();
            String title = NbBundle.getMessage(DesktopImportControllerUI.class, "DesktopImportControllerUI.file.ui.dialog.title", ui.getDisplayName());
            JPanel panel = ui.getPanel();
            FileImporter[] fi = (FileImporter[]) entry.getValue().toArray((FileImporter[]) Array.newInstance(entry.getValue().get(0).getClass(), 0));
            ui.setup(fi);
            if (panel != null) {
                final DialogDescriptor dd = new DialogDescriptor(panel, title);
                if (panel instanceof ValidationPanel) {
                    ValidationPanel vp = (ValidationPanel) panel;
                    vp.addChangeListener(new ChangeListener() {

                        @Override
                        public void stateChanged(ChangeEvent e) {
                            dd.setValid(!((ValidationPanel) e.getSource()).isProblem());
                        }
                    });
                }
                Object result = DialogDisplayer.getDefault().notify(dd);
                if (!result.equals(NotifyDescriptor.OK_OPTION)) {
                    ui.unsetup(false);
                    return;
                }
            }
            if (ui instanceof ImporterUI.WithWizard) {
                boolean finishedOk = showWizard(ui, ((ImporterUI.WithWizard) ui).getWizardDescriptor());
                if (!finishedOk) {
                    ui.unsetup(false);
                    return;
                }
            }
            ui.unsetup(true);
        }
        final List<Container> results = new ArrayList<>();
        for (int i = 0; i < importers.length; i++) {
            doImport(results, readers[i], files[i], importers[i]);
        }
        executor.execute(null, new Runnable() {

            @Override
            public void run() {
                if (!results.isEmpty()) {
                    finishImport(results.toArray(new Container[0]));
                }
            }
        });
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }
}
Also used : ImporterUI(org.gephi.io.importer.spi.ImporterUI) JPanel(javax.swing.JPanel) HashMap(java.util.HashMap) CharsetToolkit(org.gephi.utils.CharsetToolkit) FileImporter(org.gephi.io.importer.spi.FileImporter) ArrayList(java.util.ArrayList) Reader(java.io.Reader) Container(org.gephi.io.importer.api.Container) List(java.util.List) ArrayList(java.util.ArrayList) ChangeListener(javax.swing.event.ChangeListener) ValidationPanel(org.netbeans.validation.api.ui.ValidationPanel) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) ReaderInputStream(org.openide.util.io.ReaderInputStream) ChangeEvent(javax.swing.event.ChangeEvent) FileOutputStream(java.io.FileOutputStream) DialogDescriptor(org.openide.DialogDescriptor) FileObject(org.openide.filesystems.FileObject) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CharsetToolkit (org.gephi.utils.CharsetToolkit)4 IOException (java.io.IOException)3 FileInputStream (java.io.FileInputStream)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 Reader (java.io.Reader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 JPanel (javax.swing.JPanel)1 ChangeEvent (javax.swing.event.ChangeEvent)1 ChangeListener (javax.swing.event.ChangeListener)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVParser (org.apache.commons.csv.CSVParser)1 Container (org.gephi.io.importer.api.Container)1 FileImporter (org.gephi.io.importer.spi.FileImporter)1