Search in sources :

Example 6 with PathUpdate

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());
    }
}
Also used : JaxbAlignmentReader(eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentReader) AlignmentReader(eu.esdihumboldt.hale.common.align.io.AlignmentReader) JaxbAlignmentReader(eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentReader) SchemaReader(eu.esdihumboldt.hale.common.schema.io.SchemaReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) DefaultSchemaSpace(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) URL(java.net.URL) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ZipException(java.util.zip.ZipException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ArchiveProjectReader(eu.esdihumboldt.hale.common.core.io.project.impl.ArchiveProjectReader) ProjectFileInfo(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) IOProvider(eu.esdihumboldt.hale.common.core.io.IOProvider) BeforeClass(org.junit.BeforeClass)

Example 7 with PathUpdate

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();
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)

Example 8 with PathUpdate

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;
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) BufferedInputStream(java.io.BufferedInputStream) ProjectFileInfo(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate)

Example 9 with PathUpdate

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);
}
Also used : PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) URI(java.net.URI) Test(org.junit.Test)

Example 10 with PathUpdate

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);
}
Also used : PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) URI(java.net.URI) Test(org.junit.Test)

Aggregations

PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)15 URI (java.net.URI)10 Test (org.junit.Test)7 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)5 IOException (java.io.IOException)5 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)4 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)3 AlignmentType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType)2 LogProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)2 ProjectFileInfo (eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo)2 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)2 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)2 DefaultIOReporter (eu.esdihumboldt.hale.common.core.io.report.impl.DefaultIOReporter)2 Locatable (eu.esdihumboldt.hale.common.core.io.supplier.Locatable)2 OutputStream (java.io.OutputStream)2 AlignmentReader (eu.esdihumboldt.hale.common.align.io.AlignmentReader)1 JaxbAlignmentReader (eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentReader)1 AlignmentBean (eu.esdihumboldt.hale.common.align.io.impl.internal.AlignmentBean)1 CellBean (eu.esdihumboldt.hale.common.align.io.impl.internal.CellBean)1 NamedEntityBean (eu.esdihumboldt.hale.common.align.io.impl.internal.NamedEntityBean)1