blob: 15670727b53b0806412e77df39c1392ba92b4f18 [file] [log] [blame]
/**
********************************************************************************
* Copyright (c) 2018 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 model.loader;
import java.io.File;
import java.io.FilenameFilter;
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.sphinx.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.osgi.service.component.annotations.Component;
@Component(service = IMultiFileLoader.class)
public class AmaltheaMultiFileLoader implements IMultiFileLoader{
public ResourceSet loadMultipleFiles(String directoryPath) {
File folder = new File(directoryPath);
if (folder.isDirectory()) {
File[] listFiles = folder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
if (name.endsWith(".amxmi")) {
return true;
}
return false;
}
});
ResourceSet resourceSet = initializeResourceSet();
loadMultipleFiles(resourceSet, listFiles);
return resourceSet;
}
return new ResourceSetImpl();
}
private List<Amalthea> loadMultipleFiles(ResourceSet resourceSet, File[] listFiles) {
List<Amalthea> models = new ArrayList<Amalthea>();
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) {
System.err.println(e.getMessage());
}
}
return models;
}
private static ResourceSet initializeResourceSet() {
final ExtendedResourceSet resSet = new ExtendedResourceSetImpl();
resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("amxmi", new AmaltheaResourceFactory());
AmaltheaPackage.eINSTANCE.eClass(); // register the package
return resSet;
}
}