| /** |
| ******************************************************************************** |
| * Copyright (c) 2017-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.emf.metamodelviewer.utils; |
| |
| import java.io.File; |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EPackage; |
| import org.eclipse.emf.ecore.EPackage.Registry; |
| import org.eclipse.emf.ecore.EcorePackage; |
| 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.emf.ecore.util.EcoreUtil; |
| import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl; |
| import org.eclipse.jface.viewers.ComboViewer; |
| |
| public class RegisterECOREfilesAction { |
| |
| @SuppressWarnings({ "unused", "rawtypes", "unchecked" }) |
| public static ResourceSet loadECOREs(List<String> ecoreFiles, ComboViewer combo_ecore) throws IOException { |
| |
| final Registry master_ePackageRegistry = EPackage.Registry.INSTANCE; |
| |
| System.out.println(master_ePackageRegistry.size()); |
| |
| ResourceSet metaDataResourceSet = new ResourceSetImpl(); |
| |
| if (metaDataResourceSet instanceof ResourceSetImpl) { |
| ((ResourceSetImpl) metaDataResourceSet).setURIResourceMap(new HashMap<>()); |
| } |
| Map<String, Object> extensionToFactoryMap = metaDataResourceSet.getResourceFactoryRegistry() |
| .getExtensionToFactoryMap(); |
| |
| extensionToFactoryMap.put("ecore", new EcoreResourceFactoryImpl()); |
| |
| // extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl()); |
| |
| // resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap(true)); |
| |
| EcorePackage ecorePackage = EcorePackage.eINSTANCE; |
| |
| // register Ecore model in resourceSet registry |
| Map packageRegistry = metaDataResourceSet.getPackageRegistry(); |
| |
| packageRegistry.put("http://www.eclipse.org/emf/2002/Ecore", EcorePackage.eINSTANCE); |
| |
| for (String ecorePath : ecoreFiles) { |
| |
| EPackage loadEPackage = loadEPackage(ecorePath, metaDataResourceSet); |
| |
| if (packageRegistry.containsKey(loadEPackage.getNsURI())) { |
| Object object = packageRegistry.get(loadEPackage.getNsURI()); |
| |
| if (object instanceof EPackage) { |
| ((EPackage) object).getESubpackages().add(loadEPackage); |
| } |
| } else { |
| packageRegistry.put(loadEPackage.getNsURI(), loadEPackage); |
| |
| } |
| |
| // master_ePackageRegistry.put(loadEPackage.getNsURI(), loadEPackage); |
| |
| } |
| |
| EcoreUtil.resolveAll(metaDataResourceSet); |
| |
| /*-for sorting */ |
| |
| ArrayList arrayList = new ArrayList(master_ePackageRegistry.keySet()); |
| |
| Collections.sort(arrayList); |
| |
| arrayList.addAll(0, metaDataResourceSet.getPackageRegistry().keySet()); |
| |
| master_ePackageRegistry.putAll(metaDataResourceSet.getPackageRegistry()); |
| |
| combo_ecore.setInput(arrayList.toArray()); |
| |
| return metaDataResourceSet; |
| } |
| |
| private static EPackage loadEPackage(String path, ResourceSet metaResourceSet) throws IOException { |
| |
| // path = path.contains("file:////") ? path : "file:////" + getCanonicalPathFullPath(path); |
| path = getCanonicalPathFullPath(path); |
| |
| // Resource metaResource = metaResourceSet.getResource(URI.createURI(path), true); |
| Resource metaResource = metaResourceSet.getResource(URI.createFileURI(path), true); |
| |
| metaResource.load(Collections.EMPTY_MAP); |
| |
| EObject eObject = metaResource.getContents().get(0); |
| |
| return (EPackage) eObject; |
| |
| } |
| |
| private static String getCanonicalPathFullPath(String path) throws IOException { |
| |
| File file = new File(path); |
| return file.getCanonicalPath(); |
| } |
| } |