Thursday 6 September 2012

EMF API FAQ

      
How to register a metamodel (an ecore file)
If you have a metamodel MyMetamodel.ecore and you have generated the model code, you can register your metamodel with :
        EPackage.Registry.INSTANCE.put(MyMetamodelPackage.eNS_URI,
                MyMetamodel.eINSTANCE);

 How to load a resource as an xmi file
    public static Resource load(String uri) {

        Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
        Map<String, Object> m = reg.getExtensionToFactoryMap();
        m.put("xmi", new XMIResourceFactoryImpl());
        // Obtain a new resource set
        ResourceSet resSet = new ResourceSetImpl();

        // Get the resource
        Resource resource = resSet.getResource(URI.createURI(uri), true);
        return resource;
    }


How to save a resource as an xmi file
Resource result = null;
//do somthing with the resource
URI saveURI = URI.createFileURI("d:/workspaces/wp_irit_ifgui/myProject/src/fr/irit/ifclipse/xml/savedModel.xmi");
result.setURI(saveURI);
result.save(Collections.EMPTY_MAP);