Search in sources :

Example 1 with AlignmentReader

use of eu.esdihumboldt.hale.common.align.io.AlignmentReader 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 2 with AlignmentReader

use of eu.esdihumboldt.hale.common.align.io.AlignmentReader in project hale by halestudio.

the class ExamplesContent method getMappingContent.

/**
 * Get the mapping documentation content for an example project.
 *
 * @param projectId the project ID
 * @return the mapping documentation content stream or <code>null</code>
 */
private InputStream getMappingContent(String projectId) {
    if (!mappingDocExportInitialized) {
        mappingDocExport = HaleIO.createIOProvider(AlignmentWriter.class, null, ID_MAPPING_EXPORT);
        if (mappingDocExport == null) {
            log.error("Could not create mapping documentation exporter.");
        }
        mappingDocExportInitialized = true;
    }
    if (mappingDocExport == null) {
        // no mapping documentation export possible
        return null;
    }
    if (tempMappingDir == null) {
        tempMappingDir = Files.createTempDir();
        tempMappingDir.deleteOnExit();
    }
    // the file of the mapping documentation
    File mappingDoc = new File(tempMappingDir, projectId + ".html");
    if (!mappingDoc.exists()) {
        ATransaction trans = log.begin("Generate example mapping documentation");
        try {
            // create the mapping documentation
            ExampleProject exampleProject = ExampleProjectExtension.getInstance().get(projectId);
            final Project project = (Project) exampleProject.getInfo();
            // determine alignment location - contained in project file, not
            // a resource
            URI alignmentLoc = exampleProject.getAlignmentLocation();
            if (alignmentLoc == null) {
                // no alignment present
                return null;
            }
            // store configurations per action ID
            Multimap<String, IOConfiguration> confs = HashMultimap.create();
            for (IOConfiguration conf : project.getResources()) {
                confs.put(conf.getActionId(), conf);
            }
            // load schemas
            // source schemas
            LoadSchemaAdvisor source = new LoadSchemaAdvisor(SchemaSpaceID.SOURCE);
            for (IOConfiguration conf : confs.get(SchemaIO.ACTION_LOAD_SOURCE_SCHEMA)) {
                source.setConfiguration(conf);
                executeProvider(source, conf.getProviderId(), null);
            }
            // target schemas
            LoadSchemaAdvisor target = new LoadSchemaAdvisor(SchemaSpaceID.TARGET);
            for (IOConfiguration conf : confs.get(SchemaIO.ACTION_LOAD_TARGET_SCHEMA)) {
                target.setConfiguration(conf);
                executeProvider(target, conf.getProviderId(), null);
            }
            // load alignment
            // manual loading needed, as we can't rely on the environment
            // alignment advisor
            DefaultInputSupplier alignmentIn = new DefaultInputSupplier(alignmentLoc);
            AlignmentReader reader = HaleIO.findIOProvider(AlignmentReader.class, alignmentIn, alignmentLoc.getPath());
            LoadAlignmentAdvisor alignmentAdvisor = new LoadAlignmentAdvisor(null, source.getSchemaSpace(), target.getSchemaSpace(), exampleProject.getUpdater());
            reader.setSource(alignmentIn);
            executeProvider(alignmentAdvisor, null, reader);
            Alignment alignment = alignmentAdvisor.getAlignment();
            if (alignment != null) {
                // save alignment docu
                synchronized (mappingDocExport) {
                    // only a single instance
                    mappingDocExport.setAlignment(alignment);
                    mappingDocExport.setTarget(new FileIOSupplier(mappingDoc));
                    if (mappingDocExport instanceof ProjectInfoAware) {
                        ProjectInfo smallInfo = new ProjectInfo() {

                            @Override
                            public String getName() {
                                return project.getName();
                            }

                            @Override
                            public Date getModified() {
                                return null;
                            }

                            @Override
                            public Version getHaleVersion() {
                                return null;
                            }

                            @Override
                            public String getDescription() {
                                return project.getDescription();
                            }

                            @Override
                            public Date getCreated() {
                                return null;
                            }

                            @Override
                            public String getAuthor() {
                                return project.getAuthor();
                            }
                        };
                        // project);
                        ((ProjectInfoAware) mappingDocExport).setProjectInfo(smallInfo);
                    }
                    mappingDocExport.execute(null);
                }
                mappingDoc.deleteOnExit();
            }
        } catch (Throwable e) {
            log.error("Error generating mapping documentation for example project", e);
            return null;
        } finally {
            trans.end();
        }
    }
    if (mappingDoc.exists()) {
        try {
            return new FileInputStream(mappingDoc);
        } catch (FileNotFoundException e) {
            return null;
        }
    } else
        return null;
}
Also used : AlignmentReader(eu.esdihumboldt.hale.common.align.io.AlignmentReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) FileNotFoundException(java.io.FileNotFoundException) ExampleProject(eu.esdihumboldt.hale.doc.user.examples.internal.extension.ExampleProject) URI(java.net.URI) ProjectInfoAware(eu.esdihumboldt.hale.common.core.io.project.ProjectInfoAware) FileInputStream(java.io.FileInputStream) ExampleProject(eu.esdihumboldt.hale.doc.user.examples.internal.extension.ExampleProject) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) ATransaction(de.fhg.igd.slf4jplus.ATransaction) ProjectInfo(eu.esdihumboldt.hale.common.core.io.project.ProjectInfo) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LoadSchemaAdvisor(eu.esdihumboldt.hale.common.schema.io.impl.LoadSchemaAdvisor) LoadAlignmentAdvisor(eu.esdihumboldt.hale.common.align.io.impl.LoadAlignmentAdvisor) File(java.io.File) AlignmentWriter(eu.esdihumboldt.hale.common.align.io.AlignmentWriter)

Aggregations

AlignmentReader (eu.esdihumboldt.hale.common.align.io.AlignmentReader)2 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)2 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)2 ATransaction (de.fhg.igd.slf4jplus.ATransaction)1 AlignmentWriter (eu.esdihumboldt.hale.common.align.io.AlignmentWriter)1 JaxbAlignmentReader (eu.esdihumboldt.hale.common.align.io.impl.JaxbAlignmentReader)1 LoadAlignmentAdvisor (eu.esdihumboldt.hale.common.align.io.impl.LoadAlignmentAdvisor)1 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)1 IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)1 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)1 LogProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)1 ProjectInfo (eu.esdihumboldt.hale.common.core.io.project.ProjectInfo)1 ProjectInfoAware (eu.esdihumboldt.hale.common.core.io.project.ProjectInfoAware)1 ArchiveProjectReader (eu.esdihumboldt.hale.common.core.io.project.impl.ArchiveProjectReader)1 Project (eu.esdihumboldt.hale.common.core.io.project.model.Project)1 ProjectFileInfo (eu.esdihumboldt.hale.common.core.io.project.model.ProjectFileInfo)1 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1