| 1 | package de.uka.ipd.sdq.spa.util; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.util.Collections; |
| 6 | |
| 7 | import org.eclipse.emf.common.util.URI; |
| 8 | import org.eclipse.emf.ecore.EObject; |
| 9 | import org.eclipse.emf.ecore.resource.Resource; |
| 10 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 11 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 12 | import org.eclipse.emf.ecore.util.EcoreUtil; |
| 13 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
| 14 | |
| 15 | import de.uka.ipd.sdq.spa.SpaPackage; |
| 16 | |
| 17 | |
| 18 | public class EMFTools { |
| 19 | |
| 20 | @SuppressWarnings("unchecked") |
| 21 | public static EObject loadFromXMI(String fileName) { |
| 22 | // Create a resource set to hold the resources. |
| 23 | ResourceSet resourceSet = new ResourceSetImpl(); |
| 24 | |
| 25 | // Register the appropriate resource factory to handle all file |
| 26 | // extentions. |
| 27 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() |
| 28 | .put(Resource.Factory.Registry.DEFAULT_EXTENSION, |
| 29 | new XMIResourceFactoryImpl()); |
| 30 | |
| 31 | // Register the package to ensure it is available during loading. |
| 32 | resourceSet.getPackageRegistry().put(SpaPackage.eNS_URI, |
| 33 | SpaPackage.eINSTANCE); |
| 34 | |
| 35 | // Construct the URI for the instance file. |
| 36 | // The argument is treated as a file path only if it denotes an existing |
| 37 | // file. Otherwise, it's directly treated as a URL. |
| 38 | File file = new File(fileName); |
| 39 | URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()) |
| 40 | : URI.createURI(fileName); |
| 41 | |
| 42 | // Demand load resource for this file. |
| 43 | Resource resource = resourceSet.getResource(uri, true); |
| 44 | System.out.println("Loaded " + uri); |
| 45 | |
| 46 | |
| 47 | EObject eObject = (EObject)resource.getContents().iterator().next(); |
| 48 | return EcoreUtil.getRootContainer(eObject); |
| 49 | } |
| 50 | |
| 51 | @SuppressWarnings("unchecked") |
| 52 | public static void saveToXMI(EObject objectToSave, String fileName) { |
| 53 | // Create a resource set. |
| 54 | ResourceSet resourceSet = new ResourceSetImpl(); |
| 55 | |
| 56 | // Register the default resource factory -- only needed for stand-alone! |
| 57 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( |
| 58 | Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); |
| 59 | |
| 60 | // Get the URI of the model file. |
| 61 | URI fileURI = URI.createFileURI(new File(fileName).getAbsolutePath()); |
| 62 | |
| 63 | // Create a resource for this file. |
| 64 | Resource resource = resourceSet.createResource(fileURI); |
| 65 | |
| 66 | // Add the book and writer objects to the contents. |
| 67 | resource.getContents().add(objectToSave); |
| 68 | |
| 69 | // Save the contents of the resource to the file system. |
| 70 | try |
| 71 | { |
| 72 | resource.save(Collections.EMPTY_MAP); |
| 73 | } |
| 74 | catch (IOException e) {} |
| 75 | } |
| 76 | |
| 77 | } |