use of eu.esdihumboldt.hale.common.core.io.project.model.Project in project hale by halestudio.
the class AbstractAppSchemaConfigurator method updateTargetSchemaResources.
// TODO: code adapted from ArchiveProjectWriter: how to avoid duplication?
private Map<URI, String> updateTargetSchemaResources(File targetDirectory, ProgressIndicator progress, IOReporter reporter) throws IOException {
progress.begin("Copy resources", ProgressIndicator.UNKNOWN);
Project project = (Project) getProjectInfo();
// resource locations mapped to new resource path
Map<URI, String> handledResources = new HashMap<>();
try {
List<IOConfiguration> resources = project.getResources();
// every resource needs his own directory
int count = 0;
Iterator<IOConfiguration> iter = resources.iterator();
while (iter.hasNext()) {
IOConfiguration resource = iter.next();
if (resource.getActionId().equals(SchemaIO.ACTION_LOAD_TARGET_SCHEMA)) {
// get resource path
Map<String, Value> providerConfig = resource.getProviderConfiguration();
String path = providerConfig.get(ImportProvider.PARAM_SOURCE).toString();
URI pathUri;
try {
pathUri = new URI(path);
} catch (URISyntaxException e1) {
reporter.error(new IOMessageImpl("Skipped resource because of invalid URI: " + path, e1));
continue;
}
// check if path was already handled
if (handledResources.containsKey(pathUri)) {
// skip copying the resource
continue;
}
String scheme = pathUri.getScheme();
LocatableInputSupplier<? extends InputStream> input = null;
if (scheme != null) {
if (scheme.equals("http") || scheme.equals("https") || scheme.equals("file") || scheme.equals("platform") || scheme.equals("bundle") || scheme.equals("jar")) {
input = new DefaultInputSupplier(pathUri);
} else {
continue;
}
} else {
// now can't open that, can we?
reporter.error(new IOMessageImpl("Skipped resource because it cannot be loaded from " + pathUri.toString(), null));
continue;
}
progress.setCurrentTask("Copying resource at " + path);
// every resource file is copied into an own resource
// directory in the target directory
String resourceFolder = "_schemas";
if (count > 0) {
resourceFolder += count;
}
File newDirectory = new File(targetDirectory, resourceFolder);
try {
newDirectory.mkdir();
} catch (SecurityException e) {
throw new IOException("Can not create directory " + newDirectory.toString(), e);
}
// the filename
String name = path.toString().substring(path.lastIndexOf("/") + 1, path.length());
// remove any query string from the filename
int queryIndex = name.indexOf('?');
if (queryIndex >= 0) {
name = name.substring(0, queryIndex);
}
if (name.isEmpty()) {
name = "file";
}
File newFile = new File(newDirectory, name);
Path target = newFile.toPath();
// retrieve the resource advisor
Value ct = providerConfig.get(ImportProvider.PARAM_CONTENT_TYPE);
IContentType contentType = null;
if (ct != null) {
contentType = HalePlatform.getContentTypeManager().getContentType(ct.as(String.class));
}
ResourceAdvisor ra = ResourceAdvisorExtension.getInstance().getAdvisor(contentType);
// copy the resource
progress.setCurrentTask("Copying resource at " + path);
ra.copyResource(input, target, contentType, true, reporter);
// store new path for resource
String newPath = resourceFolder + "/" + name;
handledResources.put(pathUri, newPath);
count++;
}
}
} finally {
progress.end();
}
return handledResources;
}
use of eu.esdihumboldt.hale.common.core.io.project.model.Project in project hale by halestudio.
the class ArchiveProjectReader method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
// copy resources to a temporary directory
tempDir = Files.createTempDir();
IOUtils.extract(tempDir, getSource().getInput());
// create the project file via XMLProjectReader
File baseFile = new File(tempDir, "project.halex");
ProjectReader reader;
if (!baseFile.exists()) {
// look for project file
log.debug("Default project file not found, looking for other candidates.");
String candidate = ProjectIO.findProjectFile(tempDir);
if (candidate != null) {
log.info(MessageFormat.format("Loading {0} as project file from archive", candidate));
baseFile = new File(tempDir, candidate);
}
reader = HaleIO.findIOProvider(ProjectReader.class, new FileIOSupplier(baseFile), candidate);
if (reader == null) {
reporter.error(new IOMessageImpl("Could not find reader for project file " + candidate, null));
reporter.setSuccess(false);
return reporter;
}
} else {
// default project file
reader = new XMLProjectReader();
}
LocatableInputSupplier<InputStream> tempSource = new FileIOSupplier(baseFile);
// save old save configuration
LocatableInputSupplier<? extends InputStream> oldSource = getSource();
setSource(tempSource);
reader.setSource(tempSource);
reader.setProjectFiles(getProjectFiles());
IOReport report = reader.execute(progress);
Project readProject = reader.getProject();
if (readProject != null) {
/*
* Because the original source is only available here, update the
* project's resource paths here.
*
* The only drawback is that the UILocationUpdater cannot be used.
*/
LocationUpdater updater = new LocationUpdater(readProject, tempSource.getLocation());
// update resources
// resources are made absolute (else they can't be found afterwards)
updater.updateProject(false);
}
// set the real source
setSource(oldSource);
// set the read project
setProjectChecked(readProject, reporter);
return report;
}
use of eu.esdihumboldt.hale.common.core.io.project.model.Project 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;
}
use of eu.esdihumboldt.hale.common.core.io.project.model.Project in project hale by halestudio.
the class ProjectTest method testSaveLoad.
/**
* Test saving and loading an example project
*
* @throws Exception if an error occurs
*/
@Test
public void testSaveLoad() throws Exception {
// populate project
Project project = new Project();
String author;
project.setAuthor(author = "Simon");
String name;
project.setName(name = "Testprojekt");
Date created;
project.setCreated(created = new Date(0));
Date modified;
project.setModified(modified = new Date());
Version haleVersion;
project.setHaleVersion(haleVersion = new Version("2.2.0.alpha"));
String desc;
project.setDescription(desc = "Hallo Welt!\nBist Du auch hier?\nÖhm.");
IOConfiguration conf1;
project.getResources().add(conf1 = new IOConfiguration());
String advisorId1;
conf1.setActionId(advisorId1 = "some advisor");
String providerId1;
conf1.setProviderId(providerId1 = "some provider");
String key1;
Value value1;
conf1.getProviderConfiguration().put(key1 = "some key", value1 = Value.of("some value"));
Value value2;
String key2;
conf1.getProviderConfiguration().put(key2 = "some other key", value2 = Value.of("some other value"));
IOConfiguration conf2;
project.getResources().add(conf2 = new IOConfiguration());
String advisorId2;
conf2.setActionId(advisorId2 = "a certain advisor");
String providerId2;
conf2.setProviderId(providerId2 = "a special provider");
// write project
File projectFile = tmp.newFile("project.xml");
System.out.println(projectFile.getAbsolutePath());
Project.save(project, new FileOutputStream(projectFile));
// load project
Project p2 = Project.load(new FileInputStream(projectFile));
// test project
assertEquals(author, p2.getAuthor());
assertEquals(name, p2.getName());
assertEquals(created, p2.getCreated());
assertEquals(modified, p2.getModified());
assertEquals(haleVersion, p2.getHaleVersion());
assertEquals(desc, p2.getDescription());
assertEquals(2, p2.getResources().size());
Iterator<IOConfiguration> it = p2.getResources().iterator();
IOConfiguration c1 = it.next();
assertNotNull(c1);
assertEquals(advisorId1, c1.getActionId());
assertEquals(providerId1, c1.getProviderId());
assertEquals(2, c1.getProviderConfiguration().size());
assertTrue(c1.getProviderConfiguration().get(key1).getValue().equals(value1.getValue()));
assertTrue(c1.getProviderConfiguration().get(key2).getValue().equals(value2.getValue()));
IOConfiguration c2 = it.next();
assertNotNull(c2);
assertEquals(advisorId2, c2.getActionId());
assertEquals(providerId2, c2.getProviderId());
}
use of eu.esdihumboldt.hale.common.core.io.project.model.Project in project hale by halestudio.
the class JaxbToProject method convert.
/**
* Convert the given project.
*
* @param project the project to convert
* @return the project model object
*/
public static Project convert(ProjectType project) {
Project result = new Project();
result.setAuthor(project.getAuthor());
result.setCreated(toDate(project.getCreated()));
result.setDescription(project.getDescription());
result.setHaleVersion(toVersion(project.getVersion()));
result.setModified(toDate(project.getModified()));
result.setName(project.getName());
result.setSaveConfiguration(toIOConfiguration(project.getSaveConfig()));
for (IOConfigurationType resource : project.getResource()) {
result.getResources().add(toIOConfiguration(resource));
}
for (ExportConfigurationType exportConfig : project.getExportConfig()) {
String name = exportConfig.getName();
if (name != null && !name.isEmpty()) {
result.getExportConfigurations().put(name, toIOConfiguration(exportConfig.getConfiguration()));
}
}
for (ProjectFileType file : project.getFile()) {
result.getProjectFiles().add(new ProjectFileInfo(file.getName(), URI.create(file.getLocation())));
}
for (JAXBElement<?> property : project.getAbstractProperty()) {
Object value = property.getValue();
if (value instanceof PropertyType) {
addProperty(result.getProperties(), (PropertyType) value);
} else if (value instanceof ComplexPropertyType) {
addProperty(result.getProperties(), (ComplexPropertyType) value);
}
}
return result;
}
Aggregations