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));
}
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());
}
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();
}
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;
}
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;
}
}
}
Aggregations