blob: 62d814fb53308999abfab46ffe36f8332ce9c786 [file] [log] [blame]
/**
* *******************************************************************************
* Copyright (c) 2020 Robert Bosch GmbH.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
* *******************************************************************************
*/
package org.eclipse.app4mc.slg.commons.m2t;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.app4mc.amalthea.model.Amalthea;
import org.eclipse.app4mc.amalthea.model.AmaltheaPackage;
import org.eclipse.app4mc.amalthea.model.emf.AmaltheaResourceFactory;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.sphinx.emf.resource.ExtendedResourceSet;
import org.eclipse.sphinx.emf.resource.ExtendedResourceSetImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* AmaltheaMultiFileLoader
*
* Should be replaced by standard AmaltheaLoader (APP4MC version > 1.0.0)
*
*/
public class AmaltheaMultiFileLoader implements IMultiFileLoader{
private static final Logger LOG = LoggerFactory.getLogger(AmaltheaMultiFileLoader.class);
public ResourceSet loadMultipleFiles(String directoryPath) {
File folder = new File(directoryPath);
if (folder.isDirectory()) {
File[] listFiles = folder.listFiles((file, name) -> name.endsWith(".amxmi"));
ResourceSet resourceSet = initializeResourceSet();
loadMultipleFiles(resourceSet, listFiles);
return resourceSet;
}
return new ResourceSetImpl();
}
private List<Amalthea> loadMultipleFiles(ResourceSet resourceSet, File[] listFiles) {
List<Amalthea> models = new ArrayList<>();
for (File amxmiFile : listFiles) {
final Resource res = resourceSet.createResource(URI.createURI("file:////" + amxmiFile.getAbsolutePath()));
try {
res.load(null);
for (final EObject content : res.getContents()) {
if (content instanceof Amalthea) {
models.add((Amalthea) content);
}
}
} catch (IOException e) {
LOG.error(e.getMessage(),e);
}
}
return models;
}
private static ResourceSet initializeResourceSet() {
AmaltheaPackage.eINSTANCE.eClass(); // register the package
final ExtendedResourceSet resSet = new ExtendedResourceSetImpl();
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("amxmi", new AmaltheaResourceFactory(true));
return resSet;
}
}