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));
}
}
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;
}
}
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;
}
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);
}
}
}
}
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;
}
Aggregations