use of eu.esdihumboldt.hale.common.core.io.report.IOReport in project hale by halestudio.
the class AbstractHandlerTest method loadXMLInstances.
/**
* Load an instance collection from a GML file.
*
* @param schemaLocation the GML application schema location
* @param xmlLocation the GML file location
* @param interpolConfig the interpolation configuration
* @return the instance collection
* @throws IOException if reading schema or instances failed
* @throws IOProviderConfigurationException if the I/O providers were not
* configured correctly
*/
public static InstanceCollection loadXMLInstances(URI schemaLocation, URI xmlLocation, @Nullable ReaderConfiguration interpolConfig) throws IOException, IOProviderConfigurationException {
SchemaReader reader = new XmlSchemaReader();
reader.setSharedTypes(null);
reader.setSource(new DefaultInputSupplier(schemaLocation));
IOReport schemaReport = reader.execute(null);
assertTrue(schemaReport.isSuccess());
Schema sourceSchema = reader.getSchema();
InstanceReader instanceReader = new GmlInstanceReader();
instanceReader.setSource(new DefaultInputSupplier(xmlLocation));
instanceReader.setSourceSchema(sourceSchema);
if (interpolConfig != null) {
interpolConfig.apply(instanceReader);
}
IOReport instanceReport = instanceReader.execute(null);
assertTrue(instanceReport.isSuccess());
return instanceReader.getInstances();
}
use of eu.esdihumboldt.hale.common.core.io.report.IOReport in project hale by halestudio.
the class ProjectServiceImpl method save.
/**
* @see ProjectService#save()
*/
@Override
public void save() {
File projectFile;
IOConfiguration saveConfig;
synchronized (this) {
projectFile = this.projectFile;
saveConfig = main.getSaveConfiguration();
}
if (projectFile != null || canSaveTo(projectLocation)) {
Collection<IOProviderDescriptor> providers = HaleIO.getProviderFactories(ProjectWriter.class);
// use configuration from previous save if possible
if (saveConfig != null) {
// get provider ...
ProjectWriter writer = null;
for (IOProviderDescriptor factory : providers) {
if (factory.getIdentifier().equals(saveConfig.getProviderId())) {
/*
* Check if the content type the project was loaded with
* is supported for saving.
*
* Example for a changed content type: A saved project
* archive may have been extracted and the internal XML
* project file loaded.
*/
if (projectLoadContentType != null) {
if (factory.getSupportedTypes() == null || !factory.getSupportedTypes().contains(projectLoadContentType)) {
log.warn("Project cannot be saved with the same settings it was originally saved with, as the content type has changed.");
break;
}
}
try {
writer = (ProjectWriter) factory.createExtensionObject();
} catch (Exception e) {
log.error("Could not create project writer", e);
}
}
}
if (writer != null) {
// configure provider
writer.loadConfiguration(saveConfig.getProviderConfiguration());
// moved externally)
if (projectFile != null) {
writer.setTarget(new FileIOSupplier(projectFile));
} else {
writer.setTarget(new NoStreamOutputSupplier(projectLocation));
}
ListenableFuture<IOReport> result = ProjectResourcesUtil.executeProvider(writer, saveProjectAdvisor, true, null);
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
@Override
public void run() {
try {
IOReport report = result.get();
if (!report.isSuccess()) {
log.userError("The project could not be saved. Please check the report for more details.");
}
} catch (InterruptedException | ExecutionException e) {
log.userError("The project could not be saved.", e);
}
}
});
} else {
log.info("The project cannot be saved because the format the project was saved with is not available or has changed.");
// use save as instead
saveAs();
}
} else if (projectFile != null) {
// use I/O provider and content type mechanisms to try saving
// the project file
ProjectWriter writer = HaleIO.findIOProvider(ProjectWriter.class, new FileIOSupplier(projectFile), projectFile.getAbsolutePath());
if (writer != null) {
ProjectResourcesUtil.executeProvider(writer, saveProjectAdvisor, null);
} else {
log.error("The project cannot be saved because the format is not available.");
// use save as instead
saveAs();
}
} else {
saveAs();
}
} else {
saveAs();
}
}
use of eu.esdihumboldt.hale.common.core.io.report.IOReport in project hale by halestudio.
the class IOWizard method performFinish.
/**
* @see Wizard#performFinish()
*
* @return <code>true</code> if executing the I/O provider was successful
*/
@Override
public boolean performFinish() {
if (getProvider() == null) {
return false;
}
if (!applyConfiguration()) {
return false;
}
// create default report
IOReporter defReport = provider.createReporter();
// validate and execute provider
try {
// validate configuration
provider.validate();
ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
URI projectLoc = ps.getLoadLocation() == null ? null : ps.getLoadLocation();
boolean isProjectResource = false;
if (actionId != null) {
// XXX instead move project resource to action?
ActionUI factory = ActionUIExtension.getInstance().findActionUI(actionId);
isProjectResource = factory.isProjectResource();
}
// prevent loading of duplicate resources
if (isProjectResource && provider instanceof ImportProvider && !getProviderFactory().allowDuplicateResource()) {
String currentResource = ((ImportProvider) provider).getSource().getLocation().toString();
URI currentAbsolute = URI.create(currentResource);
if (projectLoc != null && !currentAbsolute.isAbsolute()) {
currentAbsolute = projectLoc.resolve(currentAbsolute);
}
for (IOConfiguration conf : ((Project) ps.getProjectInfo()).getResources()) {
Value otherResourceValue = conf.getProviderConfiguration().get(ImportProvider.PARAM_SOURCE);
if (otherResourceValue == null) {
continue;
}
String otherResource = otherResourceValue.as(String.class);
URI otherAbsolute = URI.create(otherResource);
if (projectLoc != null && !otherAbsolute.isAbsolute()) {
otherAbsolute = projectLoc.resolve(otherAbsolute);
}
String action = conf.getActionId();
// resource is already loaded into the project
if (currentAbsolute.equals(otherAbsolute) && Objects.equal(actionId, action)) {
// check if the resource is loaded with a provider that
// allows duplicates
boolean allowDuplicate = false;
IOProviderDescriptor providerFactory = IOProviderExtension.getInstance().getFactory(conf.getProviderId());
if (providerFactory != null) {
allowDuplicate = providerFactory.allowDuplicateResource();
}
if (!allowDuplicate) {
log.userError("Resource is already loaded. Loading duplicate resources is aborted!");
return false;
}
}
}
}
// enable provider internal caching
if (isProjectResource && provider instanceof CachingImportProvider) {
((CachingImportProvider) provider).setProvideCache();
}
IOReport report = execute(provider, defReport);
if (report != null) {
// add report to report server
ReportService repService = PlatformUI.getWorkbench().getService(ReportService.class);
repService.addReport(report);
// show message to user
if (report.isSuccess()) {
// let advisor handle results
try {
getContainer().run(true, false, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Completing operation...", IProgressMonitor.UNKNOWN);
try {
advisor.handleResults(getProvider());
} finally {
monitor.done();
}
}
});
} catch (InvocationTargetException e) {
log.userError("Error processing results:\n" + e.getCause().getLocalizedMessage(), e.getCause());
return false;
} catch (Exception e) {
log.userError("Error processing results:\n" + e.getLocalizedMessage(), e);
return false;
}
// add to project service if necessary
if (isProjectResource)
ps.rememberIO(actionId, getProviderFactory().getIdentifier(), provider);
return true;
} else {
// error message
log.userError(report.getSummary() + "\nPlease see the report for details.");
return false;
}
} else
return true;
} catch (IOProviderConfigurationException e) {
// user feedback
log.userError("Validation of the provider configuration failed:\n" + e.getLocalizedMessage(), e);
return false;
}
}
use of eu.esdihumboldt.hale.common.core.io.report.IOReport in project hale by halestudio.
the class XsltTransformationTest method transformData.
@Override
protected List<Instance> transformData(TransformationExample example) throws Exception {
// export alignment to XSLT
XsltExport export = new XsltExport();
export.setAlignment(example.getAlignment());
export.setSourceSchema(new DefaultSchemaSpace().addSchema(example.getSourceSchema()));
export.setTargetSchema(new DefaultSchemaSpace().addSchema(example.getTargetSchema()));
export.setParameter(XsltExport.PARAM_ROOT_ELEMENT_NAMESPACE, Value.of(example.getTargetContainerNamespace()));
export.setParameter(XsltExport.PARAM_ROOT_ELEMENT_NAME, Value.of(example.getTargetContainerName()));
File tempXsltFile = File.createTempFile("xsltest", ".xsl");
export.setTarget(new FileIOSupplier(tempXsltFile));
IOReport res = export.execute(new LogProgressIndicator());
assertTrue("XSLT export not successful", res.isSuccess());
assertTrue("Errors during XSLT export", res.getErrors().isEmpty());
// invoke XSLT on source file to produce target
File target = File.createTempFile("xsltest", ".xml");
executeXslt(example.getSourceDataInput(), tempXsltFile, target);
// load target and return instances
InstanceCollection instances = TestUtil.loadInstances(target.toURI(), example.getTargetSchema());
List<Instance> list = new ArrayList<Instance>();
ResourceIterator<Instance> it = instances.iterator();
try {
while (it.hasNext()) {
list.add(it.next());
}
} finally {
it.close();
}
// clean up
tempXsltFile.delete();
target.delete();
return list;
}
use of eu.esdihumboldt.hale.common.core.io.report.IOReport in project hale by halestudio.
the class AppSchemaFileWriterTest method writeAlignment.
private void writeAlignment(File targetFile, String contentType) throws IOException, IOProviderConfigurationException {
AbstractAppSchemaConfigurator alignWriter = new AppSchemaMappingFileWriter();
prepareProvider(alignWriter, project, tempDir.toURI());
alignWriter.setAlignment(alignment);
alignWriter.setSourceSchema(sourceSchemaSpace);
alignWriter.setTargetSchema(targetSchemaSpace);
alignWriter.setTarget(new FileIOSupplier(targetFile));
DataStore dataStoreParam = createDataStoreParam();
alignWriter.setParameter(AppSchemaIO.PARAM_DATASTORE, new ComplexValue(dataStoreParam));
alignWriter.setContentType(HalePlatform.getContentTypeManager().getContentType(contentType));
IOReport report = alignWriter.execute(new LogProgressIndicator());
assertNotNull(report);
assertTrue(report.isSuccess());
}
Aggregations