Search in sources :

Example 1 with InputSource

use of au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource in project constellation by constellation-app.

the class DelimitedSourcePane method addFile.

private void addFile(final DelimitedImportController importController) {
    final FileChooser fileChooser = new FileChooser();
    fileChooser.setInitialDirectory(defaultDirectory);
    final ImportFileParser parser = DelimitedSourcePane.this.importFileParserComboBox.getSelectionModel().getSelectedItem();
    if (parser != null) {
        final ExtensionFilter extensionFilter = parser.getExtensionFilter();
        if (extensionFilter != null) {
            fileChooser.getExtensionFilters().add(extensionFilter);
            fileChooser.setSelectedExtensionFilter(extensionFilter);
        }
    }
    final List<File> newFiles = fileChooser.showOpenMultipleDialog(DelimitedSourcePane.this.getScene().getWindow());
    if (newFiles != null) {
        if (!newFiles.isEmpty()) {
            defaultDirectory = newFiles.get(0).getParentFile();
            DelimitedSourcePane.this.importFileParserComboBox.setDisable(true);
        }
        final ObservableList<File> files = FXCollections.observableArrayList(fileListView.getItems());
        final StringBuilder sb = new StringBuilder();
        final String alertText = "The following files could not be parsed and have been excluded from the import set:\n";
        sb.append(alertText);
        for (final File file : newFiles) {
            // prevent adding the same file again
            if (fileAlreadyAdded(file)) {
                continue;
            }
            // Attempt to parse/preview, if a failure is detected don't add the file to the set of files to import.
            try {
                if (parser != null) {
                    parser.preview(new InputSource(file), null, PREVIEW_LIMIT);
                    files.add(file);
                }
            } catch (final IOException ex) {
                // Append the name of each file that could not be imported.
                sb.append("\n");
                sb.append(file.getName());
                LOGGER.log(Level.INFO, "Unable to parse the file {0}, " + "excluding from import set.", new Object[] { file.toString() });
                LOGGER.log(Level.WARNING, ex.toString());
            }
        }
        // If file names have been appended to sb, then some files could not be imported, so notify user.
        if (!sb.toString().equals(alertText)) {
            NotifyDisplayer.displayAlert("Import from File", "Invalid file(s) found", sb.toString(), Alert.AlertType.WARNING);
        }
        fileListView.setItems(files);
        if (!newFiles.isEmpty()) {
            fileListView.getSelectionModel().select(newFiles.get(0));
            fileListView.requestFocus();
        }
        final ObservableList<File> selectedFiles = fileListView.getSelectionModel().getSelectedItems();
        importController.setFiles(files, selectedFiles.isEmpty() ? null : selectedFiles.get(0));
        importController.validateFileStructure(newFiles);
    }
}
Also used : InputSource(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource) ImportFileParser(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.ImportFileParser) ExtensionFilter(javafx.stage.FileChooser.ExtensionFilter) FileChooser(javafx.stage.FileChooser) IOException(java.io.IOException) File(java.io.File)

Example 2 with InputSource

use of au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource in project constellation by constellation-app.

the class JSONImportFielParserNGTest method checkPreviewEmptyList2.

@Test
public void checkPreviewEmptyList2() throws InterruptedException {
    // Confirm that attempts to preview JSON containing a list containing
    // only empty lists return a clean IOException exception.
    final JSONImportFileParser parser = new JSONImportFileParser();
    try {
        parser.preview(new InputSource(new File(this.getClass().getResource("./resources/JSON-emptyList2.json").getFile())), null, 100);
        Assert.fail("Expected exception not received");
    } catch (IOException ex) {
        Assert.assertTrue(ex.getMessage().contains(private_noValidListMsg));
    } catch (Exception ex) {
        Assert.fail("Unexpected exception received: " + ex.getClass().getName());
    }
}
Also used : InputSource(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource) JSONImportFileParser(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.JSONImportFileParser) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 3 with InputSource

use of au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource in project constellation by constellation-app.

the class JSONImportFielParserNGTest method checkPreviewInvalidJSON.

@Test
public void checkPreviewInvalidJSON() throws InterruptedException {
    // Confirm that attempts to preview invalid JSON return a clean
    // IOException exception.
    final JSONImportFileParser parser = new JSONImportFileParser();
    try {
        parser.preview(new InputSource(new File(this.getClass().getResource("./resources/JSON-invalidContent.json").getFile())), null, 100);
        Assert.fail("Expected exception not received");
    } catch (IOException ex) {
        Assert.assertTrue(ex.getMessage().contains(private_invalidJSONMsg));
    } catch (Exception ex) {
        Assert.fail("Unexpected exception received: " + ex.getClass().getName());
    }
}
Also used : InputSource(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource) JSONImportFileParser(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.JSONImportFileParser) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 4 with InputSource

use of au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource in project constellation by constellation-app.

the class JSONImportFielParserNGTest method checkParseEmptyList3.

@Test
public void checkParseEmptyList3() throws InterruptedException {
    final JSONImportFileParser parser = new JSONImportFileParser();
    try {
        parser.parse(new InputSource(new File(this.getClass().getResource("./resources/JSON-emptyList3.json").getFile())), null);
        Assert.fail("Expected exception not received");
    } catch (IOException ex) {
        Assert.assertTrue(ex.getMessage().contains(private_noValidListMsg));
    } catch (Exception ex) {
        Assert.fail("Unexpected exception received: " + ex.getClass().getName());
    }
}
Also used : InputSource(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource) JSONImportFileParser(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.JSONImportFileParser) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 5 with InputSource

use of au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource in project constellation by constellation-app.

the class JSONImportFielParserNGTest method checkPreviewEmptyObject.

@Test
public void checkPreviewEmptyObject() throws InterruptedException {
    // Confirm that attempts to preview JSON containing only an empty object
    // return a clean IOException exception.
    final JSONImportFileParser parser = new JSONImportFileParser();
    try {
        parser.preview(new InputSource(new File(this.getClass().getResource("./resources/JSON-emptyObject.json").getFile())), null, 100);
        Assert.fail("Expected exception not received");
    } catch (IOException ex) {
        Assert.assertTrue(ex.getMessage().contains(private_noValidListMsg));
    } catch (Exception ex) {
        Assert.fail("Unexpected exception received: " + ex.getClass().getName());
    }
}
Also used : InputSource(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource) JSONImportFileParser(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.JSONImportFileParser) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Aggregations

InputSource (au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.InputSource)25 File (java.io.File)25 IOException (java.io.IOException)25 Test (org.testng.annotations.Test)23 JSONImportFileParser (au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.JSONImportFileParser)22 ArrayList (java.util.ArrayList)7 ImportFileParser (au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.ImportFileParser)2 VertexListInclusionGraph (au.gov.asd.tac.constellation.plugins.arrangements.VertexListInclusionGraph)1 ImportDefinition (au.gov.asd.tac.constellation.plugins.importexport.ImportDefinition)1 CSVImportFileParser (au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.CSVImportFileParser)1 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)1 FileNotFoundException (java.io.FileNotFoundException)1 List (java.util.List)1 FileChooser (javafx.stage.FileChooser)1 ExtensionFilter (javafx.stage.FileChooser.ExtensionFilter)1