Search in sources :

Example 1 with AlignmentType

use of eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType in project hale by halestudio.

the class TemplateProject method onSuccess.

@Override
protected void onSuccess(Void context, String projectId, File projectFile, Project project, ReportFile reportFile) {
    super.onSuccess(context, projectId, projectFile, project, reportFile);
    // update locations in project file
    LocationUpdater updater = new LocationUpdater(project, projectFile.toURI());
    updater.updateProject(false);
    resources.clear();
    List<Path> invalidSources = new ArrayList<>();
    Path projectFolder = getProjectFolder().toPath();
    // validate resources
    for (IOConfiguration config : project.getResources()) {
        Resource resource = new IOConfigurationResource(config, getProjectFile().toURI());
        // check if file URIs are valid and inside project folder
        URI source = resource.getSource();
        if (source != null) {
            Path path = null;
            if (source.getScheme() == null) {
                // is a relative URI
                path = projectFile.toPath().resolve(source.toString()).normalize();
            } else if ("file".equals(source.getScheme())) {
                // is a file URI
                path = Paths.get(source).normalize();
            }
            if (path != null) {
                if (!path.startsWith(projectFolder) || !Files.exists(path)) {
                    // invalid source
                    invalidSources.add(path);
                }
            }
        }
        resources.put(resource.getActionId(), resource);
    }
    valid = invalidSources.isEmpty();
    if (!valid) {
        StringBuilder builder = new StringBuilder("Files referenced by the project could not be found: ");
        for (int i = 0; i < invalidSources.size(); i++) {
            if (i > 0)
                builder.append(", ");
            Path path = invalidSources.get(i);
            builder.append(path.getFileName().toString());
        }
        notValidMessage = builder.toString();
    } else {
        notValidMessage = "";
    }
    // additionally, try to find out cell count
    definedRelations = 0;
    // check if default alignment file exists
    try {
        File defAlignmentFile = new File(URI.create(projectFile.toURI().toASCIIString() + "." + AlignmentIO.PROJECT_FILE_ALIGNMENT));
        if (defAlignmentFile.exists()) {
            try (InputStream in = new BufferedInputStream(new FileInputStream(defAlignmentFile))) {
                /*
					 * Try loading the file with JAXB - only supports 2.6+
					 * projects.
					 */
                AlignmentType alignment = JaxbToAlignment.load(in, null);
                // XXX ignoring base alignments
                int count = 0;
                for (Object element : alignment.getCellOrModifier()) {
                    if (element instanceof CellType) {
                        count++;
                    }
                }
                definedRelations = count;
            } catch (Exception e) {
            // ignore
            }
        }
    } catch (Exception e) {
    // ignore
    }
}
Also used : Path(java.nio.file.Path) LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOConfigurationResource(eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource) Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) IOConfigurationResource(eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource) URI(java.net.URI) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) BufferedInputStream(java.io.BufferedInputStream) CellType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType) ReportFile(eu.esdihumboldt.hale.common.headless.report.ReportFile) File(java.io.File)

Example 2 with AlignmentType

use of eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType in project hale by halestudio.

the class JaxbAlignmentIO method save.

/**
 * Save a default alignment to an output stream.
 *
 * @param alignment the alignment to save
 * @param reporter the I/O reporter to report any errors to, may be
 *            <code>null</code>
 * @param out the output stream
 * @param pathUpdate to update relative paths in case of a path change
 * @throws Exception if converting or writing the alignment fails
 * @deprecated use {@link #convert(Alignment, IOReporter, PathUpdate)} and
 *             {@link #save(AlignmentType, IOReporter, OutputStream)}
 *             instead to prevent an empty file being written on conversion
 *             errors
 */
@Deprecated
public static void save(Alignment alignment, IOReporter reporter, OutputStream out, PathUpdate pathUpdate) throws Exception {
    AlignmentType align = convert(alignment, reporter, pathUpdate);
    save(align, reporter, out);
}
Also used : AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType)

Example 3 with AlignmentType

use of eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType in project hale by halestudio.

the class JaxbToAlignment method load.

/**
 * Load a {@link AlignmentType} from an input stream. The stream is closed
 * at the end.
 *
 * @param in the input stream
 * @param reporter the I/O reporter to report any errors to, may be
 *            <code>null</code>
 * @return the alignment
 * @throws JAXBException if reading the alignment failed
 */
public static AlignmentType load(InputStream in, IOReporter reporter) throws JAXBException {
    JAXBContext jc;
    JAXBElement<AlignmentType> root = null;
    jc = JAXBContext.newInstance(JaxbAlignmentIO.ALIGNMENT_CONTEXT, AlignmentType.class.getClassLoader());
    Unmarshaller u = jc.createUnmarshaller();
    // it will debug problems while unmarshalling
    u.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
    try {
        root = u.unmarshal(new StreamSource(in), AlignmentType.class);
    } finally {
        try {
            in.close();
        } catch (IOException e) {
        // ignore
        }
    }
    return root.getValue();
}
Also used : AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) StreamSource(javax.xml.transform.stream.StreamSource) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 4 with AlignmentType

use of eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType in project hale by halestudio.

the class JaxbAlignmentIO method printCell.

/**
 * Print a cell to an output stream (intended for tests/debugging).
 *
 * @param cell the cell to print
 * @param out the output stream
 * @throws Exception if an error occurs trying to print the cell
 */
public static void printCell(MutableCell cell, OutputStream out) throws Exception {
    DefaultAlignment alignment = new DefaultAlignment();
    alignment.addCell(cell);
    IOReporter reporter = new DefaultIOReporter(new Locatable() {

        @Override
        public URI getLocation() {
            return null;
        }
    }, "Print cell", null, false);
    PathUpdate pathUpdate = new PathUpdate(null, null);
    AlignmentType at = convert(alignment, reporter, pathUpdate);
    CellType ct = (CellType) at.getCellOrModifier().get(0);
    JAXBContext jc = JAXBContext.newInstance(ALIGNMENT_CONTEXT, ObjectFactory.class.getClassLoader());
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    ObjectFactory of = new ObjectFactory();
    try {
        m.marshal(of.createCell(ct), out);
    } finally {
        out.flush();
        out.close();
    }
}
Also used : AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) DefaultIOReporter(eu.esdihumboldt.hale.common.core.io.report.impl.DefaultIOReporter) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) Marshaller(javax.xml.bind.Marshaller) DefaultIOReporter(eu.esdihumboldt.hale.common.core.io.report.impl.DefaultIOReporter) ObjectFactory(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ObjectFactory) CellType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) JAXBContext(javax.xml.bind.JAXBContext) URI(java.net.URI) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 5 with AlignmentType

use of eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType in project hale by halestudio.

the class JaxbAlignmentWriter method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Save hale alignment", ProgressIndicator.UNKNOWN);
    PathUpdate pathUpdate = new PathUpdate(getProjectLocation(), getTarget().getLocation());
    AlignmentType alignment;
    try {
        alignment = JaxbAlignmentIO.convert(getAlignment(), reporter, pathUpdate);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Error converting alignment to XML model", e));
        reporter.setSuccess(false);
        return reporter;
    }
    OutputStream out = getTarget().getOutput();
    try {
        JaxbAlignmentIO.save(alignment, reporter, out);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getMessage(), e));
        reporter.setSuccess(false);
        return reporter;
    } finally {
        out.close();
    }
    progress.end();
    reporter.setSuccess(true);
    return reporter;
}
Also used : AlignmentType(eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType) OutputStream(java.io.OutputStream) PathUpdate(eu.esdihumboldt.hale.common.core.io.PathUpdate) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Aggregations

AlignmentType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.AlignmentType)5 IOException (java.io.IOException)3 CellType (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.CellType)2 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)2 URI (java.net.URI)2 JAXBContext (javax.xml.bind.JAXBContext)2 ObjectFactory (eu.esdihumboldt.hale.common.align.io.impl.internal.generated.ObjectFactory)1 DefaultAlignment (eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)1 IOConfigurationResource (eu.esdihumboldt.hale.common.core.io.project.model.IOConfigurationResource)1 Resource (eu.esdihumboldt.hale.common.core.io.project.model.Resource)1 LocationUpdater (eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater)1 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)1 DefaultIOReporter (eu.esdihumboldt.hale.common.core.io.report.impl.DefaultIOReporter)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 Locatable (eu.esdihumboldt.hale.common.core.io.supplier.Locatable)1 ReportFile (eu.esdihumboldt.hale.common.headless.report.ReportFile)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1