Search in sources :

Example 1 with InstanceValidator

use of eu.esdihumboldt.hale.common.instance.io.InstanceValidator in project hale by halestudio.

the class FileValidateTarget method addValidator.

private void addValidator(IOProviderDescriptor selection) {
    InstanceValidator validator;
    try {
        validator = (InstanceValidator) selection.createExtensionObject();
        // set schemas
        List<? extends Locatable> schemas = getWizard().getProvider().getValidationSchemas();
        validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
        getWizard().addValidator(validator);
        ValidatorEntry entry = new ValidatorEntry(validator, selection);
        validators.add(entry);
        updateWizard(getSelectedValidator());
    } catch (Exception e) {
        this.getPage().setErrorMessage("Could not instantiate validator.");
        log.error(MessageFormat.format("Could not instantiate validator {0}", selection.getIdentifier(), e));
    }
}
Also used : InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 2 with InstanceValidator

use of eu.esdihumboldt.hale.common.instance.io.InstanceValidator in project hale by halestudio.

the class ValidationJob method run.

/**
 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
 */
@Override
protected IStatus run(IProgressMonitor monitor) {
    boolean successful = true;
    for (InstanceValidator validator : this.validators) {
        IOReporter defaultReporter = validator.createReporter();
        defaultReporter.setSuccess(false);
        IOReport report = defaultReporter;
        try {
            ATransaction trans = log.begin(defaultReporter.getTaskName());
            try {
                if (writer != null) {
                    // set validation schemas (may have been determined only
                    // during writer execution)
                    // set schemas
                    List<? extends Locatable> schemas = writer.getValidationSchemas();
                    validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
                }
                validator.setServiceProvider(serviceProvider);
                IOReport result = validator.execute(new ProgressMonitorIndicator(monitor));
                if (result != null) {
                    report = result;
                } else {
                    defaultReporter.setSuccess(true);
                }
            } catch (Throwable e) {
                defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
            } finally {
                trans.end();
            }
        } catch (Throwable e) {
            defaultReporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
        }
        if (monitor.isCanceled()) {
            reset();
            return Status.CANCEL_STATUS;
        }
        // add report to report service
        reportHandler.publishReport(report);
        // show message to user
        if (report.isSuccess()) {
            // info message
            log.info(report.getSummary());
        } else {
            // error message
            log.error(report.getSummary());
            successful = false;
        }
    }
    reset();
    if (successful) {
        log.userInfo("All validations completed successfully.");
        return Status.OK_STATUS;
    } else {
        log.userError("There were validation failures. Please check the reports for details.");
        return ERROR_STATUS;
    }
}
Also used : InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) ProgressMonitorIndicator(eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator) ATransaction(de.fhg.igd.slf4jplus.ATransaction) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 3 with InstanceValidator

use of eu.esdihumboldt.hale.common.instance.io.InstanceValidator 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)

Example 4 with InstanceValidator

use of eu.esdihumboldt.hale.common.instance.io.InstanceValidator in project hale by halestudio.

the class ExecTransformation method setupValidators.

private void setupValidators() {
    if (context.getValidateProviderIds() != null && !context.getValidateProviderIds().isEmpty()) {
        for (int i = 0; i < context.getValidateProviderIds().size(); i++) {
            String validateProviderId = context.getValidateProviderIds().get(i);
            if (!validateProviderId.trim().isEmpty()) {
                final InstanceValidator validator = HaleIO.createIOProvider(InstanceValidator.class, null, validateProviderId);
                if (validator == null) {
                    throw fail("Instance validator with ID " + validateProviderId + " not found");
                }
                // load validator settings
                validator.loadConfiguration(context.getValidateSettings().get(i));
                // set schemas
                List<? extends Locatable> schemas = target.getValidationSchemas();
                validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
                // set source
                validator.setSource(new DefaultInputSupplier(context.getTarget()));
                // apply target content type
                validator.setContentType(target.getContentType());
                this.validators.add(validator);
            }
        }
    }
}
Also used : InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 5 with InstanceValidator

use of eu.esdihumboldt.hale.common.instance.io.InstanceValidator in project hale by halestudio.

the class IOReferenceContent method getInputStream.

@Override
public InputStream getInputStream(String pluginID, String href, Locale locale) {
    // it is an I/O provider reference
    if (href.startsWith(IO_PROVIDERS_TOPIC_PATH)) {
        String providerId = href.substring(IO_PROVIDERS_TOPIC_PATH.length());
        // strip everything after a ?
        int ind = providerId.indexOf('?');
        if (ind >= 0) {
            providerId = providerId.substring(0, ind);
        }
        // strip the .*htm? ending
        if (providerId.endsWith("html") || providerId.endsWith("htm")) {
            providerId = providerId.substring(0, providerId.lastIndexOf('.'));
        }
        try {
            return getIOProviderContent(providerId);
        } catch (Exception e) {
            log.error("Error creating instance io info page.", e);
            return null;
        }
    } else // should be an I/O provider overview by type
    if (href.startsWith(OVERVIEW_TOPIC_PATH)) {
        // extract provider type name
        String providerType = href.substring(OVERVIEW_TOPIC_PATH.length());
        // strip everything after a ?
        int ind = providerType.indexOf('?');
        if (ind >= 0) {
            providerType = providerType.substring(0, ind);
        }
        // strip the .*htm? ending
        if (providerType.endsWith("html") || providerType.endsWith("htm")) {
            providerType = providerType.substring(0, providerType.lastIndexOf('.'));
        }
        Class<? extends IOProvider> providerClass = null;
        switch(providerType) {
            case "InstanceReader":
                providerClass = InstanceReader.class;
                break;
            case "InstanceWriter":
                providerClass = InstanceWriter.class;
                break;
            case "InstanceValidator":
                providerClass = InstanceValidator.class;
                break;
        }
        if (providerClass != null) {
            final Class<? extends IOProvider> provider = providerClass;
            try {
                return getContentFromTemplate("overview." + providerType, TEMPLATE_OVERVIEW, new Callable<VelocityContext>() {

                    @Override
                    public VelocityContext call() throws Exception {
                        VelocityContext context = new VelocityContext();
                        // getProviderFactorries returns
                        // Collection<IOProviderDescriptor>
                        Collection<IOProviderDescriptor> writer = HaleIO.getProviderFactories(provider);
                        context.put("providers", writer);
                        context.put("providerType", provider.getSimpleName());
                        return context;
                    }
                });
            } catch (Exception e) {
                log.error("Error creating provider overview", e);
                return null;
            }
        }
        return null;
    }
    return null;
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) InstanceWriter(eu.esdihumboldt.hale.common.instance.io.InstanceWriter) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) VelocityContext(org.apache.velocity.VelocityContext) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) TransformerException(javax.xml.transform.TransformerException) Callable(java.util.concurrent.Callable)

Aggregations

InstanceValidator (eu.esdihumboldt.hale.common.instance.io.InstanceValidator)5 Locatable (eu.esdihumboldt.hale.common.core.io.supplier.Locatable)4 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)2 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)2 ATransaction (de.fhg.igd.slf4jplus.ATransaction)1 IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)1 ProgressMonitorIndicator (eu.esdihumboldt.hale.common.core.io.ProgressMonitorIndicator)1 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1 MultiLocationOutputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.MultiLocationOutputSupplier)1 InstanceReader (eu.esdihumboldt.hale.common.instance.io.InstanceReader)1 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)1 FileTarget (eu.esdihumboldt.hale.ui.io.target.FileTarget)1 ReportService (eu.esdihumboldt.hale.ui.service.report.ReportService)1 File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1