use of eu.esdihumboldt.hale.common.core.io.PathUpdate in project hale by halestudio.
the class AppSchemaFileWriterTest method loadTestProject.
@BeforeClass
public static void loadTestProject() {
try {
URL archiveLocation = AppSchemaFileWriterTest.class.getResource(PROJECT_LOCATION);
ArchiveProjectReader projectReader = new ArchiveProjectReader();
projectReader.setSource(new DefaultInputSupplier(archiveLocation.toURI()));
IOReport report = projectReader.execute(new LogProgressIndicator());
if (!report.isSuccess()) {
throw new RuntimeException("project reader execution failed");
}
tempDir = projectReader.getTemporaryFiles().iterator().next();
project = projectReader.getProject();
assertNotNull(project);
sourceSchemaSpace = new DefaultSchemaSpace();
targetSchemaSpace = new DefaultSchemaSpace();
// load schemas
List<IOConfiguration> resources = project.getResources();
for (IOConfiguration resource : resources) {
String actionId = resource.getActionId();
String providerId = resource.getProviderId();
// get provider
IOProvider provider = null;
IOProviderDescriptor descriptor = IOProviderExtension.getInstance().getFactory(providerId);
if (descriptor == null) {
throw new RuntimeException("Could not load I/O provider with ID: " + resource.getProviderId());
}
provider = descriptor.createExtensionObject();
provider.loadConfiguration(resource.getProviderConfiguration());
prepareProvider(provider, project, tempDir.toURI());
IOReport providerReport = provider.execute(new LogProgressIndicator());
if (!providerReport.isSuccess()) {
throw new RuntimeException("I/O provider execution failed");
}
// TODO: could (should?) be done by an advisor
if (provider instanceof SchemaReader) {
Schema schema = ((SchemaReader) provider).getSchema();
if (actionId.equals(SchemaIO.ACTION_LOAD_SOURCE_SCHEMA)) {
sourceSchemaSpace.addSchema(schema);
} else if (actionId.equals(SchemaIO.ACTION_LOAD_TARGET_SCHEMA)) {
targetSchemaSpace.addSchema(schema);
}
}
}
// load alignment
List<ProjectFileInfo> projectFiles = project.getProjectFiles();
for (ProjectFileInfo projectFile : projectFiles) {
if (projectFile.getName().equals(AlignmentIO.PROJECT_FILE_ALIGNMENT)) {
AlignmentReader alignReader = new JaxbAlignmentReader();
alignReader.setSource(new DefaultInputSupplier(projectFile.getLocation()));
alignReader.setSourceSchema(sourceSchemaSpace);
alignReader.setTargetSchema(targetSchemaSpace);
alignReader.setPathUpdater(new PathUpdate(null, null));
IOReport alignReport = alignReader.execute(new LogProgressIndicator());
if (!alignReport.isSuccess()) {
throw new RuntimeException("alignment reader execution failed");
}
alignment = alignReader.getAlignment();
assertNotNull(alignment);
break;
}
}
} catch (Exception e) {
log.error("Exception occurred", e);
fail("Test project could not be loaded: " + e.getMessage());
}
}
use of eu.esdihumboldt.hale.common.core.io.PathUpdate in project hale by halestudio.
the class AppSchemaIsolatedWorkspacesMappingTest method loadAlignment.
private static Alignment loadAlignment(AlignmentReader alignReader, String resource) throws Exception {
alignReader.setSource(new DefaultInputSupplier(AppSchemaIsolatedWorkspacesMappingTest.class.getResource(resource).toURI()));
alignReader.setSourceSchema(sourceSchemaSpace);
alignReader.setTargetSchema(targetSchemaSpace);
alignReader.setPathUpdater(new PathUpdate(null, null));
IOReport report = alignReader.execute(new LogProgressIndicator());
assertTrue(report.isSuccess());
assertTrue("Errors are contained in the report", report.getErrors().isEmpty());
return alignReader.getAlignment();
}
use of eu.esdihumboldt.hale.common.core.io.PathUpdate in project hale by halestudio.
the class DefaultProjectReader method execute.
/**
* @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Load project", ProgressIndicator.UNKNOWN);
InputStream in = getSource().getInput();
if (archive) {
// read from archive
ZipInputStream zip = new ZipInputStream(new BufferedInputStream(in));
try {
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
String name = entry.getName();
progress.setCurrentTask(MessageFormat.format("Load {0}", name));
if (name.equals(ProjectIO.PROJECT_FILE)) {
try {
setProjectChecked(Project.load(new EntryInputStream(zip)), reporter);
} catch (Exception e) {
// fail if main project file cannot be loaded
throw new IOProviderConfigurationException("Source is no valid project archive", e);
}
} else {
ProjectFile file = getProjectFiles().get(name);
if (file != null) {
try {
file.load(new EntryInputStream(zip));
} catch (Exception e) {
reporter.error(new IOMessageImpl("Error while loading project file {0}, file will be reset.", e, -1, -1, name));
// reset file
file.reset();
}
}
}
}
} finally {
zip.close();
}
} else {
// read from XML
try {
setProjectChecked(Project.load(in), reporter);
} catch (Exception e) {
// fail if main project file cannot be loaded
throw new IOProviderConfigurationException("Source is no valid project file", e);
} finally {
in.close();
}
}
URI oldProjectLocation;
if (getProject().getSaveConfiguration() == null) {
oldProjectLocation = getSource().getLocation();
} else {
oldProjectLocation = URI.create(getProject().getSaveConfiguration().getProviderConfiguration().get(ExportProvider.PARAM_TARGET).as(String.class));
}
PathUpdate update = new PathUpdate(oldProjectLocation, getSource().getLocation());
// check if there are any external project files listed
if (getProjectFiles() != null) {
// only if project files set at all
for (ProjectFileInfo fileInfo : getProject().getProjectFiles()) {
ProjectFile projectFile = getProjectFiles().get(fileInfo.getName());
if (projectFile != null) {
URI location = fileInfo.getLocation();
location = update.findLocation(location, false, DefaultInputSupplier.SCHEME_LOCAL.equals(getSource().getLocation().getScheme()), false);
if (location == null && getSource().getLocation() != null) {
// 1st try: appending file name to project location
try {
URI candidate = new URI(getSource().getLocation().toString() + "." + fileInfo.getName());
if (HaleIO.testStream(candidate, true)) {
location = candidate;
}
} catch (URISyntaxException e) {
// ignore
}
// 2nd try: file name next to project
if (location != null) {
try {
String projectLoc = getSource().getLocation().toString();
int index = projectLoc.lastIndexOf('/');
if (index > 0) {
URI candidate = new URI(projectLoc.substring(0, index + 1) + fileInfo.getName());
if (HaleIO.testStream(candidate, true)) {
location = candidate;
}
}
} catch (URISyntaxException e) {
// ignore
}
}
}
boolean fileSuccess = false;
if (location != null) {
try {
DefaultInputSupplier dis = new DefaultInputSupplier(location);
try (InputStream input = dis.getInput()) {
projectFile.load(input);
fileSuccess = true;
} catch (Exception e) {
// hand down
throw e;
}
} catch (Exception e) {
reporter.error(new IOMessageImpl("Loading project file failed", e));
}
}
if (!fileSuccess) {
reporter.error(new IOMessageImpl("Error while loading project file {0}, file will be reset.", null, -1, -1, fileInfo.getName()));
projectFile.reset();
}
} else {
reporter.info(new IOMessageImpl("No handler for external project file {0} found.", null, -1, -1, fileInfo.getName()));
}
}
}
// clear project infos
/*
* XXX was there any particular reason why this was done? I suspect it
* was done so when saving the project this information is not saved
* again as-is, but on the basis of actual files written. However, this
* case is handled in the project writer already.
*
* As this information is in fact necessary when trying to identify
* certain files like the alignment, clearing the list of project files
* was commented out.
*/
// getProject().getProjectFiles().clear();
progress.end();
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.core.io.PathUpdate in project hale by halestudio.
the class PathUpdateTest method testSame.
/**
* Extended real world example - project file renamed, but in the same
* folder
*/
@Test
public void testSame() {
String orgPath = "C:/Users/sitemple/Entwicklung/hale/cst/plugins/eu.esdihumboldt.cst.test/src/testdata/unification/t1t2.hale";
String path = "C:/Users/sitemple/Entwicklung/hale/cst/plugins/eu.esdihumboldt.cst.test/src/testdata/unification/t1t2_alt.hale";
PathUpdate update = new PathUpdate(URI.create("file:/" + orgPath), URI.create("file:/" + path));
URI file = URI.create("file:/C:/Users/sitemple/Entwicklung/hale/cst/plugins/eu.esdihumboldt.cst.test/src/testdata/unification/schemas/t1.xsd");
String correct = "file:/C:/Users/sitemple/Entwicklung/hale/cst/plugins/eu.esdihumboldt.cst.test/src/testdata/unification/schemas/t1.xsd";
String newpath = update.changePath(file).toString();
assertEquals(correct, newpath);
}
use of eu.esdihumboldt.hale.common.core.io.PathUpdate in project hale by halestudio.
the class PathUpdateTest method testSimple.
/**
* Real world example
*/
@Test
public void testSimple() {
String orgPath = "C:/Users/sitemple/Entwicklung/hale/cst/plugins/eu.esdihumboldt.cst.test/src/testdata/unification/t1t2.hale";
String path = "C:/Users/sitemple/Entwicklung/hale/cst/plugins/eu.esdihumboldt.cst.test/src/testdata/propmerge/t1t2.hale";
PathUpdate update = new PathUpdate(URI.create("file:/" + orgPath), URI.create("file:/" + path));
URI file = URI.create("file:/C:/Users/sitemple/Entwicklung/hale/cst/plugins/eu.esdihumboldt.cst.test/src/testdata/unification/t1.xsd");
String correct = "file:/C:/Users/sitemple/Entwicklung/hale/cst/plugins/eu.esdihumboldt.cst.test/src/testdata/propmerge/t1.xsd";
String newpath = update.changePath(file).toString();
assertEquals(correct, newpath);
}
Aggregations