Search in sources :

Example 1 with MultiLocationOutputSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.MultiLocationOutputSupplier in project hale by halestudio.

the class StreamGmlWriter method writeParts.

/**
 * Write the given {@link InstanceCollection}s to multiple files using the
 * configured target as a base file name.<br>
 * <br>
 * Parts can only be written if the configured target is a URI to a local
 * file. The output files will be named after the target file, amended by a
 * counter. If, for example, the configured target file name is
 * <code>output.gml</code>, the files created by this method will be called
 * <code>output.0001.gml</code>, <code>output.0002.gml</code>, etc.
 *
 * @param instanceCollections the parts to write
 * @param progress Progress indicator
 * @param reporter the reporter to use for the execution report
 * @throws IOException if an I/O operation fails
 * @see #setTarget(LocatableOutputSupplier)
 */
protected void writeParts(Iterator<InstanceCollection> instanceCollections, ProgressIndicator progress, IOReporter reporter) throws IOException {
    final URI location = getTarget().getLocation();
    if (location == null) {
        reporter.error("Cannot write multiple GML files: Output location unknown");
        return;
    }
    // Can only write multiple instance collection if target is a local file
    if (!"file".equals(location.getScheme())) {
        reporter.error("Cannot write multiple GML files: Target must be a local file");
        return;
    }
    Path origPath = Paths.get(location).normalize();
    String filename;
    String extension;
    Path targetFolder;
    if (origPath.toFile().isDirectory()) {
        reporter.error("Cannot write to a directory: Target must a file");
        return;
    // TODO Support writing to a directory; use parameter to specify
    // file name prefix.
    } else {
        targetFolder = origPath.getParent();
        filename = Files.getNameWithoutExtension(origPath.toString());
        extension = Files.getFileExtension(origPath.toString());
    }
    List<URI> filesWritten = new ArrayList<>();
    int i = 1;
    while (instanceCollections.hasNext()) {
        String targetFilename = String.format("%s%s%s.%04d.%s", targetFolder.toString(), File.separator, filename, i++, extension);
        File targetFile = new File(targetFilename);
        FileOutputStream out = new FileOutputStream(targetFile);
        InstanceCollection instances = instanceCollections.next();
        write(instances, out, progress, reporter);
        filesWritten.add(targetFile.toURI());
    }
    if (filesWritten.size() > 1) {
        setTarget(new MultiLocationOutputSupplier(filesWritten));
    } else if (!filesWritten.isEmpty()) {
        setTarget(new LocatableOutputSupplier<OutputStream>() {

            @Override
            public OutputStream getOutput() throws IOException {
                throw new UnsupportedOperationException();
            }

            @Override
            public URI getLocation() {
                return filesWritten.get(0);
            }
        });
    }
}
Also used : DefinitionPath(eu.esdihumboldt.hale.io.gml.writer.internal.geometry.DefinitionPath) Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) ArrayList(java.util.ArrayList) MultiLocationOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.MultiLocationOutputSupplier) URI(java.net.URI) File(java.io.File) LocatableOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.LocatableOutputSupplier)

Example 2 with MultiLocationOutputSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.MultiLocationOutputSupplier in project hale by halestudio.

the class InstanceExportWizard method performValidation.

/**
 * Run the configured validators on the exported instance. May be overriden
 * to customize validation process.
 *
 * @return true if all validations were successful
 */
protected boolean performValidation() {
    boolean success = true;
    for (InstanceValidator validator : validators) {
        // set schemas
        List<? extends Locatable> schemas = getProvider().getValidationSchemas();
        validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
        // set service provider
        validator.setServiceProvider(HaleUI.getServiceProvider());
        ExportTarget<?> exportTarget = getSelectTargetPage().getExportTarget();
        if (exportTarget instanceof FileTarget) {
            LocatableOutputSupplier<? extends OutputStream> target = getProvider().getTarget();
            List<String> fileNames = new ArrayList<>();
            if (target instanceof MultiLocationOutputSupplier) {
                for (URI location : ((MultiLocationOutputSupplier) target).getLocations()) {
                    if (!"file".equals(location.getScheme())) {
                        continue;
                    }
                    File targetFile = new File(location);
                    fileNames.add(targetFile.getAbsolutePath());
                }
            } else {
                fileNames.add(((FileTarget<?>) exportTarget).getTargetFileName());
            }
            for (String fileName : fileNames) {
                LocatableInputSupplier<? extends InputStream> source = new FileIOSupplier(new File(fileName));
                validator.setSource(source);
                validator.setContentType(getContentType());
                IOReporter defReport = validator.createReporter();
                // validate and execute provider
                try {
                    // validate configuration
                    validator.validate();
                    IOReport report = execute(validator, defReport);
                    if (report != null) {
                        // add report to report server
                        ReportService repService = PlatformUI.getWorkbench().getService(ReportService.class);
                        repService.addReport(report);
                        if (report.isSuccess()) {
                            log.info(report.getSummary());
                        } else {
                            log.error(report.getSummary());
                            success = false;
                        }
                    }
                } catch (IOProviderConfigurationException e) {
                    log.error(MessageFormat.format("The validator '{0}' could not be executed", validator.getClass().getCanonicalName()), e);
                    success = false;
                }
            }
        } else {
            log.error("No input can be provided for validation (no file target)");
            success = false;
        }
    }
    if (success) {
        log.userInfo("All validations completed successfully.");
    } else {
        log.userError("There were validation failures. Please check the report for more details.");
    }
    return success;
}
Also used : InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) ArrayList(java.util.ArrayList) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) URI(java.net.URI) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) FileTarget(eu.esdihumboldt.hale.ui.io.target.FileTarget) ReportService(eu.esdihumboldt.hale.ui.service.report.ReportService) MultiLocationOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.MultiLocationOutputSupplier) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Aggregations

MultiLocationOutputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.MultiLocationOutputSupplier)2 File (java.io.File)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)1 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1 Locatable (eu.esdihumboldt.hale.common.core.io.supplier.Locatable)1 LocatableOutputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.LocatableOutputSupplier)1 InstanceValidator (eu.esdihumboldt.hale.common.instance.io.InstanceValidator)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 DefinitionPath (eu.esdihumboldt.hale.io.gml.writer.internal.geometry.DefinitionPath)1 FileTarget (eu.esdihumboldt.hale.ui.io.target.FileTarget)1 ReportService (eu.esdihumboldt.hale.ui.service.report.ReportService)1 FileOutputStream (java.io.FileOutputStream)1 Path (java.nio.file.Path)1