Search in sources :

Example 11 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class XsltGenerator method generateTypeTransformation.

/**
 * Generate a XSL fragment for transformation based on the given type
 * relation.
 *
 * @param templateName name of the XSL template
 * @param typeCell the type relation
 * @param targetfile the target file to write the fragment to
 * @param targetElement the target element to use to hold a transformed
 *            instance
 * @throws TransformationException if an unrecoverable error occurs during
 *             the XSLT transformation generation
 */
protected void generateTypeTransformation(String templateName, XmlElement targetElement, Cell typeCell, File targetfile) throws TransformationException {
    XslTypeTransformation xslt;
    try {
        xslt = XslTypeTransformationExtension.getInstance().getTransformation(typeCell.getTransformationIdentifier());
    } catch (Exception e) {
        throw new TransformationException("Could not retrieve XSLT transformation generator for cell function", e);
    }
    xslt.setContext(context);
    xslt.generateTemplate(templateName, targetElement, typeCell, new FileIOSupplier(targetfile));
}
Also used : TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) XslTypeTransformation(eu.esdihumboldt.hale.io.xslt.XslTypeTransformation) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) XMLStreamException(javax.xml.stream.XMLStreamException) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 12 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier 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());
}
Also used : ComplexValue(eu.esdihumboldt.hale.common.core.io.impl.ComplexValue) DataStore(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.SourceDataStoresPropertyType.DataStore) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)

Example 13 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class CSVInstanceWriterTest method writeCsvToFile.

private boolean writeCsvToFile(File tmpFile, boolean skipFirstLine, Value sep, Value quo, Value esc, InstanceCollection instances) throws Exception {
    // set instances to xls instance writer
    InstanceWriter writer = new CSVInstanceWriter();
    IContentType contentType = HalePlatform.getContentTypeManager().getContentType("eu.esdihumboldt.hale.io.csv");
    writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(skipFirstLine));
    writer.setParameter(CSVSchemaReader.PARAM_SEPARATOR, sep);
    writer.setParameter(CSVSchemaReader.PARAM_QUOTE, quo);
    writer.setParameter(CSVSchemaReader.PARAM_ESCAPE, esc);
    writer.setInstances(instances);
    // write instances to a temporary CSV file
    writer.setTarget(new FileIOSupplier(tmpFile));
    writer.setContentType(contentType);
    IOReport report = writer.execute(null);
    return report.isSuccess();
}
Also used : InstanceWriter(eu.esdihumboldt.hale.common.instance.io.InstanceWriter) CSVInstanceWriter(eu.esdihumboldt.hale.io.csv.writer.internal.CSVInstanceWriter) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) IContentType(org.eclipse.core.runtime.content.IContentType) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) CSVInstanceWriter(eu.esdihumboldt.hale.io.csv.writer.internal.CSVInstanceWriter)

Example 14 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class ProjectIO method findProjectFile.

/**
 * Find the HALE project file in a directory. If there are multiple it will
 * only find one.
 *
 * @param projectDir the project directory
 * @param supportedExtensions the set of supported extensions, each with a
 *            leading dot, or <code>null</code> if the supported extensions
 *            should be determined automatically
 * @return the name of the project file candidate in that directory,
 *         <code>null</code> if none was found
 */
public static String findProjectFile(File projectDir, Set<String> supportedExtensions) {
    final Set<String> extensions;
    if (supportedExtensions == null) {
        extensions = getSupportedExtensions();
    } else {
        extensions = supportedExtensions;
    }
    File[] candidates = projectDir.listFiles(new FileFilter() {

        @Override
        public boolean accept(File file) {
            if (file.isFile() && !file.isHidden()) {
                String lowerName = file.getName().toLowerCase();
                for (String extension : extensions) {
                    if (lowerName.endsWith(extension.toLowerCase())) {
                        return true;
                    }
                }
            }
            return false;
        }
    });
    if (candidates != null) {
        if (candidates.length == 1) {
            return candidates[0].getName();
        }
        // TODO warn that there are multiple?
        for (File candidate : candidates) {
            FileIOSupplier supplier = new FileIOSupplier(candidate);
            // find content type against stream
            IContentType contentType = HaleIO.findContentType(ProjectReader.class, supplier, null);
            if (contentType != null) {
                return candidate.getName();
            }
        }
    }
    // none found? check in subdirectories
    File[] subdirs = projectDir.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory() && !pathname.isHidden();
        }
    });
    if (subdirs != null) {
        for (File subdir : subdirs) {
            String name = findProjectFile(subdir, extensions);
            if (name != null) {
                return subdir.getName() + "/" + name;
            }
        }
    }
    return null;
}
Also used : FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) IContentType(org.eclipse.core.runtime.content.IContentType) FileFilter(java.io.FileFilter) File(java.io.File) ProjectFile(eu.esdihumboldt.hale.common.core.io.project.model.ProjectFile)

Example 15 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class ActionProjectFile method apply.

/**
 * @see ProjectFile#apply()
 */
@SuppressWarnings("unchecked")
@Override
public void apply() {
    if (applyFile == null) {
        return;
    }
    try {
        // load the temporary file using an ImportProvider
        LocatableInputSupplier<? extends InputStream> tmpIn = new FileIOSupplier(applyFile);
        // get the action
        IOAction action = IOActionExtension.getInstance().get(loadActionId);
        checkState(ImportProvider.class.isAssignableFrom(action.getProviderType()), "Load action not compatible to ImportProvider");
        // try specified provider
        ImportProvider provider = null;
        if (loadProviderId != null) {
            try {
                provider = (ImportProvider) HaleIO.createIOProvider(action.getProviderType(), null, loadProviderId);
            } catch (Exception e) {
                // ignore
                log.error("Could not get specified import provider, trying auto-detection instead", e);
            }
        }
        // find provider if necessary
        if (provider == null) {
            provider = (ImportProvider) HaleIO.findIOProvider(action.getProviderType(), tmpIn, null);
        }
        if (provider == null) {
            throw new IllegalStateException("No provider for loading project file found (Action ID " + action.getId() + ")");
        }
        // find advisor
        @SuppressWarnings("rawtypes") IOAdvisor advisor = getLoadAdvisor(loadActionId, serviceProvider);
        checkState(advisor != null, "No advisor for loading project file found");
        // configure provider
        // set given parameters
        setParameters(provider, loadParameters);
        // set source
        provider.setSource(tmpIn);
        // execute the provider
        executeProvider(provider, advisor);
    } catch (Exception e) {
        // project file apply fails currently are one logged (the project
        // reader is not involved with it)
        log.error("Error applying loaded project file", e);
    } finally {
        if (!applyFile.delete()) {
            applyFile.deleteOnExit();
            applyFile = null;
        }
    }
}
Also used : IOAdvisor(eu.esdihumboldt.hale.common.core.io.IOAdvisor) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) IOAction(eu.esdihumboldt.hale.common.core.io.IOAction) IOException(java.io.IOException)

Aggregations

FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)34 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)22 File (java.io.File)22 LogProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)9 IOException (java.io.IOException)9 IContentType (org.eclipse.core.runtime.content.IContentType)8 URI (java.net.URI)6 AlignmentWriter (eu.esdihumboldt.hale.common.align.io.AlignmentWriter)4 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)4 DefaultSchemaSpace (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace)4 NullProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.NullProgressIndicator)3 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)3 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)3 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)3 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)3 OutputStream (java.io.OutputStream)3 TransformationExample (eu.esdihumboldt.cst.test.TransformationExample)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 ProjectReader (eu.esdihumboldt.hale.common.core.io.project.ProjectReader)2 Project (eu.esdihumboldt.hale.common.core.io.project.model.Project)2